我需要在本機代碼中實現我的應用程序邏輯的一小部分。 要測試PInvoke功能,我創建了一個非託管C++ Win32 Dll和使用PInvoke來使用dll函數的WPF項目的簡單解決方案。 我碰上的問題是,我接收關於 「不平衡堆棧」 和 「可能簽名不匹配」 異常編寫可由C#/ .NET代碼消耗的非託管DLL代碼
這是我的代碼:
1)C++(DLL)
#include <stdio.h>
#include <Windows.h>
extern "C"
{
__declspec(dllexport) int add(int a, int b)
{
return a+b;
}
}
2 )C#:
public partial class MainWindow : Window
{
[DllImport("MyLibrary.dll")]
static extern int add(int a, int b);
public MainWindow()
{
InitializeComponent();
}
private void btnVersion_Click(object sender, RoutedEventArgs e)
{
var res = add(3,2);
}
}
代碼拋出一個異常,指出「這可能是因爲託管的PInvoke簽名不MATC h非託管目標籤名。檢查PInvoke簽名的調用約定和參數是否與目標非託管簽名相匹配。「
哪裏是我的錯