在C#应用程序中使用C ++ DLL时无法找到入口点

时间:2015-05-15 17:11:48

标签: c# c++

我编写了一个C#应用程序,需要在单击按钮时调用C ++ DLL中的函数。但是在点击按钮时,它会抛出'EntryPointNotFound'异常。

Below is the code snippet of C#
    public class Test
    {
        [DllImport("Demo.dll", EntryPoint = "OpenFile"]
        public static extern bool OpenFile(string fileName);
    }
private void button1_Click_1(object sender, EventArgs e)
        {
            bool check = Test.OpenFile("test.txt"); // exception thrown at this point
            if (check)
            {
                // Not entering this area.. 
            }
        }


C++ Header (.h file)

__declspec(dllexport) bool OpenFile(CString fileName);

Cpp class (.cpp )
__declspec(dllexport) bool Demo::OpenFile(CString fileName)
{
        return true;
}

请帮忙。

1 个答案:

答案 0 :(得分:0)

基本上你需要添加extern" C"到DLL代码:

extern "C" __declspec(dllexport) bool OpenFile(CString fileName);

另见 stackoverflow question