WainZhang
人生总有许多巧合,两条平行线也可能会有交汇的一天。 人生总有许多意外,握在手里面的风筝也会突然断了线。 在这个熟悉又陌生的城市中,无助地寻找一个陌生又熟悉的身影。

 

每个Windows程序都包含一个入口点函数,该函数被命名为WinMainwWinMain。下面是wWinMainsignature

C++

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);

四个参数为:

  • hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions—for example, to load icons or bitmaps.
    hInstance
    被称为实例句柄模块句柄。当可执行文件被加载到内存中时,操作系统使用这个值来标识它。某些Windows功能需要实例句柄——例如,加载图标或位图。
  • hPrevInstance has no meaning. It was used in 16-bit Windows, but is now always zero.
    hPrevInstance
    没有意义。它曾用于16位的Windows,但现在总是为零。
  • pCmdLine contains the command-line arguments as a Unicode string.
    pCmdLine
    包含作为Unicode字符串的命令行参数。
  • nCmdShow is a flag that says whether the main application window will be minimized, maximized, or shown normally.
    nCmdShow
    是一个标志,表示主应用程序窗口是最小化、最大化还是正常显示。


函数返回一个int值。操作系统不使用返回值,但是可以使用返回值将状态代码传递给您编写的其他程序。


WINAPI
是调用约定。调用约定定义函数如何从调用者接收参数。例如,它定义了参数在堆栈上出现的顺序。确保声明wWinMain函数如所示。


WinMain
函数与wWinMain函数相同,除了命令行参数作为ANSI字符串传递。最好使用Unicode版本。即使将程序编译为Unicode,也可以使用ANSI WinMain函数。要获得命令行参数的Unicode副本,请调用GetCommandLine函数。此函数将返回单个字符串中的所有参数。如果您希望参数作为argv样式的数组,请将此字符串传递给CommandLineToArgvW


编译器如何知道调用wWinMain而不是标准的main函数?实际上,Microsoft C运行时库(CRT)提供了main的实现,该实现调用WinMainwWinMain


注意

CRTmain内部做一些额外的工作。例如,在wWinMain之前调用任何静态初始化器。虽然可以告诉链接器使用不同的入口点函数,但是如果链接到CRT,则使用默认值。否则,将跳过CRT初始化代码,产生不可预知的结果。(例如,全局对象将不能正确初始化。)

 

 

这是一个空的WinMain函数。

C++

INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

    PSTR lpCmdLine, INT nCmdShow)

{

    return 0;

}

 

 

来源:https://docs.microsoft.com/zh-cn/windows/desktop/learnwin32/winmain--the-application-entry-point

posted on 2018-07-25 16:29  WainZhang  阅读(450)  评论(0编辑  收藏  举报