如何在不使用 WinMain 的情况下编写 Windows 应用程序?
用 C/C++ 编写的 Windows GUI 应用程序将“WinMain”作为入口点(而不是“main”)。 我对此的理解是编译器生成一个由 C 运行时调用的“main”函数。 这个“main”函数为 GUI 设置必要的环境并调用“WinMain”(指定实例句柄等)。
简而言之,我认为控制台和 GUI 应用程序启动存在以下差异:
控制台应用程序: C 运行时 --> “main”函数(手动编码)
GUI 应用程序: C 运行时 --> 'main' 函数(编译器生成)--> “WinMain”函数(手动编码)
我想验证这种理解,并了解如何仅使用“main”函数(即无需编写“WinMain”)来手动编码 Windows GUI。
Windows GUI applications written in C/C++ have 'WinMain' as an entry point (rather than 'main'). My understanding of this is that the compiler generates a 'main' function to be called by the C Runtime. This 'main' function sets up the necessary environment for the GUI and calls into 'WinMain' (specifying the instance handles etc.).
In short, I believe console and GUI application startup to differ in the following way:
Console application:
C Runtime --> 'main' function (hand-coded)
GUI application:
C Runtime --> 'main' function (compiler-generated) --> 'WinMain' function (hand-coded)
I would like to both validate this understanding and find out how I can hand-code a Windows GUI with just a 'main' function (i.e. without having to write 'WinMain').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的理解不正确。 main 和 WinMain 之间的区别除了一些不同的初始化代码之外,还在于传递给它的参数。
main 看起来像这样:
而 WinMain 看起来像这样:
必须设置这些参数并进行调用,这就是启动代码。 当您编译和链接程序时,链接器参数之一是入口点,根据控制台或 GUI 应用程序,这将是不同的启动代码位。
你当然可以编写自己的启动代码,只需进入你的 Visual C++ 源目录,你就可以找到启动代码,它名为 crt0.c,位于 VC\crt\src 目录中。
You have an incorrect understanding. The difference between main and WinMain, apart from some differet initialization code, is the parameters passed to it.
main looks like this:
While WinMain looks like this:
Something has to setup those parameters and make the call, and that's the startup code. When you compile and link a program, one of the linker parameters is the entry point, and that will be, depending on a console or GUI app, a different bit of startup code.
You can certainly write your own startup code, just go into your visual c++ source directory and you can find the startup code, it's called crt0.c and it's in the VC\crt\src directory.
使用 Just main,您无法编写 Winmain 代码。 为了说明理由,以下声明摘自
http://blogs.msdn.com/oldnewthing/archive/ 2007/12/03/6644060.aspx
希望这能消除您的疑虑。
With Just main, you can not code Winmain. For justifications, Following statements were taken from
http://blogs.msdn.com/oldnewthing/archive/2007/12/03/6644060.aspx
Hope this clears your doubts.
反之亦然。 编译器附带一个静态链接的目标文件,它保存实际的入口点。 该入口点进行初始化,然后调用您的入口点(即 WinMain)。
该静态部分期望调用的内容可能是可调整的。 例如,在 Visual Studio 中,链接器设置中有一个入口点名称字段。
It works the other way. There's a statically linked object file which comes with the compiler that holds the actual entry point. That entry point does initialization and then calls your entry point (i.e. WinMain).
What that static part expects to call may be tweakable. For example, in Visual Studio there's a field for the entry point name in the linker settings.