How to read user-defined resource in Visual C++ 2012?

百般思念 提交于 2019-12-22 01:17:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!