首页 > 解决方案 > 使用 DLLImport 从 C# 调用 c++,找不到对象引用错误

问题描述

我正在尝试使用 DLLImport 从 C# 调用 c++ dll,并且遇到“找不到对象引用”错误。

我以前做过这个,所以我肯定错过了一些东西,但是什么?

在 C# 中,我有:

[DllImport("Test.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void Tester();

和功能:

 public void ConnectionGo()
        {
             Tester();
        }

在 dll 中,我有:

extern "C"
{
    _declspec(dllexport) void Tester()
    {
        std::cout << "Okay" << std::endl;
    }

};

该 dll 与 C# exe 位于同一目录中(它是一个 WPF 应用程序)。它们都被构建为“Release”。

然而我得到了这个错误:

'Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.'

这可能是什么原因造成的?

标签: c#c++dllimport

解决方案


推荐阅读