?? class1.cs
字號:
using System;
// 該名稱空間包含了在Visual C#中調用API的一些必要集合
using System.Runtime.InteropServices;
// 使用Thread.Sleep方法需要的命名空間
using System.Threading;
//Ucs2轉碼
//string str = System.Text.UnicodeEncoding.Unicode.GetString(bytes)
namespace SGIPAPI_Sample_CSharp
{
//---------------------------------------------------------------------
//---------以下是DLL中需要使用的結構體的定義---------------------------
//--------Pack = 1表示結構體按一個字節對齊----------------------------
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct sgipg_submit
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string sSpNumber; //sp的接入號碼
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string sChargeNumber; //付費號碼
public byte cUserCount; //接收短消息的手機數量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21*100)]
public string sUserNumber; //接受該短消息的手機號
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string sCorpId; //企業代碼,0-99999
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string sServiceType; //業務代碼,由sp定義
public byte cFeeType; //計費類型
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string sFeeValue; //該條短消息的收費值,單位為分
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string sGivenValue; //贈送用戶的話費,0-99999
public byte cAgentFlag; //代收費標志,0:應收;1:實收
public byte cMorelatetoMTFlag; //引起MT消息的原因
public byte cPriority; //優先級0-9,從低到高
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sExpireTime; //短消息壽命的終止時間,"yymmddhhmmsstnnp","tnnp"取固定值"032+"
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string sScheduleTime; //定時發送時間
public byte cReportFlag; //狀態報告標志,0-出錯返回狀態報告;1-總返回狀態報告;2-不要狀態報告;3...
public byte cTpPid;
public byte cUdhi;
public byte cMessageCoding; //編碼方式,0:Ascii;3:Write card;4:binary;8:ucs2;15:GBK
public byte cMessageType; //0:短消息信息
public uint nMsgLen; //短消息長度(不調用sgip_submit_sm_set_messagecontent,而手動賦值的話,需要調用函數htonl()轉換為網絡字節序)
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string sMsgContent; //2048;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
public string sLinkId;
public uint nID; //唯一標識,外部定義,可以不填
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct DeliverStr
{
public uint nSrcNum;
public uint nDateTime;
public uint nSeq;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string sUserNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string sSPNumber;
public byte tp_pid;
public byte tp_udhi;
public byte cMsgCoding;
public uint nMsgLen;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 160)]
public string sMsgContent;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
public string sLinkId;
}
//Report包結構
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ReportStr
{
public uint nSrcNum;
public uint nDateTime;
public uint nSeq;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
public string sUserNumbe;
public byte cState;
public byte cErrcode;
public uint nSubmitSeq;
public uint nSubmitDateTime;
}
//MT Response結構
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MTRespStr
{
public uint nID;
public uint nSrcNum;
public uint nDateTime;
public uint nSeq;
public byte cResult;
public sgipg_submit ss;
}
//Submit錯誤結構,當Submit發送不成功時,返回該結構
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MTErrorStr
{
public uint nID;
public uint nSrcNum;
public uint nDateTime;
public uint nSeq;
//1:因為連接不上SMG網關系統 2:登錄網關失敗 3:包發送失敗且超過重發次數 4.超時無應答 5.消息長度為零 6.沒有可用的連接
public int nErrorType;
public sgipg_submit ss;
}
//----------------DLL需要使用的結構體的定義----------------------------
//---------------------------------------------------------------------
//----------------聲明兩個委托,相當于VC里面的回調函數------------------
public delegate void dtOnResponse(IntPtr stMtResp);
public delegate void dtOnDeliver(IntPtr stMoMsg);
public delegate void dtOnReport(IntPtr stReport);
public delegate void dtOnMTError(IntPtr stMtErr);
//---------------------------------------------------------------------
/// <summary>
/// Class1 的摘要說明。
/// </summary>
class Class1
{
//--------------------------------------------------------------------------
//--------------DLL中的API函數聲明------------------------------------------
// 1.Start Func
[DllImport("SGIP.dll")]
public static extern int Sgip12Start(string sLocalIP, int nLocalPort, string sPeerIP, int nPeerPort,
string sLoginName, string sLoginPwd, string sSrcNum,
dtOnDeliver pfMO, dtOnReport pfReport,dtOnResponse pfMtResp, dtOnMTError pfMtErr,
int nConnCount, bool fOuptutDebugInfo);
// 2.Submit Func
[DllImport("SGIP.dll")]
public static extern void Sgip12Submit(sgipg_submit stMt,ref uint nSeq, ref uint nDateTime);
// 3.Release
[DllImport("SGIP.dll")]
public static extern void Sgip12Release();
//--------------DLL中的API函數聲明------------------------------------------
//--------------------------------------------------------------------------
//-----------------------------委托函數的定義-------------------------------
//----------------------這兒使用IntPtr實現與結構體之間的轉換----------------
//---------------------處理MT應答的函數-------------------------------------
public static void OnResponse(IntPtr ptr)
{
MTRespStr mt_resp = new MTRespStr();
mt_resp = (MTRespStr)Marshal.PtrToStructure(ptr, typeof(MTRespStr));
Console.WriteLine("收到一條回復消息,提交結果:" + mt_resp.cResult + "\t目標手機號:" + mt_resp.ss.sUserNumber
+ "\t發送內容:" + mt_resp.ss.sMsgContent);
return;
}
//---------------------處理MO消息的函數-------------------------------------
public static void OnDeliver(IntPtr ptr)
{
DeliverStr mo_pkg = new DeliverStr();
mo_pkg = (DeliverStr)Marshal.PtrToStructure(ptr, typeof(DeliverStr));
if ( mo_pkg.cMsgCoding == 8)
{
byte[] b1 = System.Text.Encoding.Default.GetBytes(mo_pkg.sMsgContent);
string sDest = Decode(b1, 0, b1.GetLength(0), CODING.UCS2);
Console.WriteLine("收到一條手機消息,消息內容:" + sDest);
}
Console.WriteLine("收到一條手機消息,消息內容:" + mo_pkg.sMsgContent);
Console.WriteLine("\t發送號碼:" + mo_pkg.sUserNumber + "\t目標號碼:" + mo_pkg.sSPNumber + "\tLinkID:" + mo_pkg.sLinkId);
return;
}
//---------------------處理狀態報告的函數-------------------------------------
public static void OnReport(IntPtr ptr)
{
ReportStr report_pkg = new ReportStr();
report_pkg = (ReportStr)Marshal.PtrToStructure(ptr, typeof(ReportStr));
Console.WriteLine("收到一條狀態報告,結果:" + report_pkg.cState);
if ( report_pkg.cState != 0) {
Console.WriteLine("\t錯誤代碼:" + report_pkg.cErrcode);
}
return;
}
//---------------------處理發送失敗的函數-------------------------------------
public static void OnMTError(IntPtr ptr)
{
MTErrorStr mt_err = new MTErrorStr();
mt_err = (MTErrorStr)Marshal.PtrToStructure(ptr, typeof(MTErrorStr));
Console.WriteLine("發送失敗,消息內容:" + mt_err.ss.sMsgContent + "\t錯誤代碼:" + mt_err.nErrorType);
return;
}
//--------------------------------------------------------------------
//-------------------------------------------------------------------------------
//---------編解碼函數------------------------------------------------------------
public enum CODING
{
ASCII=0,BINARY=4,UCS2=8,GBK=15
}
public static String Decode(Byte[] buf,int StartIndex,int Length,CODING Coding)
{
String str=String.Empty;
if(Coding==CODING.ASCII)
{
System.Text.ASCIIEncoding AsciiEncoder=new System.Text.ASCIIEncoding();
str=new String(AsciiEncoder.GetChars(buf,StartIndex,Length));
str=str.Split('\0')[0];
}
else if(Coding==CODING.UCS2)
{
Byte[] temp=new Byte[Length];
for(int i=0; i<Length;)
{
// 高低位字節對調
temp[i] =buf[StartIndex+i+1];
temp[i+1]= buf[StartIndex+i];
i=i+2;
}
System.Text.UnicodeEncoding UnicodeEncoder=new System.Text.UnicodeEncoding();
str=new String(UnicodeEncoder.GetChars(temp,0,Length));
str=str.Split('\0')[0];
}
else if(Coding==CODING.GBK)
{
System.Text.UnicodeEncoding Encoder=new System.Text.UnicodeEncoding();
System.Text.Encoding en=System.Text.UnicodeEncoding.GetEncoding("gb2312");
str=new String(en.GetChars(buf,StartIndex,Length));
str=str.Split('\0')[0];
}
return str;
}
public static Byte[] Encode(String str,CODING coding)
{
Byte[] buf=null;
if(coding==CODING.ASCII)
{
System.Text.ASCIIEncoding AsciiEncoder=new System.Text.ASCIIEncoding();
buf=AsciiEncoder.GetBytes(str);
}
else if(coding==CODING.UCS2)
{
System.Text.UnicodeEncoding UnicodeEncoder=new System.Text.UnicodeEncoding();
buf=UnicodeEncoder.GetBytes(str);
Byte temp=0;
for(int i=0; i<buf.Length;)
{
// 高低位字節對調
temp=buf[i] ;
buf[i]= buf[i+1];
buf[i+1]=temp;
i=i+2;
}
}
else if(coding==CODING.GBK)
{
System.Text.Encoding en=System.Text.UnicodeEncoding.GetEncoding("gb2312");
buf=en.GetBytes(str);
}
return buf;
}
//-------------------------------------------------------------------------------
//---------編解碼函數------------------------------------------------------------
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
/*
public delegate void OnResponse(IntPtr stMtResp);
public delegate void OnDeliver(IntPtr stMoMsg);
public delegate void OnReport(IntPtr stReport);
public delegate void OnMTError(IntPtr stMtErr);
*/
Class1 c1 = new Class1();
dtOnResponse pfResponse = new dtOnResponse(OnResponse);
dtOnDeliver pfDeliver = new dtOnDeliver(OnDeliver);
dtOnReport pfReport = new dtOnReport(OnReport);
dtOnMTError pfMTError = new dtOnMTError(OnMTError);
//-------------啟動模塊,連接網關,收到消息以后會觸發OnSmgMsg函數------------------------------
int nRetCode = Sgip12Start("127.0.0.1", 8802, "127.0.0.1", 8801, "username", "password",
"3010090123", pfDeliver, pfReport, pfResponse, pfMTError, 1, true);
if ( 0 == nRetCode)
{
Console.WriteLine("Connect Smg success\n");
sgipg_submit ss = new sgipg_submit();
ss.sSpNumber = "8888";
ss.sChargeNumber = "000000000000000000000";
ss.cUserCount = 1;
ss.sUserNumber = "8613012345678";
ss.sCorpId = "90123";
ss.sServiceType = "DB";
ss.cFeeType = 1;
ss.sFeeValue = "00001";
ss.sGivenValue = "";
ss.cAgentFlag = 0;
ss.cMorelatetoMTFlag = 2;
ss.cPriority = 0;
ss.sExpireTime = "";
ss.sScheduleTime = "";
ss.cTpPid = 0;
ss.cUdhi = 0;
ss.cMessageCoding = 15;
ss.cMessageType = 0;
ss.nMsgLen = (uint)System.Net.IPAddress.HostToNetworkOrder(4);
ss.sMsgContent = "測試";
ss.sLinkId = "";
int nCount = 0;
while ( nCount++ < 3)
{
//----------向網關提交Submit消息----------
uint nSeq=0, nDateTime = 0;
Sgip12Submit(ss, ref nSeq, ref nDateTime);
// Sleep3秒鐘
// Thread.Sleep(3000);
}
Console.ReadLine();
//-----釋放連接-----
Sgip12Release();
}
else {
Console.WriteLine("Connect SMG Fail With Error: " + nRetCode);
Thread.Sleep(3000);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -