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

📄 mobmsgdlg.cpp

📁 一个完整的短讯网关示例,支持短讯收发,支持UDP及Text格式.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// MobMsgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MobMsg.h"
#include "MobMsgDlg.h"

#include <math.h>
#include "shlwapi.h." 
#include <io.h>
#include <string.h>
#include <fcntl.h>
#include <memory.h>
#include <Winsock2.h>
#include <windows.h> 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern 	void SendSystemInfo(CString TmpStr,int Flag);
extern 	char* ReceiveMobileModelPrevie();

HANDLE Mobile,tHread;
SOCKET sockClt;
CListBox *Lst;
char StrPort[20],StrBaudrates[20],StrStopBit[20],StrParity[20],StrByteSize[20],StrSmsCenterCode[20];
char ServerIP[20],ServerPort[20],sSendToReceiveMsg[8192];//MobChannel[20],
unsigned iServerPort;
char MobilePreive[1024],Recinstruction[1024],MsgBuf[8192];
char *str,*cptr, *ptr,buf[8192],*pSender ,msgCharLen[3],sMsgRtn[100],MobilChannel[3];
char*MobileProviver ;
char SenderBuf[1024],sendMsg[100],*pMsg,MsgInfo[8192];
int boolTmp;
DWORD num;
char StrLength[20] ;
int iTryConnCount=0;
bool result;
int iThreadFlag=0;//线程标志0:运行,1:停止线程
int iTryConnFlag =1 ;//线程标志  (手机连接线程)
int nTvodCostFlag = 0 ;//0:费用>0  1: 费用<=0
char MsgMobNo[12],RequestIcCardNo[20];//IC卡号;//发短讯手机号码
char returnmsg[1000];
char msg[8192],*pdest;;
DWORD dwThreadIdA,dwThreadIdB,dwThreadParam=0;
char TmpMsg[8192],MobNo[20];
CWnd *BtnStop;
CWnd *BtnExit;
CWnd *BtnStart;
char UserRequestMovieNo[10];//用户点播的节目号
char UserRequestMovieName[40];//用户点播的名称
CString szTemp ="";
CString TmpStr ="";
DWORD dwStatus;
HANDLE hReadMobileInfo,hTryConnectMob,event;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

char* ReceiveMobileModelPrevie()
{
	DWORD rbNum;
    OVERLAPPED lpol;
    memset(MobilePreive, 0, 1024);
    memset(&lpol, 0, sizeof(OVERLAPPED));

    if (!ReadFile(Mobile, MobilePreive, 1024, &rbNum, &lpol))
      return NULL;

    if (rbNum < 1)
      return NULL;
    else
    {
      MobilePreive[rbNum] = 0;
      return MobilePreive;
    }
}

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobMsgDlg dialog

CMobMsgDlg::CMobMsgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMobMsgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMobMsgDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMobMsgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMobMsgDlg)
	DDX_Control(pDX, IDC_LIST1, m_Lst);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMobMsgDlg, CDialog)
	//{{AFX_MSG_MAP(CMobMsgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDStart, OnStart)
	ON_BN_CLICKED(IDStop, OnStop)
	ON_BN_CLICKED(IDCExit, OnCExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMobMsgDlg message handlers

BOOL CMobMsgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0)
	{
		SendSystemInfo("程序初始化失败,不能运行!",0);
		exit(0);	
	}

	Lst = (CListBox*)GetDlgItem(IDC_LIST1);
	Mobile = NULL;
	BtnStop = (CWnd*)GetDlgItem(IDStop);
	BtnExit = (CWnd*)GetDlgItem(IDCExit);
	BtnStart = (CWnd*)GetDlgItem(IDStart);
	SendSystemInfo("服务未启动!",0);
	BtnStop->EnableWindow(false);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMobMsgDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMobMsgDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMobMsgDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
