WinMain, WndProc & Variable

Started by
24 comments, last by CrazyCdn 15 years, 1 month ago
If I have a variable declared in the WinMain function, how do I get it sent to the WndProc function so it knows about it too? I know the normal way but its not like the function is like any other... The declared variable is of a class..
Advertisement
Quote:Original post by 1st_maza
I know the normal way but its not like the function is like any other...
I don't really understand this... WndProc and WndMain are just like every other function! Or do you mean because WndProc is a callback and you don't call it directly yourself?

The normal way would be to use the SetWindowLong function to "attach" your extra data to the window handle (passing GWL_USERDATA as the nIndex parameter), and you can then use GetWindowLong in your WndProc to get it back again.
wndProc is the message handler I assume, so You would just send a message and that is past to and thru wndProc. look up the message pump for more help. You probably want user message with user data right? well look up doing that google should have a very detailed article
Bring more Pain
What I mean is that it cant really be changed (the parameters in the functionhead), last time my compiler whined when I did that..

edit:
LRESULT CALLBACK WndProc( HWND hwndMain, UINT msg, WPARAM wParam, LPARAM lParam );
It depends on what the variable is and its usage. Winproc is a function like any other standalone c style function, if the variable was in relation to a window then there is a method of adding extra space in the window class otherwise you have to extend the scope in which it is defined to be known about in the function. ie make it a global or namespaced variable.
http://msdn.microsoft.com/en-us/library/ms644928(VS.85).aspx


if you need more help let me know
Bring more Pain
My teacher slaps me on my fingers if I do another global variable :P

Namespace is something I would prefer staying away from :P

In winmain there is a class variable declared that is used in almost all functions (winmain, wndproc, initialize, draw).

It's just lately I've started using variables declared within classes, before that I got alot of slaps on my fingers from the teacher about using global variables, but now im reaching almost 2d programming so its time to change.
I think a better question to the OP would be, what exactly are you trying to pass to the WndProc. You realize its sole purpose is to process messages from Windows. Having written more then my fair share of Win32 code I can honestly say I've never needed to pass a variable from WinMain to my WndProc function.

Maybe we can help you design your program better. Explain what your trying to pass and why you think you need too.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

I have a class.
That class has alot of variables, bools mostly.
In four of my functions those variables are needed for different reasons.

winmain & wndproc, everyone knows these ones..
initialize, is used to create d3d, sprite, load image..
draw, in this function things can be altered with the image that was just loaded, examles: rotate, scale, move left or right..

I was able to send the variable through to the initialize & draw functions but I have really no idea how to send it to the wndproc function. Since this is really the first time Ive ever needed to send a variable to the wndproc.
You cant pass a variable of your own the windows procedure, atleast i dont think so, usually you would want to just have the window procedure inside the same window class but this causes even bigger problems. for now try not to use the windows procedure to much as things can get complicated when using classes with it. just handle mandatory windows stuff like quiting to program that doesnt make use of or manipulate any of your own class members/methods. For the stuff you wanted in the window proc, create another function that mimmics the message you was trying to handle in some way and call it elsewhere. Once you get a little better, you can take a look at this code, it demonstrates a few waya of getting a windows procedure in a class and working as expected.

http://www.bsdpower.com/code/2003/01/29/window-procedures-as-class-member-functions/

This topic is closed to new replies.

Advertisement