托管的 PInvoke 签名与非托管的目标签名不匹配?? 20
我用C#调用C++写的DLL,为什么有这个问题?对PInvoke函数“test2!test2.Form1::Add”的调用导致堆栈不对称。原因可能是托管的PInvoke签...
我用C#调用C++写的DLL,为什么有这个问题?
对 PInvoke 函数“test2!test2.Form1::Add”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
C++头文件
//head.h
// Returns a + b
extern "C" _declspec(dllexport) double Add(double a, double b);
// Returns a - b
extern "C" _declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
extern "C" _declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0
extern "C" _declspec(dllexport) double Divide(double a, double b);
C++源文件
// sourcefile
#include "Head.h"
#include <stdexcept>
using namespace std;
extern "C" _declspec(dllexport) double Add(double a, double b)
{
return a + b;
}
extern "C" _declspec(dllexport) double Subtract(double a, double b)
{
return a - b;
}
extern "C" _declspec(dllexport) double Multiply(double a, double b)
{
return a * b;
}
extern "C" _declspec(dllexport) double Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
C#中调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("Dll.dll")]
public static extern double Add(double a, double b);
[DllImport("Dll.dll")]
public static extern double Subtract(double a, double b);
private void button1_Click(object sender, EventArgs e)
{
double a=8.525,b=6.168;
label1.Text += Convert.ToString(Add(a, b));
}
}
}
请问问题在哪,我看了类似的问题,都是long类型在C++中跟C#中不同导致的,我这个呢? 展开
对 PInvoke 函数“test2!test2.Form1::Add”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
C++头文件
//head.h
// Returns a + b
extern "C" _declspec(dllexport) double Add(double a, double b);
// Returns a - b
extern "C" _declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
extern "C" _declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0
extern "C" _declspec(dllexport) double Divide(double a, double b);
C++源文件
// sourcefile
#include "Head.h"
#include <stdexcept>
using namespace std;
extern "C" _declspec(dllexport) double Add(double a, double b)
{
return a + b;
}
extern "C" _declspec(dllexport) double Subtract(double a, double b)
{
return a - b;
}
extern "C" _declspec(dllexport) double Multiply(double a, double b)
{
return a * b;
}
extern "C" _declspec(dllexport) double Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
C#中调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("Dll.dll")]
public static extern double Add(double a, double b);
[DllImport("Dll.dll")]
public static extern double Subtract(double a, double b);
private void button1_Click(object sender, EventArgs e)
{
double a=8.525,b=6.168;
label1.Text += Convert.ToString(Add(a, b));
}
}
}
请问问题在哪,我看了类似的问题,都是long类型在C++中跟C#中不同导致的,我这个呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询