Click to See Complete Forum and Search --> : MsgProc function parameter passing


nappaji
January 18th, 2008, 05:00 PM
<code>
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}
</code>

I have the above MsgProc function used to handle windows messages.
As you can see, during the WN_DESTROY message, I call a function called Cleanup().
I want to pass in parameters (a structure variable) into this Cleanup function. To so that, I need to pass the parameters through the MsgProc function.

How do I accomplish this? MsgProc takes in only 4 parameters, how do I add additional parameters?

kirants
January 18th, 2008, 05:44 PM
I want to pass in parameters (a structure variable) into this Cleanup function. To so that, I need to pass the parameters through the MsgProc function.
That means, your Cleanup function has to take additional parameters and not MsgProc.
If your structure is , say CleanUpStruct you would do something like this:

Pass by value

void Cleanup(CleanUpStruct st)
{
}
....
case WM_DESTROY:
CleanUpStruct st;
st.member1 = //;
Cleanup(st);

Pass by reference

void Cleanup(CleanUpStruct& st)
{
}
....
case WM_DESTROY:
CleanUpStruct st;
st.member1 = //;
Cleanup(st);

Pass in pointer

void Cleanup(CleanUpStruct* pst)
{
}
....
case WM_DESTROY:
CleanUpStruct st;
st.member1 = //;
Cleanup(&st);

nappaji
January 18th, 2008, 05:53 PM
Thanks!
But the structure variable is already built in the main function. I dont want to make it a global variable.

its like

main()
{
Cleanupstruct st;

while..
PeekMessage()
TranslateMessage()
DispatchMessage()
}

msgproc(hWnd, msh, wparam, lparam)
{
case WM_DESTROY:
Cleanup(???)
}

Cleanup(Cleanupstruct st1)
{
//free mem..
}

kirants
January 18th, 2008, 05:58 PM
variable st has meaning only within the scope of main() function body. Within msgproc it has no meaning. You need share it / pass it to the window in some way.

BTW, are you sure you want to have cleanup code in msgproc and not before exiting main ? Just curious

nappaji
January 18th, 2008, 07:14 PM
Yes, i need to have cleanup code in this place.
I have a work around for now.
Before Dispatch Message, i check the msg value
if its WM_DESTROY, I call Cleanup() in the main function
i have removed the Cleanup code in the MsgProc fucntion for now until i find a solution.
in other words
<code>
PeekMessage()
TranslateMessage()
switch(msg.message)
case WM_DESTROY:
Cleanup(Cleanupst);
DispatchMessage()

</code>

MikeAThon
January 18th, 2008, 08:42 PM
In your window class, you can define extra bytes for each instance of a window (i.e., the cbWndExtra member of the WNDCLASS structure). You can specify (say) four extra bytes, and use these bytes to store a pointer to Main's Cleanupstruct "st" variable.

To store the pointer inside your Main() function, after the window has been created, use SetWindowLong() with GWL_USERDTA, and specify the proper offset.

To access this pointer from inside the MsgProc callback, use GetWindowLong with GWL_USERDATA, and cast the result to type (Cleanupstruct*).

Mike