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

📄 dllado.cpp

📁 VC写的实现HTTP协议的完成程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	m_dwLastError = e.Error();
#ifdef _DEBUG
	AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
#endif	
}


///////////////////////////////////////////////////////
//
// CADOParameter Class
//

CADOParameter::CADOParameter(int nType, long lSize, int nDirection, CString strName)
{
	m_pParameter = NULL;
	m_pParameter.CreateInstance(__uuidof(Parameter));
	m_strName = _T("");
	m_pParameter->Direction = (ParameterDirectionEnum)nDirection;
	m_strName = strName;
	m_pParameter->Name = m_strName.AllocSysString();
	m_pParameter->Type = (DataTypeEnum)nType;
	m_pParameter->Size = lSize;
}

BOOL CADOParameter::SetValue(int nValue)
{
	_variant_t vtVal;

	ASSERT(m_pParameter != NULL);

	vtVal.vt = VT_I2;
	vtVal.iVal = nValue;

	try
	{
		if(m_pParameter->Size == 0)
			m_pParameter->Size = sizeof(int);

		m_pParameter->Value = vtVal;
		return TRUE;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
		return FALSE;
	}
}


BOOL CADOParameter::SetValue(long lValue)
{
	_variant_t vtVal;

	ASSERT(m_pParameter != NULL);

	vtVal.vt = VT_I4;
	vtVal.lVal = lValue;

	try
	{
		if(m_pParameter->Size == 0)
			m_pParameter->Size = sizeof(long);

		m_pParameter->Value = vtVal;
		return TRUE;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::SetValue(double dblValue)
{
	_variant_t vtVal;

	ASSERT(m_pParameter != NULL);

	vtVal.vt = VT_R8;
	vtVal.dblVal = dblValue;

	try
	{
		if(m_pParameter->Size == 0)
			m_pParameter->Size = sizeof(double);

		m_pParameter->Value = vtVal;
		return TRUE;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::SetValue(CString strValue)
{
	_variant_t vtVal;

	ASSERT(m_pParameter != NULL);

	if(!strValue.IsEmpty())
		vtVal.vt = VT_BSTR;
	else
		vtVal.vt = VT_NULL;

	//Corrected by Giles Forster 10/03/2001
	vtVal.bstrVal = strValue.AllocSysString();

	try
	{
		if(m_pParameter->Size == 0)
			m_pParameter->Size = sizeof(char) * strValue.GetLength();

		m_pParameter->Value = vtVal;
		return TRUE;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::SetValue(COleDateTime time)
{
	_variant_t vtVal;

	ASSERT(m_pParameter != NULL);

	vtVal.vt = VT_DATE;
	vtVal.date = time;

	try
	{
		if(m_pParameter->Size == 0)
			m_pParameter->Size = sizeof(DATE);

		m_pParameter->Value = vtVal;
		return TRUE;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::SetValue(_variant_t vtValue)
{

	ASSERT(m_pParameter != NULL);

	try
	{
		if(m_pParameter->Size == 0)
			m_pParameter->Size = sizeof(VARIANT);

		m_pParameter->Value = vtValue;
		return TRUE;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::GetValue(int& nValue)
{
	_variant_t vtVal;
	int nVal = 0;

	try
	{
		vtVal = m_pParameter->Value;

		switch(vtVal.vt)
		{
		case VT_BOOL:
			nVal = vtVal.boolVal;
			break;
		case VT_I2:
		case VT_UI1:
			nVal = vtVal.iVal;
			break;
		case VT_INT:
			nVal = vtVal.intVal;
			break;
		case VT_NULL:
		case VT_EMPTY:
			nVal = 0;
			break;
		default:
			nVal = vtVal.iVal;
		}	
		nValue = nVal;
		return TRUE;
	}
	catch(_com_error& e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::GetValue(long& lValue)
{
	_variant_t vtVal;
	long lVal = 0;

	try
	{
		vtVal = m_pParameter->Value;
		if(vtVal.vt != VT_NULL && vtVal.vt != VT_EMPTY)
			lVal = vtVal.lVal;
		lValue = lVal;
		return TRUE;
	}
	catch(_com_error& e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::GetValue(double& dbValue)
{
	_variant_t vtVal;
	double dblVal;
	try
	{
		vtVal = m_pParameter->Value;
		switch(vtVal.vt)
		{
		case VT_R4:
			dblVal = vtVal.fltVal;
			break;
		case VT_R8:
			dblVal = vtVal.dblVal;
			break;
		case VT_DECIMAL:
			//Corrected by Jos?Carlos Mart韓ez Gal醤
			dblVal = vtVal.decVal.Lo32;
			dblVal *= (vtVal.decVal.sign == 128)? -1 : 1;
			dblVal /= pow(10, vtVal.decVal.scale); 
			break;
		case VT_UI1:
			dblVal = vtVal.iVal;
			break;
		case VT_I2:
		case VT_I4:
			dblVal = vtVal.lVal;
			break;
		case VT_INT:
			dblVal = vtVal.intVal;
			break;
		case VT_NULL:
		case VT_EMPTY:
			dblVal = 0;
			break;
		default:
			dblVal = 0;
		}
		dbValue = dblVal;
		return TRUE;
	}
	catch(_com_error& e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::GetValue(CString& strValue, CString strDateFormat)
{
	_variant_t vtVal;
	CString strVal = _T("");

	try
	{
		vtVal = m_pParameter->Value;
		switch(vtVal.vt) 
		{
		case VT_R4:
			strVal = DblToStr(vtVal.fltVal);
			break;
		case VT_R8:
			strVal = DblToStr(vtVal.dblVal);
			break;
		case VT_BSTR:
			strVal = vtVal.bstrVal;
			break;
		case VT_I2:
		case VT_UI1:
			strVal = IntToStr(vtVal.iVal);
			break;
		case VT_INT:
			strVal = IntToStr(vtVal.intVal);
			break;
		case VT_I4:
			strVal = LongToStr(vtVal.lVal);
			break;
		case VT_DECIMAL:
			{
				//Corrected by Jos?Carlos Mart韓ez Gal醤
				double val = vtVal.decVal.Lo32;
				val *= (vtVal.decVal.sign == 128)? -1 : 1;
				val /= pow(10, vtVal.decVal.scale); 
				strVal = DblToStr(val);
			}
			break;
		case VT_DATE:
			{
				COleDateTime dt(vtVal);

				if(strDateFormat.IsEmpty())
					strDateFormat = _T("%Y-%m-%d %H:%M:%S");
				strVal = dt.Format(strDateFormat);
			}
			break;
		case VT_EMPTY:
		case VT_NULL:
			strVal.Empty();
			break;
		default:
			strVal.Empty();
			return FALSE;
		}
		strValue = strVal;
		return TRUE;
	}
	catch(_com_error& e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::GetValue(COleDateTime& time)
{
	_variant_t vtVal;

	try
	{
		vtVal = m_pParameter->Value;
		switch(vtVal.vt) 
		{
		case VT_DATE:
			{
				COleDateTime dt(vtVal);
				time = dt;
			}
			break;
		case VT_EMPTY:
		case VT_NULL:
			time.SetStatus(COleDateTime::null);
			break;
		default:
			return FALSE;
		}
		return TRUE;
	}
	catch(_com_error& e)
	{
		dump_com_error(e);
		return FALSE;
	}
}

BOOL CADOParameter::GetValue(_variant_t& vtValue)
{
	try
	{
		vtValue = m_pParameter->Value;
		return TRUE;
	}
	catch(_com_error& e)
	{
		dump_com_error(e);
		return FALSE;
	}
}


void CADOParameter::dump_com_error(_com_error &e)
{
	CString ErrorStr;


	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());
	ErrorStr.Format( "CADOParameter Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n",
		e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
	m_strLastError = ErrorStr;
	m_dwLastError = e.Error();
#ifdef _DEBUG
	AfxMessageBox(ErrorStr, MB_OK | MB_ICONERROR);
#endif	
}

IMPLEMENT_DYNAMIC(CADOException, CException)

CADOException::CADOException(int nCause, CString strErrorString) : CException(TRUE)
{
	m_nCause = nCause;
	m_strErrorString = strErrorString;
}

CADOException::~CADOException()
{

}

int CADOException::GetError(int nADOError)
{
	switch (nADOError)
	{
	case noError:
		return CADOException::noError;
		break;
	default:
		return CADOException::Unknown;
	}

}

void AfxThrowADOException(int nADOError, CString strErrorString)
{
	throw new CADOException(CADOException::GetError(nADOError), strErrorString);
}

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

// ADOtest.cpp : Defines the entry point for the console application.
//
//zrr

void AddLog(const char* format,...)
{
	FILE* pfLog;
	char current[36];
	char tp[36];
	struct _timeb timebuffer;
	char lpFileName[MAX_PATH];
	_strdate(current);
	_ftime(&timebuffer);
	strcpy(tp,ctime(&(timebuffer.time)));
	current[2]='_';
	current[5]=0;
	tp[strlen(tp)-1]=0;
	GetModuleFileName(NULL,lpFileName,MAX_PATH);
#if MONTH_LOG
	sprintf(strrchr(lpFileName,'\\')+1,
		"Service%s_%.2s.txt\0",strrchr(tp,' ')+1,current);
#else
	sprintf(strrchr(lpFileName,'\\')+1,
		"Out%s_%s.txt\0",strrchr(tp,' ')+1,current);
#endif
	current[2]='-';
	tp[strrchr(tp,' ')-tp]=0;
	pfLog=fopen(lpFileName,"a");
	va_list args;
	va_start(args,format);
#if _DEBUG
	vfprintf(stdout,format,args);
#endif
	fprintf(pfLog,"%s<%s.%.3u> ",current,
		strrchr(tp,' ')+1,timebuffer.millitm);
	vfprintf(pfLog,format,args);
	va_end(args);
	fflush(pfLog);
	fclose(pfLog);
}

int getarg(sarg* tt,char* filename)
{
	FILE* pf=NULL;
	char tchar[_MAX_PATH];
	char lpFileName[MAX_PATH];
	GetModuleFileName(NULL,lpFileName,MAX_PATH);
	sprintf(strrchr(lpFileName,'\\')+1,"%s","allsp.arg\0");
	pf=fopen(lpFileName,"r");
	if(pf==NULL)
	{
		AddLog("arg file %s open error\n",filename);
		return 0;
	}
	memset(tt,0,sizeof(sarg));

	fgets(tchar,_MAX_PATH,pf);
	tchar[strlen(tchar)-1]=0;
	sprintf(tt->machine_ip,"%s\0",strchr(tchar,'=')+1);

	fgets(tchar,_MAX_PATH,pf);
	tchar[strlen(tchar)-1]=0;
	sprintf(tt->catalog,"%s\0",strchr(tchar,'=')+1);

	fgets(tchar,_MAX_PATH,pf);
	tchar[strlen(tchar)-1]=0;
	sprintf(tt->user,"%s\0",strchr(tchar,'=')+1);

	fgets(tchar,_MAX_PATH,pf);
	tchar[strlen(tchar)-1]=0;
	if(strchr(tchar,'='))
		sprintf(tt->pass,"%s\0",strchr(tchar,'=')+1);
	else sprintf(tt->pass,"");

	fclose(pf);

	return 0;
}


DLLADO_API DWORD BeginGW(LPVOID)
//int main(int argc, char* argv[])
{

	char lpFileName[MAX_PATH];
	GetModuleFileName(NULL,lpFileName,MAX_PATH);
	sprintf(strrchr(lpFileName,'\\')+1,"%s","allsp.arg\0");
	getarg(&targ,lpFileName);


	CADODatabase pAdoDb;
	CString strConnection = _T("");
	//ole db
	//strConnection = _T("Provider=sqloledb;Data Source=192.168.0.222;Initial Catalog=httpsend;"
	//	"User Id=SA;Password=;");

	strConnection=_T("Provider=sqloledb;Data Source=");
	strConnection=strConnection+targ.machine_ip;
	strConnection=strConnection+";Initial Catalog="+targ.catalog;
	strConnection=strConnection+";User Id="+targ.user;
	strConnection=strConnection+";Password="+targ.pass+";";
	//odbc db
	//strConnection = _T("Driver={SQL Server};Server=CHAOMIN;Trusted_Connection=no;"
	//	"Database=5168;Uid=MyUserName;Pwd=;");
	//access db
	//strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbTest.mdb");

	AddLog("connect string=%s\n",strConnection);
	if(pAdoDb.Open((LPCTSTR)strConnection)) { 
		//if(pAdoDb.Open()) {
		AddLog("pAdoDb is open.\n");
		while (gw_flag) {
			CString strQry = _T("");
			//strQry=_T("select top 5 msg_id,msg,src_termid from msg_esme_state order by msg_id desc");
			strQry=_T("select top 10 a.spurl,a.sp_id,a.sppassword,a.ltuid,a.ltpwd,b.fid,b.src_termid,b.dst_termid,b.msg,b.rec_time,b.src_time,b.msg_format from sp_info a,from_mobile b where (a.spcode=left(b.dst_termid,6) or a.spcode=left(b.dst_termid,8)) and b.msg_state=1 and a.spurl<>'0' and resend<3 order by b.fid");
			//strQry=_T("select top 10 a.spurl,a.sp_id,a.sppassword,a.ltuid,a.ltpwd,b.fid,b.src_termid,b.dst_termid,b.msg,b.rec_time,b.src_time,b.msg_format from sp_info a,from_mobile b where (a.spcode=left(b.dst_termid,6) or a.spcode=left(b.dst_termid,8)) and  a.spurl<>'0'  order by b.fid");
			CADORecordset pRs(&pAdoDb);

			if(!pRs.Open((LPCTSTR)strQry))
				return FALSE;
			//int numRecords;
			//numRecords = pRs.GetRecordCount();
			while(!pRs.IsEof())
			{
				CString spurl = _T("");
				int sp_id;
				CString sppassword = _T("");
				CString ltuid = _T("");
				CString ltpwd = _T("");
				int fid;
				CString src_termid = _T("");
				CString dst_termid = _T("");
				CString msg = _T("");
				CString rec_time = _T("");
				CString src_time = _T("");
				int msg_format;
				char string_msg_format[10];
				char string_fid[10];

				CString strDateFormat = _T("%Y-%m-%d %H:%M:%S");
				CString strSQL = _T("");


				pRs.GetFieldValue("SPURL", spurl);
				pRs.GetFieldValue("SP_ID", sp_id);
				pRs.GetFieldValue("SPPASSWORD", sppassword);
				pRs.GetFieldValue("LTUID", ltuid);
				pRs.GetFieldValue("LTPWD", ltpwd);
				pRs.GetFieldValue("FID", fid);
				pRs.GetFieldValue("SRC_TERMID", src_termid);
				pRs.GetFieldValue("DST_TERMID", dst_termid);
				pRs.GetFieldValue("MSG", msg);
				pRs.GetFieldValue("REC_TIME", rec_time, strDateFormat);
				pRs.GetFieldValue("SRC_TIME", src_time, strDateFormat);

				pRs.GetFieldValue("MSG_FORMAT", msg_format);
				_itoa(msg_format,string_msg_format,10);
				CString strURL = _T("");

				strURL="sp_id="+ltuid+"&upassword="+ltpwd;
				strURL=strURL+"&src="+src_termid+"&dst="+dst_termid;
				strURL=strURL+"&rec_time="+rec_time+"&src_time="+src_time;
				strURL=strURL+"&msg="+msg+"&msg_format="+string_msg_format;
				printf("strurl=%s\n",strURL);

				_itoa(msg_format,string_fid,10);
				strSQL=_T("update from_mobile set resend=resend+1 where fid=");
				strSQL=strSQL+string_fid;


				//spurl=_T("http://192.168.0.222:5168/httpsms.aspx");
				//strURL=_T("spusername=testuser&upassword=test&sp_id=1&resend=1&dst=13693524736&fee_dst=13693524736&src=516851&msg=testzrr-01&msg_format=0&svc=bzyj&pid=0&udhi=0");
				CURL *curl;
				curl = curl_easy_init();
				if(curl) {
					curl_easy_setopt(curl, CURLOPT_URL, spurl);

					curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
					curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);

					strURL.Replace(" ","%20");
					//printf("repl strurl=%s\n",strURL);

					if (!pAdoDb.Execute(strSQL))
						return FALSE;


					curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strURL);
					//curl_easy_setopt(curl, CURLOPT_HTTPGET, strURL);

					curl_easy_perform(curl);

					curl_easy_cleanup(curl);
				}//end if(curl)

				strSQL=_T("update from_mobile set msg_state=2 where fid=");
				strSQL=strSQL+string_fid;

				if (!pAdoDb.Execute(strSQL))
					return FALSE;

				pRs.MoveNext();
			}
			pRs.Close();
			Sleep(3);
		}//end while(gw_flag)
		pAdoDb.Close();
		return TRUE;
	}//end pAdoDb.open

	else return FALSE;
}

DLLADO_API void StopGW()
{
	gw_flag=0;
}

⌨️ 快捷键说明

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