简体   繁体   中英

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.

I reference this DLL using a #using directive, and the DLL is recognized and the project compiles.

However, during runtime, I get a FileNotFoundException , which is pretty weird since the DLL is present in the source directory of the project.

The DLL is compiled in VS 2015 with .NET Version 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.

Kindly advise on what could be the issue?

EIDT - ADDED SOME CODE

C#

namespace TestManagedLibrary
{
    public class Class1
    {
        public int i;
        public Class1()
        {
            i = 5;
        }

        public int returnValue()
        {
            return i;
        }
    }
}

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

You can enable the internal fusion logging to find out the details:

  1. Run Developer Command Prompt with administrative privileges.
  2. Run fuslogvw
  3. In the Assembly Binding Log Viewer hit settings and set either Log bind failures to disk or Log all binds to disk .
  4. Start your service
  5. Hit Refresh in the Assembly Binding Log Viewer , pick your executable and hit View Log

The compiled DLL should have been in the same location as the executable for the CLR to search for it. In my case, the .NET compiled DLL was in the solution folder and not searchable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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