积极答复者
一个关于c#调用C++编写的dll的问题,求助!!

问题
-
C++编写dll如下:
头文件:
//ddll.h
extern"C" int __declspec(dllexport) add(int i, int j);
实现文件:
//ddll.cpp
#include "stdafx.h"
#include"ddll.h"
int add(int i, int j)
{
return i+j;
}
以上生成dd2.dll;
下面是c#调用过程:
新建win32项目之后,先添加类,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace call
{
class Class1
{
[DllImport("dd2.dll")]
public static extern int add(int i, int j);
}
}
然后再调用:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace call{class Program{static void Main(string[] args){int i;i = Class1.add(10, 20);Console.WriteLine(i);Console.ReadKey();}}}出现问题:托管调试助手“PInvokeStackImbalance”在“D:\VS2010\控件\call\call\bin\Debug\call.vshost.exe”中检测到故障。其他信息: 对 PInvoke 函数“call!call.Class1::add”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。求教,该怎么解决???- 已移动 Jackie-Sun 2011年7月29日 7:35 (发件人:Visual C#)
答案
-
我在类中更改了一下,就可以了,更改如下:
namespace call
{
class Class1
{
[DllImport("dd2.dll",EntryPoint="add",CallingConvention =CallingConvention.Cdecl)]
public static extern int add(int i, int j);
}
}
请问这样更改的依据是??
还有,dll中有多个函数,例如还有minus(减法),mul(乘)等,那上述加黑导入处的语句该如何编写呢??谢谢
- 已标记为答案 Paul Zhou 2011年8月8日 5:12
全部回复
-
我在类中更改了一下,就可以了,更改如下:
namespace call
{
class Class1
{
[DllImport("dd2.dll",EntryPoint="add",CallingConvention =CallingConvention.Cdecl)]
public static extern int add(int i, int j);
}
}
请问这样更改的依据是??
还有,dll中有多个函数,例如还有minus(减法),mul(乘)等,那上述加黑导入处的语句该如何编写呢??谢谢
- 已标记为答案 Paul Zhou 2011年8月8日 5:12