0

I am working with specific hardware and got .lib and .dll from the h/w vendor. Need to use native dll function from C# so I am trying to use C++/CLI concept in between.

My native code is building fine and producing .lib and .dll file, I used this file with C++/CLI DLL and it also build well and giving .dll file. when using this file in C# , I am getting error like:-

Unhandled Exception: System.IO.FileLoadException: A procedure imported by 'CLI_DLL.dll' could not be loaded.
   at C_sharp_Code.Program.Main(String[] args)

Important thing is when I am trying to access native dll from CLR console application, it is working fine but when i am calling native function from CLR DLL then dll is building fine but giving below error when using same from C# console application.

C++/CLI .h file

#pragma once

using namespace System;


extern "C" _declspec(dllimport) bool N_Enabale_Port();

namespace CLI_DLL {

    public ref class Class1
    {
        // TODO: Add your methods for this class here.
    public:
        bool M_Enabale_Port();

    };
}

C++/CLI CPP

#include "stdafx.h"

#include "CLI_DLL.h"


bool CLI_DLL::Class1::M_Enabale_Port()
{

    bool temp =  N_Enabale_Port();
    return temp;

}

C# Code

using CLI_DLL;

namespace C_sharp_Code
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {

                Class1 obj = new Class1();

                bool x = obj.M_Enabale_Port();
                Console.WriteLine(x);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }



    }
}

Any Advice where I am missing something..

Arjun
  • 3,491
  • 4
  • 25
  • 47
  • Looks like the native dll could not be found. Make sure it's copied to the build output. By the way, if you just have simple functions like the one in your snippet (not too much structures in parameters etc) you may want to consider P/Invoke instead. – Lucas Trzesniewski Aug 12 '16 at 09:48
  • I put h/w dll, native dll files (.lib and .dll) as well as c++/Cli dll at the folder where c# .exe is generated, how Can I get the exact problem, what dll is missing inside??? – Arjun Aug 12 '16 at 09:53
  • [A procedure imported by {myassembly} could not be loaded](http://stackoverflow.com/q/1945589/3764814) / [A procedure imported by {dll} could not be loaded](http://stackoverflow.com/q/16704208/3764814) / [How do I find the source of a “A procedure imported by 'xxx.dll' could not be loaded.” exception?](http://stackoverflow.com/q/18198163/3764814) (pick your favorite duplicate) – Lucas Trzesniewski Aug 12 '16 at 09:56
  • Actully in a single solution , handling all projects, each project have its own debug/bin folder but in root there are a debug folder , when I tried to copy c# exe in it, it work fine. what it find here which it was missing where c# exe was created. Any idea??? is it because I build in debug mode??? – Arjun Aug 12 '16 at 10:05
  • C++ and C# projects have a different standard for where they place their build result. C# copies to bin\Debug, C++ copies to the solution's Debug folder, usually ..\Debug. MSBuild doesn't solve that problem, it has no idea that your C# project has a dependency on an unmanaged C++ dll. You have to copy that dll yourself and that's where mistakes like this one can happen, seems you copied the wrong one. Lots of possible workarounds, the simplest is to just change the build directory of your C# project from bin\Debug to ..\Debug. – Hans Passant Aug 12 '16 at 10:14

0 Answers0