C2440 Can't convert LRESULT to WNDPROC in C++ WinApi - c++

I'm trying to write this win32 program with WinApi and I'm stuck because the tutorial I'm following seems to have a problem.
MainWindow.h:
class MainWindow
{
public:
MainWindow(HINSTANCE);
~MainWindow(void);
LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
// [...]
MainWindow.cpp:
MainWindow::MainWindow(HINSTANCE hInstance) : hwnd(0)
{
WNDCLASSEX WndClsEx;
// [...]
WndClsEx.lpfnWndProc = &MainWindow::WndProcedure;
// [...]
}
LRESULT CALLBACK MainWindow::WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
// [...]
}
I must be referencing MainWindow::WndProcedure wrong because I'm following the signature exactly as the tutorial says, however the lpfnWndProc line in the constructor gives a compile-time error:
error C2440: '=' : cannot convert from 'LRESULT (__stdcall MainWindow::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'

replace
LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
by
static LRESULT CALLBACK WndProcedure(HWND, UINT, WPARAM, LPARAM);
The this pointer is a hidden parameter in your function call and by declaring it static the this pointer is not a parameter anymore and the signature of the two functions match.

That's because your WndProcedure function must be either a global function or a static member function.

You can't use a non-static member function as a window procedure. If you declare WndProcedure as static it should compile. A non-member function would work as well.
Non-static member functions have a different signature than static members. This is because they receive an implicit this parameter in addition to the explicitly defined parameters.

Related

__stdcall in function paramater [duplicate]

This question already has answers here:
How to declare an __stdcall function pointer
(1 answer)
Function pointer and calling convention
(3 answers)
Closed 8 months ago.
does anybody know if is possible to add __stdcall (CALLBACK) in function parameter like this?:
void Function(LRESULT CALLBACK (*f)(HWND, UINT, WPARAM, LPARAM));
It gives me following error:
a calling convention may not be followed by a nested declarator
Any solutions?
Thx in advance <3
Put the calling convention inside the parenthesis.
void Function(LRESULT (CALLBACK *f)(HWND, UINT, WPARAM, LPARAM));
Usually it is seen in the manual, for example CallWindowProcW function
the lpPrevWndFunc parameter has the data type WNDPROC. The WNDPROC type is declared as follows:
LRESULT (CALLBACK* WNDPROC) (HWND, UINT, WPARAM, LPARAM);
Thus, the correct syntax is (WNDPROC -> f)
void Function(LRESULT (CALLBACK* f)(HWND, UINT, WPARAM, LPARAM));

D3DApp Class Problems

So I am in the process of creating a WNDCLASSEX within a method contained in D3DApp class which I am going to be deriving from with another class e.g. Engine, Game, etc... called InitMainWindow (which is also protected in the base class):
bool D3DApp::InitMainWindow() {
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc; // This is where the error is!
wc.hInstance = mAppInst;
wc.hCursor = LoadCursor(mAppInst, IDC_ARROW);
wc.lpszClassName = L"MainWindow";
RegisterClassEx(&wc);
RECT rect = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT };
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
mMainWnd = CreateWindowEx(NULL, L"MainWindow", L"Test", WS_OVERLAPPEDWINDOW, 300, 300, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, mAppInst, NULL);
ShowWindow(mMainWnd, NULL);
return true;
}
My WNDPROC Callback declaration and definition is within the same D3DApp base class where InitMainWindow() is located, defined as such:
virtual LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
When I am filling out the lpfnWndProc variable within the WNDCLASSEX struct I receive a C2440 error stating the following
'=': cannot convert from 'LRESULT (__cdecl D3DApp::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'
Do I have to create a definition of my WNDPROC Callback in the class deriving from D3DApp? Or do I have to completely remove this callback from the base class and derived class and place it outside and above the scope of my WinMain function? I've been researching for about 2 days on how to fix this error and have had no luck on finding any possible fixes... I think my C++ skills just may not be as excellent as I think they are. But if anyone can help me understand where my problem is that would be amazing!
p.s and yes I know I have some error checking to do but I just want to make sure it works for my compiler first :)
Here's the signature of WNDPROC:
typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
but your declaration is different:
LRESULT WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
You should either declare it outside of the class like this:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
or declare it as static and remove virtual keyword. Also notice, you should add CALLBACK which resolves to __stdcall calling convention as your project is configured to use __cdecl calling convention

