Jump to content
  • Advertisement
Sign in to follow this  
Eldritch

Passing a WinProc function as pointer to WndClass struct

This topic is 3419 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!