📄 class1.cs
字号:
using System;
// 该名称空间包含了在Visual C#中调用API的一些必要集合
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
namespace CMPPAPI_Sample_CSharp
{
//
//定义Cmpp2.0 消息结构.
//
//**********************************************************************
// STRUCTURE OF CMPP_HEAD.
//**********************************************************************
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CMPP_HEAD
{
public uint nTotalLength;
public uint nCommandId;
public uint nSeqId;
}
//**********************************************************************
// STRUCTURE OF CMPP_CONNECT.
//**********************************************************************
[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;
}
//**********************************************************************
// STRUCTURE OF CMPP_CONNECT_RESP.
//**********************************************************************
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CMPP_CONNECT_RESP
{
public byte uchStatus;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sAuthISMG;
public byte cVersion;
}
//**********************************************************************
// STRUCTURE OF CMPP_SUBMIT_DELIVER_RESP.
//**********************************************************************
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CMPP_SUBMIT_RESP
{
public long nMsgid;
public byte uchResult;
}
//**********************************************************************
// STRUCTURE OF CMPP_DELIVER_StatusReport.
//**********************************************************************
[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;
}
//**********************************************************************
// STRUCTURE OF CMPP_DELIVER.
//**********************************************************************
[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;
}
//**********************************************************************
// STRUCTURE OF CMPP_SUBMIT.
//**********************************************************************
[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;
}
//**********************************************************************
// STRUCTURE OF CMPP_QUERY.
//**********************************************************************
[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;
}
//**********************************************************************
// STRUCTURE OF CMPP_QUERY_RESP.
//**********************************************************************
[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送达失败数量.
}
//**********************************************************************
// STRUCTURE OF CMPP_CANCEL.
//**********************************************************************
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CMPP_CANCEL
{
public long nMsgid;
}
//**********************************************************************
// STRUCTURE OF CMPP_CANCEL_RESP.
//**********************************************************************
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CMPP_CANCEL_RESP
{
public byte uchSuccessId;
}
//**********************************************************************
// STRUCTURE OF CMPP_ACTIVETEST_RESP.
//**********************************************************************
[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_queryresp;
public CMPP_DELIVER pk_deliver;
public CMPP_CANCEL_RESP pk_cancelresp;
public CMPP_ACTIVETEST_RESP pk_activetestresp;
}
//**********************************************************************
// STRUCTURE OF CMPP_SMG_TO_SP.
//**********************************************************************
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CMPP_SMGTOSP
{
public CMPP_HEAD pk_head;
public CMPP_BODY pk_body;
}
public delegate void OnSmgMsg(IntPtr css);
public delegate void OnLogMsg(string str);
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
public static void OnMsg(IntPtr ptr)
{
CMPP_SMGTOSP css = new CMPP_SMGTOSP();
css = (CMPP_SMGTOSP)Marshal.PtrToStructure(ptr, typeof(CMPP_SMGTOSP));
uint nCmdId = css.pk_head.nCommandId;
Console.WriteLine(css.pk_head.nCommandId);
if ( nCmdId == 0x80000004) {
}
else if ( nCmdId == 0x0000005) {
}
return;
}
public static void OnLog(string str)
{
Console.WriteLine(str);
return;
}
// API函数声明
// 1.Start Func
[DllImport("CMPPAPI.dll")]
public static extern int Cmpp2Start(string pchSmgIp, int nMtPort, int nMoPort,
string pchUserName, string pchUserPwd, byte uchVersion,
OnSmgMsg pf, int nConnType, OnLogMsg pf2);
// 2.Submit Func
[DllImport("CMPPAPI.dll")]
public static extern int Cmpp2Submit(byte uchPKtotal, byte uchPKnumber,
byte uchNeedreport, byte uchMsglevel, string pchServiceid,
byte uchFeeusertype, string pchFeeterminalid, byte uchTppid,
byte uchTpudhi, byte uchMsgfmt, string pchMsgsrc,
string pchFeetype, string pchFeecode, string pchValidtime,
string pchAttime, string pchSrcid, byte uchDestusrtl,
string pchDestterminalid, byte uchMsglen, string pchMsgcontent);
// 3.Release
[DllImport("CMPPAPI.dll")]
public static extern int Cmpp2Release();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c1 = new Class1();
OnSmgMsg pt = new OnSmgMsg(OnMsg);
OnLogMsg pLog = new OnLogMsg(OnLog);
int nRetCode = Cmpp2Start("127.0.0.1", 7890, 7891, "901234", "1234", (byte)0x20, pt, 0, pLog);
if ( 0 == nRetCode)
{
Console.WriteLine("Connect Smg success\n");
while ( true)
{
nRetCode = Cmpp2Submit(1, 1,
1, 3, "2939",
0, "", 0,
0, 0, "901234",
"02", "000020", "",
"", "01850", 1,
"8613660617374", 132, "收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话收到给我电话");
if ( 0 == nRetCode)
{
Console.WriteLine("Submit Success.");
}
else {
Console.WriteLine("Submit Fail.");
}
Thread.Sleep(3000);
}
Cmpp2Release();
}
else {
Console.WriteLine("Connect SMG fail.");
}
//
// TODO: 在此处添加代码以启动应用程序
//
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -