简体   繁体   English

从C ++ / CLI调用C#dll时出现FileNotFoundException

[英]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 . DLLVS 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: 您可以启用内部融合日志记录以查找详细信息:

  1. Run Developer Command Prompt with administrative privileges. 使用管理权限运行Developer Command Prompt
  2. Run fuslogvw 运行fuslogvw
  3. In the 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 diskLog all binds to disk
  4. Start your service 开始你的服务
  5. Hit 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM