如何创建资源dll [英] How do I create a resource dll

查看:147
本文介绍了如何创建资源dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建资源dll? dll将有一组.png文件。在某种程度上这些.png文件应该从dll中暴露。我的应用程序将需要引用这个DLL来获取一个.png文件。

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.

推荐答案

资源dll与任何其他dll相同,它只有很少或没有代码, 。

A resource dll is the same as any other dll, it just has little or no code in it, and relatively more resources.

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

Microsoft doesn't have a predefined resource type for PNG files, but you can define your own

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

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"





$ b b

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


Then execute these commands at a command prompt.

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

第一个命令将resources.rc编译成resources.res
第二个命令将resources.res转换为dll。

Thats it. the first command compiles resources.rc into resources.res the second command turns resources.res into a dll.

你现在应该有一个dll c $ c> resources.dll ,其中包含单个png文件。
在实践中,当然,你会想把 #defines 放在与使用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.

要在C ++中使用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);
}

这篇关于如何创建资源dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