如何在 VC++ 静态库中加载自定义二进制资源作为 dll 的一部分?

我有自定义二进制资源(动画游标),希望将其作为资源存储在 Visual Studio C++ 的静态库中.事实证明,如果自定义二进制资源是自定义资源且位于静态库中,则 ::LoadCursor() 将不会加载或由 ::FindResource() 找到自定义二进制资源.

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 will not get loaded by ::LoadCursor() or found by ::FindResource() if it is a custom resource and in a static library.

这个问题提供了一些解决方法.

按照它的建议,如果我将 *.res 文件作为配置属性->链接器->附加依赖项"添加到 exe 中,那么静态库将能够找到该资源.

Following its advice, if I add the *.res file to an exe as a "Configuration Property->Linker->Additional Dependency" then the static library will be able to find the resource.

但是,如果静态库是 dll 的一部分,并且我将其作为附加依赖项链接,则不会再次找到它!

BUT if the static library is part of a dll and I link it in as an Additional Dependency it is not found again!

如何链接 dll 中的资源?

How can I link the resources in a dll?

或者只是在静态库中找到二进制文件?问题中的方法相当繁琐.

Or just make the binary be found in the static lib? The methods in the question are pretty cumbersome.

推荐答案

在添加资源"对话框中单击导入",选择所有文件 (.)",以便它允许您导入任何类型的文件,然后只需选择您想要的文件.当自定义资源类型对话框弹出时,在资源类型"字段中输入 RCDATA.

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.

如果你打开 .rc 文件,你会看到如下内容:

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

/////////////////////////////////////////////////////////////////////////////
//
// RCDATA
//

IDR_RCDATA1          RCDATA               "myfile.whatever"

它将生成带有以下行的 resource.h:

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;
}

其中 pMyBinaryData 是指向此可执行文件的第一个字节的指针.欲了解更多信息,请访问资源函数

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;
}

当您使用这样的资源构建项目时,该资源将成为您的程序 (dll) 的一部分.

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

相关文章