蜗牛的家
男儿当自强
posts - 48,  comments - 21,  trackbacks - 0
  1#include "StdAfx.h"
  2#include "HttpDown.h"
  3
  4
  5//*****************************************************************************
  6CBSCallbackImpl::CBSCallbackImpl(HWND hWnd, HANDLE hEventStop)
  7{
  8    m_hWnd = hWnd;  // the window handle to display status
  9
 10    m_hEventStop = hEventStop;  // the event object to signal to stop
 11
 12    m_ulObjRefCount = 1;
 13}

 14
 15// IUnknown
 16STDMETHODIMP CBSCallbackImpl::QueryInterface(REFIID riid, void **ppvObject)
 17{
 18    TRACE(_T("IUnknown::QueryInterface\n"));
 19
 20    *ppvObject = NULL;
 21
 22    // IUnknown
 23    if (::IsEqualIID(riid, __uuidof(IUnknown)))
 24    {
 25        TRACE(_T("IUnknown::QueryInterface(IUnknown)\n"));
 26
 27        *ppvObject = this;
 28    }

 29    // IBindStatusCallback
 30    else if (::IsEqualIID(riid, __uuidof(IBindStatusCallback)))
 31    {
 32        TRACE(_T("IUnknown::QueryInterface(IBindStatusCallback)\n"));
 33
 34        *ppvObject = static_cast<IBindStatusCallback *>(this);
 35    }

 36
 37    if (*ppvObject)
 38    {
 39        (*reinterpret_cast<LPUNKNOWN *>(ppvObject))->AddRef();
 40
 41        return S_OK;
 42    }

 43
 44    return E_NOINTERFACE;
 45}
                                             
 46
 47STDMETHODIMP_(ULONG) CBSCallbackImpl::AddRef()
 48{
 49    return ++m_ulObjRefCount;
 50}

 51
 52STDMETHODIMP_(ULONG) CBSCallbackImpl::Release()
 53{
 54    return --m_ulObjRefCount;
 55}

 56
 57// IBindStatusCallback
 58STDMETHODIMP CBSCallbackImpl::OnStartBinding(DWORD, IBinding *)
 59{
 60    return S_OK;
 61}

 62STDMETHODIMP CBSCallbackImpl::GetPriority(LONG *)
 63{
 64    return E_NOTIMPL;
 65}

 66STDMETHODIMP CBSCallbackImpl::OnLowResource(DWORD)
 67{
 68    return S_OK;
 69}

 70STDMETHODIMP CBSCallbackImpl::OnStopBinding(HRESULT, LPCWSTR)
 71{
 72    return S_OK;
 73}

 74STDMETHODIMP CBSCallbackImpl::GetBindInfo(DWORD *, BINDINFO *)
 75{
 76    return S_OK;
 77}

 78STDMETHODIMP CBSCallbackImpl::OnDataAvailable(DWORD, DWORD,
 79                                              FORMATETC *, STGMEDIUM *)
 80{
 81    return S_OK;
 82}

 83STDMETHODIMP CBSCallbackImpl::OnObjectAvailable(REFIID, IUnknown *)
 84{
 85    return S_OK;
 86}

 87
 88
 89//-----------------------------------------------------------------------------
 90STDMETHODIMP CBSCallbackImpl::OnProgress(ULONG ulProgress,
 91                                         ULONG ulProgressMax,
 92                                         ULONG ulStatusCode,
 93                                         LPCWSTR szStatusText)
 94{
 95#ifdef _DEBUG
 96    static const LPCTSTR plpszStatus[] = 
 97    {
 98        _T("BINDSTATUS_FINDINGRESOURCE"),  // 1
 99        _T("BINDSTATUS_CONNECTING"),
100        _T("BINDSTATUS_REDIRECTING"),
101        _T("BINDSTATUS_BEGINDOWNLOADDATA"),
102        _T("BINDSTATUS_DOWNLOADINGDATA"),
103        _T("BINDSTATUS_ENDDOWNLOADDATA"),
104        _T("BINDSTATUS_BEGINDOWNLOADCOMPONENTS"),
105        _T("BINDSTATUS_INSTALLINGCOMPONENTS"),
106        _T("BINDSTATUS_ENDDOWNLOADCOMPONENTS"),
107        _T("BINDSTATUS_USINGCACHEDCOPY"),
108        _T("BINDSTATUS_SENDINGREQUEST"),
109        _T("BINDSTATUS_CLASSIDAVAILABLE"),
110        _T("BINDSTATUS_MIMETYPEAVAILABLE"),
111        _T("BINDSTATUS_CACHEFILENAMEAVAILABLE"),
112        _T("BINDSTATUS_BEGINSYNCOPERATION"),
113        _T("BINDSTATUS_ENDSYNCOPERATION"),
114        _T("BINDSTATUS_BEGINUPLOADDATA"),
115        _T("BINDSTATUS_UPLOADINGDATA"),
116        _T("BINDSTATUS_ENDUPLOADINGDATA"),
117        _T("BINDSTATUS_PROTOCOLCLASSID"),
118        _T("BINDSTATUS_ENCODING"),
119        _T("BINDSTATUS_VERFIEDMIMETYPEAVAILABLE"),
120        _T("BINDSTATUS_CLASSINSTALLLOCATION"),
121        _T("BINDSTATUS_DECODING"),
122        _T("BINDSTATUS_LOADINGMIMEHANDLER"),
123        _T("BINDSTATUS_CONTENTDISPOSITIONATTACH"),
124        _T("BINDSTATUS_FILTERREPORTMIMETYPE"),
125        _T("BINDSTATUS_CLSIDCANINSTANTIATE"),
126        _T("BINDSTATUS_IUNKNOWNAVAILABLE"),
127        _T("BINDSTATUS_DIRECTBIND"),
128        _T("BINDSTATUS_RAWMIMETYPE"),
129        _T("BINDSTATUS_PROXYDETECTING"),
130        _T("BINDSTATUS_ACCEPTRANGES"),
131        _T("???")  // unknown
132    }
;
133#endif
134
135    TRACE(_T("IBindStatusCallback::OnProgress\n"));
136    TRACE(_T("ulProgress: %lu, ulProgressMax: %lu\n"), ulProgress, ulProgressMax);
137    TRACE(_T("ulStatusCode: %lu "), ulStatusCode);
138
139    if (ulStatusCode < UF_BINDSTATUS_FIRST ||
140        ulStatusCode > UF_BINDSTATUS_LAST)
141    {
142        ulStatusCode = UF_BINDSTATUS_LAST + 1;
143    }

144
145#ifdef _DEBUG
146    TRACE(_T("(%s), szStatusText: %ls\n"),
147        plpszStatus[ulStatusCode - UF_BINDSTATUS_FIRST],
148        szStatusText);
149#endif
150
151
152    if (m_hWnd != NULL)
153    {
154        DOWNLOADSTATUS downloadStatus =
155        { ulProgress, ulProgressMax, ulStatusCode, szStatusText };
156        ::SendMessage(m_hWnd, WM_USER_DISPLAYSTATUS,
157            0, reinterpret_cast<LPARAM>(&downloadStatus));
158    }

159
160    if (m_hEventStop != NULL)
161    {
162        if (::WaitForSingleObject(m_hEventStop, 0== WAIT_OBJECT_0)
163        {
164
165            TRACE(_T("Download to give up\n"));
166
167            ::SendMessage(m_hWnd, WM_USER_CANCELDOWNLOAD, 00);
168            return E_ABORT;  // canceled by the user
169        }

170    }

171
172    return S_OK;
173}

174//*****************************************************************************
175
176CHttpDown::CHttpDown(void)
177{
178    m_hThread = NULL;
179    m_hEventStop = CreateEvent(NULL,TRUE, FALSE, NULL);
180
181    ASSERT(NULL != m_hEventStop);
182}

183
184CHttpDown::~CHttpDown(void)
185{
186    EndDownLoad();
187    CloseHandle(m_hEventStop);
188}

189
190UINT CHttpDown::Download(LPVOID pParam)
191{
192    CHttpDown *const pThis = static_cast<CHttpDown *>(pParam);
193    CString strURL, strFileName, strFilePath;
194    TCHAR   lpCurrentPath[MAX_PATH];   
195    ::GetCurrentDirectory(MAX_PATH, lpCurrentPath);
196    
197    if (pThis->m_DownList.size() <= 0)
198        return 0;
199
200    /*
201    URLDownloadToCacheFile is a blocking function. Even though the data is
202    downloaded asynchronously the function does not return until all the
203    data is downloaded
204    */

205
206    CBSCallbackImpl bsc(pThis->m_hWnd, pThis->m_hEventStop);
207    
208    std::map<CString, CString>::iterator iterIdx = pThis->m_DownList.begin();
209
210    while(iterIdx != pThis->m_DownList.end() && WaitForSingleObject(pThis->m_hEventStop, 0== WAIT_TIMEOUT)
211    {
212        strURL = iterIdx->second;
213        strFilePath.Format(_T("%s\\%s"), lpCurrentPath, iterIdx->first);
214
215        const HRESULT hr = ::URLDownloadToCacheFile(NULL,
216            strURL,
217            strFileName.GetBuffer(MAX_PATH),
218            URLOSTRM_GETNEWESTVERSION,
219            0,
220            &bsc);
221    
222        // empty the filename string if failed or canceled
223        if (!SUCCEEDED(hr))
224        {
225            strFileName.ReleaseBuffer(-1);
226            pThis->m_DownList.clear();
227            //如果下载失败或是取消下载就关闭窗口
228            //::PostMessage(pThis->m_hWnd, WM_CLOSE, 0, 0);
229
230            return 0;
231        }

232        else
233        {
234            ::CopyFile(strFileName, strFilePath, FALSE);
235        }

236
237        iterIdx++;
238    }

239
240    pThis->m_DownList.clear();
241    ::PostMessage(pThis->m_hWnd, WM_USER_ENDDOWNLOAD, 00);
242
243    return 0;
244}

245
246BOOL CHttpDown::AddFile(CString strURL, CString strPath)
247{
248    if (strcmp(strURL, _T("")) == 0 || strcmp(strPath, _T("")) == 0)
249        return FALSE;
250
251    m_DownList[strPath] = strURL;
252    return TRUE;
253}

254
255void CHttpDown::DelFile(CString strPath)
256{
257    if (strcmp(strPath, _T("")) == 0)
258        return;
259
260    std::map<CString, CString>::iterator iterIdx = m_DownList.find(strPath);
261
262    if (iterIdx != m_DownList.end())
263        m_DownList.erase(iterIdx);
264}

265
266BOOL CHttpDown::BeginDownLoad()
267{
268    EndDownLoad();
269    ResetEvent(m_hEventStop);
270
271    DWORD ThreadID;
272    m_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Download, this0&ThreadID);
273
274    if (m_hThread == INVALID_HANDLE_VALUE)
275    {
276        return FALSE;
277    }

278
279    return TRUE;
280}

281
282void CHttpDown::EndDownLoad()
283{
284    if (m_hThread == NULL)
285        return;
286
287    DWORD dwResult;
288    GetExitCodeThread(m_hThread, &dwResult);
289    
290    if (dwResult == 259)
291    {
292        SetEvent(m_hEventStop);
293        ::WaitForSingleObject(m_hThread, INFINITE);
294        CloseHandle(m_hThread);
295        m_hThread = NULL;
296    }

297
298    CloseHandle(m_hThread);
299    m_hThread = NULL;
300}

301
302//进度消息处理示例
303/*
304LRESULT OnDisplayStatus(WPARAM wParam, LPARAM lParam)
305{
306    const DOWNLOADSTATUS *const pDownloadStatus =
307        reinterpret_cast<DOWNLOADSTATUS *>(lParam);
308
309    // form the status text
310    CString strStatus;
311
312    if (pDownloadStatus != NULL)
313    {
314
315        VERIFY(strStatus.LoadString(pDownloadStatus->ulStatusCode -
316            UF_BINDSTATUS_FIRST +
317            _T("Finding resource")));
318        strStatus += _T("  ");
319        strStatus += pDownloadStatus->szStatusText;
320
321        CString strProgress;
322        strProgress.Format(_T(" %lu of %lu"),
323            pDownloadStatus->ulProgress,
324            pDownloadStatus->ulProgressMax);
325
326        strStatus += strProgress + _T("\r\n");
327    }
328
329    return 0;
330}*/
  1#pragma once
  2#include <map>
  3
  4//The message of status update
  5#define  WM_USER_DISPLAYSTATUS  WM_USER + 1001
  6#define  WM_USER_ENDDOWNLOAD    WM_USER + 1002
  7#define  WM_USER_CANCELDOWNLOAD WM_USER + 1003
  8
  9enum
 10{
 11    UF_BINDSTATUS_FIRST = BINDSTATUS_FINDINGRESOURCE,
 12    UF_BINDSTATUS_LAST = BINDSTATUS_ACCEPTRANGES
 13}
;
 14
 15
 16//The status of Download
 17struct DOWNLOADSTATUS
 18{
 19    ULONG ulProgress;
 20    ULONG ulProgressMax;
 21    ULONG ulStatusCode;
 22    LPCWSTR szStatusText;
 23}
;
 24
 25class CBSCallbackImpl : public IBindStatusCallback
 26{
 27public:
 28    CBSCallbackImpl(HWND hWnd, HANDLE hEventStop);
 29
 30    // IUnknown methods
 31    STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject);
 32    STDMETHOD_(ULONG, AddRef)();
 33    STDMETHOD_(ULONG, Release)();
 34
 35    STDMETHOD(OnProgress)(ULONG ulProgress,    ULONG ulProgressMax,
 36        ULONG ulStatusCode,
 37        LPCWSTR szStatusText);
 38
 39    // IBindStatusCallback methods
 40    STDMETHOD(OnStartBinding)(DWORD, IBinding *);
 41    STDMETHOD(GetPriority)(LONG *);
 42    STDMETHOD(OnLowResource)(DWORD);
 43    STDMETHOD(OnStopBinding)(HRESULT, LPCWSTR);
 44    STDMETHOD(GetBindInfo)(DWORD *, BINDINFO *);
 45    STDMETHOD(OnDataAvailable)(DWORD, DWORD, FORMATETC *, STGMEDIUM *);
 46    STDMETHOD(OnObjectAvailable)(REFIID, IUnknown *);
 47
 48protected:
 49    ULONG m_ulObjRefCount;
 50
 51private:
 52    //Parent windows handle
 53    HWND m_hWnd;
 54    //The event of Stop
 55    HANDLE m_hEventStop;
 56}