char* ConvertL2R(char *sMsg)
{
	int i;
	char cTmp;
	char sRe[1000];
	memset(sRe,0,1000);
	memcpy(sRe,sMsg,strlen(sMsg));
	i=strlen(sMsg);
	if (i == 0)
		return "";

	if (i % 2 == 1)
		memcpy(sRe+strlen(sRe),"F",1);
	int sLenght =strlen(sRe);
	for (i=0; i<sLenght; i++)
	{
		cTmp = sRe[i]; //取出当前字符
		sRe[i] = sRe[i+1];//得出下一字符
		sRe[i+1] = cTmp;
		i++;
	}
	return  sRe;
}
CString str_Gb2UniCode(CString test)
{
	WCHAR wBuf[20000];
	memset(wBuf,0,20000*sizeof(WCHAR));

	unsigned char SendBuf[20000];
	memset(SendBuf,0,20000);

	char UniStr[20000];
	char UniChar[10];
	memset(UniStr,0,20000);

	int nLengthW = MultiByteToWideChar(936, 0, (LPCTSTR)test, -1, wBuf, test.GetLength());
	if (nLengthW >0)
	{
		for (int iii = 0; iii < nLengthW-1; iii++)
		{
			SendBuf[2*iii+0] = wBuf[iii] >> 8;
			SendBuf[2*iii+1] = wBuf[iii] & 0x00ff;
			memset(UniChar,0,10);
			sprintf(UniChar,"%2.2x%2.2x",wBuf[iii] >> 8, wBuf[iii] & 0x00ff);
			strcat(UniStr,UniChar);
		}
		return UniStr;
	}
	else
	{
		int cur;
		CString str;
		char returnstr[8192];
		char ws[8192],mid[2];
		memset(ws,0,8192);
		memcpy(ws,(LPCTSTR)test,strlen(test));
		int ilen = strlen(ws);
		memset(returnstr,0,8192);
		for(int i = 0;i<ilen;i++)
		{
			memset(mid,0,2);
			memcpy(mid,ws+i,1);
			cur = mid[0];
			str.Format("%4.4X",cur);
			memcpy(returnstr+strlen(returnstr),str,strlen(str));
		}
		return returnstr;
	}
}
//-------------------------------------转成十进制
int ToConBuf(char *conBuf)
{
	int status =0;
	int iLenght =strlen(conBuf);
	for (int i = 0; i < iLenght; i++)
	{
		status *= 16;
		if (conBuf[i] >= '0' && conBuf[i] <= '9')
		status += (conBuf[i] - '0');
		else if (conBuf[i] >= 'a' && conBuf[i] <= 'f')
			status += (10 + conBuf[i] - 'a');
		else if (conBuf[i] >= 'A' && conBuf[i] <= 'F')
			status += (10 + conBuf[i] - 'A');
	}
	return status;
}
///////////////////////////////////////////////////////////
//////////////以下为转换发送短讯内容的代码
////////////////////////////////////////////////////////////
char* TextToPdu(CString sCenter, CString sPhone, CString sMsg)
{
	char cTmpcharMsg[1000],CharMessageInfo[8192];
	CString sAddr, sBody,LMsg, LBody,LSendCenterMobNoAdd;
	sAddr = "";	sBody = "";	LMsg = "";	LBody = "";
	//-------------------------------------短讯中心号码
	memset(returnmsg,0,1000);
	memcpy(returnmsg,"91",2);//将短信息中心号码前面加上字符91,91是国际化的意思
	memset(cTmpcharMsg,0,1000);
	char InputParam[100];
	memset(InputParam,0,100);
	memcpy(InputParam,(LPCTSTR)sCenter,strlen(sCenter));
	char *CenterNo;
	CenterNo = ConvertL2R(InputParam);
	memcpy(returnmsg + 2,CenterNo,strlen(CenterNo));
	sCenter = returnmsg;
	sAddr.Format("%2.2X",sCenter.GetLength()/2);
	LSendCenterMobNoAdd = sAddr + sCenter;
	//-------------------------------------短讯中心号码截取完毕

	//-------------------------------------发送目的地手机号码
	//sPhone = ConvertL2R( "86"+sPhone );  //对手机号码进行奇偶校验并交换位置
	memset(InputParam,0,100);
	memcpy(InputParam,"86",2);
	memcpy(InputParam + 2,(LPCTSTR)sPhone,strlen(sPhone));
	CenterNo = ConvertL2R(InputParam);
	sPhone = CenterNo;
	sMsg = str_Gb2UniCode( sMsg ); //对短讯内容进行转换为UniCode
	LMsg.Format("%2.2X",(sMsg.GetLength() / 2));


	//---------------------将短讯信息进行组合
	sPhone  = "11000D91" + sPhone + "000800" + LMsg + sMsg;

	memset(CharMessageInfo,0,8192);
	memcpy(CharMessageInfo,sPhone,strlen(sPhone));
	LBody.Format("%2.2X",(sPhone.GetLength() / 2));
	char cLbody[10];
	memset(cLbody,0,10);
	memcpy(cLbody,(LPCTSTR)LBody,strlen((LPCTSTR)LBody));

	memset(returnmsg,0,8192);
	memcpy(returnmsg,(LPCTSTR)LSendCenterMobNoAdd,strlen(LSendCenterMobNoAdd));
	memcpy(returnmsg+strlen(returnmsg),CharMessageInfo,strlen(CharMessageInfo));
	
	int j = ToConBuf(cLbody);
	_itoa(j,StrLength, 10);//将其转成十进制数
	return returnmsg;
}
///////////////////////////////////////////////////////////
//////////////以下为发送短讯的代码
////////////////////////////////////////////////////////////
void SendMsgInfo(char *CallCenterNo, char *MobNo, char *MsgInfo)
{
	//系统根据所收到服务器所传错误报告后自动执行短讯发送功能
	//::Sleep(3000);
	//SendSystemInfo("短信发送成功!",1);
	//return;
	char CharCallCenterNo[20],*instruction,sMobNo[20],sMsgInfo[8192],cSendShortMsgToMob[8192];
	memset(sMobNo,0,20);
	memcpy(sMobNo,MobNo,strlen(MobNo));

	memset(sMsgInfo,0,20);
	memcpy(sMsgInfo,MsgInfo,strlen(MsgInfo));

	memset(CharCallCenterNo,0,20);
	if (strlen(CallCenterNo)==0)
		memcpy(CharCallCenterNo,"8613800755500",13);
	else 
		memcpy(CharCallCenterNo,CallCenterNo,strlen(CallCenterNo));
	memset(cSendShortMsgToMob,0,8192);
	memcpy(cSendShortMsgToMob,CharCallCenterNo,strlen(CharCallCenterNo));//将短信中心号码及手机号码存入
	instruction =TextToPdu(CharCallCenterNo, sMobNo, MsgInfo);
	char Sendinstruction[20] ;
	memset(Sendinstruction,0,20);
	memcpy(Sendinstruction,"AT\r",4);///##########执行AT操作
	if (!WriteFile(Mobile, Sendinstruction, strlen(Sendinstruction), &num, NULL))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -