简体   繁体   中英

C# : DLLImport - DLL Not Found exception

Suppose I want to call c++ functions from ac# code, I am having the following problem:

Case 1:

class abc
{
private :
    int a ;

public :
    int  getValue()
    {
        return 100;
    }
};

int GetCounter()
{
    abc* p = new abc();

    int i = p->getValue();
    return i;
}

This case when calling the function from C# throws me a DLL not found exception.

Case 2:

int GetCounter()
{
    int i = 333;
    return i;
}

The case when calling the function from C# works just fine.

Any ideas why? How can I fix it?

use this sample Line of code in CPP Project (MathFuncsDll.h)

extern "C" __declspec(dllexport) double Add(double a, double b);
extern "C" __declspec(dllexport) double Sub(double a, double b);
extern "C" __declspec(dllexport) double Mul(double a, double b);
extern "C" __declspec(dllexport) double Div(double a, double b);

in C# Code use like this

 [DllImport("MathFuncs.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern Double Add(Double a, Double b);

 [DllImport("MathFuncs.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern Double Mul(Double a, Double b);

 [DllImport("MathFuncs.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern Double Sub(Double a, Double b);

 [DllImport("MathFuncs.dll",CallingConvention = CallingConvention.Cdecl)]
 public static extern Double Div(Double a, Double b);

 [DllImport("MathFuncs.dll",CallingConvention = CallingConvention.Cdecl)]
 public static extern Double Bat(Double a, Double b);

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