WEB开发网
开发学院软件开发VC 基于 Web 的程序版本检查 阅读

基于 Web 的程序版本检查

 2006-07-21 11:09:14 来源:WEB开发网   
核心提示: if (CWebVersion::Online()) {CWebVersion ver("pub.chinafsdu.net");if (ver.ReadVersion("version.txt"),"pub","pub
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;
}

上一页  1 2 3 4  下一页

Tags:基于 Web 程序

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接