WINCE实现网络更新程序(转)

33 篇文章 1 订阅
28 篇文章 0 订阅

由于工作须要做一个可以通过网络更新应用程序,经研究思考后和结合自身程序的须要,我就弄了一个FTP下载的代码,其实也蛮简单,就是直接去连接FTP,连接后再去下载文件到本地目录,如果文件已存在则自动覆盖(默认),主要代码也参考了网络流行的FTP下载类,头文件如下,如果大家在网上找不到此类的话也可以和我联系索取。

#ifndef _CE_INET_
#define _CE_INET_
#include <wininet.h>
#pragma comment(lib, "wininet.lib")


class CCEInternetSession; // from CObject
class CCEInternetConnection;
   class CCEFtpConnection;
class CCEInternetFile;    // from CStdioFile (FILETXT.CPP)



class CCEFtpFileFind;

class CCEInternetException;
/
// Global Functions
bool __stdcall _CEParseURLWorker(LPCTSTR pstrURL,
LPURL_COMPONENTS lpComponents, DWORD& dwServiceType,
INTERNET_PORT& nPort, DWORD dwFlags);

BOOL __stdcall  CEParseURL(LPCTSTR pstrURL, DWORD& dwServiceType,
CString& strServer, CString& strObject, INTERNET_PORT& nPort);
BOOL __stdcall  CEParseURLEx(LPCTSTR pstrURL, DWORD& dwServiceType,
CString& strServer, CString& strObject, INTERNET_PORT& nPort,
CString& strUsername, CString& strPassword, DWORD dwFlags = 0);

DWORD __stdcall  CEGetInternetHandleType(HINTERNET hQuery);

BOOL __stdcall _CEQueryCStringInternetOption(HINTERNET hHandle, DWORD dwOption, CString& refString);

void __stdcall CEThrowInternetException(DWORD dwContext, DWORD dwError = 0);
#define AFX_INET_SERVICE_FTP        INTERNET_SERVICE_FTP
#define AFX_INET_SERVICE_HTTP       INTERNET_SERVICE_HTTP
#define AFX_INET_SERVICE_GOPHER     INTERNET_SERVICE_GOPHER

// these are types that MFC parsing functions understand

#define AFX_INET_SERVICE_UNK        0x1000
#define AFX_INET_SERVICE_FILE       (AFX_INET_SERVICE_UNK+1)
#define AFX_INET_SERVICE_MAILTO     (AFX_INET_SERVICE_UNK+2)
#define AFX_INET_SERVICE_MID        (AFX_INET_SERVICE_UNK+3)
#define AFX_INET_SERVICE_CID        (AFX_INET_SERVICE_UNK+4)
#define AFX_INET_SERVICE_NEWS       (AFX_INET_SERVICE_UNK+5)
#define AFX_INET_SERVICE_NNTP       (AFX_INET_SERVICE_UNK+6)
#define AFX_INET_SERVICE_PROSPERO   (AFX_INET_SERVICE_UNK+7)
#define AFX_INET_SERVICE_TELNET     (AFX_INET_SERVICE_UNK+8)
#define AFX_INET_SERVICE_WAIS       (AFX_INET_SERVICE_UNK+9)
#define AFX_INET_SERVICE_AFS        (AFX_INET_SERVICE_UNK+10)
#define AFX_INET_SERVICE_HTTPS      (AFX_INET_SERVICE_UNK+11)


