Hi all,
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 )
{
HRSRC hRsrc;
HGLOBAL hResource;
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 );
return cResource;
}
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:HINSTANCE hinstDLL_;
/**
* @brief DLL Entry point.
*/
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
hinstDLL_ = hinstDLL;
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
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 :)
Thanks!