why am I getting this error when compiling an mfc application?

I have this code:
void CALLBACK CTestTimeUpDlg::MyTimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT_PTR nIDEvent, // timer identification
DWORD dwTime // system time
)
{
const int m_TimerValue=0;
double timeValueSec=m_TimerValue/1000.0;
CString valueString;
valueString.Format(L"%3.3f",timeValueSec);
m_TimerDisplayValue.SetWindowTextW(valueString);
}
void CTestTimeUpDlg::OnBnClickedButtonStart()
{
m_TimerValue=0;
m_Timer = SetTimer(1, 1, &CTestTimeUpDlg::MyTimerProc);
}
but when I compiled it, I am getting this error:
'CWnd::SetTimer' : cannot convert parameter 3 from 'void (__stdcall CTestTimeUpDlg::* )(HWND,UINT,UINT_PTR,DWORD)' to 'void (__stdcall *)(HWND,UINT,UINT_PTR,DWORD)'
the code is similar to the code from Microsoft documentation:
http://msdn.microsoft.com/en-us/library/49313fdf.aspx
You should make CTestTimeUpDlg::MyTimerProc static. However, by doing this, you can't access instance members such as m_TimerDisplayValue.
You shouldn't use callback in this case. Set lpfnTimer NULL, as the first timer in the sample of the link. That way, the timer posts the message WM_TIMER, and you can handle it by your non-static member function.
ADD:
Seems the document (plus my words above) is lacking in explanation.
Do as followings to implement a handler of WM_TIMER.
Declare handler in your class declaration:
afx_msg void OnTimer(UINT_PTR nIDEvent);
In your cpp file, add message mapping:
BEGIN_MESSAGE_MAP(CTestTimeUpDlg, ...)
ON_WM_TIMER()
END_MESSAGE_MAP()
and implementation:
void CTestTimeUpDlg::OnTimer(UINT_PTR nIDEvent)
{
// your code here...
}

Storing a function pointer as a member within a class