class CCEInternetSession : public CObject
{
public:
CCEInternetSession(LPCTSTR pstrAgent = NULL,
DWORD dwContext = 1,
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS,
LPCTSTR pstrProxyName = NULL,
LPCTSTR pstrProxyBypass = NULL,
DWORD dwFlags = 0);

BOOL QueryOption(DWORD dwOption, LPVOID lpBuffer, LPDWORD lpdwBufLen) const;
BOOL QueryOption(DWORD dwOption, DWORD& dwValue) const;
BOOL QueryOption(DWORD dwOption, CString& refString) const;

BOOL SetOption(DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength,
DWORD dwFlags = 0);
BOOL SetOption(DWORD dwOption, DWORD dwValue, DWORD dwFlags = 0);

CStdioFile* OpenURL(LPCTSTR pstrURL,
DWORD dwContext = 1, DWORD dwFlags = INTERNET_FLAG_TRANSFER_ASCII,
LPCTSTR pstrHeaders = NULL, DWORD dwHeadersLength = 0);

CCEFtpConnection* GetFtpConnection(LPCTSTR pstrServer,
LPCTSTR pstrUserName = NULL, LPCTSTR pstrPassword = NULL,
INTERNET_PORT nPort = INTERNET_INVALID_PORT_NUMBER,
BOOL bPassive = FALSE);


BOOL EnableStatusCallback(BOOL bEnable = TRUE);

DWORD ServiceTypeFromHandle(HINTERNET hQuery);

// operations

DWORD GetContext() const;
operator HINTERNET() const;
virtual void Close();

// overridables
virtual void OnStatusCallback(DWORD dwContext, DWORD dwInternetStatus,
LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);

// implementation
DECLARE_DYNAMIC(CCEInternetSession)
~CCEInternetSession();

protected:
DWORD m_dwContext;
HINTERNET m_hSession;
INTERNET_STATUS_CALLBACK m_pOldCallback;
BOOL m_bCallbackEnabled;

public:
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
#endif
};


/
//CInternetConnection
class CCEInternetConnection : public CObject
{
public:
CCEInternetConnection(CCEInternetSession* pSession, LPCTSTR pstrServer,
INTERNET_PORT nPort = INTERNET_INVALID_PORT_NUMBER,
DWORD dwContext = 1);

// Operations
operator HINTERNET() const;
DWORD GetContext() const;
CCEInternetSession* GetSession() const;

CString GetServerName() const;

BOOL QueryOption(DWORD dwOption, LPVOID lpBuffer, LPDWORD lpdwBufLen) const;
BOOL QueryOption(DWORD dwOption, DWORD& dwValue) const;
BOOL QueryOption(DWORD dwOption, CString& refString) const;

BOOL SetOption(DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength,
DWORD dwFlags = 0);
BOOL SetOption(DWORD dwOption, DWORD dwValue, DWORD dwFlags = 0);

// Implementation
protected:
HINTERNET m_hConnection;
DWORD m_dwContext;
CCEInternetSession* m_pSession;
virtual void Close();

CString m_strServerName;
INTERNET_PORT m_nPort;

public:
~CCEInternetConnection();
DECLARE_DYNAMIC(CCEInternetConnection)

#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
void AssertValid() const;
#endif
};

//
class CCEFtpConnection : public CCEInternetConnection
{
public:
CCEFtpConnection(CCEInternetSession* pSession, HINTERNET hConnected,
LPCTSTR pstrServer, DWORD dwContext);
CCEFtpConnection(CCEInternetSession* pSession, LPCTSTR pstrServer,
LPCTSTR pstrUserName = NULL, LPCTSTR pstrPassword = NULL,
DWORD dwContext = 0,
INTERNET_PORT nPort = INTERNET_INVALID_PORT_NUMBER,
BOOL bPassive = FALSE);

BOOL SetCurrentDirectory(LPCTSTR pstrDirName);

BOOL GetCurrentDirectory(CString& strDirName) const;
BOOL GetCurrentDirectory(LPTSTR pstrDirName, LPDWORD lpdwLen) const;
BOOL GetCurrentDirectoryAsURL(LPTSTR pstrName, LPDWORD lpdwLen) const;
BOOL GetCurrentDirectoryAsURL(CString& strDirName) const;

BOOL RemoveDirectory(LPCTSTR pstrDirName);
BOOL CreateDirectory(LPCTSTR pstrDirName);
BOOL Rename(LPCTSTR pstrExisting, LPCTSTR pstrNew);
BOOL Remove(LPCTSTR pstrFileName);

BOOL PutFile(LPCTSTR pstrLocalFile, LPCTSTR pstrRemoteFile,
DWORD dwFlags = FTP_TRANSFER_TYPE_BINARY, DWORD dwContext = 1);

BOOL GetFile(LPCTSTR pstrRemoteFile, LPCTSTR pstrLocalFile,
BOOL bFailIfExists = FALSE,
DWORD dwAttributes = FILE_ATTRIBUTE_NORMAL,
DWORD dwFlags = FTP_TRANSFER_TYPE_BINARY, DWORD dwContext = 1);

CCEInternetFile* OpenFile(LPCTSTR pstrFileName,
DWORD dwAccess = GENERIC_READ,
DWORD dwFlags = FTP_TRANSFER_TYPE_BINARY, DWORD dwContext = 1);

virtual void Close();

// implementation
~CCEFtpConnection();

protected:
CString m_strServerName;

public:
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
virtual void AssertValid() const;
#endif

DECLARE_DYNAMIC(CCEFtpConnection)
};

