在c#代码

时间:2017-05-05 06:15:17

标签: c# c++ dll

我试图用C#代码调用C ++ DLL。 我的头文件 -

#define MLTtest __declspec(dllexport)
class MLTtest testBuilder
{
public:
    testBuilder(void);
    ~testBuilder(void);


    int testfunc (int iNumber);
};

我的.CPP班级

int testBuilder::testfunc (int iNumber)
{

    return iNumber*2 ;
}

这是我使用该DLL的C#代码。

class Program
{

    [DllImport(@"C:\Sources\Operations\Online.Dev\BIN\Debug\Mlt1090d64.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "testfunc")]
    public static extern int testfunc(int n);

    static void Main(string[] args)
    {
        try
        {
            int x = testfunc (50);
        }
        catch (Exception ex)
        {
        }
    }
}

但我一直得到这个例外:

  

无法找到名为' testfunc'的入口点在DLL中   ' C:\源头\操作\ Online.Dev \ BIN \调试\ Mlt1090d64.dll'

1 个答案:

答案 0 :(得分:3)

问题是您尝试调用类成员方法。

在.cpp文件中放置流动函数(不是类成员)

extern "C" int __declspec(dllexport) testfunc(int iNumber)
{
  return iNumber*2;
}

并在.cs

中更新
[DllImport(@"C:\Sources\Operations\Online.Dev\BIN\Debug\Mlt1090d64.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int testfunc(int n);