⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 SGIPAPI_Sample_CSharp.rar
💻 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 + -