URLDownloadToFile 始终 return S_OK
URLDownloadToFile always return S_OK
我是 c++ 的新手,尝试编写一个更新函数。
使用 URLDownloadToFile 下载没有问题,但如果我将 url 更改为无效的,它仍然 return S_OK ...我如何检查下载是否成功成功与否?
#include <WinInet.h>
#include <iomanip>
int download_file (const TCHAR urldownload[],const TCHAR target[] )
{
DownloadProgress progress;
IBindStatusCallback* callback = (IBindStatusCallback*)&progress;
SCP(40, NULL); cout << target;
HRESULT status = URLDownloadToFile(NULL, urldownload, target, 0, static_cast<IBindStatusCallback*>(&progress));
Sleep(200);
DeleteUrlCacheEntry(urldownload);
wcout << status;
if (status == S_OK) cout << "yes";
else(cout << "Download failed");
Sleep(10000); return 1;
}
class DownloadProgress : public IBindStatusCallback {
public:
HRESULT __stdcall QueryInterface(const IID &, void **) {
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void) {
return 1;
}
ULONG STDMETHODCALLTYPE Release(void) {
return 1;
}
HRESULT STDMETHODCALLTYPE OnStartBinding(DWORD dwReserved, IBinding *pib) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetPriority(LONG *pnPriority) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnLowResource(DWORD reserved) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(HRESULT hresult, LPCWSTR szError) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(DWORD *grfBINDF, BINDINFO *pbindinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(REFIID riid, IUnknown *punk) {
return E_NOTIMPL;
}
virtual HRESULT __stdcall OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
{
//wcout << ulProgress << L" of " << ulProgressMax << endl; Sleep(200);
if (ulProgress != 0 && ulProgressMax != 0)
{
double output = (double(ulProgress) / ulProgressMax)*100;
cout << "\r" << "Downloading: " << fixed << setprecision(2) << output << " % " ; Sleep(20);
}
return S_OK;
}
};
MSDN article has the answer 给你:
URLDownloadToFile
returns S_OK
even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile
. For best control over the download and its progress, an IBindStatusCallback
interface is recommended.
您需要提供状态回调以接收异步操作的状态。您的代码片段已经有了基础。 OnProgress
和 OnStopBinding
应该会得到下载失败的结果。
我是 c++ 的新手,尝试编写一个更新函数。
使用 URLDownloadToFile 下载没有问题,但如果我将 url 更改为无效的,它仍然 return S_OK ...我如何检查下载是否成功成功与否?
#include <WinInet.h>
#include <iomanip>
int download_file (const TCHAR urldownload[],const TCHAR target[] )
{
DownloadProgress progress;
IBindStatusCallback* callback = (IBindStatusCallback*)&progress;
SCP(40, NULL); cout << target;
HRESULT status = URLDownloadToFile(NULL, urldownload, target, 0, static_cast<IBindStatusCallback*>(&progress));
Sleep(200);
DeleteUrlCacheEntry(urldownload);
wcout << status;
if (status == S_OK) cout << "yes";
else(cout << "Download failed");
Sleep(10000); return 1;
}
class DownloadProgress : public IBindStatusCallback {
public:
HRESULT __stdcall QueryInterface(const IID &, void **) {
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void) {
return 1;
}
ULONG STDMETHODCALLTYPE Release(void) {
return 1;
}
HRESULT STDMETHODCALLTYPE OnStartBinding(DWORD dwReserved, IBinding *pib) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetPriority(LONG *pnPriority) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnLowResource(DWORD reserved) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(HRESULT hresult, LPCWSTR szError) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(DWORD *grfBINDF, BINDINFO *pbindinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(REFIID riid, IUnknown *punk) {
return E_NOTIMPL;
}
virtual HRESULT __stdcall OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
{
//wcout << ulProgress << L" of " << ulProgressMax << endl; Sleep(200);
if (ulProgress != 0 && ulProgressMax != 0)
{
double output = (double(ulProgress) / ulProgressMax)*100;
cout << "\r" << "Downloading: " << fixed << setprecision(2) << output << " % " ; Sleep(20);
}
return S_OK;
}
};
MSDN article has the answer 给你:
URLDownloadToFile
returnsS_OK
even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before callingURLDownloadToFile
. For best control over the download and its progress, anIBindStatusCallback
interface is recommended.
您需要提供状态回调以接收异步操作的状态。您的代码片段已经有了基础。 OnProgress
和 OnStopBinding
应该会得到下载失败的结果。