EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Main vs WinMain

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. First argument is the instance handle of the current process. Next is the previous instance. Command line arguments comes as next argument. Finally shell passes the show/display attribute of main window. WinMain returns success as zero and error as non zero.

WinMain

The WinMain function is called by the system as the initial entry point for a Win32-based application.

Syntax

int WINAPI WinMain(
  HINSTANCE hInstance,     /* [input] handle to current instance */
  HINSTANCE hPrevInstance, /* [input] handle to previous instance */
  LPSTR lpCmdLine,         /* [input] pointer to command line */
  int nCmdShow             /* [input] show state of window */
);

Parameters

  • hInstance - Handle to the current instance of the application.
  • hPrevInstance - Handle to the previous instance of the application. For a Win32-based application, this parameter is always NULL.
  • lpCmdLine - Pointer to a null-terminated string specifying the command line for the application, excluding the program name. To retrieve the entire command line, use the GetCommandLine() function.
  • nCmdShow - Specifies how the window is to be shown.

Return Values

If the function succeeds, terminating when it receives a WM_QUIT message, it should return the exit value contained in that message's wParam parameter. If the function terminates before entering the message loop, it should return zero.

WinMain Demo Application

WinMain Demo Application

WinMain Demo Source Code

#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   MessageBox(NULL, lpCmdLine, "WinMain Demo", 0);
10    return 0;
11  }
12 

WinMain - Execution

c:\winmain\Debug>winmain.exe This is our WinMain Demo. Happy programming!

WinMain - life cycle diagram

WinMain Application process life cycle

Win32 program can be executed by double clicking the executable from explorer window or from command line by typing the command name and argument to it. Windows operating system shell (cmd.exe) or explorer acts as a parent process. It invokes CreateProcess() API and provides executable path, command line arguments, environment variables and other necessary parameters. Windows kernel subsystem creates a process or task and one new main thread begins execution. Parent process wait till the end of execution of this child process. This process main thread takes the entry point function which is WinMain(). C Win32 compiler takes care of this mapping. It places a startup routine which is needed by C program startup and WinMain() is the first entry function called. WinMain() generally creates a main Window and process a message loop till the main window is dismissed. Here we have called MessageBox() function to create a popup dialog box. This dialog will stay in the screen and WinMain() will be in execution. This function comes from user32.dll and the message loop is processed inside this win32 library. This message loop waits till we press escape or hit enter or click on OK button. The dialog message loop ends and MessageBox() function returns. Now it reaches at the end of WinMain() and it returns 0 or success. This return value is processed in C startup and passed to ExitProcess() call. ExitProcess() informs Windows kernel task scheduler to end the process or main thread and mark the exit code of the process as zero or success. Now this process ends and parent process can read the status exit code.

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 1

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 16:44:57(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.equestionanswers.com. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.