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.

Dani AI

Generated

Most likely cause: a macro name collision. The compiler errors shown (C2275 / C2065 at the line MSG msg;) happen when the token MSG is getting replaced by a preprocessor macro (for example #define MSG UserMsg) or otherwise mangled before the compiler sees the Windows typedef. That matches ’s note about finding a #define MSG ... somewhere — once MSG is a macro the statement MSG msg; expands to something that is not a type and the parser fails.

Useful diagnostic steps (quick checklist):

  • Search the whole solution and the include paths for #define MSG (Visual Studio: Edit → Find and Replace → Find in Files; include External Dependencies and SDK folders).
  • Inspect Project Properties → C/C++ → Preprocessor → Preprocessor Definitions for an accidental MSG entry.
  • Check any precompiled header (e.g. stdafx.h), resource.h, and third‑party headers that are included before windows.h.
  • Generate the preprocessed source (Project Properties → C/C++ → Preprocessing → Preprocess to a File or use cl.exe /P) and search the produced .i file to see where MSG is defined/changed.

Practical fixes (safe order):

  • Remove or rename the offending #define at its source (preferred).

  • If the macro comes from a third‑party header that cannot be changed, include that header, then explicitly #undef MSG before using the Win32 MSG type (temporary workaround). Example pattern:

    #include "thirdparty.h"
    #ifdef MSG
    #undef MSG
    #endif
    #include <windows.h>   // or ensure windows.h is included before the offending header
  • Minimize collisions by defining WIN32_LEAN_AND_MEAN before including windows.h, and compare project settings between the working Win32 project reported by and the failing project to spot differences in include order or preprocessor definitions.

Caution: do not edit system SDK headers; prefer fixing project/preprocessor settings or changing the third‑party header. The existence of tagMSG already (the C2011 redefinition seen when manually declaring tagMSG) confirms the Windows typedef is present — the problem is a macro clash rather than a missing Windows type.

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.