Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a resource dll

Tags:

c++

How do I create a resource dll ? The dll will be having a set of .png files. In a way these .png files should be exposed from the dll. My application would need to refer this dll to get a .png file.

like image 756
Rakesh Agarwal Avatar asked Feb 08 '10 06:02

Rakesh Agarwal


1 Answers

A resource dll is the same as any other dll, it just has little or no code in it, and relatively more resources.

Microsoft doesn't have a predefined resource type for PNG files, but you can define your own

The most minimal possible resource dll is just a compiled .rc file passed to the linker like this.


//save this as resources.rc (supply your own .png file)

#define RT_PNG 99

#define ID_DIGG 1

ID_DIGG  RT_PNG  "image\\digg.png"

Then execute these commands at a command prompt.

rc resources.rc
link /dll /noentry /machine:x86 resources.res

Thats it. the first command compiles resources.rc into resources.res the second command turns resources.res into a dll.

You should now have a dll called resources.dll that contains a single png file. In practice, of course, you will want to put the #defines in a header file that you share with the code that uses the dll.

To use the dll in C++, your code would look something like this.

#define RT_PNG   MAKEINTRESOURCE(99)
#define ID_DIGG  MAKEINTRESOURCE(1)

HMODULE hMod = LoadLibraryEx("resources.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
if (NULL != hMod)
{
    HRSRC hRes = FindResource(hMod, RT_PNG, ID_DIGG);
    if (NULL != hRes)
    {
        HGLOBAL hgbl = LoadResource(hMod, hRes)
        void *  pPng = LockResource(hgbl);
        UINT32  cbPng = SizeofResource(hMod, hRes);

        // pPng now points to the contents of your your .png file
        // and cbPng is its size in bytes

    }

    // Don't free the library until you are done with pPng
    // FreeLibrary(hMod);
}
like image 77
John Knoeller Avatar answered Oct 03 '22 01:10

John Knoeller