简体   繁体   English

C#:DLLImport-找不到DLL异常

[英]C# : DLLImport - DLL Not Found exception

Suppose I want to call c++ functions from ac# code, I am having the following problem: 假设我想从ac#代码中调用c ++函数,遇到以下问题:

Case 1: 情况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. 从C#调用函数时,这种情况会抛出DLL找不到异常。

Case 2: 情况2:

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

The case when calling the function from C# works just fine. 从C#调用函数的情况很好。

Any ideas why? 有什么想法吗? How can I fix it? 我该如何解决?

use this sample Line of code in CPP Project (MathFuncsDll.h) 在CPP项目(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 在C#代码中使用像这样

 [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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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