I have a DLL which has a method (extractRcdata) which extracts a resorurce from itself. This works perfectly when it's run as a standalone binary, but not as a dll.
When run as an exe, GetModuleHandle(NULL) is used for hMod, so the resource gets extracted just fine. When build as a DLL, GetModuleHandle(NULL) now returns the handle for the exe invoking the DLL, not the DLL itself.Code:
* @brief Extracts given resource and returns it as a character array.
* @param hMod Module to extract resource from.
* @param resourceId ID of the resource to extract
* @return Character array containing the resource.
char* extractRCDATA( int resourceId )
return extractRCDATA( GetModuleHandle(NULL), resourceId );
char* extractRCDATA( HMODULE hMod, int resourceId )
char* cResource = new char;
hRsrc = FindResource(hMod, MAKEINTRESOURCE( resourceId ), RT_RCDATA);
logError( TEXT("FindResource") , GetLastError(), "Errors.log", false );
hResource = LoadResource(hMod, hRsrc);
logError( TEXT("LoadResource") , GetLastError(), "Errors.log", false );
cResource = (char*)LockResource(hResource);
logError( TEXT("LockResource") , GetLastError(), "Errors.log", false );
My hope was to use a global variable in my dll wrapper which would be populated from the HINSTANCE of dllMain, like so:
hinstDLL_ then gets passed to the constructor of my class, and eventually passed to extractRCDATA(). However _hinstDLL seems to be null?Code:
* @brief DLL Entry point.
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
// attach to process
// return FALSE to fail DLL load
hinstDLL_ = hinstDLL;
// detach from process
// attach to thread
// detach from thread
return TRUE; // successful
myClass myInstance( _hinstDLL );
I have two questions;
1: Where am I going wrong with _hinstDLL? - Why is it null?
2: Is there a better way to go about this? - Using C++, no MFC/ATL.
Thanks guys, I've extensively goggled this over the weekend and have concluded that I must be missing something - it can't be as complex as I'm making it out to be :)