Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between WinMain,main and DllMain in C++

Tags:

What is the difference between the three functions and when to use them??

like image 302
Ahmed Avatar asked Jan 06 '09 14:01

Ahmed


People also ask

What is the difference between main and WinMain?

WinMain() is Windows specific entry point to a Windows-based graphical application (you have windows stuff). main() is a standard C++ entry point (in Windows, it's a console based application)... That said, you can use GUI stuff in console applications and allocate console in GUI applications.

What is DllMain used for?

DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL.

What is WinMain in 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 DllMain?

The DllMain function is an optional method of entry into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to LoadLibrary and FreeLibrary.


2 Answers

main() means your program is a console application.

WinMain() means the program is a GUI application -- that is, it displays windows and dialog boxes instead of showing console.

DllMain() means the program is a DLL. A DLL cannot be run directly but is used by the above two kinds of applications.

Therefore:

  • Use WinMain when you are writing a program that is going to display windows etc.
  • Use DLLMain when you write a DLL.
  • Use main in all other cases.
like image 120
Frederick The Fool Avatar answered Sep 27 '22 23:09

Frederick The Fool


WinMain is used for an application (ending .exe) to indicate the process is starting. It will provide command line arguments for the process and serves as the user code entry point for a process. WinMain (or a different version of main) is also a required function. The OS needs a function to call in order to start a process running.

DllMain is used for a DLL to signify a lot of different scenarios. Most notably, it will be called when

  1. The DLL is loaded into the process: DLL_PROCESS_ATTACH
  2. The DLL is unloaded from the process: DLL_PROCESS_DETACH
  3. A thread is started in the process: DLL_THREAD_ATTACH
  4. A thread is ended in the process: DLL_THREAD_DETACH

DllMain is an optional construct and has a lot of implicit contracts associated with it. For instance, you should not be calling code that will force another DLL to load. In general it's fairly difficult function to get right and should be avoided unless you have a very specific need for it.

like image 40
JaredPar Avatar answered Sep 27 '22 21:09

JaredPar