Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinMain redefinition

Tags:

c++

winapi

I'm just starting out in C++, and I'm running into an error I can't fix.

Here's all my code so far (can't even get hello world to work):

#include "stdafx.h"
#include <windows.h>


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
               LPSTR lpCmdLine, int nCmdShow)
{
                   MessageBox(NULL, L"Hello World!",
                       L"Hello World!",
                       MB_ICONEXCLAMATION | MB_OK);
                   return 0;
}

But that gives this error when I try to run it:

Test.cpp(11): error C2373: 'WinMain' : redefinition; different type modifiers C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winbase.h(2588) : see declaration of 'WinMain'

When I look at the declaration of WinMain, I see that there is an "__in" before each of the parameters. I tried adding that, but had no luck. I also tried replacing WINAPI with CALLBACK, but that didn't work either.

like image 699
rob Avatar asked Sep 17 '25 09:09

rob


1 Answers

The simple solution is to

    Use a standard main function.

Like this:

#undef UNICODE
#define UNICODE
#incude <windows.h>

int main()
{
    MessageBox(
        0,
        L"Hello World!",
        L"Hello World!",
        MB_ICONEXCLAMATION | MB_SETFOREGROUND
        );
}

Now your only problem is to build it as a GUI subsystem application using Microsoft's toolset, which is a bit more than retarded in this respect (the GNU toolchain does not have such a problem).

For that, with Microsoft's link, use this linker option (in addition to selecting the GUI subsystem): /entry:mainCRTStartup.

Note that you can put that option in an environment variable called LINK.

Happy coding! :-)

like image 85
Cheers and hth. - Alf Avatar answered Sep 18 '25 22:09

Cheers and hth. - Alf