Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing WinMain() with main() function in Win32 programs

Tags:

c++

winapi

I searched a little bit on StackOverflow and Google but couldn't get the idea. I want to start my application with this type of user programming:

int main() {   Window App("Test", 640, 480);    while(App.IsOpen())   {     // Do the stuff   } } 

But this isn't possible because I should pass the hInstance and hPrevInstance and other parameters to a WinMain function. Actually there is a Window class which I designed to make the window creation a little bit easier. I saw this implementation on SFML but I don't know how it did come to this.

Right now I'm using the usual way:

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR, int) {   Window App(hInst, hPrevInst, "Test", 640, 480);    while(App.IsOpen())   {     // Do the stuff   } } 

Thanks.

like image 378
MahanGM Avatar asked Aug 02 '12 20:08

MahanGM


People also ask

Should I use WinMain or wWinMain?

The only difference between WinMain and wWinMain is the command line string and you should use wWinMain in Unicode applications (and all applications created these days should use Unicode). You can of course manually call GetCommandLineW() in WinMain and parse it yourself if you really want to.

Which function in win32 API is equivalent to the main () function of C or C++?

WinMain() is the C entry point function of any windows application. Like normal DOS/console based application which has main() function as C entry point, in windows we have WinMain() instead. WinMain() is a function which is called by system during creation of a process.

What is a WinMain function?

The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred. You can use the ANSI WinMain function even if you compile your program as Unicode. To get a Unicode copy of the command-line arguments, call the GetCommandLine function.

Which is the parameter of WinMain ()?

hInstance and hPrevInstance: - are parameters of the WinMain function, hInstance is the handle to the current instance of the application, and the hPrevInstance is a handle to the previous instance of the application, hPrevInstance is always NULL.


1 Answers

You can use standard main in a "windows" app (that is, a GUI subsystem Windows application) even with the Microsoft tools, if you add the following to the Microsoft linker options:

/subsystem:windows /ENTRY:mainCRTStartup 

Note that this is not necessary for the GNU toolchain.

Still for the Microsoft tools you can alternatively add this to your main file:

#ifdef _MSC_VER #    pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup") #endif 

James McNellis tells you how to get the hInstance.

like image 186
jcoder Avatar answered Sep 19 '22 20:09

jcoder