[英]FileNotFoundException when calling C# dll from C++/CLI
I have a C# dll
, with some methods, which I am trying to access in my native project with /CLR
support. 我有一个C# dll
,有一些方法,我试图在我的原生项目中使用/CLR
支持访问。
I reference this DLL
using a #using
directive, and the DLL
is recognized and the project compiles. 我使用#using
指令引用此DLL
,并识别DLL
并编译项目。
However, during runtime, I get a FileNotFoundException
, which is pretty weird since the DLL is present in the source directory of the project. 但是,在运行时,我得到一个FileNotFoundException
,这很奇怪,因为DLL存在于项目的源目录中。
The DLL
is compiled in VS 2015
with .NET Version 4.5.2
. 该DLL
在VS 2015
使用.NET 4.5.2
编译。 Since I have CLR
support on my C++ mixed, I have edited the project file to make TargetFrameworkVersion
as 4.5.2, but still the runtime
does not work. 由于我对C ++混合使用CLR
支持,因此我编辑了项目文件以使TargetFrameworkVersion
为4.5.2,但runtime
仍然不起作用。
Kindly advise on what could be the issue? 请告知可能是什么问题?
EIDT - ADDED SOME CODE EIDT - 添加了一些代码
C# C#
namespace TestManagedLibrary
{
public class Class1
{
public int i;
public Class1()
{
i = 5;
}
public int returnValue()
{
return i;
}
}
}
C++/CLI C ++ / CLI
#using <TestManagedLibrary.dll>
using namespace System;
using namespace System::Runtime::InteropServices; // Marshal
using namespace TestManagedLibrary;
ref class ManagedFoo
{
public:
ManagedFoo()
{
Console::WriteLine(_T("Constructing ManagedFoo"));
Class1 ^testObject = gcnew Class1();
int a = testObject->returnValue();
}
};
First of all you need to ensure that the TestManagedLibrary.dll
file is located in a place where Fusion could find it. 首先,您需要确保TestManagedLibrary.dll
文件位于Fusion可以找到它的位置。 Your first try should be the location of the executable you are running. 您的第一次尝试应该是您正在运行的可执行文件的位置。
One way to handle this is via the reference properties. 处理此问题的一种方法是通过引用属性。 If the reference to your TestManagedLibrary.dll
is set with the copy local
flag than during the build the referenced library is going to be copied from the referenced location to the output directory. 如果使用copy local
标志设置对TestManagedLibrary.dll
的引用而不是在构建期间,则引用的库将从引用的位置复制到输出目录。
You can enable the internal fusion logging to find out the details: 您可以启用内部融合日志记录以查找详细信息:
Developer Command Prompt
with administrative privileges. 使用管理权限运行Developer Command Prompt
。 fuslogvw
运行fuslogvw
Assembly Binding Log Viewer
hit settings and set either Log bind failures to disk
or Log all binds to disk
. 在Assembly Binding Log Viewer
命中设置并设置Log bind failures to disk
或Log all binds to disk
。 Refresh
in the Assembly Binding Log Viewer
, pick your executable and hit View Log
在Assembly Binding Log Viewer
点击Refresh
,选择可执行文件并点击View Log
The compiled DLL
should have been in the same location as the executable for the CLR
to search for it. 编译的DLL
应该与CLR
的可执行文件位于相同的位置以进行搜索。 In my case, the .NET
compiled DLL
was in the solution folder and not searchable. 在我的例子中, .NET
编译的DLL
在解决方案文件夹中,而不是可搜索的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.