Home > Visual C++ > Calling FindResource() from DLL

Calling FindResource() from DLL

Resources such as Bitmap,Cursor,Icon etc can be attached to Win32/MFC projects as well as to DLL projects, really :)

Suppose we have a DLL Project, and as part of the project we have some resources. How can I call one function implemented in the DLL project from one calling project or application ??

Simple ??  really !!!..

you are right.. you can find a function named FindResource().

as from MSDN the description of FindResource() is as below

The FindResource function determines the location of a resource with the specified type and name in the specified module.

Happy  :) ???

Hope your problems are solved now

then what’s the problem??

Instead of default NULL argument , we must pass the Module Handle.

You can get the Module Handle by the function GetModuleHandle(NULL);

But unfortunately, GetModuleHandle(NULL) will return a handle of the calling process (.exe) which is not good for you. :(

So??

:)

Just pass the name of the DLL in the GetModuleHandle();

for example GetModuleHandle(_T(“SampleDLL”);

HRSRC hSrc = NULL;
HMODULE hMod = GetModuleHandle(_T("SampleDLL.dll"));
hSrc = FindResource(hMod, MAKEINTRESOURCE(IDR_BIN_DATA1), _T("BINDATA"));
if(hSrc == NULL)
      return 0;

:)

Categories: Visual C++
  1. August 10, 2009 at 8:08 pm | #1

    You are really rockin my world – thank u very much!!!! *dance* ^^

  2. suneel
    October 24, 2009 at 4:34 am | #2

    thanks for the tip. i am not a windows programmer, code only in native c++.

    i was trying to use resources, but cant seem to get it to work. I use vc2008 Express Edition. This does not have the resource editor, so i used ResEdit.

    Now i created a resource folder and added the .rc file and compiled it fine. But when i do FindResource i get NULL.

    On the msdn site it says you need to set #include “rffilename.rc” in the Compile-time directives box. Where is this box? i cant seem to find this in vc2008 EE. perhaps it does not work because of this?

  3. suneel
    October 24, 2009 at 4:45 am | #3

    …also i have other questions

    1. the generated resources.h file is present in my .rc file. Now if i dont include the resource.h in my code, then it cant find the name-string of the resource. But when i opened the resource file, i see that resource.h is present in it. On including resource.h in my code, it compiled. Is it expected to include this?

    2. Also what is the difference b/w .rc and /res. In my build folder i see 2 files. In the program options->reouurces->general i see resource file name as
    $(IntDir)/$(InputName).res
    Is this correct?

    as you can see i am noob and i no clue :(

  4. October 26, 2009 at 10:01 am | #4

    hi Suneel..

    just go through this link. Hope this will work for you..

    http://www.codeproject.com/KB/system/mctutorial.aspx

  5. October 26, 2009 at 10:30 am | #5

    Hi Suneel,
    i’m trying to understand your problem… :)
    for the 1st one, you are using the ReEdit instead of Resource Editor provided by VS. (I don’t know actually. Because, i’m not much familiar with the ResEdit)
    Anyway, you must include it manually. By default, the resource.h is included in the “Application Main File.h”

    .rc means resource script files.
    /res is created by the resource compiler.

  6. suneel
    October 26, 2009 at 10:42 am | #6

    hi thanks for the reply! i was able to figure out the problem.

    actually vc2008 express edition, you need to goto project properties and set the name of the *res file. By default it keeps it as the project name. Like this $(IntDir)/$(InputName).res . But my rs file has a different name, so was compiled to a different name.

    and yeah i figured you need to include the generated resource file, to get the id of the resource.

    its all working great now! i have successfully loaded an ogg audio binary file into memory as a user defined resource and even custom decoded it from ram and got playback using the vorbis lib.

    thanks for this great resource tip :) i wonder how i could do this in linux?

  7. October 26, 2009 at 10:44 am | #7

    oh..Thats nice..

    BTW. I don’t nothing about Linux. :(
    so sorry ..

  1. No trackbacks yet.