Sign in to follow this  

Passing a WinProc function as pointer to WndClass struct

This topic is 3200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! I am trying to do the following:
// First .H file /////////////////////////

// Prototype of message handler function
typedef LRESULT (CALLBACK *MessageHandler)(HWND, UINT, WPARAM, LPARAM);

class FirstClass
{
public:
    bool Create(HINSTANCE instance, MessageHandler msgfunc);
};

// First .CPP file /////////////////////////

// Create function
bool Canvas::Create(HINSTANCE instance, MessageHandler msgfunc)
{
// Setup window class
	WNDCLASS wnd =
	{
		CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
		(WNDPROC)msgfunc,   // <----
		0,
		0,
		instance,
		LoadIcon(NULL, IDI_APPLICATION),
		LoadCursor(NULL, IDC_ARROW),
		NULL,
		NULL,
		L"MyAppClass"
	};

     ...
}

// In another .H file  /////////////////////////
class MyClass
{
public:
     bool Init();
     LRESULT CALLBACK HandleMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam);
};

// In another .CPP file /////////////////////////
bool MyClass::Init()
{
    FirstClass* c = new FirstClass();
    if (c->Create(m_instance, &MyClass::HandleMessage))
    {
       return true;
    }

    return false;
}


The problem I get with the c->Create(...) call is the following: cannot convert parameter 3 from 'LRESULT (__stdcall MyClass::* )(HWND,UINT,WPARAM,LPARAM)' to 'MessageHandler' I do not want to make the HandleMessage method static..

Share this post


Link to post
Share on other sites
You have to make it static... But you can pass the instance as the last argument of CreateWindowEx and you'll get it back in the LPCREATESTRUCT you receive in WM_NCCREATE (or WM_CREATE), at which point you can save it with SetWindowLongPtr and reuse it for the next messages. Then you may call the non-static member function (the static one being mostly some kind of "proxy").

Share this post


Link to post
Share on other sites

This topic is 3200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this