How do I create a resource dll

前端 未结 1 1670
孤街浪徒
孤街浪徒 2021-02-02 02:09

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 dl

相关标签:
1条回答
  • 2021-02-02 03:11

    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);
    }
    
    0 讨论(0)
提交回复
热议问题