WndProc()

Pages: 123
Hi, i am learning c++ through a book i have, and i dont fully understand what the following line of code does:

LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);

Could someone please explain everything in this line and what it does, in VERY simple terms please

:)
Last edited on
???
I know its a windows procedure, but im not too sure on how its used, or how windows uses it
Last edited on
? :)
Write the context of that line
this is it copy and pasted out of the cpp file:

//-----------------------------------------------------------------
// Skeleton Application
// C++ Source - Skeleton.cpp
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "Skeleton.h"

//-----------------------------------------------------------------
// Global Function Declarations
//-----------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);



anyone?
LRESULT is the return type of the function. CALLBACK is its calling convention ( http://en.wikipedia.org/wiki/Calling_convention ). WndProc is the name of the function. hWindow, msg, wParam and lParam are the names of its parameters, while HWND, UINT, WPARAM and LPARAM are the parameters' types. Windows API programming is full of these ugly all caps typedefs. LRESULT, for instance, is actually just an int, IIRC.

When you write a Windows API program, you have to define a Window Class. You define it by filling a struct and passing it to some function (I haven't done this in a while). One of the items in the struct holds a callback function that will be used to process Windows Messages (which might be triggered by user actions such as clicking, typing of moving the mouse, or sent by the system). That callback function is usually named WndProc, but it could be named anything, as long as the types are all correct.

When WndProc is called, the system will pass those four parameters to it so you can process the message. What you named hWindow is a handle to the window which sent the message. msg is the message itself, and the other two can change according to which message is being passed. Further reading: http://msdn.microsoft.com/en-us/library/ff381408(VS.85).aspx

PS: for most things you might want to do, it's easier, more satifying and much more fun to use a GUI toolkit rather than raw Windows API.
Last edited on
Thanks for that, as LRESULT is a long type, why not just put that actual type in, instead of putting LRESULT?
I edited my post quite a bit. You might want to read the rest of it.

To answer your question, I guess the reason they use all those typedefs is so they can change the inner representation if needed and keep programs as they are. There's no excuse for them being so ugly, though.
Last edited on
Thanks, i have a few questions...

1. So basically, do we use "typedef" to give a type another name?

2. What is typedef short for?

3. Im also abit confused on paremeters, i know paremeters are variables and the actaully data in the variable is refered to as an argument, but im not sure why we use them, whats the point in them, are they just variables we can use in the function it belongs to? Are paremeters basically private variables?
Last edited on
Also...

4. when you include a header file, whats the difference between these...

a. "example.h"

b. <example.h>

Last edited on
1: Yes, basically that is what typedef does. One reason you would do it is say you want to create a function that requires an int, but that int has a specific use and is returned from a specific set of functions, it should not be just any old number. You would do something like typedef int mynum (<- I'm not %100 on the syntax there atm). You would define the functions that return that int like this: myint myfunction(...), and the ones that use that special int like this void myfunction(myint);

2: Typedef = Type Definition. It means "This is how I define this data type".

3: I'm not sure how to answer this question. A variable isn't an argument until it's passed to a function. A parameter can be thought of as an argument with a specific purpose.

4.a: Tells the compiler to use the relative path of your executable when looking for that file. So if "myprog.exe" is in "C:\MyFolder" the complier also checks that folder for "example.h

4.b: This tells the compiler to look for <example.h> in the paths included in the linker. If you are using an IDE there are a few paths that are predefined if you want to use a third party library like Boost or SDL you need to add these paths.
Last edited on
if your interested in windows programming with the windows api this guy is the best. I had his programming windows 95 book as well. this book though is a tomb and will teach you everything under the sun with windows.

http://www.charlespetzold.com/pw5/index.html
Thanks for all the replies.

you say "A parameter can be thought of as an argument with a specific purpose"

could you or someone please give me a simple example when you would want a function with a paremeter?

I mean, why define an argument, why not just define a variable in the actual function itself?
Last edited on
Also, what exactly is a "macro" in programming? That word has come up quite abit.
And the part where it has "CALLBACK" in the wndproc(), is that a typedef for __stdcall
__stdcall is not a type and you can't typedef a non-type

CALLBACK is a #define

#define CALLBACK __stdcall
Last edited on
I believe a macro is merely a substitution
eg.
#define QWRTY cout << "Hello World"

anytime you use QWRTY; it will result in a cout << "Hello World"
closed account (yAqiE3v7)
Well since the definition was already cleared, a more neat way of putting it would be:
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM)


But hey whatever works and if you like it. :)
Pages: 123