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

📄 smsdlg.cpp

📁 发送短信的例程.利用东软的发送短信的API函数完成的功能.
💻 CPP
字号:
// smsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "sms.h"
#include "smsDlg.h"
#include "SMSConnect.h"
#include "SMSSetParam.h"

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

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

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()

/////////////////////////////////////////////////////////////////////////////
// CSmsDlg dialog

CSmsDlg::CSmsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSmsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSmsDlg)
	m_mobile = _T("");
	m_sendContent = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CSmsDlg::~CSmsDlg(void)
{
	delete mySmsParam;
}

void CSmsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSmsDlg)
	DDX_Control(pDX, IDC_LIST_RECV, m_recvlist);
	DDX_Text(pDX, IDC_SENDMOBILE, m_mobile);
	DDX_Text(pDX, IDC_SENDCONTENT, m_sendContent);
	DDV_MaxChars(pDX, m_sendContent, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSmsDlg, CDialog)
//{{AFX_MSG_MAP(CSmsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_SHUTDOWN, OnShutdown)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SETTING, OnSetting)
ON_BN_CLICKED(IDC_CONNECTSET, OnConnectSet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSmsDlg message handlers

BOOL CSmsDlg::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
	
	// TODO: Add extra initialization here
	mySmsParam=new SMSSetParam;
	mySmsParam->m_PkTotal="1";
	mySmsParam->m_PkNumber = "1";
	mySmsParam->m_FeeCode = "1";
	mySmsParam->m_FeeType = "1";
	mySmsParam->m_MsgFmt = "0";
	
	conn.m_gwip="127.0.0.1";
	conn.m_gwport="7890";
	conn.m_user="test";
	conn.m_pwd="pwd";
	
	restart=0;
	SetTimer(1,500,NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSmsDlg::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 CSmsDlg::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 CSmsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSmsDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	restart=1;
}

void CSmsDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	
	
	if (sock<=0)
	{
		MessageBox("Please Connect First!");
		return ;
	}
	UpdateData();
	CMPP_SUBMIT mycmpp_submit={0};
	CMPP_SUBMIT_RSP mycmpp_submit_rsp={0};
	memcpy(&mycmpp_submit.Msg_Content,
		m_sendContent.GetBuffer(0),
		(m_sendContent.GetLength()>MSG_MAXLEN) ? MSG_MAXLEN : m_sendContent.GetLength()
		);
	mycmpp_submit.Msg_Content[MSG_MAXLEN]=0;
	memcpy(&mycmpp_submit.Src_Id,
		mySmsParam->m_SrcId.GetBuffer(0),
		(mySmsParam->m_SrcId.GetLength()>sizeof(mycmpp_submit.Src_Id))?sizeof(mycmpp_submit.Src_Id):mySmsParam->m_SrcId.GetLength()
		);
	
	int ret=cmpp_submit
		(
		sock,
		&mycmpp_submit,
		&mycmpp_submit_rsp
		); 
	
	if (ret!=ERROR_OK)
	{
		if (ret==ERROR_SOCKET_WRITE) 
			MessageBox("socket写失败");
		if (ret==ERROR_SOCKET_READ)	
			MessageBox("socket读失败");
		if (ret==ERROR_RSP_TIMEOUT)  
			MessageBox("网关超时应答(API等待5秒)");
	}
	else
	{
		CString tstr="";
		tstr+=m_mobile;
		tstr+=" ";
		tstr+=m_sendContent+"  发送成功!";
		m_recvlist.AddString(tstr);
	}
	
}



void CSmsDlg::OnShutdown() 
{
	// TODO: Add your control notification handler code here
	if (sock>0)
		cmpp_connect_terminate(sock);
	sock=0;
}

void CSmsDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	if (sock>0)
		cmpp_connect_terminate(sock);
	
	CDialog::OnCancel();
}

void CSmsDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (restart==2)
	{
		static int countrestart=1;
		countrestart++;
		if (countrestart>10)
		{
			countrestart=1;
			ReStart();
		}
	}
	else if(restart==1)
	{
		ReStart();
	}
	
	static bool bcangetdeliver=true;
	if (sock>0 && bcangetdeliver)
	{
		bcangetdeliver=false;
		CMPP_DELIVER mycmpp_deliver={0};
		CMPP_DELIVER *pmycmpp_deliver;
		pmycmpp_deliver=new CMPP_DELIVER;
		memset(pmycmpp_deliver,0,sizeof(CMPP_DELIVER));
		/*返回值:
		ERROR_SOCKET_WRITE    socket写失败
		ERROR_SOCKET_READ	socket读失败
		0       得到MO消息
		1       得到状态报告
		2		无消息返回
		3       返回非CMPP_DELIVER消息
		param1:  socket号,又cmpp_connect函数返回.
param2:	 返回DELIVER消息包。*/
		
		char strSock[50]={0};
		int err=get_cmpp_deliver(sock,pmycmpp_deliver);
		if (err==ERROR_SOCKET_WRITE)
		{
			sprintf(strSock,"%dsocket写失败,正在重新联接网络",sock);
			restart=1;
		}
		else if(err==ERROR_SOCKET_READ)
		{
			sprintf(strSock,"%dsocket读失败,正在重新联接网络",sock);
			restart=1;
		}
		else if(err==0)
			sprintf(strSock,"%d得到MO消息",sock);
		else if(err==1)
			sprintf(strSock,"%d得到状态报告",sock);
		else if(err==2)
			sprintf(strSock,"%d无消息返回",sock);
		else if(err==3)
			sprintf(strSock,"%d返回非CMPP_DELIVER消息",sock);
		else
			sprintf(strSock,"%d不可能出现的值!",sock);
		m_recvlist.AddString(strSock);
		//		if(pmycmpp_deliver->Msg_Id>0)
		//			MessageBox("a");
		delete pmycmpp_deliver;
		bcangetdeliver=true;
	}
	CDialog::OnTimer(nIDEvent);
}

void CSmsDlg::OnSetting() 
{
	// TODO: Add your control notification handler code here
	mySmsParam->DoModal();	
}

void CSmsDlg::ReStart()
{
	static int rscount=0;
	rscount++;
	if (rscount>5)
	{
		restart=0;
		rscount=0;
		MessageBox("I am Try 5 times to Connect \n    Can not Connect to the GateWay!");
		return;
	}
	OnShutdown();
	CMPP_CONNECT_RSP pcmpp_connect_rsp={0};
	
	sock=cmpp_connect
		(
		conn.m_gwip.GetBuffer(0),
		conn.m_gwport.GetBuffer(0),
		conn.m_user.GetBuffer(0),
		conn.m_pwd.GetBuffer(0),
		&pcmpp_connect_rsp
		);
	if (sock<0)
	{
		if (sock==ERROR_SOCKET_CREATE) 
		{
			restart=1;
			MessageBox("socket创建失败");
		}
		else if (sock==ERROR_CONNECT)
		{
			restart=1;
			MessageBox("socket连接失败");
		}
		else if (sock==ERROR_SOCKET_WRITE)  
		{
			restart=1;
			MessageBox("socket写失败");
		}
		else if (sock==ERROR_SOCKET_READ)
		{
			restart=1;
			MessageBox("socket读失败");
		}
		else if (sock==ERROR_RSP_TIMEOUT) 
		{
			restart=2;
			MessageBox("网关超时应答(API等待5秒)");
		}
		else
		{
			char tstr[10]={0};
			sprintf(tstr,"%d",sock);
			MessageBox(tstr);
		}
	}
	else
	{
		restart=0;
		m_recvlist.AddString("联接网络成功!");
	}
}

void CSmsDlg::OnConnectSet() 
{
	// TODO: Add your control notification handler code here
	conn.DoModal();
}

⌨️ 快捷键说明

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