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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With