tags:

views:

68

answers:

1

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.

+4  A: 

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);
}
John Knoeller
wow...thanks a lot..this looks like an useful information :)
Rakesh Agarwal
by using this technique, can I change the my self-created IE toolbar button's icon at real time?
karikari
@karikari: I don't think so. I can't how this technique helps with real time changes. The resources in the DLL are defined at link time, they aren't dynamic.
John Knoeller
Have you try Web of Trust browser extension? They have an IE toolbar icon that changes at run time. I'm still exploring their code, and still dont understand how they did it.
karikari