c#调用C/C++代码时出现的PInvokeStackImbalance异常实例及解决方式
来源:互联网 发布:怎么拿到网站数据库 编辑:程序博客网 时间:2024/05/20 07:35
c#中调用c/C++的dll时,需要加上CallingConvention特性参数,否则容易出现PInvokeStackImbalance异常。例如,以下问题描述及解决 方式。
(一)问题描述
在利用C#调用本地dll库时,原先在c/c++中的函数原型如下:
extern "C" __declspec(dllexport) int playSpecificSound(wchar_t* fileName);
在c#中原先这样声明:
[DllImport("ASR\\ASR.dll", EntryPoint = "playSpecificSound")]
public static extern int PlaySpecificSound(IntPtr fileNameWithAbsoultePath);
在C#中的调用方式为:
PlaySpecificSound(Encoding.Unicode.GetBytes(Encoding.Unicode.GetBytes(fileName)));
问题出现:在调用过程中,总是出现PInvokeStackImbalance的异常。原本以为是x64系统导致的,但未能解决。
(二)问题解决
当C#中修改为如下的声明时(加上CallingConvention = CallingConvention.Cdecl),一切正常,问题解决。
[DllImport("ASR\\ASR.dll", EntryPoint = "playSpecificSound", CallingConvention = CallingConvention.Cdecl)]
public static extern int PlaySpecificSound(IntPtr fileNameWithAbsoultePath);
- c#调用C/C++代码时出现的PInvokeStackImbalance异常实例及解决方式
- C#关闭pinvokestackimbalance异常
- 求助C#调用C++dll异常解决
- pInvokeStackImbalance 调试异常的解决办法。
- 调用Class.forName().newInstance()生成实例时出现IllegalAccessException异常的解决。
- 【C#】常见的异常及异常处理方式
- PInvokeStackImbalance异常
- VS2010出现重复的代码项原因及解决方式
- C# 调用C++DLL参数异常解决方法
- VS2010产生C的dll方法和C#调用方式
- c# 调用ictclas.dll出现的异常
- Unity调用外接摄像头的实例代码(C#)
- Unity调用外接摄像头的实例代码(C#)
- linux c程序调用lua代码的实例
- C#项目中发生PInvokeStackImbalance异常,堆栈不对称异常
- Java调用C#WEBSERVICE需要注意的细节及实例
- GO程序调用C及C++库的实现方式
- 【c++】C#调用C++ DLL 托管方式
- P4876 近似排列计数
- 1134. Vertex Cover (25)
- Secret Origins
- 安卓开发-ListView的适配器和监听器/ExpandableListView的使用
- Android 试题滑动刻度尺--自定义View
- c#调用C/C++代码时出现的PInvokeStackImbalance异常实例及解决方式
- 【LeetNode2-1-7】Two sum--C++源代码(VS2015)
- MongodbTemplate的增删改操作
- [LeetCode] 19. Remove Nth Node From End of List
- 2017/10/05 明天就回去了..
- HDU
- 二.像素操作
- 百度地图学习总结(2)—我爱北京天安门,Hello-BaiduMap
- java正则表达式的使用-抓取百度搜索的结果