静态库中的资源 [英] resources in static libraries

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

问题描述

你可以将资源嵌入静态库吗?

我正在使用.rc文件中指定为.bin文件的自定义二进制资源

of我有静态库项目。我使用FindResource,LoadResource,

LockResource来获取资源。但是FindResource返回null。如果

我使用完全相同的代码从用于测试静态库的控制台应用程序的资源中检索资源,那么它可以工作

完美。我无法看到资源是如何编译到静态的

库中的,因为它的大小似乎表明它已经存在。它正在使用

GetModuleHandle(NULL)来获取进程句柄。

任何想法可能出错?

这是静态库中的代码:


BOOL loaddata()

{

HMODULE hModule = GetModuleHandle(NULL);

HRSRC hRsrc = FindResource(hModule,MAKEINTRESOURCE(IDR_NODES),NODES);

if(hRsrc!= NULL)

{

HGLOBAL hGMem = LoadResource(hModule,hRsrc);

if(hGMem!= NULL)

{

ressize = SizeofResource(hModule,hRsrc);

resdata =(NODE *)LockResource(hGMem);

if(ressize> 0)返回TRUE;

else FreeResource(hGMem);

}

}

返回FALSE;

}


,其中hRsrc变为NULL。

在用于测试它的控制台应用程序中正常工作的代码如下:

像这样: />
HMODULE hModule = GetModuleHandle(NULL);

HRSRC hRsrc = FindResource (hModule,MAKEINTRESOURCE(IDR_BIN11),BIN1);

HGLOBAL hGMem = LoadResource(hModule,hRsrc);

LPBYTE lpData =(LPBYTE)LockResource(hGMem) ;

DWORD dwSize = SizeofResource(hModule,hRsrc);

FreeResource(hGMem);


它运行正常。

can you embed a resource into a static libary?
I''m using a custom binary resource specified as a .bin file in the .rc file
of the static library project I''ve got. I use FindResource, LoadResource,
LockResource to get the resource. However FindResource is returning null. If
I use exactly the same code to retrieve a resource out of the resource of
the console application used to test the static library, then it works
perfectly. I can''t see how the resource isn''t being compiled into the static
library, as its size would seem to indicate that it has. It''s using
GetModuleHandle(NULL) in order to get the process handle.
Any idea what could be wrong?
This is the code in the static library:

BOOL loaddata()
{
HMODULE hModule = GetModuleHandle(NULL);
HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(IDR_NODES), "NODES");
if(hRsrc != NULL)
{
HGLOBAL hGMem = LoadResource(hModule, hRsrc);
if(hGMem != NULL)
{
ressize = SizeofResource(hModule, hRsrc);
resdata = (NODE*)LockResource(hGMem);
if(ressize > 0) return TRUE;
else FreeResource(hGMem);
}
}
return FALSE;
}

in which hRsrc goes to NULL.
The code that works fine in the console application used to test it goes
like this:
HMODULE hModule = GetModuleHandle(NULL);
HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(IDR_BIN11), "BIN1");
HGLOBAL hGMem = LoadResource(hModule, hRsrc);
LPBYTE lpData = (LPBYTE)LockResource(hGMem);
DWORD dwSize = SizeofResource(hModule, hRsrc);
FreeResource(hGMem);

and it works fine.

推荐答案

Bonj写道:
你能将资源嵌入到静态库中吗?
can you embed a resource into a static libary?




不,你不能。反正不是资源。有时人们会使用

" bin2obj"程序将二进制blob数据转换为可以通过名称引用的
初始化const数据的一部分。这样的blob可以放入静态库中,但资源不可能。


不幸的是,MS没有包含''用VC ++编写bin2obj''程序。 Borland

用于发货,但它使得OMF文件不是COFF文件,所以VC链接器

无论如何都会阻塞它们。你可能会发现这样一个程序有点

在''网上花费。


-cd



No, you can''t. Not as a resource anyway. Sometimes people will use a
"bin2obj" program to convert a binary blob of data into a section of
initialized const data that can be referenced by name. Such a blob can be
put into a static library, but resources can''t be.

Unfortunately, MS doesn''t include a ''bin2obj'' program with VC++. Borland
used to ship one, but it made OMF files not COFF files, so the VC linker
would choke on them anyway. You might find such a program with a little
spelunking around the ''net.

