locked
UrlDownloadToFile with variable RRS feed

  • Question

  • Hello,

    I am trying to download a file from URL with the code:

    #include "stdafx.h"

    #include <ctime>
    #include <iostream>
    #include <Windows.h>
    #include <WinInet.h>
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>
    #include <sstream>
    #include <stdio.h>
    #include <string.h>
    #include <shellapi.h>
    #include <cstring>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <process.h>
    #include <conio.h>
    #include <cmath>
    #include <time.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <urlmon.h>
    #include <cstdlib>


    #include "download.h"
    #include "curl\include\curl\curl.h"


    #pragma comment ( lib, "Wininet.lib" )
    #pragma comment(lib,"shell32.lib")
    #pragma comment(lib, "ws2_32")
    #pragma comment(lib, "urlmon.lib")
    //#pragma comment(lib, "libwininet.a")

    int main() {

    string downloading;

    string save_as;

    cout << "Url File to download: ";

    cin >> downloading;

    cout << endl << "Save as: ";

    cin >> save_as;

    DeleteUrlCacheEntry(downloading); URLDownloadToFile(NULL, downloading, save_as, 0, NULL);

    return 0;

    }

    Well, I know the problem. I just can use LPCWSTR

    But I dont know how to fix the problem

    How can I convert a string to a LPCWSTR?

    I often tried to convert string to LPCWSTR but I always get strange codes with numbers and letters: for example 4000x65

    (example not 1:1)

    Thanks in advance

    PS:

    Please no answeres like that:

    DeleteUrlCacheEntry(L"http//example.com");
    URLDownloadToFile(NULL, L"http//example.com", L"file.txt", 0, NULL);



    Tuesday, January 20, 2015 8:10 PM

Answers

  • You can use MultiByteToWideChar() or you can use something like

    std::wstring stemp = std::wstring(s.begin(), s.end());
    LPCWSTR sw = stemp.c_str();

    Thanks


    Rupesh Shukla


    • Edited by Pintu Shukla Tuesday, January 20, 2015 8:43 PM
    • Marked as answer by Kevin Summer Thursday, January 22, 2015 4:22 PM
    Tuesday, January 20, 2015 8:42 PM

All replies

  • You can use MultiByteToWideChar() or you can use something like

    std::wstring stemp = std::wstring(s.begin(), s.end());
    LPCWSTR sw = stemp.c_str();

    Thanks


    Rupesh Shukla


    • Edited by Pintu Shukla Tuesday, January 20, 2015 8:43 PM
    • Marked as answer by Kevin Summer Thursday, January 22, 2015 4:22 PM
    Tuesday, January 20, 2015 8:42 PM
  • In case of Unicode configuration, use wstring, wcout, wcin and L”...”. The .c_str() function for wstring will return LPCWSTR.

    If you do not need Unicode, then change the project properties or just use DeleteUrlCacheEntryA and URLDownloadToFileA.


    • Edited by Viorel_MVP Wednesday, January 21, 2015 9:09 AM
    Wednesday, January 21, 2015 9:06 AM
  • Thanks for the fast help guys!

    @Pintu Shukla:

    Thanks it worked!

    Thursday, January 22, 2015 4:22 PM