GetModuleHandle(NULL)与hInstance [英] GetModuleHandle(NULL) vs hInstance
问题描述
使用Windows API进行编程时,我总是始终将 WinMain
中的 HINSTANCE
设置为全局变量。如果要单击确定按钮,则可以这样做(考虑到全局 HINSTANCE g_hInstance
):
When programming using the Windows API, I've always made the HINSTANCE
from WinMain
a global variable immediately. If I want to make an OK button, I'd do it like so (given global HINSTANCE g_hInstance
):
return CreateWindow("BUTTON", "OK", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 10, 10, 100, 30, exampleParentWindow, EXAMPLECHILDID, g_hInstance, NULL);
但是最近我一直在确定实例句柄,而不必将其作为参数传递或阻塞使用对 GetModuleHandle(NULL)
*的调用来建立全局名称空间。因此,上面的示例如下所示:
but lately I've been seeing the instance handle determined without having to be passed as a parameter or clogging up the global namespace, using a call to GetModuleHandle(NULL)
*. So, the example above would look like this:
return CreateWindow("BUTTON", "OK", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON, 10, 10, 100, 30, exampleParentWindow, EXAMPLECHILDID, GetModuleHandle(NULL), NULL);
*如果编译器支持,则可以编写 GetModuleHandle(nullptr)
和语句将具有相同的结果。
*If your compiler supports it, you can write GetModuleHandle(nullptr)
and the statement will have the same result.
调用 GetModuleHandle(NULL)<有什么好处(如果有)
是否显式指定了实例句柄?
What's the advantage (if any) of calling GetModuleHandle(NULL)
over explicitly specifying the instance handle?
精细打印:我知道这有一个答案,但它没有被表述为自己的答案
推荐答案
在EXE中,它没有任何区别。来自 WinMain()
和 GetModuleHandle(NULL)
的 hInstance
请参考相同的 HINSTANCE
(.exe文件的模块)。但是,如果您在DLL内部创建窗口,确实会有所不同,因为您必须使用DLL的 hInstance
但 GetModuleHandle(NULL)
仍将返回加载DLL的EXE的 HINSTANCE
。
In an EXE, it does not make any difference. hInstance
from WinMain()
and GetModuleHandle(NULL)
both refer to the same HINSTANCE
(the module of the .exe file). But it does make a difference if you are creating windows inside of a DLL instead, since you have to use the DLL's hInstance
but GetModuleHandle(NULL)
will still return the HINSTANCE
of the EXE that loaded the DLL.
这篇关于GetModuleHandle(NULL)与hInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!