//----------------------------------------------------------------------
// Internet File Access Wrapper

class CCEInternetFile : public CStdioFile
{
// Constructors
protected:
CCEInternetFile(HINTERNET hFile, LPCTSTR pstrFileName,
CCEInternetConnection* pConnection, BOOL bReadMode);
CCEInternetFile(HINTERNET hFile, HINTERNET hSession,
LPCTSTR pstrFileName, LPCTSTR pstrServer, DWORD dwContext,
BOOL bReadMode);

// Attributes
protected:
HINTERNET m_hFile;
public:
operator HINTERNET() const;
DWORD GetContext() const;

// Operations
BOOL SetReadBufferSize(UINT nReadSize);

BOOL QueryOption(DWORD dwOption, LPVOID lpBuffer, LPDWORD lpdwBufLen) const;
BOOL QueryOption(DWORD dwOption, DWORD& dwValue) const;
BOOL QueryOption(DWORD dwOption, CString& refString) const;

BOOL SetOption(DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength,
DWORD dwFlags = 0);
BOOL SetOption(DWORD dwOption, DWORD dwValue, DWORD dwFlags = 0);

// Overridables


virtual UINT Read(void* lpBuf, UINT nCount);
virtual void Abort();


virtual void Close();
virtual DWORD GetLength() const;

virtual BOOL ReadString(CString& rString);
virtual LPTSTR ReadString(LPTSTR pstr, UINT nMax);

// Not supported by CInternetFile
void LockRange(DWORD dwPos, DWORD dwCount);
void UnlockRange(DWORD dwPos, DWORD dwCount);
CFile* Duplicate() const;
virtual void SetLength(DWORD dwNewLen);

// Implementation
public:
virtual ~CCEInternetFile();

protected:
BOOL m_bReadMode;
DWORD m_dwContext;
HINTERNET m_hConnection;

CString m_strServerName;

UINT m_nWriteBufferSize;
UINT m_nWriteBufferPos;
LPBYTE m_pbWriteBuffer;

UINT m_nReadBufferSize;
UINT m_nReadBufferPos;
LPBYTE m_pbReadBuffer;
UINT m_nReadBufferBytes;

#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

friend class CCEInternetSession;
friend class CCEFtpConnection;
DECLARE_DYNAMIC(CCEInternetFile)
};


//-----------------------------------------------------------------------
class CCEFtpFileFind : public CObject
{
public:
CCEFtpFileFind(CCEFtpConnection* pConnection, DWORD dwContext = 1);
virtual ~CCEFtpFileFind();

    void Close();
virtual BOOL FindFile(LPCTSTR pstrName = NULL,
DWORD dwFlags = INTERNET_FLAG_RELOAD);
virtual BOOL FindNextFile();
CString GetFileURL() const;
CString GetFilePath() const;
CString GetFileName() const;

// implementation
protected:
virtual void CloseContext();
CCEFtpConnection* m_pConnection;
DWORD m_dwContext;
    void* m_pFoundInfo;
    void* m_pNextInfo;
    HANDLE m_hContext;
    bool m_bGotLast;
    CString m_strRoot;
    TCHAR m_chDirSeparator;     // not '//' for Internet classes
public:
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
virtual void AssertValid() const;
#endif

DECLARE_DYNAMIC(CCEFtpFileFind)
};
///
// CInternetException

class CCEInternetException : public CException
{
public:
// Constructor
CCEInternetException(DWORD dwError);

// Attributes
DWORD m_dwError;
DWORD m_dwContext;

// Implementation
public:
~CCEInternetException();
#ifdef _DEBUG
virtual void Dump(CDumpContext& dc) const;
#endif
virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError,
PUINT pnHelpContext = NULL);
DECLARE_DYNAMIC(CCEInternetException)
};

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值