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.
Asked
Active
Viewed 7.6k times
4 Answers
110
If memory serves, GetModuleHandle(NULL);
returns the instance handle.

Jerry Coffin
- 476,176
- 80
- 629
- 1,111
-
17Not totally correct: It reutrns the HINSTANCE of the exe. If the code executes in a DLL, this may lead to wrong behaviours – Serge Wautier Nov 17 '09 at 16:38
-
2@Serge: from what he's saying, the HINSTANCE of the executable is exactly what he wants. – Jerry Coffin Nov 17 '09 at 16:40
-
6+1: By passing in a module name, that function can be used to get the `HINSTANCE` of DLLs as well. Note that `HINSTANCE` and `HMODULE` are essentially equivalent in modern versions of Windows. – Adrian McCarthy Nov 17 '09 at 16:47
-
2Adrian, how would the code in the library know in which module (exe/dll) it sits? – Serge Wautier Nov 17 '09 at 20:13
26
__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!)

Community
- 1
- 1

Serge Wautier
- 21,494
- 13
- 69
- 110
-
1Ignoring the cumbersome [GetModuleHandleEx](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683200.aspx) with `GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS`, this is really the only **reliable** way to find the module handle, and it should be the accepted answer. – IInspectable Feb 12 '15 at 16:43
8
If you are using MFC, you can use AfxGetInstanceHandle.
If you are not using MFC you can use: GetWindowLong(hWnd, GWL_HINSTANCE)

Brian R. Bondy
- 339,232
- 124
- 596
- 636
-
2That assumes I already have a window (and thus, and hwnd)... I'm trying to push the job of window creation out to my library... – dicroce Nov 17 '09 at 16:25
-
Please review this link, I think you may run into the problem Raymond is talking about: http://blogs.msdn.com/oldnewthing/archive/2005/04/18/409205.aspx – Brian R. Bondy Nov 17 '09 at 16:29
-
Both AfxGetInstanceHandle and GetWindowLong returns HINSTANCE of the application, but you can call AfxGetInstanceHandle without creating a window. – jrbjazz Nov 17 '09 at 16:37
-
You could set a global variable if you really wanted to as well and extern it in. – Brian R. Bondy Nov 17 '09 at 16:48
-
The ``GetWindowLong()`` version assumes a 32 bit platform build. But on Win64, you run into trouble. – BitTickler Aug 11 '18 at 15:21
0
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);

Marcel Mateman
- 97
- 4
-
3The question is tagged *winapi*. There is no `AfxGetStaticModuleState` in the Windows API. – IInspectable Feb 12 '15 at 14:41