该例程演示了C#调用API函数,而且回调函数的参数包含结构体,使用C#的委托和IntPtr方法实现.由于我使用C#刚两天,这是我写的***个C#程序,因此例程写的可能有点粗糙,但是编译和运行完全没有问题.CMPP2.0的API封装成了标准C调用API的方法,提供以下三个接口,使用的时候只要有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();
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
C#调用API,如何声明结构体,如何使用委托实现回调函数,如何实现使用自定义结构体作为参数的回调函数,请仔细查看例程源码。注意:CMPPAPI.dll要和可执行文件放到同一个目录下,或者放到可执行文件能找到的目录,或者放到系统目录下(如:C:\windows \system32).
//Class1.cs
using System;
// 该名称空间包含了在Visual C#中调用API的一些必要集合
using System.Runtime.InteropServices;
// 使用Sleep方法需要的命名空间
using System.Threading;
namespace CMPPAPI_Sample_CSharp
{
[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)]
publicstruct CMPP_CONNECT_RESP
{
public byte uchStatus;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sAuthISMG;
public byte cVersion;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct CMPP_SUBMIT_RESP
{
public long nMsgid;
public byte uchResult;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct 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)]
publicstruct CMPP_MO_MSGCONTENT
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
public string sMsgcontent;
public CMPP_STATUS_REPORT csr;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct CMPP_DELIVER
{
public longnMsgid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string sDestid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string sServiceid;
public byteuchTppid;
public byteuchTpudhi;
public byteuchMsgfmt;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string sSrcterminalid;
public byteuchRegisteredDelivery;
public byteuchMsglength;
public CMPP_MO_MSGCONTENT mo_msg;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string sReserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct CMPP_SUBMIT
{
public longnMsgid;
public byteuchPkTotal;
public byteuchPkNumber;
public byteuchRegisteredDelivery;
public byteuchMsgLevel;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string sServiceId;
public byteuchFeeUserType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string sFeeTerminalId;
public byteuchTpPid;
public byteuchTpUdhi;
public byteuchMsgFmt;
[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 byteuchMsgLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
public string sMsgContent;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string sReserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct 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)]
publicstruct 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)]
publicstruct CMPP_CANCEL
{
public long nMsgid;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct CMPP_CANCEL_RESP
{
public byte uchSuccessId;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct CMPP_ACTIVETEST_RESP
{
public byte uchReserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
publicstruct CMPP_BODY
{
public CMPP_CONNECT_RESP pk_connectresp;
public CMPP_SUBMIT_RESPpk_submitresp;
public CMPP_QUERY_RESPpk_queryre
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
以上介绍C#调用API
【编辑推荐】