Downloading a File

Hello! I have found a general guideline with Windows API to download a file, though it's not working for me. Here's my code...

1
2
3
4
5
6
7
8
9
10
11
#include<windows.h>
#include <tchar.h>
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib,"wininet.lib")
void update()
{
	HRESULT hr;
	LPCTSTR Url = _T("http://cplusplus.com/img/cpp-logo.png"), File = _T("C:\\cpp-logo.png");
	hr = URLDownloadToFile (0, Url, File, 0, 0);
}


What am I missing or doing wrong? It compiles and runs without a problem, but the file doesn't download to my C:\ drive. (The above is just an example with actual filenames/urls switched out for the c++ forums logo).
Are you running elevated? Writing to the root of C:\ requires administrative privileges, and in Windows Vista and Windows 7, that means that you require elevation. Do you fit in this scenario?
I run Windows XP, and yeah I'm in admin. Also, I tried saving it locally, but that didn't work. So I'm guessing there's something wrong with that code. I just found it from another online source, so I don't even know if it's supposed to work. The thread was so old though that I didn't want to bump it and ask from there.
I haven't read the documentation of the function in MSDN Online, but I did see that it returns an HRESULT as return value. Have you checked this HRESULT for failure? If yes, what is the failure code?
It returns some really large negative value, which stays the same every time. I'm going to try this later today...

1
2
3
4
5
6
7
switch (hr)
{
    case S_OK: cout << "Successful download\n"; break;
    case E_OUTOFMEMORY: cout << "Out of memory error\n"; break;
    case INET_E_DOWNLOAD_FAILURE:    cout << "Cannot access server data\n"; break;
    default: cout << "Unknown error"; break;
}


If it does result in a successful download start, then I'll check progress as gone over in the MSDN documentation with a for loop after. I'll be doing this later today.

http://msdn.microsoft.com/en-us/library/ms775123%28VS.85%29.aspx
Which one was it (the HRESULT error)?

Remember that HRESULT's are special. The number is decomposed into several parts (the failure bit, the interace bits, the error number bits, and the facility bits, if I remember correctly).
Display the error using std::hex, so you can see the bits of the code that webJose is referring to. All standard HRESULT errors start with 8 (in hex), the next 3 digits (more or less) are the facility, and the final 4 the code.

e.g. INET_E_DOWNLOAD_FAILURE = 0x800C0008

8 = error
C = FACILITY_INTERNET
8 = the error code

Andy

(see MSDN for the exact meaning of the various high order bits)
Last edited on
Running this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<windows.h>
#include <tchar.h>
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib,"wininet.lib")
using namespace std;
void update()
{
	HRESULT hr;
	LPCTSTR Url = _T("http://cplusplus.com/img/cpp-logo.png"), File = _T("C:\\cpp-logo.png");
	hr = URLDownloadToFile (0, Url, File, 0, 0);
	switch (hr)
	{
		case S_OK:
			cout << "Successful download\n";
			break;
		case E_OUTOFMEMORY:
			cout << "Out of memory error\n";
			break;
		case INET_E_DOWNLOAD_FAILURE:
			cout << "Cannot access server data\n";
			break;
		default:
			cout << "Unknown error\n";
			break;
	}
	printf("%x",hr);
}


gave me this...

Unknown error
800c0005


I looked up what that error code means in the "urlmon.h" file, and it looks like this case causes it...
 
#define INET_E_RESOURCE_NOT_FOUND        _HRESULT_TYPEDEF_(0x800C0005L) 

When I add case INET_E_RESOURCE_NOT_FOUND: to my program, that case is true. So... What does that mean? :O Am I passing the Url part wrong?

For future reference, here's the error code list:
INET_E_INVALID_URL (800C000)
INET_E_NO_SESSION (800C0003)
INET_E_CANNOT_CONNECT (800C0004)
INET_E_RESOURCE_NOT_FOUND (800C0005)
INET_E_OBJECT_NOT_FOUND (800C0006)
INET_E_DATA_NOT_AVAILABLE (800C0007)
INET_E_DOWNLOAD_FAILURE (800C0008)
INET_E_AUTHENTICATION_REQUIRED (800C0009)
INET_E_NO_VALID_MEDIA (800C000A)
INET_E_CONNECTION_TIMEOUT (800C000B)
INET_E_INVALID_REQUEST (800C000C)
INET_E_UNKNOWN_PROTOCOL (800C000D)
INET_E_SECURITY_PROBLEM (800C000E)
INET_E_CANNOT_LOAD_DATA (800C000F)
INET_E_CANNOT_INSTANTIATE_OBJECT (800C0010)
INET_E_INVALID_CERTIFICATE (800C0019)
INET_E_REDIRECT_FAILED (800C0014)
INET_E_REDIRECT_TO_DIR (800C0015)
INET_E_CANNOT_LOCK_REQUEST (800C0016)
INET_E_USE_EXTEND_BINDING (800C0017)
INET_E_TERMINATED_BIND (800C0018)
INET_E_ERROR_FIRST (800C0002)
INET_E_CODE_DOWNLOAD_DECLINED (800C0100)
INET_E_RESULT_DISPATCHED (800C0200)
INET_E_CANNOT_REPLACE_SFP_FILE (800C0300)
INET_E_CODE_INSTALL_SUPPRESSED (800C0400)
INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY (800C0500)
Last edited on
I cut and pasted your code and compiled and ran it with no problems. The return code was 0 = S_OK, the message "Successful download", and the .png file turned up in C:\ as expected.

Is there anything, such as a firewall, that might be preventing your app from seeing the Internet?
Last edited on
It seems I have issues! Yes it works, all I needed to do was disable my firewall (which I thought I did originally).
Topic archived. No new replies allowed.