Archived

This topic is now archived and is closed to further replies.

NewDeal

function pointers (easy)

Recommended Posts

NewDeal    134
How do i declare a pointer to a function ? In this case: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); Please help make my compiler stop bitching.

Share this post


Link to post
Share on other sites
jenova    122
HRESULT (CALLBACK * Func)(HWMD, UINT, WPARAM, LPARAM);

"Func" is the variable name.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by DrPizza
Use a typedef.



  
typedef LRESULT CALLBACK WndProcType(HWND, UINT, WPARAM, LPARAM);

WndProcType* WndProcType_ptr;



Share this post


Link to post
Share on other sites
aftermath    100
If you want to use the function pointer, you will need to make a typedef for it:
  
typedef LRESULT CALLBACK WINDOW_PROC_CB(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/* Thats not going to do anything but make declaring the function easyer */

And where you want to declare you function:
  
WINDOW_PROC_CB* WinProc = NULL;

And you set it like this:
  
WinProc = MyClassThingyWindowProc;
/* Where MyClassThingyWindowProc is declared like this: LRESULT CALLBACK MyClassThingyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/* And MyClassThingyWindowProc() can be anything, like a function in a class

If you were seting the function from inside a class, in the constructor you would need:
  
// ...

WinProc = this->SomeProc; // Where SomeProc will need the proper declereation stated above.


I hope that helped you

- err, the last signiture sucked bigtime!

Share this post


Link to post
Share on other sites
NewDeal    134
What Jenova suggested seems to work.

I dont need to call the function directly. its used only when passing the wndproc parameter to the RegisterClass() function.

Its for an OpenGL/DirectX wrapper sorta thing btw.

Thanks all

Share this post


Link to post
Share on other sites
Pyabo    124
...and don''t forget, you cannot set a function pointer to a class method. I hope in AfterMath''s example that "SomeProc" is just a WNDPROC member of that class, and not one of its methods.

Share this post


Link to post
Share on other sites
aftermath    100
Since a window proc must be a global when seting it (at least not in a class), you must have a dummy proc, or a re-router, as you will:
  
TheClass* g_pClass = NULL;

LRESULT CALLBACK WndProcDummy(HWND, UINT, WPARAM, LPARAM)
{
assert( g_pTheClass );
return ( g_pTheClass->WinProc(...) );
}

TheClass::TheClass()
{
// ...

g_pClass = this; /* This is IMPORTANT, I had a 1 week
bug because of this :) */

/ ...
}

TheClass::SomeFunctionThatCreatesAWindow()
{
// ...

wc.WinProc = WndProcDummy;
// ...

}

I might be of subject here :|

- err, the last signiture sucked bigtime!

Share this post


Link to post
Share on other sites