Download file from URL (URL as a string)

Hello

First of all I just wanna say that the tutorial on this site to learn the C++ basics is amazing. I have been using it for days now and I love it!

So I tohught I would do a little program that could download some files aswell as run them. I ofcourse googled this first and I found this thread.

http://www.cplusplus.com/forum/beginner/25170/

The code I found was

1
2
3
4
5
6
7
8
9
#include <tchar.h>
#include <urlmon.h> //"urlmon.h: No such file or directory found"
#pragma comment(lib, "urlmon.lib")
int main()
{
	HRESULT hr = URLDownloadToFile ( NULL, _T("your web page"), _T("c:/web_page.html"), 0, NULL );
 //"HRESULT undeclared (first use this function)" and "expected `;' before "hr" 
	return 0;
}


This is what I made, I added a string that I want to be able to edit with cin as I run the program. But for some reason this does not work. Also added the file location as a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>
#include <string>
#include <string.h>
#include <tchar.h>
#include <urlmon.h> //"urlmon.h: No such file or directory found"
#pragma comment(lib, "urlmon.lib")
using namespace std;

int main()
{
	string location = "C:/";
	string url = "test with a url here";
	cout << "downloading Piriform CCleaner...";
	HRESULT hr = URLDownloadToFile ( NULL, _T(url), _T(location), 0, NULL );
	cout << "Done!" << endl;
}


Errors I get

1
2
1>c:\users\thomas\documents\visual studio 2010\projects\dload\dload\main.cpp(13): error C2664: 'URLDownloadToFileA' : cannot convert parameter 2 from 'std::string' to 'LPCSTR'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


Thanks for any help provided.
Last edited on
Bump?
Maybe url.c_str() ?
I agree with Fransje... That bit in the middle looks quite clearly like Windows style programming which is all written in C, not C++...
All you have todo is convert your strings by appending .c_str() to the end of the name (you can do this in the function call).
HRESULT hr = URLDownloadToFile(NULL, _T(url.c_str()), _T(location.c_str()), 0, NULL);
Topic archived. No new replies allowed.