基于 Web 的程序版本检查
2006-07-21 11:09:14 来源:WEB开发网if (CWebVersion::Online()) {
CWebVersion ver("pub.chinafsdu.net");
if (ver.ReadVersion("version.txt"),"pub","pub") {
DWORD maj = ver.dwVersionMS;
DWORD min = ver.dwVersionLS;
}
}
下面是CWebVersion的声明: ////////////////////////////////////////////////////////////////
// WebVersion.h
//
#pragma once
class CWebVersion {
protected:
enum { BUFSIZE = 64 };
LPCTSTR m_lpServer; // server name
DWORD m_dwError; // most recent error code
TCHAR m_errInfo[256]; // extended error info
TCHAR m_version[BUFSIZ]; // version number as text
void SaveErrorInfo(); // helper to save error info
public:
DWORD dwVersionMS; // version number: most-sig 32 bits
DWORD dwVersionLS; // version number: least-sig 32 bits
CWebVersion(LPCTSTR server) : m_lpServer(server) { }
~CWebVersion() { }
static BOOL Online();
BOOL ReadVersion(LPCTSTR lpFileName,LPCSTR lpszUserName,LPCSTR lpszPassword);
LPCTSTR GetVersionText() { return m_version; }
DWORD GetError() { return m_dwError; }
LPCTSTR GetExtendedErrorInfo() { return m_errInfo; }
};
CWebVersion 的实现文件
////////////////////////////////////////////////////////////////
// WebVersion.cpp
//
#include "stdafx.h"
#include "WebVersion.h"
#include "InetHandle.h"
//////////////////
// Check if connected to Internet.
//
BOOL CWebVersion::Online()
{
DWORD dwState = 0;
DWORD dwSize = sizeof(DWORD);
return InternetQueryOption(NULL,
INTERNET_OPTION_CONNECTED_STATE, &dwState, &dwSize)
&& (dwState & INTERNET_STATE_CONNECTED);
}
//////////////////
// Read version number as string into buffer
//
BOOL CWebVersion::ReadVersion(LPCTSTR lpFileName,LPCSTR lpszUserName,LPCSTR lpszPassword)
{
CInternetHandle hInternet;
CInternetHandle hFtpSession;
CInternetHandle hFtpFile;
m_version[0] = 0;
m_dwError=0; // assume success
m_errInfo[0]=0; // ..
DWORD nRead=0;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL,
NULL, 0);
if (hInternet!=NULL) {
hFtpSession = InternetConnect(hInternet, m_lpServer,
INTERNET_DEFAULT_FTP_PORT, lpszUserName, lpszPassword, INTERNET_SERVICE_FTP,
0, NULL);
if (hFtpSession!=NULL) {
hFtpFile = FtpOpenFile(hFtpSession, lpFileName,
GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, NULL);
if (hFtpFile!=NULL) {
InternetReadFile(hFtpFile, m_version, BUFSIZE, &nRead);
if (nRead>0) {
m_version[nRead] = 0;
int Mhi,Mlo,mhi,mlo;
_stscanf(m_version, "%x,%x,%x,%x", &Mhi, &Mlo, &mhi, &mlo);
dwVersionMS = MAKELONG(Mlo,Mhi);
dwVersionLS = MAKELONG(mlo,mhi);
return TRUE;
}
}
}
}
// Failed: save error code and extended error info if any.
m_dwError = GetLastError();
if (m_dwError==ERROR_INTERNET_EXTENDED_ERROR) {
DWORD dwErr;
DWORD len = sizeof(m_errInfo)/sizeof(m_errInfo[0]);
InternetGetLastResponseInfo(&dwErr, m_errInfo, &len);
}
return FALSE;
}
- ››基于IP地址的vsftp服务器
- ››基于MySQL 水平分区的优化示例
- ››web安全之信息刺探防范1
- ››基于CentOS5的Linux下pptp和openvpn的搭建及配置
- ››webqq 最新加密算法
- ››webdriver 数据库验证方法
- ››WebSphere Application Server 7.0 XML Feature P...
- ››Web2.0网络时代基于社会影响力的声望值
- ››Web服务器搭建:配置Linux+Apache+Mysql+PHP(或Pe...
- ››WebLogic调整Java虚拟机性能优化参数
- ››基于JavaScript的网页版塔防游戏
- ››基于Android平台 QQ大战360手机游戏爆红
更多精彩
赞助商链接