;
 57
 58class CHttpDown
 59{
 60public:
 61    CHttpDown(void);
 62    virtual ~CHttpDown(void);
 63
 64    inline void SetParentWindow(HWND hwnd) {m_hWnd = hwnd;}
 65
 66    BOOL BeginDownLoad();
 67    void EndDownLoad();
 68
 69    BOOL AddFile(CString strURL, CString strPath);
 70    void DelFile(CString strPath);
 71
 72protected:
 73    static UINT Download(LPVOID pParam);
 74
 75private:
 76    HWND m_hWnd;
 77    HANDLE m_hEventStop;
 78    HANDLE m_hThread;
 79
 80    std::map<CString, CString> m_DownList;
 81}
;
 82
 83/*
 84资源声明
 85#define IDS_BINDSTATUS01                106
 86#define IDS_BINDSTATUS02                107
 87#define IDS_BINDSTATUS03                108
 88#define IDS_BINDSTATUS04                109
 89#define IDS_BINDSTATUS05                110
 90#define IDS_BINDSTATUS06                111
 91#define IDS_BINDSTATUS07                112
 92#define IDS_BINDSTATUS08                113
 93#define IDS_BINDSTATUS09                114
 94#define IDS_BINDSTATUS10                115
 95#define IDS_BINDSTATUS11                116
 96#define IDS_BINDSTATUS12                117
 97#define IDS_BINDSTATUS13                118
 98#define IDS_BINDSTATUS14                119
 99#define IDS_BINDSTATUS15                120
100#define IDS_BINDSTATUS16                121
101#define IDS_BINDSTATUS17                122
102#define IDS_BINDSTATUS18                123
103#define IDS_BINDSTATUS19                124
104#define IDS_BINDSTATUS20                125
105#define IDS_BINDSTATUS21                126
106#define IDS_BINDSTATUS22                127
107#define IDS_BINDSTATUS23                128
108#define IDS_BINDSTATUS24                129
109#define IDS_BINDSTATUS25                130
110#define IDS_BINDSTATUS26                131
111#define IDS_BINDSTATUS27                132
112#define IDS_BINDSTATUS28                133
113#define IDS_BINDSTATUS29                134
114#define IDS_BINDSTATUS30                135
115#define IDS_BINDSTATUS31                136
116#define IDS_BINDSTATUS32                137
117#define IDS_BINDSTATUS33                138
118#define IDS_BINDSTATUS34                139
119IDS_BINDSTATUS01        "Finding resource"
120IDS_BINDSTATUS02        "Connecting"
121IDS_BINDSTATUS03        "Redirecting"
122IDS_BINDSTATUS04        "Begin download data"
123IDS_BINDSTATUS05        "Downloading data"
124IDS_BINDSTATUS06        "End download data"
125IDS_BINDSTATUS07        "Begin download components"
126IDS_BINDSTATUS08        "Installing components"
127IDS_BINDSTATUS09        "End download components"
128IDS_BINDSTATUS10        "Using cached copy"
129IDS_BINDSTATUS11        "Sending request"
130IDS_BINDSTATUS12        "Class ID available"
131IDS_BINDSTATUS13        "MIME type available"
132IDS_BINDSTATUS14        "Cache file name available"
133IDS_BINDSTATUS15        "Begin synchronous operation"
134IDS_BINDSTATUS16        "End synchronous operation"
135IDS_BINDSTATUS17        "Begin upload data"
136IDS_BINDSTATUS18        "Uploading data"
137IDS_BINDSTATUS19        "End upload data"
138IDS_BINDSTATUS20        "Protocol class ID available"
139IDS_BINDSTATUS21        "Encoding"
140IDS_BINDSTATUS22        "Verfied MIME type available"
141IDS_BINDSTATUS23        "Class install location available"
142IDS_BINDSTATUS24        "Decoding"
143IDS_BINDSTATUS25        "Loading MIME handler"
144IDS_BINDSTATUS26        "Content-Disposition attachment"
145IDS_BINDSTATUS27        "MIME filter to report a change in MIME type"
146IDS_BINDSTATUS28        "CLSID can instantiate"
147IDS_BINDSTATUS29        "IUnknown released"
148IDS_BINDSTATUS30        "Direct bind"
149IDS_BINDSTATUS31        "Raw MIME type"
150IDS_BINDSTATUS32        "Detecting proxy"
151IDS_BINDSTATUS33        "Accept ranges"
152IDS_BINDSTATUS34        "???"
153*/