I created this account to ask a question because I'm pretty lost right now and I couldn't find an answer. I'm not sure if what I'm trying to do is even possible, or perhaps I'm taking an incorrect approach.
I'm writing a windows socket wrapper class for asynchronous sockets, mostly so I never have to write this code again, and I'm trying to store a function pointer as a member variable inside my netcode class that points to a windows event handler that's called inside the application's normal win32 message pump that points elsewhere. Basically, I'm trying to create a programmer-defined callback function so that when I compile this netcode class into a .dll I never have to touch it again.
As a clarification, what I'm trying to do is, for example, write the event handler for WM_SOCKET (which is a windows message I have defined) somewhere other than inside the network code class (so it can be portable to different win32/C++ projects) and outside of the main win32 message pump (so my WndProc() doesn't become absolutely massive).
I don't even know what to search for, but all of the information I'm finding is about creating a pointer to a member function or a member of a template, which isn't what I want.
Here's the code I've already tried to write (only the relevant bits), which is what is giving me problems. I'm probably royally screwing something up here.
//// Sockets.h
class NetworkConnection
{
private:
void (*WMSocketFunction)(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
public:
void HandleEvents(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void registerSocketFunction( void (*SocketFunction(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)));
};
//// Sockets.cpp
#include "Sockets.h"
void NetworkConnection::registerSocketFunction( void (*SocketFunction(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)))
{
WMSocketFunction = SocketFunction;
};
void NetworkConnection::HandleEvents(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
WMSocketFunction(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
}
//// Application.cpp
void SocketEventHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Do socket stuff here
};
int SetupApp()
{
NetworkConnection NetConnection;
NetConnection.registerSocketFunction(&SocketEventHandler);
return 0;
}
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_SOCKET:
{
NetConnection.HandleEvents(hwnd, msg, wParam, lParam);
break;
}
case WM_DESTROY:
{
ReleaseDevices();
break;
}
case WM_KEYDOWN:
{
#ifdef _DEBUG
if (lParam == VK_ESCAPE)
PostQuitMessage(0);
#endif
}
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
I'd be really grateful for some help, here. I totally have no clue why this isn't working.
The errors I receive are:
Error 1 error C2440: '=' : cannot convert from 'void *(__cdecl *)(HWND,UINT,WPARAM,LPARAM)' to 'void (__cdecl *)(HWND,UINT,WPARAM,LPARAM)' sockets.cpp (in function registerSocketFunction)
Error 2 error C2275: 'HWND' : illegal use of this type as an expression sockets.cpp (in function HandleEvents)
Error 3 error C2146: syntax error : missing ')' before identifier 'hwnd' sockets.cpp (in function HandleEvents)
Error 4 error C2059: syntax error : ')' sockets.cpp (in function HandleEvents)
Error 8 error C2664: 'NetworkConnection::registerSocketFunction' : cannot convert parameter 1 from 'void (__cdecl *)(HWND,UINT,WPARAM,LPARAM)' to 'void *(__cdecl *)(HWND,UINT,WPARAM,LPARAM)' application.cpp
If you need any better explanation, because I've likely explained this problem poorly, please don't hesitate to ask.
I'm using Visual Studio 2010, so I don't think I can use C++/11 features like std::function.
Exclude arguments names from function pointer declaration
Use typedef to make code cleaner
typedef void(*WMSocketFunction_Ptr)(HWND, UINT, WPARAM, LPARAM);
class NetworkConnection
{
private:
WMSocketFunction_Ptr WMSocketFunction;
public:
void HandleEvents(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void registerSocketFunction(WMSocketFunction_Ptr SocketFunction);
};
void NetworkConnection::registerSocketFunction(WMSocketFunction_Ptr SocketFunction)
{
WMSocketFunction = SocketFunction;
};
Prefer to use STL facilities to naked pointers: std::function of boost::function
typedef std::function<void(HWND, UINT, WPARAM, LPARAM)> WMSocketFunction_Ptr;
You have to enclose the function pointer name in brackets like this: void (*SocketFunction)(...)
So instead of your method declaration:
void registerSocketFunction( void (*SocketFunction(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)));
You have to write:
void registerSocketFunction( void (*SocketFunction)(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam));
Also rewrite your method body to this:
void NetworkConnection::registerSocketFunction( void (*SocketFunction)(HWND, UINT, WPARAM, LPARAM))
{
WMSocketFunction = SocketFunction;
};
Next thing, i think your HandleEvents is unfinished yet, you have a syntax error there, i think you wanted to call the function behind the pointer.
void NetworkConnection::HandleEvents(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
WMSocketFunction(hwnd,msg,wParam,lParam);
}
Here is my suspicion. I think the way you've declared the function argument SocketFunction for registerSocketFunction is wrong.
void NetworkConnection::registerSocketFunction( void (*SocketFunction(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)))
should be,
void NetworkConnection::registerSocketFunction( void (*SocketFunction)(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam))
I ran a similar test on GCC, and it threw a similar error, so this might be your problem.

Global friend function unable to access private scope even though declared global

I'm sure it is something real easy I've overlooked but I'm tearing my hair over this error message. I very seldom use friend functions.
error: 'LRESULT Window_Proc(HWND, UINT, WPARAM, LPARAM)' should have been declared inside '::'
Here's my definition:
namespace rayc
{
class win32_window: public window {
public:
win32_window();
~win32_window();
void show();
void hide();
void pump_message();
private:
friend LRESULT CALLBACK ::Window_Proc(HWND, UINT, WPARAM, LPARAM);
void set_closed(bool);
HWND hWin;
};
}
I don't quite understand this error message, it is defined as belonging to global scope, is it not?!
Compiling with MinGW-64 TDM.
I don't quite understand this error message, it is defined as belonging to global scope, is it not?!
It is not, unless you have included a forward declaration at the global namespace before that friend declaration. If a friend declaration introduces a new symbol, then such symbol is located at the enclosing namespace of the class that contains it.
Try adding
LRESULT CALLBACK Window_Proc(HWND, UINT, WPARAM, LPARAM);
at the global namespace, before declaring your class.