-
URLDownloadToFile string
I am trying to use URLDownloadToFile to download from a given URL. I am using the following function:
URLDownloadToFile Function ()
Here is my code:
Code:
#include <urlmon.h>
#include <iostream>
#pragma comment(lib, "urlmon.lib")
#include <stdio.h>
#include <string>
#include <windows.h>
using namespace std;
int main (){
string url = "http://www.google.com/";
URLDownloadToFile( NULL, url, "index.html", 0, NULL );
return 0;
}
I get the following:
Code:
error C2664: 'URLDownloadToFileA' : cannot convert parameter 2 from 'std::string' to 'LPCSTR'
This seems like a simple error - what do I need to do to correct it?
Thanks.
-
URLDownloadToFile takes a const char * as its second argument, not a string. That what that error is telling you. Lucky for you the std::string class has the c_str() function to convert it to a const char *.
-
Thanks - that worked a treat. However, it all seems to break down when I try to use strings in Win32 programs. Is there a tutorial on how I could go about doing this? For example, if I have this code:
Code:
#include <windows.h>
#include <cstdio>
#include "resource.h"
#include <urlmon.h>
#include <iostream>
#pragma comment(lib, "urlmon.lib")
#include <string>
#include <cstring>
#include <stdio.h>
string url = "http://";
BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_DOWNLOAD: //Pressing download button.
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_URL)); //Extracting the rest of the web address from a text box.
char* buf;
buf = (char*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(hwnd, IDC_URL, buf, len + 1); //Assigning the URL to 'buf'.
string final = url + atof(buf); //Adding the rest of the web address to 'http://'
const char *finalurl = final.c_str ( ); //Converting to c_str();
URLDownloadToFile( NULL, finalurl, "index.html", 0, NULL ); //Downloading it.
ShellExecute(NULL, "open", "index.html", NULL, NULL, SW_SHOWNORMAL); //Opening it.
GlobalFree(buf);
break;
}
case IDC_CLOSE:
EndDialog(hwnd, 0);
break;
}
break;
case WM_CLOSE:
EndDialog(hwnd, 0);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DOWNLOADER), NULL, ToolDlgProc);
}
When I try to compile this, I get errors such as error C2784, error C2782 and error C2676. It looks like I am doing something very wrong (I am guessing that you need to use a different syntax for strings in Win32).
Thanks.
-
The Win32 API is written in C, not C++. Therefore, if you're going to use std::string, you will need to convert to C strings (through c_str()) to call functions requiring char *s. Don't give Visual Studio compiler codes only...we don't have them memorized! Give the entire error along with the appropriate lines.
-
Thanks for the response - sorry about the errors - each one was about 5 lines long and just looked like gibberish to me!
So I basically need to convert each string to a c_string before using it, eg:
Code:
std::string url = "http://";
const char *url2 = url.c_str ( );
...and then use:
Code:
std::string final = url2 + atof(buf);
const char *final2 = final.c_str ( );
...and it should work? Thanks.
-
...that seems to have got rid of most of the errors, but now I get this error:
Code:
error C2111: '+' : pointer addition requires integral operand
This is referring to this line:
Code:
string final = url + atof(buf);
Once again - I guess I am just doing something wrong that doesn't work in Win32.
-
*Edited*
Code:
std::string final = url + atof(buf);
atof converts string to floating-point value.
Code:
std::string final = url + buf;
-
Thanks - I tried that too, but now I get:
Code:
error C2110: '+' : cannot add two pointers
-
Never mind - I changed it to this:
Code:
std::string final;
final += "http://";
final += buf;
const char *finalurl = final.c_str ( );
...and it seems to work now.