【发布时间】:2020-12-18 04:50:56
【问题描述】:
我的 c++ 函数如下所示
# define MyFunction _declspec(dllexport)
extern "C" {
MyFunction int SubtractNumbers(int a, int b)
{
return a - b;
}
MyFunction const char* GetStringKey()
{
return "My String";
}
}
从windows窗体调用c++函数如下,
[DllImport(cppFunctionsDll, CallingConvention = CallingConvention.Cdecl)]
private const string DllFilePath = @"D:\\Projects\\December\\17-12-2020\\project-device-
setup\\Debug\\AccurynCPP.dll";
[DllImport(DllFilePath, CallingConvention = CallingConvention.Cdecl)]
public static extern int SubtractNumbers(int a, int b);
[DllImport(DllFilePath, CallingConvention = CallingConvention.Cdecl)]
public static extern string GetStringKey();
public void GetSubtract()
{
lblSubtract.Text= Convert.ToString(SubtractNumbers(1, 2));
}
public void GetStringFunction()
{
lblstringKey.Text= GetStringKey();
}
从上述函数中,GetSubtract() 完美运行。但是 GetStringKey() 不起作用。当它在调试时达到它的功能时,它会自动取消Visual Studio中的运行模式。我该如何解决这个问题。
【问题讨论】:
-
你试过
CallingConvention = CallingConvention.Cdecl换GetStringKey吗?您可以在这里找到一些信息:stackoverflow.com/questions/15660722/… -
您没有在
extern C端指定调用约定,但在 .NET 端指定了两个不同的调用约定。一切都需要完全正确才能正常工作。您也没有指定应如何将返回的字符串封送回调用方。 -
@Flydog57,你能举例说明一下吗
标签: c# c++ winforms calling-convention