Problem with URLDownloadToFile()

I have looked online and found many threads started by people with this same problem, but none of the solutions on those threads have worked for me or the problem was never resolved.

The following code works fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
#include <tchar.h>
#include <urlmon.h>
#include <iostream>
#include <fstream>
#include <string>

#pragma comment(lib, "urlmon.lib")

int main()
{
    std::string line;
    HRESULT hr;
    hr = URLDownloadToFile(NULL,_T("http://website_name.com/"),_T("temp.txt"),0,NULL);
    return 0;
}


This does not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <tchar.h>
#include <urlmon.h>
#include <iostream>
#include <fstream>
#include <string>

#pragma comment(lib, "urlmon.lib")

int main()
{
    std::string line;
    std::cout<<"Enter website URL: ";
    getline(std::cin,line);
    HRESULT hr;
    hr = URLDownloadToFile(NULL,(LPCWSTR)line.c_str(),_T("temp.txt"),0,NULL);
    return 0;
}


It compiles correctly, but when I type_cast line as LPCWSTR, it turns to junk. I've tried using LPCSTR and LPCTSTR but neither work. For instance, I get the following error message when I use LPCSTR in place of LPCWSTR:

cannot convert parameter 2 from 'LPCSTR' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Or, when I keep it as LPCWSTR and try to declare line as type std::wstring, I get a bunch of error messages concerning getline:

see declaration of 'std::getline'
1>          could be 'wchar_t'
1>          or       'char'


I have no clue what to do anymore. Please help. Thank you.

EDIT: Nevermind. Got it to work. Had to use std::wcin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <tchar.h>
#include <urlmon.h>
#include <iostream>
#include <fstream>
#include <string>

#pragma comment(lib, "urlmon.lib")

int main()
{
    std::wstring line;
    std::cout<<"Enter website URL: ";
    getline(std::wcin,line);
    HRESULT hr;
    hr = URLDownloadToFile(NULL,(LPCWSTR)line.c_str(),_T("temp.txt"),0,NULL);
    return 0;
} 
Last edited on
Topic archived. No new replies allowed.