商城 设为首页注册本站  论坛  繁體中文

慧民电脑芯片级维修-电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Windows >> 系统优化 >> 优化性能 >> Windows正文

C#中调用带回调函数和自定义结构体的DLL例程

文章来源:CSDN.NET 作者:佚名 更新时间:2004-9-8 【 】 【加入收藏

  开发环境:  WinXP Pro(SP2英文版) + VS.NET 2003中文版
  接口库版本: CMPP2.0 API第二版(V2.6)
  该例程演示了如何在C#里面调用VC6.0开发的带回调函数的API,而且回调函数的参数包含结构体,使用C#的委托和IntPtr方法实现.
  由于我使用C#刚两天,这是我写的第一个C#程序,因此例程写的可能有点粗糙,但是编译和运行完全没有问题.
  CMPP2.0的API封装成了标准C调用的方法,提供以下三个接口,使用的时候只要有CMPPAPI.dll就可以了.
  #define DllExport extern "C" __declspec(dllexport)
  DllExport int __stdcall Cmpp2Start(LPCTSTR pchSmgIp, int nMtPort, int nMoPort, LPCTSTR pchUserName, LPCTSTR pchUserPwd, unsigned char uchVersion, void (__stdcall *OnSmgMsg)(CMPP_SMGTOSP* css), int nConnType, void (__stdcall *OnLogFile)(LPCTSTR str));
  DllExport int __stdcall Cmpp2Submit(unsigned char uchPKtotal, unsigned char uchPKnumber, unsigned char uchNeedreport, unsigned char uchMsglevel, LPCTSTR pchServiceid, unsigned char uchFeeusertype, LPCTSTR pchFeeterminalid, unsigned char uchTppid, unsigned char uchTpudhi, unsigned char uchMsgfmt, LPCTSTR pchMsgsrc, LPCTSTR pchFeetype, LPCTSTR pchFeecode, LPCTSTR pchValidtime, LPCTSTR pchAttime, LPCTSTR pchSrcid, unsigned char uchDestusrtl, LPCTSTR pchDestterminalid, unsigned char uchMsglen, LPCTSTR pchMsgcontent);
  DllExport int __stdcall Cmpp2Release();
  在C#里面如何调用API,如何声明结构体,如何使用委托实现回调函数,如何实现使用自定义结构体作为参数的回调函数,请仔细查看例程源码。注意:CMPPAPI.dll要和可执行文件放到同一个目录下,或者放到可执行文件能找到的目录,或者放到系统目录下(如:C:\windows\system32).
  
  下面是C#下面的完整的调用代码
  
  //Class1.cs
  
  using System;
  // 该名称空间包含了在Visual C#中调用API的一些必要集合
  using System.Runtime.InteropServices;
  // 使用Sleep方法需要的命名空间
  using System.Threading;
  
  namespace CMPPAPI_Sample_CSharp
  {
  //---------------------------------------------------------------------
  //---------以下是DLL中需要使用的结构体的定义---------------------------
  //--------Pack = 1表示结构体按一个字节对齐----------------------------
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_HEAD
  {
  public uint nTotalLength;
  public uint nCommandId;
  public uint nSeqId;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_CONNECT
  {
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
  public string sSourceAddr;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
  public string sAuthSource;
  public byte cVersion;
  public uint nTimeStamp;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_CONNECT_RESP
  {
  public byte uchStatus;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
  public string sAuthISMG;
  public byte cVersion;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_SUBMIT_RESP
  {
  public long nMsgid;
  public byte uchResult;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_STATUS_REPORT
  {
  public long nMsgid;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
  public string sStat;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
  public string sSubmitTime;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
  public string sDoneTime;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
  public string sDestTerminalId;
  public uint nSmscSeq;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_MO_MSGCONTENT
  {
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
  public string sMsgcontent;
  public CMPP_STATUS_REPORT csr;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_DELIVER
  {
  public long nMsgid;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
  public string sDestid;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
  public string sServiceid;
  public byte uchTppid;
  public byte uchTpudhi;
  public byte uchMsgfmt;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
  public string sSrcterminalid;
  public byte uchRegisteredDelivery;
  public byte uchMsglength;
  
  public CMPP_MO_MSGCONTENT mo_msg;
  
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  public string sReserved;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_SUBMIT
  {
  public long nMsgid;
  public byte uchPkTotal;
  public byte uchPkNumber;
  public byte uchRegisteredDelivery;
  public byte uchMsgLevel;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
  public string sServiceId;
  public byte uchFeeUserType;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
  public string sFeeTerminalId;
  public byte uchTpPid;
  public byte uchTpUdhi;
  public byte uchMsgFmt;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
  public string sMsgSrc;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
  public string sFeeType;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
  public string sFeeCode;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
  public string sValidTime;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
  public string sAtTime;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
  public string sSrcId;
  public byte uchDstUsrTl;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21*100)]
  public string sDstTerminalId;
  public byte uchMsgLength;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
  public string sMsgContent;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  public string sReserved;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_QUERY
  {
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  public string sTime;
  public byte uchQueryType;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
  public string sQueryCode;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  public string sReserved;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_QUERY_RESP
  {
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  public string sTime;
  public byte uchQueryType;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
  public string sQueryCode;
  public uint nMTTLMsg; //从SP接收消息总数.
  public uint nMTTLUsr; //从SP接收用户总数.
  public uint nMTScs;  //成功转发数量.
  public uint nMTWT;  //待转发数量.
  public uint nMTFL;  //转发失败数量.
  public uint nMOScs;  //向SP成功送达数量.
  public uint nMOWT;  //向SP待送达数量.
  public uint nMOFL;  //向SP送达失败数量.
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_CANCEL
  {
  public long nMsgid;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_CANCEL_RESP
  {
  public byte uchSuccessId;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_ACTIVETEST_RESP
  {
  public byte uchReserved;
  }
  
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct CMPP_BODY
  {
  public CMPP_CONNECT_RESP pk_connectresp;
  public CMPP_SUBMIT_RESP pk_submitresp;
  public CMPP_QUERY_RESP pk_queryre
  • 上一篇Windows:

  • 下一篇Windows:
  • 最 新 热 门
     低配置电脑如何优化Vista系统运行如飞似XP
     安全修改注册表默认响应方式
     禁用Vista下的硬盘监控任务栏警告
     巧用GhostEXP 将补丁加入GHO文件
     windows运行中几个特殊的执行命令
     技巧:用MSDaRT解决Vista启动故障
     XP中一人远程管理关机好方法
     一次简单修改 让WinRAR解压缩速度更快
     彻底禁用暴风顽固进程 让系统清爽起来
     通吃主流格式 让系统图片查看器更完美
    最 新 推 荐
     Windows XP系统性能更优越的四招
     终极优化:Windows Vista系统服务设置完全攻…
     全面解读Vista服务 - Vista 系统服务手动优…
     让Windows XP跑得更快更稳
     Windows系统通用加速优化技巧
     加一减一让系统性能更快
     提高运行速度 教你一步一步优化XP系统
     有备而来 治疗Vista优化后遗症
     前端平台经理解说:破解Vista肥胖之谜
     鲜为人知却非常高效的4个 Vista优化秘笈
    相 关 文 章

    Windows Vista系统中调查雅黑字体大小
    XP中调整Thumbs.db加快图片预览速度
    VC#2005 WinForm 开发体验
    C# 中数组和类型转换的功能实现
    如何用c#来制作一个activeX控件或类似的东西
    用 C# 通过内码实现汉字转化为拼音
    C#与Excel的交互功能代码实例
    在 C# 中处理结构内的数组源代码分析
    浅谈 C# 中的代码协同 (Coroutine) 执行支持
    C#Windows控件的焦点事件顺序

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

      Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    门市地址:广东省佛山市南海区黄岐黄海路133号
    本网站所有内容未经许可不得转载或做其他使用
    电话:0757-81139980 QQ:83306923