Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this c++ code:

HRESULT hr = URLDownloadToFile(NULL, _T(""), _T("file.txt"), 0, NULL);

but i need to have a different file name each time i open the executable. So i have a string s. In it i store the file name. Now i tried:

What I have tried:

HRESULT hr = URLDownloadToFile(NULL, _T(""), _T(s.c_str()), 0, NULL);

but it gives me following errors:

C2065 E0020 C2660

I dont know how to resolve this issue. could someone please help me and send example code if possible.

Thanks in advance
Updated 31-Jul-21 2:47am
k5054 29-Jul-21 13:51pm    
Error code C2065 refers to an undeclared identifier. Have you declared and initialized s?

_T() is a [compile time] macro which adds the L prefix in UNICODE or nothing in ANSI.

In this case, you will need to know whether you are compiling for UNICODE and use string or wstring/u16string appropriately. Better, use URLDownloadToFileA or URLDownloadToFileW explicitly with the proper string.
Share this answer
To be compatibel to all TCHAR Versions use you can use a String based to this:
typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > tstring;

tstring s(_T("file.txt"));

HRESULT hr = URLDownloadToFile(NULL, _T(""), s.c_str(), 0, NULL);
Share this answer
Joe Woodbury 31-Jul-21 20:22pm    
The stipulation was that the file name not be hard coded; presumably it is user entered in some manner. This solution doesn't solve the conversion issue.
merano99 1-Aug-21 18:11pm    
The Titel ist: "How to use a string instead of a hardcoded value"
The file name is in a variable - in a string, and is therefore very easy to change. It was precisely about the remaining conversion problem, which I believe to have elegantly solved here. I don't think the downvotes are justified.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900