154
155

posted on 2008-12-23 13:21 黑色天使 阅读(2280) 评论(10)  编辑 收藏 引用 所属分类: 网络开发

FeedBack:
# re: CHttpDwonLoad Bete 1.0[未登录]
2008-12-23 13:26 | jacky
Bete? 还是Beta
bete:[bet][法]野兽,愚人

DwonLoad??  回复  更多评论
  
# re: CHttpDwonLoad Bete 1.0
2008-12-23 14:04 | 肥仔
楼上的眼睛好犀利  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 14:32 | 黑色天使
@jacky
.......我可能有点晕了,最近太忙了  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 14:48 | ToperRay
如何使用啊?  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 15:01 | ToperRay
strFileName没有被赋值,哥们。


  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 15:31 | ToperRay
IBindStatusCallback

未实现Authenticate 函数。  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 15:39 | LZ
@ToperRay
系统回调的赋值  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 15:42 | LZ
@ToperRay
OK,记下了,下次补上Authenticate实现  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-23 15:51 | LZ
//设定消息处理的窗口句柄
HttpDown.SetParentWindow(hwnd);
//添加要下载的文件路径
HttpDown.AddFile();
//开始下载
m_HttpDown.BeginDownLoad();
//这两个函数都是消息通知,自己添加自定义消息处理
LRESULT CAutoUpdateDlg::EndDown(WPARAM wParam, LPARAM lParam)
{
//下载完成以后调用的函数
return 0;
}

