问题
all
I have source below:

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:
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.
来源:https://stackoverflow.com/questions/22548937/how-to-read-user-defined-resource-in-visual-c-2012