Simple Windows API Application questions

I have a couple questions about the code I got from an online Windows API tutorial. Please when you answer these questions don't use complicated computer speech. If I understood that stuff I would have been able to look this stuff up somewhere else. Thanks.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}


WINAPI and CALLBACK are calling conventions. What is a calling convention? Also what is LRESULT before the windows procedure?

In the CreateWindowEx() function:

1
2
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);


WS_EX_CLIENTEDGE is defined as an extended window style, and WS_OVERLAPPEDWINDOW is defined as a window style. What's the difference? Then, the TranslateMessage() function is said to convert virtual key messages to character messages. What are virtual key messages? Could you give an example? Then in the end I have:

return Msg.wParam

What is the point of this? What is the value being returned? And in the windows procedure:

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

What is wParam and lParam?
Situations like this is why I think that attempting to learn a new language or how to program to a particular OS by searching around around the internet and peicing together tutorials is not a good idea.
It tends to be error prone, create more questions than answers and there is a lot of stale infoRMATION floating about the internet.
YOU CAN'T BEAT A GOOD BOOK!

I started to write an answer and thought Why am I doing all this writing? YOU NEED A BOOK ON THIS !

But on the subject of calling conventions, this will help:
http://www.codeproject.com/KB/cpp/calling_conventions_demystified.aspx



Last edited on
Microsoft provides a number of #defines in their Windows API header files to make programming Windows applications portable across OS versions.

You are going to get a fairly technical answer because you are asking fairly technical questions.


A calling convention is the way in which a function is called. There are several things to account for when a function is called, but the most important are: how are the arguments passed to the function and how is the result returned?

WINAPI and CALLBACK are found in windef.h as
1
2
#define WINAPI __stdcall
#define CALLBACK __stdcall 

There are others, but Win32 functions are almost always stdcall. What the "standard calling" convention means is basically that:
1. All the function's arguments are pushed onto the stack in reverse order.
2. The function pops the arguments off the stack when it returns.
3. The result is returned in EAX or EDX:EAX depending on its size.
(There are variations for non-integer results, but you can forget it for now.)

For example, if I were to call a function int foo( int a, char b ); as x = ( 42, 'A' ); that would generate machine code that looks something like the following (simplified):
1
2
3
4
  PUSH 'A'
  PUSH 42
  CALL @__foo
  MOV EAX --> [x]

Since the function pops all the arguments, line 4 didn't need to be preceeded by any code that popped the 42 and 'A' from the stack (it was already done). The result value was put in the EAX register, so we stored its value in the memory location at x.


Return values in C and C++ are expressed before the function name. In the above example, foo() returned an integer. Hence, the function was typed (or "prototyped") as:
int foo( int a, char b );
Again, the windows headers #define values to make life easier. LRESULT ultimately evaluates to essentially
#define __int64 LRESULT
So a LRESULT value is a 64-bit integer, meaning that WinMain() returns a 64-bit integer to the OS.

The difference between window styles and extended window styles is that extended window styles are additional (or new) window styles that didn't exist when the original window styles were first created.


Virtual key codes are a way of tracking which keys are pressed or released (or auto-repeating) from the keyboard. Keyboards don't actually send ASCII characters to the computer. That is, pressing the 'A' key doesn't send an 'A' to the computer. It sends a number. Different keyboards may send different numbers. (PC keyboards tend to all use the same number, but that is beside the point.)

Windows takes that number and converts it into another number: the Virtual Key code. Applications that use the Win32 API can then use these known numbers (because they are pre-defined to be the same thing no matter what the actual hardware attached to the computer is) to determine the state of the keyboard keys.

For standard I/O streams and other character applications, these Virtual Key codes are translated into the ASCII character codes we all know and love. Raw key messages (obtained from GetMessage()) don't have that extra information, so TranslateMessage() must be used to recognize key messages and modify them to have that extra, useful, friendly information.


Messages are the way in which Windows communicates with applications. Applications, by themselves, typically don't have direct access to the hardware. (For reasons I won't go into, that is a Good Thing.)

Windows, as the Operating System --meaning it operates (or manages) the computer system (hardware and software), is a kind of translator between the hardware and the application software.

An analogy would be something to the effect of someone from the UK going to Mexico as part of a business deal. While in Mexico, the English-speaking brit has no clue what the locals are saying. He needs someone to translate for him. So if the brit were to say "you're stupid" the translator would have the tact to say "no me cae bien". If he were in Japan he might get a more literal translation: "baka na". But the effect on the recipient is the same in both cases.

A messages is composed of three parts: the message code (what kind of message it is), and two additional codes that have meaning based upon the message. Read more here:
http://winprog.org/tutorial/message_loop.html

Well, that's about it. Good luck!
Topic archived. No new replies allowed.