How to load a custom binary resource in a VC++ static library as part of a dll?

前端 未结 3 938
离开以前
离开以前 2020-12-01 04:42

I have custom binary resources (animated cursors) that would like to store as resources in a static lib in Visual Studio C++. It turns out that custom binary resources wi

相关标签:
3条回答
  • 2020-12-01 05:12

    In case you use dll using MFC (and CWinApp), you can obtain the HMODULE from CWinApp.

    extern MyDllApp theApp;
    HMODULE module = (HMODULE)theApp.m_hInstance;
    HRSRC myResource = ::FindResource(module,
                MAKEINTRESOURCE(IDR_FILE_RESOURCE), _T("GROUP_NAME"));
    

    If you supply NULL in FindResource, application won't find your resource.

    0 讨论(0)
  • 2020-12-01 05:27

    In Add Resource dialog click Import, select "All Files (.)" so that it allows you to import file of any type, and then just select the file you want there. When Custom Resource Type dialog pops up, type RCDATA into "Resource type" field.

    If you open .rc file, you will see something like this:

    /////////////////////////////////////////////////////////////////////////////
    //
    // RCDATA
    //
    
    IDR_RCDATA1          RCDATA               "myfile.whatever"
    

    and it will generate resource.h with following line:

    #define IDR_RCDATA1                  101
    

    In code you access it like this:

    #include "resource.h"
    #include <windows.h>
    
    int main(int argc, char* argv[])
    {
        HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
        HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
        void* pMyBinaryData = ::LockResource(myResourceData);
        return 0;
    }
    

    where pMyBinaryData is pointer to first byte of this executable. For more information visit Resource Functions

    Here's an example how you would save binary resource like this on disk:

    #include "resource.h"
    #include <windows.h>
    #include <fstream>
    
    int main(int argc, char* argv[])
    {
        HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
        unsigned int myResourceSize = ::SizeofResource(NULL, myResource);
        HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
        void* pMyBinaryData = ::LockResource(myResourceData);
    
        std::ofstream f("C:\\x.bin", std::ios::out | std::ios::binary);
        f.write((char*)pMyBinaryData, myResourceSize);
        f.close();
    
        return 0;
    }
    

    When you build project with resource like that, this resource will become part of your program (dll).

    0 讨论(0)
  • 2020-12-01 05:35

    The Problem of @LihO answer is:

    The first parameter of FindResource is the ModuleID of the Module containing the resources. If this is set to NULL the function will search in created process (.exe), not the DLL.

    But how to get the HMODULE insinde a static LIB?

    • add a function / parameter, which will get the HMODULE from the DLL. The HMODULE / HINSTANCE (is the same) can be retrieved in DLLMain.
    • Try this GetCurrentModule

    Edit:

    See also: Add lib resource to a library

    0 讨论(0)
提交回复
热议问题