无法加载 C++/CLI DLL 资源 [英] Can`t Load C++/CLI DLL resources

查看:67
本文介绍了无法加载 C++/CLI DLL 资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想查看资源名称,但什么也没出现.

I'm trying just to see resource names but nothing appears.

我在 Visual Studio 2010 中制作并编译了一个 C++/CLI(托管)DLL,并添加了一些资源文件作为测试(一个图标和一个位图).我已经检查过 PE Explorer,资源肯定在那里.

I've made and compiled a C++/CLI (Managed) DLL in Visual Studio 2010 and added some Resource files as a test (one icon and one bitmap). I've checked with PE Explorer and the resources definitely are there.

我的简单代码:

Assembly asm = Assembly.LoadFrom("C:\\test.dll");
String[] res = asm.GetManifestResourceNames();

我知道 DLL 已加载,因为当我调试时,我可以看到asm"变量中的所有信息.我也可以从 DLL 导入数据(使用 MEF).

I know that the DLL is loaded because when I debug i can see all the infos in the 'asm' variable. Also i can Import data (using MEF) from the DLL.

因此,DLL 具有资源,并且代码肯定会加载程序集.但是为什么我的 'res' 变量总是返回空字符串列表?

So, the DLL has the resources and the code IS loading the assembly for sure. But why my 'res' variable always returns empty string list?

我创建了一个 C# 类库 (.dll),其中包含一个仅用于测试的资源.现在它起作用了!!但仍然在我的 C++/CLI DLL 中没有出现资源.不知何故,它们在 DLL 中,但代码无法访问它(仅在 C++ DLL 中).也许它与托管/非托管代码有关,但由于我使用 CLR 编译它,因此情况似乎并非如此.有什么建议吗?

I've created a C# Class Library (.dll) with a resource just for a test. Now it works!! But still in my C++/CLI DLL the resources do not appear. Somehow they are in the DLL but the code cant reach it (only in the C++ DLL). Maybe it would have something to do with managed/unmanaged code, but since i'm compiling it with CLR it does not seem to be the case. Any suggestions?

解决方案我懂了!以防万一有人需要.

SOLUTION I've got it! Just in case someone needs.

根据这些主题:

在 C++/CLI 项目中嵌入资源

Embedding resource in a C++/CLI project

http://bytes.com/topic/net/answers/571530-loading-markup-xamlreader-load-resource-file#post2240705

问题正是 C++/CLI 的问题.您必须在项目属性的链接器选项卡下的输入项中添加它.现在它似乎工作正常.谢谢

the problem is exactly the C++/CLI thing. You have to add it in Input item under Linker tab in Project Properties. Now it seems to work fine. Thanks

推荐答案

我有一个类似的问题,你的问题帮助我解决了它.我的项目平台是 C++/CLI,我的 DLL 平台是 c#.

I have a similar problem and your question helps me to solve it. my project platform is C++/CLI and my DLL platform is c#.

我想将DLL打包到我的执行文件中,因此首先我们应该通过以下步骤将DLL放入项目资源文件中:

I want to pack DLL into my executive file, hence we should put DLL in the project resource file through below steps at first:

1.复制项目路径中的DLL.

1.copy DLL in project path.

2.将 DLL 名称(例如 test.dll)放在下面的位置属性->链接器->输入->嵌入式托管资源文件

2.put DLL name(e.g. test.dll) in below place properties->linker->input->Embeded Managed Resource File

那么我们应该阅读和使用嵌入式DLL:

then we should read and use embedded DLL:

Stream^ stream = Assembly::GetExecutingAssembly()->GetManifestResourceStream("test.dll");

array<unsigned char>^ dllRawBuffer = gcnew array<unsigned char>(stream->Length);

int res = stream->Read(dllRawBuffer, 0, stream->Length);
stream->Close();

Assembly^ dllAssembly = Assembly::Load(dllRawBuffer);

System::Type^ testclass = dllAssembly->GetType("TestNamespace.TestClass");

MethodInfo^ TestMethod = testclass->GetMethod("TestMethodName");

// Create an instance.
Object^ Testobj = Activator::CreateInstance(testclass);


// Execute the method.

array<Object^>^ params = gcnew array<Object^>(2);
params[0] = 2;
params[1] = 3;

Object^ result = TestMethod->Invoke(Testobj, params);

显然,此解决方案仅适用于托管 DLL.

obviously, this solution only works for managed DLLs.

这篇关于无法加载 C++/CLI DLL 资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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