C# 调用 C++ 类库的传参问题
最近经常用到 C# 调用 C++ 开发的动态类库,遇到了一些问题,多次尝试后终于解决了,这里简单的做下记录方便以后查询。
1、不返回值的参数
C++ 原型:
1 | bool SendNewSms( char *szTel, char *szMessage); |
C# 引用:
1 2 | [DllImport( "CdmaCard.dll" ,EntryPoint= "SendNewSms" )] public static extern bool SendNewSms( string phone, string msg); |
2、带返回值(char *)
C++ 原型:
1 | BOOL GetCardErrorMessage( char *szErrorMessage , int errorCode); |
C# 引用:
1 2 3 4 | [DllImport( "CdmaCard.dll" ,EntryPoint= "GetCardErrorMessage" )] public static extern int GetCardErrorMessage(StringBuilder msg, int errorCode); StringBuilder buf = new StringBuilder(1024); //指定的 Buf 大小必须大于可能的最大长度 GetCardErrorMessage(buf,1); |
3、带返回值(其他类型)
C++ 原型:
1 | BOOL GetSmsSaveStation ( int *nSmsStation); |
C# 引用:
1 2 | [DllImport( "CdmaCard.dll" ,EntryPoint= "GetSmsSaveStation" )] public static extern bool GetSmsSaveStation( ref int nStation); |
4、传递结构体指针(C++ 填充)
C++ 原型:
1 2 3 4 5 6 7 | struct NET_INFO_STRUCT { DWORD nDurationTime; //持续时间 double nReceiveByte; //接收字节 double nSendByte; //发送字节 }; BOOL NetGetConnectDetail(NET_INFO_STRUCT *lpNetInfo); |
C# 引用:
1 2 3 4 5 6 7 8 9 10 11 12 | public struct NET_INFO_STRUCT { public uint nDurationTime; //持续时间 public double nReceiveByte; //接收字节 public double nSendByte; //发送字节 } [DllImport( "CdmaCard.dll" ,EntryPoint= "NetGetConnectDetail" )] public static extern int NetGetConnectDetail( ref NET_INFO_STRUCT pNetInfo); NET_INFO_STRUCT netInfo = new NET_INFO_STRUCT(); NetGetConnectDetail( ref netInfo); |
5、传递结构体数组(C++ 来填充)
C++ 原型:
1 2 3 4 5 6 7 | struct UIM_BOOK_STRUCT { int UimIndex; char szName[15]; char szPhone[21]; }; int ReadUimAllBook(UIM_BOOK_STRUCT lpUimBookItem[], int nMaxArraySize); |
C# 引用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] //可以指定编码类型 public struct UIM_BOOK_STRUCT { public int UimIndex; [MarshalAs(UnmanagedType.ByValTStr, SizeConst= 15)] public string szName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst= 21)] public string szPhone; }; [DllImport( "CdmaCard.dll" ,EntryPoint= "ReadUimAllBook" )] public static extern int ReadUimAllBook( out UIM_BOOK_STRUCT [] lpUimBookItem, int nMaxArraySize); UIM_BOOK_STRUCT[] p = new UIM_BOOK_STRUCT[20]; int ret = ReadUimAllBook(p,p.Length); |
6、注意问题
类型不一致,会导致调用失败:
(1)、 long 类型,在 C++ 中是 4 字节的整数,在 C# 中是 8 字节的整数;
(2)、字符串类型的设置不正确;
以下是几个简单的 Windows 调用:
1 2 3 4 5 6 7 8 9 10 11 | [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously [DllImport( "User32.dll" , CharSet=CharSet.Auto)] public static extern bool ScreenToClient(IntPtr hWnd, ref System.Drawing.Point rect); [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously [DllImport( "User32.dll" , CharSet=CharSet.Auto)] public static extern bool GetWindowRect(IntPtr hWnd, out System.Drawing.Rectangle rect); [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously [DllImport( "User32.dll" , CharSet=CharSet.Auto)] public static extern bool UnregisterClass([MarshalAs(UnmanagedType.LPTStr)] string className, IntPtr instanceHandle); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库