The entrypoint for all Win32 applications, even MFC-based apps. Even though MFC does not appear to have a WinMain, the MFC dll provides one for you through the miracles of abstraction.

The WinMain function prototype looks like this:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPCSTR lpCmdLine, int nCmdShow);

The function returns int, which is the return code of your application (as with any other app), and the calling convention is of type WINAPI, which is the standard API-type calling convention (sometimes you will see int PASCAL WinMain; these are the same)

The arguments are as follows:
HINSTANCE hInstance: First off, an HINSTANCE is Hungarian Notation for a Handle to the instance of the application (in reality, think module, Dll's have their own HINSTANCE). When you create the entry point, you get a Handle to yourself. Fairly easy. You can use it to load in your resources, and get information about your application.

HINSTANCE hPrevInstance: This lets you know if any other instances of the same module are in memory, and it gives you the handle to them. Otherwise it is null. A lot of programs only allow one instance to be running at a time (ICQ, AIM, etc). This is how they do it. They check to see if the instance is null, and if isn't they send a focus message to the other instance, and then gracefully clean up and exit.

LPCSTR lpCmdLine: A long pointer to const character string, #define'd for UNICODE and backwards compatability. It is the command line that called the program. This can be used to detect switches and to detect any documents that your document needs to open on launch.

int nShowCmd: The window creation code, that your application can choose to obey, or ignore. Usually you do the Right Thing (tm) and pass it into ShowWindow. This means that it will obey the "start this program minimized" shortcut property, etc. Otherwise, you can totally ignore it. Think of it as a runtime suggestion.

I think WinMain is gets called by WinExec (an old function). Check a stack trace if you want to be more sure. The entry point for a Dll is DllMain, and unlike WinMain, it is optional. It gets called when that type of module is loaded.

Log in or register to write something here or to contact authors.