LRESULT CAutoUpdateDlg::OnDisplayStatus(WPARAM wParam, LPARAM lParam)
{
//处理下载进度的函数

const DOWNLOADSTATUS *const pDownloadStatus =
reinterpret_cast<DOWNLOADSTATUS *>(lParam);
CString strStatus;

if (pDownloadStatus != NULL)
{
#ifdef _DEBUG
VERIFY(strStatus.LoadString(pDownloadStatus->ulStatusCode -
UF_BINDSTATUS_FIRST +
IDS_BINDSTATUS01));
strStatus += _T(" ");
strStatus += pDownloadStatus->szStatusText;

CString strProgress;
strProgress.Format(_T(" %lu of %lu"),
pDownloadStatus->ulProgress,
pDownloadStatus->ulProgressMax);

strStatus += strProgress + _T("\r\n");
TRACE(strStatus);
#endif

m_Progress.SetPos(MAX_PROGRESS_NUM/((pDownloadStatus->ulProgressMax/(pDownloadStatus->ulProgress+1))+1));
}

return 0;
}

//下载失败我就在函数里处理关闭主窗口,具体自己实现
// empty the filename string if failed or canceled
if (!SUCCEEDED(hr))
{
strFileName.ReleaseBuffer(-1);
pThis->m_DownList.clear();
//如果下载失败或是取消下载就关闭窗口
::PostMessage(pThis->m_hWnd, WM_CLOSE, 0, 0);

return 0;
}

  回复  更多评论
  
# re: CHttpDownLoad Beta 1.0
2008-12-25 11:54 | tangxinfa
这里有个类似的东西:HTTP服务器上断点下载文件 http://www.cnitblog.com/wangk/archive/2007/05/22/5942.html  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(2)

随笔分类

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