1
votes

all

I have source below:

enter image description here

in my .rc file

IDR_XML1                XML                     "LoginQuery.xml"

in my resource.h file

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
//

#define IDR_XML1                        106

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        107
#define _APS_NEXT_COMMAND_VALUE         40002
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

and in my .cpp file.

HMODULE handle = ::GetModuleHandle(NULL);
HRSRC rc = ::FindResource(handle, MAKEINTRESOURCE(IDR_XML1), MAKEINTRESOURCE("XML"));
HGLOBAL rcData = ::LoadResource(handle, rc);
DWORD size = ::SizeofResource(handle, rc);
const char* data = static_cast<const char*>(::LockResource(rcData));

But data returns null.

What am I doing wrong?

EDIT

My C++ project is dll project, and I am reading the file inside of that project.

1
Replace MAKEINTRESOURCE("XML") with just plain "XML". MAKEINTRESOURCE expects an int as its parameter, hence the name. - Igor Tandetnik
Also, you need to pass a correct module handle - namely, the HINSTANCE handle passed to DllMain. GetModuleHandle(NULL) gives you the handle to the hosting EXE (which, naturally, doesn't have that resource). - Igor Tandetnik

1 Answers

4
votes

Your dll entry is something like:

BOOL WINAPI DllMain(_In_  HINSTANCE hinstDLL, _In_  DWORD fdwReason, _In_  LPVOID lpvReserved)

hinstDLL is instance of your dll, I recommend to have global variable to keep this instance and assign it instantly after dll is loaded.

HINSTANCE g_hInstance;
BOOL WINAPI DllMain(_In_  HINSTANCE hinstDLL, _In_  DWORD fdwReason, _In_  LPVOID lpvReserved)
{
    g_hInstance = hinstDLL;
    /*code*/
}

And you resource load should look something like:

HRSRC rc = ::FindResource(g_hInstance, MAKEINTRESOURCE(IDR_XML1), MAKEINTRESOURCE(XML));
HGLOBAL rcData = ::LoadResource(g_hInstance, rc);
DWORD size = ::SizeofResource(g_hInstance, rc);
const char* data = static_cast<const char*>(::LockResource(rcData));

BTW. nothing about your question but variable named rc usually is used for RECT type.