C++ 中 WinMain、main 和 DllMain 的区别

发布于 2024-07-12 07:53:15 字数 28 浏览 4 评论 0原文

这三个函数有什么区别以及什么时候使用它们?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2024-07-19 07:53:15

ma​​in() 表示您的程序是一个控制台应用程序

WinMain() 表示该程序是一个 GUI 应用程序——即,它显示窗口和对话框而不是显示控制台。

DllMain() 表示该程序是一个 DLL。 DLL不能直接运行,但可以被上述两种应用程序使用。

因此:

  • 当您编写要显示窗口等的程序时,请使用 WinMain。
  • 当您编写 DLL 时,请使用 DLLMain。
  • 在所有其他情况下使用 main。

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.
π浅易 2024-07-19 07:53:15

WinMain 用于应用程序(以 .exe 结尾),指示进程正在启动。 它将为进程提供命令行参数,并充当进程的用户代码入口点。 WinMain(或 main 的不同版本)也是必需的函数。 操作系统需要调用一个函数来启动进程的运行。

DllMain 用于 DLL 来表示许多不同的场景。 最值得注意的是,它将在以下情况下被调用

  1. : DLL 加载到进程中: DLL_PROCESS_ATTACH
  2. DLL 从进程中卸载:
  3. DLL_PROCESS_DETACH 进程中启动线程: DLL_THREAD_ATTACH
  4. 进程中结束线程: DLL_THREAD_DETACH

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

  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.

空袭的梦i 2024-07-19 07:53:15

[问题的附录]

另外不要忘记 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文