PInvoke调用导致堆栈不对称 c#调用C++win32非托管dll的问题深度分析
2013-12-16 15:58
459 查看
问题描述:对 PInvoke 函数“xxFunction()”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配
问题解决:1、在c#中函数声明处改一个参数,[DllImport("xx.dll", EntryPoint=“xxFunction”, CallingConvention = CallingConvention.Cdecl)]调用时不变
2、在c++代码中改对应的c++函数参数从extern“C” _declspec(dllexport) void xxFunction()改成
extern“C” _declspec(dllexport) void __stdcall xxFunction()
问题分析:
在c++WIN32程序中有三种calling convention(呼叫约定):__cdecl, __stdcall, __fastcall默认为__cdecl,而c#中默认为CallingConvention =CallingConvention.Winapi,两个平台呼叫约定不一致,所以会出现提示的不匹配错误。
__cdecl为调用函数即C#中清理堆栈中保存的参数。参数的大小不确定时用这个,比如string
__stdcall对应c#中CallingConvention =CallingConvention.Winapi,它由c++中函数自动清理。
Win32 calling convention(呼叫约定)的三种约定具体分析见/article/6814453.html
http://www.cnblogs.com/dust/articles/1190641.html。
问题解决:1、在c#中函数声明处改一个参数,[DllImport("xx.dll", EntryPoint=“xxFunction”, CallingConvention = CallingConvention.Cdecl)]调用时不变
2、在c++代码中改对应的c++函数参数从extern“C” _declspec(dllexport) void xxFunction()改成
extern“C” _declspec(dllexport) void __stdcall xxFunction()
问题分析:
在c++WIN32程序中有三种calling convention(呼叫约定):__cdecl, __stdcall, __fastcall默认为__cdecl,而c#中默认为CallingConvention =CallingConvention.Winapi,两个平台呼叫约定不一致,所以会出现提示的不匹配错误。
__cdecl为调用函数即C#中清理堆栈中保存的参数。参数的大小不确定时用这个,比如string
__stdcall对应c#中CallingConvention =CallingConvention.Winapi,它由c++中函数自动清理。
Win32 calling convention(呼叫约定)的三种约定具体分析见/article/6814453.html
http://www.cnblogs.com/dust/articles/1190641.html。
相关文章推荐
- c语言中如何给字符串数组赋值
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之四 - 参数化
- Milenage算法的改写(c,c++变成java)
- Effective C++读书笔记(2)
- 要心中有“数”——C语言初学者代码中的常见错误与瑕疵(8)
- C++ Boost Thread 编程指南
- NEW IN C++ 11
- readers and writers problem 读者写者问题 写者优先 C++实现
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之三 - 事件机制
- C++计时的几种方法说明及例程
- Effective C++读书笔记(1)
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之二 - 断言
- C++字符转UTF-8字符
- C++编写Config类读取配置文件
- C++界面库大全2013
- C++ Keywords Alternative Operator Names
- c++原型模式(Prototype)
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之一 - 初识gtest
- c++文件读写
- C语言中的可变参数函数的浅析(以Arm 程序中的printf()函数实现为例) .