Overriding WinMain
I am just starting out with C ++ and I have an error that I cannot fix.
Here's all my code so far (can't even get hello world):
#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 it gives this error when I try to run it:
Test.cpp (11): error C2373: "WinMain": override; modifiers of various types C: \ Program Files (x86) \ Microsoft SDK \ Windows \ v7.0A \ include \ winbase.h (2588): see the "WinMain" declaration
When I look at the WinMain declaration, I see that there is "__in" in front of each parameter. I tried adding this, but no luck. I also tried replacing WINAPI with CALLBACK, but that didn't work either.
source to share
The simple solution is
Use a standard functionmain
.
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 with Microsoft's toolbox, which is a little behind in this regard (there is no such problem in the GNU programming chain).
To do this with Microsoft link
use this version of the linker (in addition to the GUI subsystem selection) /entry:mainCRTStartup
.
Note that this parameter can be placed in an environment variable called link
.
Happy coding! :-)
source to share
WinMain is a C function, so you need to wrap it with extern "C"
#include "stdafx.h"
#include <windows.h>
extern "C"
{
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, L"Hello World!",
L"Hello World!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
}
source to share