C#调用C++编写的DLL函数各种参数传递问题
来源:互联网 发布:金融程序员 编辑:程序博客网 时间:2024/06/08 02:03
1. 不返回值的参数
C++ 原型:
- bool SendNewSms(char *szTel, char *szMessage);
C#引用;
- [DllImport( "CdmaCard.dll",EntryPoint="SendNewSms")]
- public static extern bool SendNewSms(string phone,string msg);
2. 带返回值(char *)
C++原型:
- BOOL GetCardErrorMessage(char *szErrorMessage , int errorCode);
- [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++原型:
- BOOL GetSmsSaveStation (int *nSmsStation);
C#引用
- [DllImport( "CdmaCard.dll",EntryPoint="GetSmsSaveStation")]
- public static extern bool GetSmsSaveStation(ref int nStation);
4. 传递结构体指针(C++填充)
C++原型:
- struct NET_INFO_STRUCT
- {
- DWORD nDurationTime; //持续时间
- double nReceiveByte; //接收字节
- double nSendByte; //发送字节
- };
- BOOL NetGetConnectDetail(NET_INFO_STRUCT *lpNetInfo);
C#引用
- 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++原型:
- struct UIM_BOOK_STRUCT
- {
- int UimIndex;
- char szName[15];
- char szPhone[21];
- };
- int ReadUimAllBook(UIM_BOOK_STRUCT lpUimBookItem[],int nMaxArraySize);
C#引用
- [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);
类型不一致,会导致调用失败,
(1) long 类型,在C++中是4字节的整数,在C#中是8字节的整数;
(2) 字符串类型的设置不正确;
以下是几个简单的window调用
- [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);
转自:http://blog.csdn.net/superhackerzhang/article/details/7648360
阅读全文
0 0
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 不指定
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C#调用C函数(DLL)传递参数问题
- C#调用C函数(DLL)传递参数问题
- C#调用C++dll 结构体参数传递问题
- C#调用C++dll 结构体参数传递问题
- android.intent.action.BOOT_COMPLETED开启无法重启的问题
- Chrome 开发工具之Timeline
- 使用php列出日志文件
- ionic页面的生命周期
- 6. ZigZag Conversion
- C#调用C++编写的DLL函数各种参数传递问题
- 线程安全与可重入函数
- 使用 Wowza 录制直播流 (HTTPLiveStreamRecord)
- 在IDEA中如何取消打过的断点
- [乐意黎原创] Error: listen EADDRNOTAVAIL 119.23.235.10:3000 at Object.exports._errnoException (util.js:10
- VS中监视窗口,即时窗口和输出窗口的使用
- D 03_JavaScript引入方式和注解
- Opencv学习之非线性滤波
- webjs--实现多图片的上传