熱點推薦:
您现在的位置: 電腦知識網 >> 操作系統 >> Windows優化 >> 正文

C#中調用帶回調函數和自定義結構體的DLL例程

2013-11-12 00:08:16  來源: Windows優化 

  開發環境:  WinXP Pro(SP英文版) + VSNET 中文版
  接口庫版本: CMPP API第二版(V)
  該例程演示了如何在C#裡面調用VC開發的帶回調函數的API而且回調函數的參數包含結構體使用C#的委托和IntPtr方法實現
  由於我使用C#剛兩天這是我寫的第一個C#程序因此例程寫的可能有點粗糙但是編譯和運行完全沒有問題
  CMPP的API封裝成了標准C調用的方法提供以下三個接口使用的時候只要有CMPPAPIdll就可以了
  #define DllExport extern C __declspec(dllexport)
  DllExport int __stdcall CmppStart(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 CmppSubmit(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 CmppRelease();
  在C#裡面如何調用API如何聲明結構體如何使用委托實現回調函數如何實現使用自定義結構體作為參數的回調函數請仔細查看例程源碼注意:CMPPAPIdll要和可執行文件放到同一個目錄下或者放到可執行文件能找到的目錄或者放到系統目錄下(如:C:\windows\system)
  
  下面是C#下面的完整的調用代碼
  
  //Classcs
  
  using System;
  // 該名稱空間包含了在Visual C#中調用API的一些必要集合
  using SystemRuntimeInteropServices;
  // 使用Sleep方法需要的命名空間
  using SystemThreading;
  
  namespace CMPPAPI_Sample_CSharp
  {
  //
  //以下是DLL中需要使用的結構體的定義
  //Pack = 表示結構體按一個字節對齊
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_HEAD
  {
  public uint nTotalLength;
  public uint nCommandId;
  public uint nSeqId;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_CONNECT
  {
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sSourceAddr;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sAuthSource;
  public byte cVersion;
  public uint nTimeStamp;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_CONNECT_RESP
  {
  public byte uchStatus;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sAuthISMG;
  public byte cVersion;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_SUBMIT_RESP
  {
  public long nMsgid;
  public byte uchResult;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_STATUS_REPORT
  {
  public long nMsgid;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sStat;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sSubmitTime;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sDoneTime;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sDestTerminalId;
  public uint nSmscSeq;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_MO_MSGCONTENT
  {
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sMsgcontent;
  public CMPP_STATUS_REPORT csr;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_DELIVER
  {
  public long nMsgid;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sDestid;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sServiceid;
  public byte uchTppid;
  public byte uchTpudhi;
  public byte uchMsgfmt;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sSrcterminalid;
  public byte uchRegisteredDelivery;
  public byte uchMsglength;
  
  public CMPP_MO_MSGCONTENT mo_msg;
  
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sReserved;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_SUBMIT
  {
  public long nMsgid;
  public byte uchPkTotal;
  public byte uchPkNumber;
  public byte uchRegisteredDelivery;
  public byte uchMsgLevel;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sServiceId;
  public byte uchFeeUserType;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sFeeTerminalId;
  public byte uchTpPid;
  public byte uchTpUdhi;
  public byte uchMsgFmt;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sMsgSrc;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sFeeType;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sFeeCode;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sValidTime;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sAtTime;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sSrcId;
  public byte uchDstUsrTl;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = *)]
  public string sDstTerminalId;
  public byte uchMsgLength;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sMsgContent;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sReserved;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_QUERY
  {
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sTime;
  public byte uchQueryType;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sQueryCode;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sReserved;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_QUERY_RESP
  {
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  public string sTime;
  public byte uchQueryType;
  [MarshalAs(UnmanagedTypeByValTStr SizeConst = )]
  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(LayoutKindSequential Pack = )]
  public struct CMPP_CANCEL
  {
  public long nMsgid;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_CANCEL_RESP
  {
  public byte uchSuccessId;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_ACTIVETEST_RESP
  {
  public byte uchReserved;
  }
  
  [StructLayout(LayoutKindSequential Pack = )]
  public struct CMPP_BODY
  {
  public CMPP_CONNECT_RESP pk_connectresp;
  public CMPP_SUBMIT_RESP pk_submitresp;
  public CMPP_QUERY_RESP pk_queryre
From:http://tw.wingwit.com/Article/os/youhua/201311/10754.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.