Thread: URLDownloadToFile string

  1. #1
    george7378
    Guest

    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.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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 *.

  3. #3
    george7378
    Guest
    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.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  5. #5
    george7378
    Guest
    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.

  6. #6
    george7378
    Guest
    ...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.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    *Edited*

    Code:
    std::string final = url + atof(buf);
    atof converts string to floating-point value.

    Code:
    std::string final = url + buf;
    Last edited by kmdv; 04-16-2011 at 08:04 AM.

  8. #8
    george7378
    Guest
    Thanks - I tried that too, but now I get:

    Code:
    error C2110: '+' : cannot add two pointers

  9. #9
    george7378
    Guest
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URLDownloadToFile<A/W>() does nothing
    By abraham2119 in forum C Programming
    Replies: 2
    Last Post: 05-05-2009, 05:56 PM
  2. URLDownloadToFile() - urlmon.a in gcc
    By abraham2119 in forum C Programming
    Replies: 1
    Last Post: 05-03-2009, 10:39 PM
  3. URLDownloadToFile dont work in worker thread
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2007, 12:49 AM
  4. URLDownloadToFile
    By Echidna in forum Windows Programming
    Replies: 0
    Last Post: 04-04-2002, 09:47 PM
  5. Need Help with the URLDownloadToFile function
    By pinkcheese in forum Windows Programming
    Replies: 6
    Last Post: 03-29-2002, 09:15 PM