Determine the current HINSTANCE?

回眸只為那壹抹淺笑 提交于 2019-12-04 08:01:54

问题


The HINSTANCE of a win32 application is passed to WinMain, but is there any other way of determining the current HINSTANCE (in case you couldn't tell, I'm very new to win32 programming!)? I need to create a window inside of a library and (since the library is cross platform), id prefer not to have to pass it in.


回答1:


If memory serves, GetModuleHandle(NULL); returns the instance handle.




回答2:


__ImageBase is your friend, especially in the case of libraries.

Note that the linked blog post (by R. Chen, although not the same post as the one linked by Brian Bondy) is worth reading (including the comments!)




回答3:


If you are using MFC, you can use AfxGetInstanceHandle.

If you are not using MFC you can use: GetWindowLong(hWnd, GWL_HINSTANCE)




回答4:


The function AfxGetStaticModuleState() does the trick. If you call it within a dll, the functions returns the handle to the dll, if the call within a exe it returns the handle to the executable.

DWORD size;
TCHAR fileName [MAX_PATH];
HMODULE hModule = AfxGetStaticModuleState()->m_hCurrentInstanceHandle;
::GetModuleFileName (hModule, fileName, size);


来源:https://stackoverflow.com/questions/1749972/determine-the-current-hinstance

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