C++ 中 WinMain、main 和 DllMain 的区别
这三个函数有什么区别以及什么时候使用它们?
What is the difference between the three functions and when to use them??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这三个函数有什么区别以及什么时候使用它们?
What is the difference between the three functions and when to use them??
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
main() 表示您的程序是一个控制台应用程序。
WinMain() 表示该程序是一个 GUI 应用程序——即,它显示窗口和对话框而不是显示控制台。
DllMain() 表示该程序是一个 DLL。 DLL不能直接运行,但可以被上述两种应用程序使用。
因此:
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:
WinMain 用于应用程序(以 .exe 结尾),指示进程正在启动。 它将为进程提供命令行参数,并充当进程的用户代码入口点。 WinMain(或 main 的不同版本)也是必需的函数。 操作系统需要调用一个函数来启动进程的运行。
DllMain 用于 DLL 来表示许多不同的场景。 最值得注意的是,它将在以下情况下被调用
DllMain 是一个可选构造,有很多与之相关的隐性合同。 例如,您不应调用强制加载另一个 DLL 的代码。 一般来说,这是一个相当难以正确使用的功能,除非您有非常具体的需求,否则应该避免使用它。
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
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.
[问题的附录]
另外不要忘记 DllEntryPoint:
当涉及加载时间时,入口点是 DllMain。
(例如 COM 进程内服务器 DLL)。
当涉及运行时间时,入口点是 DllEntryPoint。
(例如,LoadLibrary 被调用)。
[Addendum to your question]
Also don't forget the DllEntryPoint:
When loading time is involved the entry point is DllMain.
(Ex. COM in-process server DLL).
When running time is involved the entry point is DllEntryPoint.
(Ex. LoadLibrary get called).