Woes of the static impaired
I have an object: CMain.
This object has a static function (used as the message processing function for a dialog window, which is also a member of class CMain):
static BOOL CALLBACK CMain::DialogMsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
My app launches fine, the dialog window appears, and the people are happy.
However, due to the fact that the function is static, I cannot gain access to the rest of the CMain function.
So far, I have tried to save a pointer to CMain using SetWindowLong(). I then try to retrieve the pointer inside the DialogMsgProc function using GetWindowLong():
//This line is called after I create the dialog box:
SetWindowLong(hwnd_tabctrl, DWL_USER, (LONG)this);
//This line is called from within the DialogMsgProc() function:
CMain* pMain = (CMain*)GetWindowLong(hwnd, DWL_USER);
As is, this setup fails to work. After calling GetWindowLong(), pMain == 0. However, calls to FormatMessage(...GetLastError())...) both return "The Operation Completed Successfully.
So what am I doing wrong? Do I need to retool my conversions between a pointer and a long? Am I misusing the concept of "static"? Any answers/thoughts/ideas are welcome.
-Mike
Static functions can only access static variables. Make some of the variables that the window function uses a static variable
That was a really fast response, Cybertron. Making some of the variables static will not completely work, because the dialog function needs to have access to the rest of the object. The only reason I am making the function static is because the compiler (MSCV++ 6.0) seems to require it (I am assuming that that is because it is a callback function).
Shouldn''t I be able to pass a pointer of CMain into the window''s extra memory through SetWindowLong(), and then retrieve it later with GetWindowLong()? As far as I can tell, this should be perfectly permissable, and not affected by the static definition of the dialog function. Any other ideas?
-Mike
Shouldn''t I be able to pass a pointer of CMain into the window''s extra memory through SetWindowLong(), and then retrieve it later with GetWindowLong()? As far as I can tell, this should be perfectly permissable, and not affected by the static definition of the dialog function. Any other ideas?
-Mike
When you registered the dialog''s Window Class, did you set the class structure''s cbWndExtra member to a non-zero value? Something like this should work:
Once you''ve done that, then the window actually has a place to store the pointer you''re passing in with SetWindowLong. Other than this, the way you''re going about doing this looks correct.
Hope this helps,
Cheers, dorix
wndClass.cbWndExtra = sizeof(CMain*);
Once you''ve done that, then the window actually has a place to store the pointer you''re passing in with SetWindowLong. Other than this, the way you''re going about doing this looks correct.
Hope this helps,
Cheers, dorix
Well I just got it to work. The problem was basically that I tried to get a pointer from GetWindowLong() before the window was fully created, thus giving me an invalid pointer. I changed the winclass.cbWndExtra to equal sizeof(CMain*), but for some reason my app worked whether winclass.cbWndExtra == 0 or sizeof(CMain*). Thanks for the replies, guys!
-Mike
-Mike
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement