Reputation: 744
I am new to c++ and this code always returns NULL even though i know the file exists:
HMODULE hModule = GetModuleHandle(TEXT("C:\\Users\\Steve\\Desktop\\stub.exe"));
Interestingly, if i copy stub.exe to C:\Windows\system32, it finds the module with this code:
HMODULE hModule = GetModuleHandle(TEXT("stub.exe"));
Am i missing something incredibly basic?
Upvotes: 3
Views: 29976
Reputation: 179779
You can only call GetModuleHandle(L"C:\\Users\\Steve\\Desktop\\stub.exe");
when you're running C:\Users\Steve\Desktop\stub.exe
.
But in general, you don't call GetModuleHandle
for your EXE name. Since there's only one EXE per process, you just call GetModuleHandle(0)
.
Upvotes: 9
Reputation: 26171
Firstly, GetModuleHandle
requires that you load the dll into the process before hand.
Windows has specific paths that it uses to search for modules, as well as some switches to force 'safe' dll loading, you may want to look into SetDllDirectory
and AddDllDirectory
if you wish to expand the locations searched. See this for an explanation of how Windows search for binaries.
Upvotes: 3
Reputation: 14031
Here is a quote from MSDN:
GetModuleHandle Function
Retrieves a module handle for the specified module. The module must have been loaded by the calling process.
This means that you have to load the module with LoadLibrary first. System32 might get special treatment, but you really should not be copying anything there. The folder is strictly for the operating system's usage.
Upvotes: 0