Sign in to follow this  

variables in a static function

This topic is 4868 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

Okay, so I've been using this little Windows wrapper for my games for a while now, it does everything I need. But now I'm trying to extend it, and I'm running into problems. All my callback functions are static (which I assume is the way it's supposed to be). But the problem is that in my main message processor, I'm trying to call a dialog box. For this I need the handle to the current instance (hInst). Problem is, my current instance isn't static and thus I can't access it. If I add the "static" keyword to it, MSVC throws me a bunch of "unresolved external symbol" errors. How do I fix this? (and why is coding for Windows using classes such a pain?)

Share this post


Link to post
Share on other sites
Callback functions often have a DWORD or LPVOID parameter associated with them. In these cases, I usually pass a pointer to the object (this) to the callback function. Like with CreateThread, I have two functions, one that is static, and is of the DWORD WINAPI ThreadProc_Static(LPVOID lpParameter) form, and the other that is a member function of the form DWORD ThreadProc() form. In the static function, I simply cast the lpParameter argument to the appropriate class pointer type, and call the member ThreadProc() function. So I basically get a callback function that ends up being a member function.

class CClass
{
public:
CClass() : mhStop(false), mhThread(NULL)
{
mhThread = CreateThread(NULL, 0, CClass::ThreadProc_Static, (LPVOID)this, 0, NULL);
}
~CClass()
{
mStop = true;
if (WaitForSingleObject(mhThread, 1000) == WAIT_TIMEOUT)
TerminateThread(mhThread, -1);
CloseHandle(mhThread);
}
private:
HANDLE mhThread;
bool mhStop;
static DWORD WINAPI ThreadProc_Static(LPVOID lpParameter)
{
return reinterpret_cast<CClass*>(lpParamter)->ThreadProc();
}
DWORD ThreadProc()
{
while (!mhStop)
{
Sleep(100);
}
return 0;
}
};

Share this post


Link to post
Share on other sites
Thanks O_o, your solution worked.

Agony, yours looks a lot more flexible though, I'll be sure to keep it around for future reference. One question though, I thought callback functions were supposed to have four parameters? (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)?

Thanks for the help guys!

Share this post


Link to post
Share on other sites

This topic is 4868 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