Resource in MSVC ++ 2013

I want to access my resource as char*

either vector or FILE. I tried below and I got null. I do not understand why. The first parameter is optional the last one I took from the documentation page. It seems that RT_RCDATA is what I want. IDK why i am getting null

HRSRC rc = FindResourceEx(0, RT_RCDATA, MAKEINTRESOURCE(IDR_MyResource), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));

      

I am getting error 1813. I have one executable without DLL. The resource is in the executable file

+3


source to share


2 answers


Answering this question and your previous question here , this answer applies to FindResource

, although you can adapt toFindResourceEx

Parameters FindResource

are a module for search, id and type.

  • The first is the handle to the instance that will look for the resource table. You can use NULL

    for the current process, otherwise it's in a DLL, you need to save the instance handle from DllMain, usually globally ghInst

    , and use it for your target purpose.

  • The second one should be your resource ID. If you are using macro ids, for example from the header resource.h

    , the identifier must be wrapped MAKEINTRESOURCE(id)

    . Otherwise it uses the same string (as string) for your resource id.

  • The third type of resource. When you declared your custom resource, you gave it a type in your script resource (something like MYRES; Ex: I'm using XML for XML files, for example). A parameter type

    is a value as a string.

Hence, looking for the user id resource MY_ID

(taken from the included resource.h

id file ) of type MYDATA

in the current process resource table:

HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));

      

Likewise, download the same resource from the resource DLL table, assuming you saved the DLL instance handle for some global ghInst

in PROCESS_ATTACH

you DllMain

,:

HRSRC hRes = FindResource(ghInst, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));

      


Missing identifier

It's not uncommon to "forget" to correctly declare the identifier used for a resource in a .h file that is included in the script and C / C ++ resource code, but the script resource will happily compile if your resource contains the following: script



MY_ID MYDATA "filename.bin"

      

and MY_ID

not , defined by the macro as a numeric identifier and included in your script resource, this will "call" the named resource "MY_ID"

(note its string) into the output resource table. Worse, if id-define-macro correctly included in the C / C ++ code when trying to load this thing, then this:

HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(MY_ID), _T("MYDATA"));

      

will compile as the C / C ++ code has MY_ID

correctly accessed, but the resulting identifier is not the same as the one used in the resource file that was the string name. Therefore, the download will fail. A sure sign that this has happened is if it 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 to be the case, make sure that your .rc file and your C / C ++ code use the same macro for your resource ID. It cannot be the same "name". It should be the same macro. If the macro is not available to the resource compiler, it will use that name as a string, which is usually not what you want.

Good luck.

+4


source


2 things:



  • MAKEINTRESOURCE(RT_RCDATA)

    must comply RT_RCDATA

    in accordance with the documents.

  • The first parameter is a descriptor for the binary (DLL / exe) containing the resource, NULL implies the executable.

+2


source







All Articles