当前位置: 代码迷 >> VC >> c++中调用c#写的dll的有关问题
  详细解决方案

c++中调用c#写的dll的有关问题

热度:8180   发布时间:2013-02-25 00:00:00.0
c++中调用c#写的dll的问题

System::Reflection::Assembly ^ LoadAssembly(String ^filename)
{
Assembly ^assembly = nullptr;
FileStream ^fs = File::Open(filename, FileMode::Open);
MessageBox(NULL,TEXT("hello"),TEXT("msg"),MB_OK);
if (fs != nullptr)
{
MemoryStream ^ms = gcnew MemoryStream();
if (ms != nullptr)
{
cli::array<unsigned char> ^buffer = gcnew cli::array<unsigned char>(1024);
int read = 0;
while ((read = fs->Read(buffer, 0, 1024))>0)
ms->Write(buffer, 0, read);
assembly = Assembly::Load(ms->ToArray());
ms->Close();
delete ms;
}
fs->Close();
delete fs;
}
return assembly;
}

在这里调用
System::Reflection::Assembly ^assembly = LoadAssembly("c:/test/test.dll")

assembly->createinstance("namespace.class") 这句会抛出异常 "exception has been thrown by the target of an invocation"

如果把dll换到当前程序路径下,并把这句LoadAssembly("c:/test/test.dll") 改为LoadAssembly("test.dll") 则能够正确运行。问题到底出在哪里呢?

------解决方案--------------------------------------------------------
("c:/test/test.dll")
=>
("c:\\test\\test.dll")

------解决方案--------------------------------------------------------
Load()仅在符合条件的位置寻找程序集,且参数好像不支持路径的

用LoadFrom()或LoadFile()试试
------解决方案--------------------------------------------------------
探讨
Load()仅在符合条件的位置寻找程序集,且参数好像不支持路径的

用LoadFrom()或LoadFile()试试
  相关解决方案