sendmesthread.cpp

来自「Send SMS useing Com,it is simple」· C++ 代码 · 共 290 行

CPP
290
字号
// SendMesThread.cpp : implementation file
//

#include "stdafx.h"
#include "AdoDataGrid.h"
#include "SendMesThread.h"
#include "DlgSendMes.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern char s_AllContext[500];
extern int  i_ThreadNum;
extern int  i_Range;
extern BOOL b_SendFlag;
extern CStringArray s_Send;
extern int i_HaoMaDuan;
extern BOOL b_FengDuan;
extern CString s_Begin;
const char buf[]="0123456789";
/////////////////////////////////////////////////////////////////////////////
// CSendMesThread

IMPLEMENT_DYNCREATE(CSendMesThread, CWinThread)

CSendMesThread::CSendMesThread()
{
}

CSendMesThread::CSendMesThread(CString s_tn,CString s_Conn,HWND hMain,CStringArray* iAr)
{
	using namespace ado20;	// ADO #import
	::CoInitialize( NULL );	
	s_Th_Tn=s_tn;
	m_strConnect=s_Conn;
	t_hMain=hMain;
	pIArray = iAr;
}
CSendMesThread::~CSendMesThread()
{
	::CoUninitialize( );	
}

BOOL CSendMesThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	GetData_T(s_Th_Tn,m_strConnect,t_hMain);
	return TRUE;
}

int CSendMesThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CSendMesThread, CWinThread)
	//{{AFX_MSG_MAP(CSendMesThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendMesThread message handlers

