0

假设我想从 ac# 代码调用 c++ 函数,我遇到以下问题:

情况1:

class abc
{
private :
    int a ;

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

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

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

从 C# 调用函数时,这种情况会引发 DLL not found 异常。

案例二:

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

从 C# 调用函数的情况很好。

任何想法为什么?我该如何解决?

4

1 回答 1

0

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

在 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);
于 2013-05-21T05:23:13.647 回答