ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

在C#中与C/C++例程连接会导致rip

2019-07-04 03:53:20  阅读:213  来源: 互联网

标签:c pinvoke dll dllimport interopservices


我有一个供应商的API调用,我想在我的C#Windows Forms应用程序中使用它.问题是无论我尝试什么P/Invoke,我都会在线路上发生崩溃,说明呼叫已经使堆栈失衡.我尝试与供应商合作,但他们没有Visual Studio 2012,也没有任何C#或.NET的经验,经过几次来回的电子邮件,他们基本上都洗了问题.

这是他们的官方C/C++声明

const char *<dll name here>(int Param1,
                            const char *Param2,
                            const char *Param3,
                            unsigned long Param4,
                            unsigned short Param5,
                            unsigned short Param6,
                            unsigned short Param7,
                            unsigned short Param8,
                            unsigned short Param9);

这是我尝试过的最近的P / Invoke DLLImport行.

[System.Runtime.InteropServices.DllImport(
    "<dll name here>", EntryPoint = "<AnsiFunctionName>", ExactSpelling = true,
    CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern String <AnsiFunctionName>(int Param1,
                                               String Param2,
                                               String Param3,
                                               ulong Param4,
                                               Int16 Param5,
                                               Int16 Param6,
                                               Int16 Param7,
                                               Int16 Param8,
                                               Int16 Param9);

这是实际的错误消息:

Subject Line: PInvokeStackImbalance was detected

A call to P/Invoke function ” has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the P/Invoke signature match the target unmanaged signature.

我尝试了各种各样的东西,甚至设置了一个void返回类型我尝试不使用返回类型.我试过而不是Strings,StringBuilders和IntPtrs.无论我尝试什么,都没有用.

我在阅读Stack Overflow上的一篇文章之后向供应商提到,返回字符串是危险的,不应该这样做.他们的回应是:

“The string return problems mentioned on that page don’t apply to
our functions. They return a pointer to a statically-allocated string, so you don’t need to free it (and must not try to do so). It looks like the “UnmanagedType.LPStr” should work (though the IntPtr thing should work too, with the proper conversions or casting).

更新#1:正如评论中所提到的,我在使用uint,而不是ulong,直到这篇文章.我只是觉得我让比较看起来更好. uint和ulong都不起作用.

更新#2:也许给出实际的功能名称会有所帮助.它是Armadillo CreateCodeShort3A.展望未来,我使用纯.NET,但我仍然需要与之接口,目前还不是真的可行.更重要的是,这个问题让我很好奇为什么我无法解决像P / Invoke那样简单的事情.毕竟,有多少变化? (不是一个严肃的问题).

解决方法:

以下是我可以看到的可能性:

>返回值不应该是String.使用IntPtr并转换为Marshal.PtrToStringAnsi的字符串.
> C类型unsigned long应该与uint匹配.您在C#中使用了ulong,它是64位类型.在Windows上的C中,unsigned long是无符号的32位类型.
> C类型的unsigned short应与ushort匹配.
> C DLL使用的调用约定可能是cdecl.将CallingConvention = CallingConvention.Cdecl添加到您的P/Invoke.
>不要将SetLastError用于此类函数.这与Win32 API函数一起使用,它通过GetLastError返回错误条件.这个DLL文件几乎肯定不会.

所以P / Invoke应该是这样的:

[DllImport(@"mydll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern IntPtr FunctionName(
    int Param1, 
    string Param2, 
    string Param3, 
    uint Param4, 
    ushort Param5, 
    ushort Param6, 
    ushort Param7, 
    ushort Param8, 
    ushort Param9
);

标签:c,pinvoke,dll,dllimport,interopservices
来源: https://codeday.me/bug/20190704/1373349.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有