当前位置: 首页 > news >正文

C# 调用 C++ Dll 类库的传参问题

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);

C# 引用:

[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);

6、注意问题

类型不一致,会导致调用失败:

(1)、 long 类型,在 C++ 中是 4 字节的整数,在 C# 中是 8 字节的整数;

(2)、字符串类型的设置不正确;

以下是几个简单的 Windows 调用:

[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);

相关文章:

  • 转:fastText原理及实践(达观数据王江)
  • 算法问题实战策略
  • BZOJ 2584: [Wc2012]memory(扫描线+线段树)
  • 用最新NLP库Flair做文本分类
  • ASP.NET Core 2.0 : 三. 项目结构
  • 前后端分离架构中接口测试最佳实践
  • js闭包与高阶函数
  • java知识点1(this指针)
  • Confluent 修改开源许可证,限制云供应商滥用
  • vagrant设置虚拟机的名字
  • 寒假作业安排及注意点
  • 团队管理 - 团队发展五阶段
  • 线性代数---范数
  • Delphi 调用C#编写的WebService 参数为Null解决方法
  • BZOJ 1449 球队收益(最小费用最大流)
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 2017年终总结、随想
  • exports和module.exports
  • extjs4学习之配置
  • IP路由与转发
  • Java 网络编程(2):UDP 的使用
  • java正则表式的使用
  • Selenium实战教程系列(二)---元素定位
  • supervisor 永不挂掉的进程 安装以及使用
  • Swoft 源码剖析 - 代码自动更新机制
  • Vultr 教程目录
  • WePY 在小程序性能调优上做出的探究
  • 不上全站https的网站你们就等着被恶心死吧
  • 高度不固定时垂直居中
  • 简单基于spring的redis配置(单机和集群模式)
  • 解析 Webpack中import、require、按需加载的执行过程
  • 前端路由实现-history
  • 前嗅ForeSpider采集配置界面介绍
  • 说说动画卡顿的解决方案
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • #mysql 8.0 踩坑日记
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (c语言)strcpy函数用法
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (第一天)包装对象、作用域、创建对象
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (四)Linux Shell编程——输入输出重定向
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .Mobi域名介绍
  • .NET CORE 第一节 创建基本的 asp.net core
  • .net core 连接数据库,通过数据库生成Modell
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .NET简谈设计模式之(单件模式)
  • .Net中ListT 泛型转成DataTable、DataSet
  • .sys文件乱码_python vscode输出乱码
  • @RequestParam,@RequestBody和@PathVariable 区别
  • @Responsebody与@RequestBody