-cd


好的,谢谢

我发现你实际上可以让它工作但它很乱!

我基本上是通过添加空白资源文件来实现的到主应用程序,

然后将静态库的资源文件的相对路径添加到

''资源包含''编译时指令部分主应用程序的
资源文件,以及主要

应用程序中的#including静态库的resource.h。但是.bin文件必须位于主要的
应用程序的目录中。

就像我说的那样,它很混乱,因此我觉得只值得它当你已经在源代码管理下获得多个资源文件时,例如不同的人创建了不同的语言,并且他们想要测试它们。


只是另一个问题 - 内存的哪一部分有更快的数据

访问权限,LockResource返回的全局命名空间指针,或指针

到你分配并复制数据的堆?


" Carl Daniel [VC ++ MVP]" < cp ***************************** @ mvps.org.nospam>

写道消息新闻:关于************** @ TK2MSFTNGP11.phx.gbl ...
OK thanks
I''ve found you can actually get it to work but it''s messy!
I basically did it by adding a blank resource file to the main application,
and then adding the relative path to the static library''s resource file to
the ''resource includes'' compile-time directive section of the main app''s
resource file, and #including the static library''s resource.h in the main
application. But the .bin file has to be in the directory of the main
application.
Like I say, it''s messy and as such I think only really worth it when you''ve
got multiple resource files under source control, for instance different
languages created by different people, and they want to test them.

Just another question though - which section of memory has quicker data
access, the global namespace pointer returned by LockResource, or a pointer
to the heap that you allocate and copy the data into?

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:On**************@TK2MSFTNGP11.phx.gbl...
Bonj写道:
你能嵌入一个资源变成静态库?
can you embed a resource into a static libary?



不,你不能。反正不是资源。有时人们会使用
bin2obj。程序将二进制blob数据转换为可以通过名称引用的初始化const数据的一部分。这样的blob可以放入静态库中,但是资源不可能。不幸的是,MS没有包含VC ++的bin2obj程序。 Borland
曾经发过一个,但它使得OMF文件不是COFF文件,所以无论如何VC链接器都会阻塞它们。你可能会发现这样一个程序在网上有一点点洞察。

-cd



No, you can''t. Not as a resource anyway. Sometimes people will use a
"bin2obj" program to convert a binary blob of data into a section of
initialized const data that can be referenced by name. Such a blob can be
put into a static library, but resources can''t be.

Unfortunately, MS doesn''t include a ''bin2obj'' program with VC++. Borland
used to ship one, but it made OMF files not COFF files, so the VC linker
would choke on them anyway. You might find such a program with a little
spelunking around the ''net.

-cd



Bonj写道:
好的,谢谢
我发现你实际上可以让它工作但它很乱!
我基本上是通过添加一个空白资源文件到主应用程序,然后将静态库的资源文件的相对路径添加到''资源包含''编译时
指令部分主应用程序的资源文件,以及主要应用程序中包含
静态库的resource.h。但是.bin
文件必须在主应用程序的目录中。
就像我说的那样,它很混乱,因此我觉得只有当你这样做时才真的值得在源代码管理下有多个资源文件,例如由不同的人创建的不同语言,他们想要测试它们。


是的 - 这是用资源做的唯一方法。资源不是b $ b中的资源。任何意义上的stsatic库,它只需要与lib一起拖动。

只是另一个问题 - 哪个部分内存更快
数据访问,LockResource返回的全局命名空间指针,
或指向您分配并将数据复制到的堆的指针?
OK thanks
I''ve found you can actually get it to work but it''s messy!
I basically did it by adding a blank resource file to the main
application, and then adding the relative path to the static
library''s resource file to the ''resource includes'' compile-time
directive section of the main app''s resource file, and #including the
static library''s resource.h in the main application. But the .bin
file has to be in the directory of the main application.
Like I say, it''s messy and as such I think only really worth it when
you''ve got multiple resource files under source control, for instance
different languages created by different people, and they want to
test them.
Yep - that''s about the only way to do it with resources. The resource isn''t
"in" the stsatic library in any sense of the word, it simply has to be
dragged along with the lib.
Just another question though - which section of memory has quicker
data access, the global namespace pointer returned by LockResource,
or a pointer to the heap that you allocate and copy the data into?




差异。


-cd



Makes no difference.

-cd


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

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