📄 smssdk_demodlg.cpp
字号:
// SmsSDK_DemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SmsSDK_Demo.h"
#include "SmsSDK_DemoDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSmsSDK_DemoDlg dialog
CSmsSDK_DemoDlg::CSmsSDK_DemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSmsSDK_DemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSmsSDK_DemoDlg)
m_lCorpID = 100000;
m_strLoginName = _T("Admin");
m_strPasswd = _T("");
m_strServer = _T("www.mobset.com");
m_strMsg = _T("");
m_strMobile = _T("");
m_strSmsID = _T("");
m_strProxyHost = _T("192.168.0.1");
m_strProxyPasswd = _T("");
m_lProxyPort = 1080;
m_lProxyType = 0;
m_strProxyUser = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bConnect = false;
}
void CSmsSDK_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSmsSDK_DemoDlg)
DDX_Control(pDX, IDC_CONNECT, m_B_Connect);
DDX_Text(pDX, IDC_CORPID, m_lCorpID);
DDV_MinMaxLong(pDX, m_lCorpID, 100000, 900000);
DDX_Text(pDX, IDC_LOGINNAME, m_strLoginName);
DDV_MaxChars(pDX, m_strLoginName, 16);
DDX_Text(pDX, IDC_PASSWD, m_strPasswd);
DDV_MaxChars(pDX, m_strPasswd, 16);
DDX_Text(pDX, IDC_SERVER, m_strServer);
DDV_MaxChars(pDX, m_strServer, 32);
DDX_Text(pDX, IDC_MSG, m_strMsg);
DDV_MaxChars(pDX, m_strMsg, 500);
DDX_Text(pDX, IDC_MOBILE, m_strMobile);
DDV_MaxChars(pDX, m_strMobile, 16);
DDX_Text(pDX, IDC_SMSID, m_strSmsID);
DDX_Text(pDX, IDC_PROXY_HOST, m_strProxyHost);
DDV_MaxChars(pDX, m_strProxyHost, 32);
DDX_Text(pDX, IDC_PROXY_PASSWD, m_strProxyPasswd);
DDV_MaxChars(pDX, m_strProxyPasswd, 32);
DDX_Text(pDX, IDC_PROXY_PORT, m_lProxyPort);
DDV_MinMaxUInt(pDX, m_lProxyPort, 0, 65535);
DDX_CBIndex(pDX, IDC_PROXYTYPE, m_lProxyType);
DDX_Text(pDX, IDC_PROXY_USER, m_strProxyUser);
DDV_MaxChars(pDX, m_strProxyUser, 32);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSmsSDK_DemoDlg, CDialog)
//{{AFX_MSG_MAP(CSmsSDK_DemoDlg)
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_RECV, OnRecv)
ON_BN_CLICKED(IDC_STATUS, OnStatus)
ON_BN_CLICKED(IDC_KYSMS, OnKysms)
ON_BN_CLICKED(IDC_SEND2, OnSend2)
//}}AFX_MSG_MAP
ON_MESSAGE( SMS_RECV , On_Sms_Recv )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSmsSDK_DemoDlg message handlers
BOOL CSmsSDK_DemoDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CSmsSDK_DemoDlg::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 CSmsSDK_DemoDlg::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 CSmsSDK_DemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSmsSDK_DemoDlg::OnConnect()
{
if ( !m_bConnect ) //未连接,开始连接
{
if (!UpdateData(true))
return ;
Sms_Proxy(m_lProxyType,m_strProxyHost,m_lProxyPort,m_strProxyUser,m_strProxyPasswd); //设置代理
long lRet = Sms_Connect(m_strServer,m_lCorpID,m_strLoginName,m_strPasswd,15,this->GetSafeHwnd());
CString strMsg;
switch ( lRet )
{
case 0:
strMsg = "连接短信服务器成功!" ;
m_bConnect = true;
m_B_Connect.SetWindowText("断 开" );
break;
case -1:
strMsg = "连接短信服务器失败!" ;
break;
case -2:
strMsg = "连接服务器超时,请检查网络情况!" ;
break;
case -3:
strMsg = "登录失败,帐号有误,请检查!" ;
break;
case -4:
strMsg = "登录失败,相同帐号已在别处登录!" ;
break;
default:
strMsg = "连接短信服务器失败,原因未明!";
break;
}
MessageBox( strMsg , "信息" , MB_ICONINFORMATION );
}
else
{
//原已连接,现在断开
Sms_DisConnect();
m_bConnect = false;
m_B_Connect.SetWindowText("连 接" );
}
}
void CSmsSDK_DemoDlg::OnSend()
{
if ( !m_bConnect )
{
MessageBox( "发送短信之前请先连接到短信服务器!", "错误" , MB_ICONWARNING );
return ;
}
if ( !UpdateData(true ) )
return ;
if ( m_strMobile.GetLength() <= 0 )
{
MessageBox( "发送的目标不能为空,请输入!" , "错误" , MB_ICONWARNING );
return ;
}
if ( m_strMsg.GetLength() <= 0 )
{
MessageBox( "发送的内容不能为空,请输入!" , "错误" , MB_ICONWARNING );
return ;
}
CString strMsg;
long lSmsID;
//long lRet = Sms_Send( m_strMobile , m_strMsg,&lSmsID ); //正常发送
//long lRet = Sms_Send2( "001" , m_strMobile , m_strMsg,&lSmsID ); //带扩展号码发送
long lRet = Sms_Send3( "001" , m_strMobile , m_strMsg,"2005-08-19 14:50:05",&lSmsID ); //带扩展号码发送,带定时短信发送
switch( lRet )
{
case -1:
strMsg="接口未初始化,发送失败!";
break;
case -2:
strMsg="发送超时!";
break;
case -3:
strMsg="发送失败,可能是帐号过期或余额不足。";
break;
case -4:
strMsg="发送失败,发送的信息内容中含有敏感关键字,禁止发送。";
break;
case -5:
strMsg="发送失败,发送的目标号码为黑名单,禁止发送。";
break;
default:
strMsg.Format( "发送短信成功,发送数量为:%d,短信ID:%d!" , lRet,lSmsID );
m_strSmsID.Format( "%d" , lSmsID ); //返回SMSID
UpdateData(false);
}
MessageBox( strMsg , "信息" , MB_ICONINFORMATION );
}
void CSmsSDK_DemoDlg::OnOK()
{
return;
CDialog::OnOK();
}
void CSmsSDK_DemoDlg::OnRecv()
{
char szRecv[64]={0};
char szMobile[64]={0};
char szMsg[512]={0};
char szTime[32]={0};
//long lRet = Sms_Get(szMobile,szMsg,szTime);
long lRet = Sms_Get2(szMobile,szRecv,szMsg,szTime); //取回发送与接收号码。
if ( lRet >= 0 )
{
CString strMsg;
strMsg.Format( "取短信成功,还剩%d条短信未取:\r\n发送者:%s\r\n接收时间:%s\r\n短信内容:%s" , lRet,szMobile , szTime , szMsg );
MessageBox( strMsg , "信息" , MB_ICONINFORMATION );
}
else
{
MessageBox( "取短信失败,短信队列为空!","错误" , MB_ICONWARNING );
}
}
void CSmsSDK_DemoDlg::OnStatus()
{
UpdateData(true);
long lSmsID = atol(m_strSmsID );
if ( lSmsID <= 0 )
{
MessageBox( "请先在左边输入短信ID才能查询到其状态!" , "错误" , MB_ICONWARNING );
return ;
}
long lStatus = Sms_Status(lSmsID);
CString strMsg;
switch( lStatus )
{
case -1:
strMsg.Format( "查询状态成功,返回编号是:%d,接口未初始化!" , lStatus );
break;
case -2:
strMsg.Format( "查询状态成功,返回编号是:%d,查询超时!" , lStatus );
break;
case -3:
strMsg.Format( "查询状态成功,返回编号是:%d,没有此短信!" , lStatus );
break;
default:
strMsg.Format( "查询状态成功,返回编号是:%d,具体信息请查看常量的定义!" , lStatus );
break;
}
MessageBox( strMsg , "信息" , MB_ICONINFORMATION );
}
afx_msg LONG CSmsSDK_DemoDlg::On_Sms_Recv( WPARAM wParam , LPARAM lParam )
{
//此为接收到短信的通知信息
OnRecv(); //接收信息
return 1;
}
void CSmsSDK_DemoDlg::OnKysms()
{
long lKYSms = Sms_KYSms();
CString strMsg;
switch( lKYSms )
{
case -1:
strMsg.Format( "操作失败,返回编号是:%d,接口未初始化!" , lKYSms );
break;
case -2:
strMsg.Format( "操作失败,返回编号是:%d,查询超时!" , lKYSms );
break;
default:
strMsg.Format( "查询成功,剩余可用短信量为:%d!" , lKYSms );
break;
}
MessageBox( strMsg , "信息" , MB_ICONINFORMATION );
}
void CSmsSDK_DemoDlg::OnSend2()
{
if ( !m_bConnect )
{
MessageBox( "发送短信之前请先连接到短信服务器!", "错误" , MB_ICONWARNING );
return ;
}
if ( !UpdateData(true ) )
return ;
if ( m_strMobile.GetLength() <= 0 )
{
MessageBox( "发送的目标不能为空,请输入!" , "错误" , MB_ICONWARNING );
return ;
}
if ( m_strMsg.GetLength() <= 0 )
{
MessageBox( "发送的内容不能为空,请输入!" , "错误" , MB_ICONWARNING );
return ;
}
CString strMsg;
//正常发送
SmsSend sms[2]={0}; //发送两条信息
strcpy(sms[0].szMobile , m_strMobile );
strcpy(sms[0].szMsg , m_strMsg );
sms[1] = sms[0]; //发送两条相同的信息
long lRet = Sms_SendEx( sms,2,true);
/*
//带扩展号码发送
SmsSend2 sms[2]={0}; //发送两条信息
strcpy(sms[0].szSendExNum, "001" );
strcpy(sms[0].szMobile , m_strMobile );
strcpy(sms[0].szMsg , m_strMsg );
sms[1] = sms[0]; //发送两条相同的信息
long lRet = Sms_SendEx2( sms,2,true);
*/
/*
//带扩展号码发送,带定时短信发送
SmsSend3 sms[2]={0}; //发送两条信息
strcpy(sms[0].szSendExNum, "001" );
strcpy(sms[0].szMobile , m_strMobile );
strcpy(sms[0].szMsg , m_strMsg );
strcpy(sms[0].szTimer , "2005-08-19 14:50:05" );
sms[1] = sms[0]; //发送两条相同的信息
long lRet = Sms_SendEx3( sms,2,true);
*/
switch( lRet )
{
case -1:
strMsg="接口未初始化,发送失败!";
break;
case -2:
strMsg="发送超时!";
break;
case -3:
strMsg="发送失败,可能是帐号过期或余额不足。";
break;
case -4:
strMsg="发送失败,发送的信息内容中含有敏感关键字,禁止发送。";
break;
default:
strMsg.Format( "发送短信成功,发送数量为:%d,短信ID:%d!" , lRet,sms[0].lSmsID );
m_strSmsID.Format( "%d" , sms[0].lSmsID ); //返回SMSID
UpdateData(false);
}
MessageBox( strMsg , "信息" , MB_ICONINFORMATION );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -