Hello all.
I have some problems with compilation while working with WinApi.
Here is the code:

// include the basic windows header file
#include <windows.h>
#include <windowsx.h>

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    // the handle for the window, filled by a function
    HWND hWnd;
    // this struct holds information for the window class
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = "WindowClass1";

    // register the window class
    RegisterClassEx(&wc);

    // create the window and use the result as the handle
    hWnd = CreateWindowEx(0,
                          "WindowClass1",    // name of the window class
                          "Our First Windowed Program",   // title of the window
                          WS_OVERLAPPEDWINDOW,    // window style
                          300,    // x-position of the window
                          300,    // y-position of the window
                          500,    // width of the window
                          400,    // height of the window
                          NULL,    // we have no parent window, NULL
                          NULL,    // we aren't using menus, NULL
                          hInstance,    // application handle
                          NULL);    // used with multiple windows, NULL

    // display the window on the screen
    ShowWindow(hWnd, nCmdShow);

    // enter the main loop:

    // this struct holds Windows event messages
    MSG msg;

    // wait for the next message in the queue, store the result in 'msg'
    while(GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;
}

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc (hWnd, message, wParam, lParam);
}

If you look at it it seams that there are no problems, but there are some errors rising while compiling it, the two most importants are :
Error 1 error C2065: 'msg' : undeclared identifier
Error 2 error C2275: 'MSG' : illegal use of this type as an expression
Thise errors are given on the next line:

MSG msg;

But i think the code is correct . Im using Visual Studio Professional edition.
Thanks in advance.

Recommended Answers

All 9 Replies

Include winuser.h
structure MSG is defined in that header

>>Include winuser.h

That should not be necessary since he's already including windows.h

>>Im using Visual Studio Professional edition.
Which version -- there are several of them.

>>but there are some errors rising while compiling it, the two most importants are :
Are there other errors listed before the two you posted? Correct those first, recompile then see if those two errors disappear.

The other errors rise after the two given(and are refernced to the msg variable, so if i correct the two given,i'l get rid of the rest).
The include didnt helped.

I compiled your program with vc++ 2008 Express and had no errors or warnings. I also have Microsoft Windows SDK installed, but since you have the Pro edition that shouldn't be your problem. Are you certain you created a "win32 project" instead of a console project? Attached is the project I created.

Thanks for the project.
It does work and does compile, in the last project i had WIndows Sdk (latest) and DirectX Sdk only. But will try removing the DirectX Sdk.

//Dont know why the edit button is not showed.
Edit:
Tried to remove the reference to directx sdk, reseting the windows sdk directory (resetting the lib directory also), but none of that helped .
Btw, i use Visual Studio 2008 Professional Edition.

Once again i dont see the edit button...
I've created an Empty Project, and winapi hello world

#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
MessageBox(NULL,"a","b",0);
return 0;
}

Worked just fine.
I also searched for the definition of MSG and found #Define MSG UserMsg. Triyed to put it in my code and it said that UserMsg is not recognized... Maybe there is any problem with my sdk (its v6.0A) ?
Going to check other sdk i have here (Windows Server 2003 R2). Maybe thise would help...
Edit:
Tested, same problem.

The IDE will take you directly to where MSG is declared -- just put the mouse on the 'MSG' then right click. From the popup menu select the second item "Go To Definition". When I do that the IDE takes me to WinUser.h and tagMsg structure.

Tryied to define :
tagMSG varxx;
FAR varxx;
And same error rised, even if i included the #WinUser.h
The interesting thing is that i tried to define tagMSG structure, and compiler said that the structure is already defined. (error C2011: 'tagMSG' : 'struct' type redefinition)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.