MSG msg;

Hi,in the WinMain()its got:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT(“Skeleton”);
WNDCLASSEX wndclass;
HWND hWindow;
MSG msg;

The thing i dont quite understand in this is the msg variable which is of the MSG type. I have googled this and i cant find nothing. Why am i predefining msg? what is it? and where do i use it in WinMain()?
Last edited on
Why is this not in the Windows Programming thread?
http://www.cplusplus.com/forum/windows/

I have googled this and i cant find nothing.

I just typed: "MSG win32 api" in the search box, and got this:
http://www.google.com/search?hl=en&source=hp&biw=1280&bih=835&q=MSG+win32+api&btnG=Google+Search&aq=f&aqi=&aql=&oq=

and the first result got me this:
http://www.toymaker.info/Games/html/wndproc.html

Why am i predefining msg?

You need MSG in windows programming the way you need int's and char's and string's in console programming

what is it?

"Windows sends some messages directly to your callback function but others are placed in a queue. These tend to be keyboard messages or other input..."
source: http://www.toymaker.info/Games/html/winmain.html

where do i use it in WinMain()?

If you make a window more complex than a simple message box, you will need to process MSG's
from the code you posted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//snipped
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT(“Skeleton”);
WNDCLASSEX wndclass;
HWND hWindow;
MSG msg;
//fill up the window structure
//..and icons and cursors
//...and colors
//then, register your 'class'
//...and create your window
//...don't forget to 'show' it
//and, finally we're at the message loop, where 'msg' is processed:
/* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&msg, NULL, 0, 0) > 0)
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&msg);
        /* Send message to WindowProcedure */
        DispatchMessage(&msg);
    }
//return something... but we're not there, yet ;)
}

- the message loop (and comments) came from Code::Blocks (with a bit of modification -credit theForger's winAPI tutorial)
Last edited on
closed account (z05DSL3A)
TpOreilly,

As in a previous post [1] I refer you to the MSDN tutorial:
Learn to Program for Windows in C++
http://msdn.microsoft.com/en-us/library/ff381399(v=VS.85).aspx


[1] http://cplusplus.com/forum/beginner/36706/page2.html#msg200331
Thanks, when we use the GetMessage(&msg, NULL, 0, 0); function why do we fill &msg with the information about the message, because by doing that are we not putting information into msg's address in memory?

Should it not be:

GetMessage(msg, NULL, 0, 0);
Last edited on
???
?????
that helps...
... why do we fill &msg with the information about the message, because by doing that are we not putting information into msg's address in memory?

That's what Windows (the os) expects. Anytime a window (your app) is created, the os creates a message queue for that thread. GetMessage() will retrieve a message from the queue, and returns a '0' or '1' (actually nonzero) or '-1' (for errors).
We fill &msg not only for GetMessage() but for TranslateMessage() and DispatchMessage too.

GetMessage(msg, NULL, 0, 0);// assume earlier we declared: MSG msg;

GetMessage() expects a pointer to a message structure, so we pass an alias of its instance i.e &msg. Anything other than that gives a compile error. Also remember that passing a copy of your structure to a function is inefficient.

ref.
1. http://msdn.microsoft.com/en-us/library/ff381405%28v=VS.85%29.aspx
2. http://msdn.microsoft.com/en-us/library/ms644936%28v=VS.85%29.aspx
edit: added references...
Last edited on
Do we fill &msg and not msg because msg is a structure, so if we tried to fill it in a variable, msg, we would not be filling the structure, thats why we fill &msg because we are telling it where the attributes are located?
I would guess we are passing the address (&msg) as opposed to the actual variable (msg) because we want the function to modify the variable we are passing so we can use the information later.
hmmm, im still a little confused, would you be able to answer my previous question in a different way please, i mean, you said "want the function to modify the variable we are passing so we can use the information later", im abit confused about that part :)

Also, a question on the message which GetMessage() recieves...

I understand that GetMessage() pulls the first message of the head of the queue, my question is what if the message doesnt apply to the application, does it just reject it?
I understand that GetMessage() pulls the first message of the head of the queue, my question is what if the message doesnt apply to the application, does it just reject it?

Easier to think that GetMessage() only return -1/0/nonzero -that's all it does.

If you refer to the GetMessage() loop I posted earlier you'd see that all nonzero messages (paint, keystroke, mouse clicks, etc) will go into the loop; if needed, TranslateMessage() will process it first
then everything will passed on to DispatchMessage() which passes it to the window procedure (there should be one in every nontrivial win32 app).

There'd be a switch-case block in the window procedure that handles all the messages passed to it by DispatchMessage() -the ones a programmer wants/needs to handle will be processed by the app, while the ones that "doesnt apply to the application" will be handled by the DefWindoProc() function.

p/s
1. all of these are of course in the links posted in this very thread
2. I'm not sure about this, but has anybody recommended you Petzold's (holy) book?
3. how much have you coded win32 apps? any tutorial you're currently following?

Ahh ok, thanks :) Now back to the other question of...

"Do we fill &msg and not msg because msg is a structure, so if we tried to fill it in a variable, msg, we would not be filling the structure, thats why we fill &msg because we are telling it where the attributes are located?"

Am i on the right track with that, or am i completely wrong in saying that :)

? :D
I think so. Your wording is a bit awkward. If you were to pass simply msg, you would pass a copy and any changes made to it in the function would be lost after it returns. By passing the address, the function can modify the variable you have (putting in message information) so you can use it after the function ends. Consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void modify_normal(int normal) {
    ++normal; // add 1
}

void modify_pointer(int* ptr) {
    ++(*ptr);
}

int main() {
    int msg = 0;
    std::cout<<"msg = "<<msg<<std::endl;
    modify_normal(msg);
    std::cout<<"msg = "<<msg<<std::endl;
    modify_pointer(&msg);
    std::cout<<"msg = "<<msg<<std::endl;
    return 0;
}

Run this and you'll see that msg isn't changed in the first instance, but is in the second.
Ahhhhhhh, i see. We cant just give it msg because we would not be assigning anything to msg, we would just be passing over whatever msg is equal to.

Thanks
Topic archived. No new replies allowed.