|
-
March 3rd, 2007, 09:46 AM
#1
[SOLVED] Calling non-static member functions from within a static member function.
Hello all. I'm not sure if this is the right forum to post this in, so if it isn't I apologize in advance.
I'm making a windows program that is entirely wrapped in a class. So this also includes my window procedure, which had to be defined as a static function. Like this:
Code:
class cUTool2004
{
public:
cUTool2004(void);
~cUTool2004(void);
DWORD Setup(HINSTANCE hInstance, LPSTR lpCmdLine, int nCmdShow);
DWORD Cleanup(void);
private:
HWND hWindow;
HFONT hfMainTitle;
HFONT hfMainNotInstalled;
int iMainTab;
int iRegistryTab;
int iDemosTab;
bool bUTFound;
bool UTisInstalled(void);
int LoadTab(int iTab);
int LoadMainTab(void);
int UnloadTab(int iTab);
int UnloadMainTab(void);
static LRESULT WndMsgHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
Now, I'm trying to call UnloadTab() from WndMsgHandler() but this gives me compile error C2352: 'cUTool2004::UnloadTab' : illegal call of non-static member function. I guess this is because non-static functions are supposed to get the hidden 'this' pointer, but because WndMsgHandler is static it doesn't have the 'this' pointer. Correct?
Does this then mean that non-static functions can't be called from static ones? That would mean changing many of my memberfunctions to statics... That can't be right, can it? Or are there other ways around this?
Bah, this is probably just some stupid oversight of me... Anyway, thanks in advance for any pointers (LOL PUN) in the right direction.
Last edited by Jehjoa; March 5th, 2007 at 09:20 AM.
Reason: Problem has been solved.
I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here... 
-
March 3rd, 2007, 10:23 AM
#2
Re: Calling non-static member functions from within a static member function.
Hi jehjoa,
I'm new to winapi myself, so I may be wrong about this. However, I class static as something that dont change.
In my mind, the value of the message handler is constantly changing. When you move the mouse, press a key on the keyboard, maximize or minimize your window, etc...
Maybe thats why you get the error "illegal call of non-static member function". Maybe it not supposed to be static. I may be wrong, but its something to think about.
-
March 3rd, 2007, 11:37 AM
#3
Re: Calling non-static member functions from within a static member function.
 Originally Posted by Jehjoa
I guess this is because non-static functions are supposed to get the hidden 'this' pointer, but because WndMsgHandler is static it doesn't have the 'this' pointer. Correct?
Correct.
 Originally Posted by Jehjoa
Does this then mean that non-static functions can't be called from static ones? That would mean changing many of my memberfunctions to statics...
No, making the member function static wouldn't change anything, since they wouldn't have the this pointer too. And all the functions can be called from static functions, you just need to pass them the this pointer. Usually, the best way to to that is to pass the this pointer as the last parameter of CreateWindow, and then catch the WM_NCCREATE message and store the pointer you passed - which is available in the lpCreateParams member variable of the LPCREATESTRUCT structure - using SetWindowLong and GWL_USERDATA:
Code:
void MyClass::SomeFunction()
{
CreateWindow(...., GetModuleHandle(NULL), this);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
MyClass *c = (MyClass*)GetWindowLong(hwnd, GWL_USERDATA);
if (msg == WM_NCCREATE)
{
LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam;
c = (MyClass*)cs->lpCreateParams;
SetWindowLong(hwnd, GWL_USERDATA, (LONG)c);
}
//now you can access all the functions.
}
Last edited by kkez; March 3rd, 2007 at 11:40 AM.
-
March 3rd, 2007, 04:41 PM
#4
Re: Calling non-static member functions from within a static member function.
 Originally Posted by Y0rkieP
Hi jehjoa,
I'm new to winapi myself, so I may be wrong about this. However, I class static as something that dont change.
In my mind, the value of the message handler is constantly changing. When you move the mouse, press a key on the keyboard, maximize or minimize your window, etc...
Maybe thats why you get the error "illegal call of non-static member function". Maybe it not supposed to be static. I may be wrong, but its something to think about.
Hey there, Y0rkieP. 
The static keyword is actually a C++ keyword, it has nothing to do with the WinAPI. In the case of a static variable, it means that it's value is never lost when the function ends. It's actually saved for next calls of that function. A static member function indicates that every instance of a class uses the exact same function. (Correct me if I'm wrong, anybody.) So it has nothing to do with the messages my function receives. Thanks for trying to help, though.
 Originally Posted by kkez
Usually, the best way to to that is to pass the this pointer as the last parameter of CreateWindow, and then catch the WM_NCCREATE message and store the pointer you passed - which is available in the lpCreateParams member variable of the LPCREATESTRUCT structure - using SetWindowLong and GWL_USERDATA:
Hmm I see. Well, that would work but there's just one problem: The window is created from a dialog template with CreateDialog. The window does receive the WM_CREATE message, but the lpCreateParams variable points to a SHORT value that specifies the size of the window data. So I can't use it...
The only other way I can think of getting the this pointer in WndMsgHandler, is by sending it with a user defined message ( WM_USER++; ), and then sending that message right after window has been created. I would then store the pointer in a local static variable.
I assume this will work, but it feels rather like a hack... Is there a better way?
Last edited by Jehjoa; March 3rd, 2007 at 04:44 PM.
I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here... 
-
March 4th, 2007, 11:36 AM
#5
Re: Calling non-static member functions from within a static member function.
The window is created from a dialog template with CreateDialog. The window does receive the WM_CREATE message, but the lpCreateParams variable points to a SHORT value that specifies the size of the window data. So I can't use it...
Both DialogBox and CreateDialog are actually macros that calls respectively DialogBoxParam and CreateDialogParam. The additional parameter is passed along with the WM_INITDIALOG message in the lParam variable. Here's were you receive the this pointer.
... is by sending it with a user defined message ( WM_USER++; )
Your custom messages should start from WM_APP, not from WM_USER. Read the WM_APP docs.
-
March 5th, 2007, 09:19 AM
#6
Re: Calling non-static member functions from within a static member function.
OK! I'm using the WM_APP method for now because I implemented it before your reply and it works, but thanks for telling me about CreateDialogParam.
I love this forum.
I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here... 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|