How do I obtain the HINSTANCE for the CreateWindowEX function when using it outside of WinMain?

和自甴很熟 提交于 2019-12-02 04:02:20

The winapi carries some baggage that it inherited from Windows versions 1 through 3. They used co-operative multi-tasking, running the same "task" was implemented by using the same program and passing it different hInstance values. Important tricks to get a multi-tasking GUI operating system crammed into 640 kilobytes of RAM.

That stopped being relevant 21 years ago with the release of Windows NT, shortly followed by Windows 95. Versions of Windows that first supported the notion of a process. An important concept for security and system stability, there is a very strong isolation between running programs. Accordingly, the relevance of the HINSTANCE disappeared, every process gets the same value. A pseudo handle. Much the same for a HMODULE, it is simply the address in memory where the module was loaded.

Accordingly, passing NULL for the hInstance argument is fine. It means "the current process" and you can't create a window for another process. And storing the hInstance you get from WinMain() in a global variable is fine as well, there is only ever one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!