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

📄 sendsmsdlg.cpp

📁 手机编程之接收发送短信
💻 CPP
字号:
// SendSMSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SendSMS.h"
#include "SendSMSDlg.h"
#include "DBAccess.h"
#include "DBAccess2.h"
//#include "SmsTraffic.h"

#include "Sms.h"
#include "Comm.h"

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

CString strSMCenter;
HANDLE m_hMutex;
int CheckReadCom()
{
	SM_BUFF buff;
	SM_PARAM SmParam[256];	// 发送/接收短消息缓冲区

	CString strTime,strNumber,strContent;
	int ret,len,i;
	char szCallerID[256];
	char szContent[256];
	char szTime[256];
	
	len = ReadComm(buff.data,256);
	buff.len = len;
	if ( len > 16 )	//temp
	{
		ret = gsmParseMessage(SmParam, &buff);
		if (ret > 0)
		{
			for (i =0;i<ret;i++)
			{
				strNumber = SmParam[i].TPA;
				strContent = SmParam[i].TP_UD;
				strTime = "20" + CString(&SmParam[i].TP_SCTS[0],2) 
					+ "-" + CString(&SmParam[i].TP_SCTS[2],2) 
					+ "-" + CString(&SmParam[i].TP_SCTS[4],2)
					+ " " + CString(&SmParam[i].TP_SCTS[6],2) 
					+ ":" + CString(&SmParam[i].TP_SCTS[8],2) 
					+ ":" + CString(&SmParam[i].TP_SCTS[10],2);

				// 去掉号码前的"86"
				if(strNumber.Left(2) == "86")  strNumber = strNumber.Mid(2);
				sprintf(szCallerID,"%s",strNumber);
				sprintf(szContent,"%s",strContent);
				sprintf(szTime,"%s",strTime);
				DBAccess.InsertRcvdMessge(szCallerID,szContent,szTime);

				gsmDeleteMessage(SmParam[i].index);
				Sleep(100);
				switch (gsmGetResponse(&buff))
				{
					case GSM_OK:
						break;
					case GSM_ERR:
						break;
					default:
						break;
				}
			}
		}
	}

	return 0;
}

int ReadCom()
{
	SM_BUFF buff;
	SM_PARAM SmParam[256];	// 发送/接收短消息缓冲区

	CString strTime,strNumber,strContent;
	int ret,i;
	char szCallerID[256];
	char szContent[256];
	char szTime[256];

	ret = gsmReadMessageList();
	memset(&buff, 0, sizeof(buff));
	Sleep(1000);
	switch (gsmGetResponse(&buff))
	{
		case GSM_OK: 
			ret = gsmParseMessageList(SmParam, &buff);
			if (ret > 0)
			{
				for (i =0;i<ret;i++){
					strNumber = SmParam[i].TPA;
					strContent = SmParam[i].TP_UD;
					strTime = "20" + CString(&SmParam[i].TP_SCTS[0],2) 
						+ "-" + CString(&SmParam[i].TP_SCTS[2],2) 
						+ "-" + CString(&SmParam[i].TP_SCTS[4],2)
						+ " " + CString(&SmParam[i].TP_SCTS[6],2) 
						+ ":" + CString(&SmParam[i].TP_SCTS[8],2) 
						+ ":" + CString(&SmParam[i].TP_SCTS[10],2);

					// 去掉号码前的"86"
					if(strNumber.Left(2) == "86")  strNumber = strNumber.Mid(2);
					sprintf(szCallerID,"%s",strNumber);
					sprintf(szContent,"%s",strContent);
					sprintf(szTime,"%s",strTime);
					DBAccess.InsertRcvdMessge(szCallerID,szContent,szTime);

					gsmDeleteMessage(SmParam[i].index);
					Sleep(100);
					switch (gsmGetResponse(&buff))
					{
						case GSM_OK:
							break;
						case GSM_ERR:
							break;
						default:
							break;
					}
				}
			}
			break;
		case GSM_ERR:
			break;
		default:
			break;
	}
	return 0;
}
DWORD WINAPI ProcessThread( LPVOID lpParameter )
{
	CSendSMSDlg *pDlg = (CSendSMSDlg*)lpParameter;

    int bStopThread = 1;int ret;
	char szCallerID[256];
	char szCalledID[256];char szSql[256];
	char id[256];
	SM_BUFF buff;
	SM_PARAM SmParam;
	CString strCaller,strContent;
	CString m_strSMCEnter(strSMCenter);
    while(bStopThread){
		memset(szCallerID,0,256);
		memset(szCalledID,0,256);

		WaitForSingleObject(m_hMutex, 1000000);
        return FALSE;
		ret = DBAccess.DealNoteSms(szCallerID,szCalledID,id);
		ReleaseMutex(m_hMutex);
		if (ret<0)
		{
			CheckReadCom();
			Sleep(1000);
//			(pDlg->m_ListLog).AddString("abc");
			continue;
		}
		strCaller.Format("%s",szCallerID);
		strContent.Format("%s",szCalledID);

		memset(&SmParam, 0, sizeof(SM_PARAM));

		// 在号码前加"86"
		if(strCaller.Left(2) != "86")  strCaller = "86" + strCaller;

		// 填充短消息结构
		strcpy(SmParam.SCA, m_strSMCEnter);
		strcpy(SmParam.TPA, strCaller);
		strcpy(SmParam.TP_UD, szCalledID);
		SmParam.TP_PID = 0;
		SmParam.TP_DCS = GSM_UCS2;

		
		gsmSendMessage(&SmParam);
		memset(&buff, 0, sizeof(buff));
		int cnt = 0;
		do
		{	
			Sleep(200);
			switch (gsmGetResponse(&buff))
			{
				case GSM_WAIT:
					break;
				case GSM_OK: 
					sprintf(szSql,"update OutBox set bSendOut = 2 where id = %s",id);//发送正确
					cnt = 20;
					break;
				case GSM_ERR:
					sprintf(szSql,"update OutBox set bSendOut = 3 where id = %s",id);//发送失败
					cnt = 20;
					break;
			}		
		}while( cnt++ < 10 );
		if ( cnt >= 10 && cnt < 20 )
		{	
			//发送超时
			sprintf(szSql,"update OutBox set bSendOut = 4 where id = %s",id);//发送失败
		}

		Sleep(100);//延迟0.1秒后再次查询

		
		WaitForSingleObject(m_hMutex, 1000000);
		ret = DBAccess.UpdateState(szSql);
		ReleaseMutex(m_hMutex);
		Sleep(1000);
    }
    
    return 0;
}



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

/////////////////////////////////////////////////////////////////////////////
// CSendSMSDlg dialog

CSendSMSDlg::CSendSMSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSendSMSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSendSMSDlg)
		// 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);
	hProcessThread = NULL;
	hReceiveThread = NULL;
}

void CSendSMSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendSMSDlg)
	DDX_Control(pDX, IDC_LIST_LOG, m_ListLog);
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSendSMSDlg, CDialog)
	//{{AFX_MSG_MAP(CSendSMSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendSMSDlg message handlers

BOOL CSendSMSDlg::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
	::CloseComm();
	

	char szIniFile[256];
    memset(szIniFile,0,256);
    ::GetModuleFileName (NULL,szIniFile,256);//得到运行程序的全路径和程序名
    char * pPos=strrchr(szIniFile,'\\');
    pPos[1]='\0';
    strcat(szIniFile,"SendSms.ini");

	 //从配置文件中读取数据库配置信息
    char szSvcVCDSN[256];
	char szCom[256];
	char szPort[256];
	char szSMC[256];
	char szCOMName[256],szPORTName[256],szSMCName[256];
	char szTmp[256];	int iTmp;
    int rc = GetPrivateProfileString("SvcConfig","BatchSendDatabase", "", szSvcVCDSN, 256, szIniFile);
    if(rc < 1){
        PostMessage(WM_QUIT);;
        return -2;
    }
	//连接联通数据库

	rc =DBAccess.InitDB(szSvcVCDSN);
	if(rc != 0){
		AfxMessageBox("数据库连接失败!");
        PostMessage(WM_QUIT);
		return -3;
    }
	m_ListLog.AddString("联通数据库连接成功!");

	m_hMutex = CreateMutex(NULL,FALSE,NULL);
    if ( ! m_hMutex )
    {
        return FALSE;
    }
    
	rc = GetPrivateProfileString("SvcConfig","NUM_COM", "", szTmp, 256, szIniFile);
    if(rc < 1){
        PostMessage(WM_QUIT);
        return -2;
    }
	iTmp=atoi(szTmp);

	
	for (int i=1;i<=iTmp;i++)
	{
		sprintf(szCOMName,"COM_%d",i);
		sprintf(szPORTName,"PORT_%d",i);
		sprintf(szSMCName,"SMC_%d",i);

		rc = GetPrivateProfileString("SvcConfig",szCOMName, "", szCom, 256, szIniFile);
		if(rc < 1){
			continue;
		}
		rc = GetPrivateProfileString("SvcConfig",szPORTName, "", szPort, 256, szIniFile);
		if(rc < 1){
			continue;
		}
		rc = GetPrivateProfileString("SvcConfig",szSMCName, "", szSMC, 256, szIniFile);
		strSMCenter = szSMC;

		if (!::OpenComm(szCom, atoi(szPort)))
		{
			CString strError;
			strError.Format("无法打开端口%s port %s! 现在设置吗?",szCom,szPort);
			AfxMessageBox(strError);
			continue;
		}
		//打开配置文件
		if (!gsmInit())
		{
			AfxMessageBox("该端口上没有发现MODEM!");
			continue;
		}
		//创建读取线程
		hProcessThread=CreateThread(NULL,0,ProcessThread, (LPVOID)this->m_hWnd,0,NULL);
		if(hProcessThread){
			CloseHandle(hProcessThread);
		}
		else{
			PostMessage(WM_QUIT);
			continue;
		}
		CString strTmp;
		strTmp.Format("处理线程%d创建成功!",i);
		m_ListLog.AddString(strTmp);
		Sleep(1000);
	}

	if (i==64){
        PostMessage(WM_QUIT);;
        return -2;
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BOOL CSendSMSDlg::DestroyWindow() 
{
    if ( m_hMutex )
    {
        CloseHandle(m_hMutex);
    }	
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

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