void CSendMesThread::GetData_T(CString str,CString s_Conn,HWND hMain)
{
	CDlgSendMes d_Send;
	CString q;
	if(d_Send.DoModal()==IDOK)
	{

	}
	else
	{
		i_ThreadNum--;
		b_SendFlag=TRUE;
		b_FengDuan=FALSE;
		return;
	}
	if(b_FengDuan)
	{
		
		char nobuf[12]="";
		char Hinobuf[6]="";
		char Lonobuf[7]="";
		char tempbuf[7]="";
		int  ino=0;
		strcpy(nobuf,s_Begin);
		memcpy(Hinobuf,nobuf,5);
		memcpy(Lonobuf,nobuf+5,s_Begin.GetLength()-5);
		s_Send.RemoveAll();
		ino=atoi(Lonobuf);
		for(int i=0;i<i_HaoMaDuan;i++)
		{
			if(ino<10)
				sprintf(tempbuf,"00000");
			else if(ino>=10 && ino<100)
				sprintf(tempbuf,"0000");
			else if(ino>=100 && ino<1000)
			    sprintf(tempbuf,"000");
			else if(ino>=1000 && ino<10000)
				sprintf(tempbuf,"00");
			else if(ino>=10000 && ino<100000)
				sprintf(tempbuf,"0");
			else if(ino>=100000)
				sprintf(tempbuf,"");
			q.Format("%s%s%d",Hinobuf,tempbuf,ino);
			s_Send.Add(q);
			ino++;
		}
		q.Format("%s",s_AllContext);
		s_Send.Add(q);
		i_Range=i_HaoMaDuan;
		b_FengDuan=FALSE;
		::PostMessage(hMain,WM_READDATAOVER,0,0);
		i_ThreadNum--;
	}
	else
	{
		CString m_Query;
		m_Query="select 手机号 from "+str+" order by 手机号";
		_CommandPtr cmd;  //command object
		_RecordsetPtr rs;    //recordset object
		_ConnectionPtr conn; //connection object
		_variant_t vra;
		VARIANT *vt1 = NULL;
		try
		{
			//create instance of Command, Connection and Recordset.
			cmd.CreateInstance( __uuidof(Command));
			rs.CreateInstance(__uuidof(Recordset));
			conn.CreateInstance(__uuidof(Connection));
			// used for RXF_text data exchange
			
			conn->CursorLocation = adUseClient;
			//Get connection string from edit control and open connection
			conn->Open(_bstr_t( s_Conn.GetBuffer(0) ), L"", L"", -1);
			
			//Assign oppened connection object to Command object
			cmd->ActiveConnection = conn;
			
			//Get query text from edit control
			cmd->CommandText = (_bstr_t) m_Query.GetBuffer(0);
			cmd->CommandType = adCmdText;
			
			//execute the command and assign returning Recordset to previously 
			//instanciated recordset object
			try{
				rs = cmd->Execute(&vra,vt1, adCmdText);
			}
			catch(_com_error &e)
			{
				m_Query="select Mobile from "+str+" order by Mobile";
				cmd->CommandText = (_bstr_t) m_Query.GetBuffer(0);
				rs = cmd->Execute(&vra,vt1, adCmdText);
			}
			
		
			_variant_t t;
			int len=0;
			rs->MoveFirst();
			s_Send.RemoveAll();
			if(pIArray != NULL)
			{	
				int iLastRow = 0,index = 0;
				CString strTmp;
				i_Range = pIArray->GetSize();
				while(!rs->EOF)
				{					
					try
					{
						rs->MoveFirst();
						if(index >= pIArray->GetSize())
							break;
						iLastRow = atoi(pIArray->GetAt(index));
						rs->Move(iLastRow-1);
						try{
							t=rs->GetCollect(_variant_t("手机号"));
						}
						catch(_com_error &e)
						{
							t=rs->GetCollect(_variant_t("Mobile"));
						}
						t.ChangeType(VT_BSTR);
						q=t.bstrVal;
						if(!( strspn(q,buf)<(size_t)q.GetLength() ) && ((size_t)q.GetLength()==11) && \
							(q.Find("135",0)==0 || q.Find("136",0)==0 || \
							 q.Find("137",0)==0 || q.Find("138",0)==0 || \
							 q.Find("139",0)==0 ) )
							s_Send.Add(q);
						else
							i_Range--;	
						//rs->MoveNext();
						index ++;					
					}
					catch(_com_error &e)
					{
						GenerateError(e.Error(), e.Description());
						::PostMessage(hMain,WM_RESET1,0,0);
						break;
					}
					catch(CMemoryException e)
					{
						AfxMessageBox("内存操作错误!");
						break;
					}
				}
			}
			else
			{
				s_Send.RemoveAll();
				while(!rs->EOF)
				{	
					try
					{
						try{
							t=rs->GetCollect(_variant_t("手机号"));
						}
						catch(_com_error &e)
						{
							t=rs->GetCollect(_variant_t("Mobile"));
						}
						t.ChangeType(VT_BSTR);
						q=t.bstrVal;
						if(!( strspn(q,buf)<(size_t)q.GetLength() ) && ((size_t)q.GetLength()==11) && \
							(q.Find("135",0)==0 || q.Find("136",0)==0 || \
							 q.Find("137",0)==0 || q.Find("138",0)==0 || \
							 q.Find("139",0)==0 ) )
							s_Send.Add(q);
						else
							i_Range--;
						rs->MoveNext();
					}
					catch(_com_error &e)
					{
						GenerateError(e.Error(), e.Description());
						::PostMessage(hMain,WM_RESET1,0,0);
						break;
					}
					catch(CMemoryException e)
					{
						AfxMessageBox("内存操作错误!");
						break;
					}
				}
			}
		
		}

		//Error catching
		catch (_com_error &e)
		{
			CString temps="您所选的数据表没有 \"手机号\" 字段";
			GenerateError(e.Error(), e.Description(),temps);
			::PostMessage(hMain,WM_RESET1,0,0);
		}
		catch(CMemoryException e)
		{
			AfxMessageBox("内存操作错误!");
		}
		catch (...)
		{
    
		}
		conn->Close();
		q.Format("%s",s_AllContext);
		s_Send.Add(q);
		i_ThreadNum--;
		if(s_Send.GetSize()>1)
			::PostMessage(hMain,WM_READDATAOVER,0,0);
		else
			::PostMessage(hMain,WM_RESET1,0,1);
		
    }
}

void CSendMesThread::GenerateError(HRESULT hr, PWSTR pwszDescription,CString temps)
{
	CString strError;
	//strError.Format("Run-time error '%d (%x)'", hr, hr);
	//strError += "\n\n";
	strError += pwszDescription;
	strError += "\n\n";
	strError += temps;
	AfxMessageBox(strError);
}

⌨️ 快捷键说明

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