Can`t Load C++/CLI DLL resources

我是研究僧i 提交于 2020-12-12 06:04:26

问题


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

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.

My simple code:

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

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.

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?

EDIT: 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.

According to these topics:

Embedding resource in a C++/CLI project

and

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

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


回答1:


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#.

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.copy DLL in project path.

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

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

obviously, this solution only works for managed DLLs.



来源:https://stackoverflow.com/questions/15087265/cant-load-c-cli-dll-resources

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!