gpt4 book ai didi

c++ - 如何创建资源 dll

转载 作者:可可西里 更新时间:2023-11-01 16:29:01 24 4
gpt4 key购买 nike

如何创建资源 dll?该 dll 将具有一组 .png 文件。在某种程度上,这些 .png 文件应该从 dll 中公开。我的应用程序需要引用此 dll 以获取 .png 文件。

最佳答案

资源dll 与任何其他dll 相同,只是其中的代码很少或没有代码,而资源相对较多。

Microsoft 没有为 PNG 文件预定义的资源类型,但您可以定义自己的资源类型

最小可能的资源 dll 只是像这样传递给链接器的已编译 .rc 文件。


//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"

然后在命令提示符下执行这些命令。

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

就是这样。第一个命令将 resources.rc 编译成 resources.res第二个命令将 resources.res 变成一个 dll。

您现在应该有一个名为 resources.dll 的 dll,其中包含一个 png 文件。当然,在实践中,您会希望将 #defines 放在与使用 dll 的代码共享的头文件中。

要在 C++ 中使用 dll,您的代码应如下所示。

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

关于c++ - 如何创建资源 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2219975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com