Reputation:
I want to access my resource as a char*
or vector or a FILE. I tried the below and I got null. I don't understand why. The first parameter is optional the last I took from the documentation page. RT_RCDATA seems to be what I want. IDK why I am getting null
HRSRC rc = FindResourceEx(0, RT_RCDATA, MAKEINTRESOURCE(IDR_MyResource), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
I get the error 1813. I have a single executable with no dlls. The resource is in the executable
Upvotes: 3
Views: 265
Reputation: 66254
Answering (I hope) this question and your prior question here, This answer applies to FindResource
, though you can adapt to FindResourceEx
The parameters to FindResource
are module-to-search, id, and type.
The first is the instance handle who'd resource table to search. You can use NULL
for the running process, Otherwise this is in a DLL you need to save off your instance handle from DllMain, usually in a global ghInst
, and use that for your search target.
The second should be your resource id. If you're using macro ids such as from a resource.h
header, the id must be wrapped with MAKEINTRESOURCE(id)
. Otherwise its the same string (as a string) you used for your resource id.
type
parameter is that value as a string.Therefore, finding the custom resource of id MY_ID
(taken from an included resource.h
id file) of type MYDATA
in the current processes resource table would be:
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));
Similarly, loading the same resource from a DLL resource table, assuming you saved the instance handle of the DLL to some global ghInst
in the PROCESS_ATTACH
of your DllMain
, is:
HRSRC hRes = FindResource(ghInst, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));
Missing ID
It is not uncommon to "forget" to properly declare the identifier used for the resource in a .h file that is included in the resource script and C/C++ code, but the resource script will happily still compile. If the following is in your resource script
MY_ID MYDATA "filename.bin"
and MY_ID
is not defined via macro as a numeric id and included in your resource script, this will "name" a resource called "MY_ID"
(note its a string) in the output resource table. Worse, if the id-defining-macro is properly included in the C/C++ code trying to load this thing, then this:
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));
will compile, since the C/C++ code has MY_ID
properly available, but the resulting id is not the same as the one used in the resource file, which was a string name. Thus the load will fail. A sure sign this happened is if this fails:
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));
but this works:
HRSRC hRes = FindResource(NULL, _T("MY_ID"), _T("MYDATA"));
If you find this is the case, make sure your .rc file and your C/C++ code are using the same macro for your resource identifier. It can't just be the same "name". It has to be the same macro. If the macro is not available to the resource compiler it will use the name as a string, which is usually not what you want.
Best of luck.
Upvotes: 4
Reputation: 7980
2 things:
MAKEINTRESOURCE(RT_RCDATA)
should be RT_RCDATA
according to the docs.
The first parameter is the handle to the binary (DLL/exe) holding the resource, NULL implies the executable.
Upvotes: 2