Converting a wstring to LPCTSTR

So apparently, when I try building my program, I get a compile error when I'm building for x64, but not for x86 (and it works flawlessly for x86). I have no idea why the difference exists, but I need to compile my program for the x64 architecture. If it matters at all, I'm using Microsoft Visual Studio 2008 Professional Edition. Here's my issue...

This line doesn't work for me...
1
2
wstring link=L"http://cplusplus.com/img/cpp-logo.png";
LPCTSTR=link.c_str();


And results in the following error:


------ Build started: Project: Help, Configuration: Debug x64 ------
Compiling...
Main.cpp
c:\documents and settings\______\my documents\visual studio 2008\projects\copying\copying\readcats.h(41) : error C2440: 'initializing' : cannot convert from 'const wchar_t *' to 'LPCTSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Documents and Settings\______\My Documents\Visual Studio 2008\Projects\Copying\Copying\x64\Debug\BuildLog.htm"
Help - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I tried using wchar_t* instead, but that didn't fix it. Anybody know a way to avoid this problem or what I need to do to change my code? Thanks!

EDIT: Btw I get the same error with MS VS 2010 Professional.
Last edited on
closed account (1vRz3TCk)
LPCTSTR is a long pointer to a tchar-string

You could try something like:
1
2
std::basic_string<TCHAR> link = TEXT("http://cplusplus.com/img/cpp-logo.png");
LPCTSTR = link.c_str();


The variable name is missing in the second assignment. Since you explicitly use wstring for wide-character strings it makes no sense to use the generic type LPCTSTR. Try LPCWSTR instead.
Last edited on
The variable name is missing in the second assignment. Since you explicitly use wstring for wide-character strings it makes no sense to use the generic type LPCTSTR. Try LPCWSTR instead.


Yeah I missed that. In my program, I do have a variable name, so the problem isn't actually with that.

I found out what the issue was. With the x64 compiler, an LPCSTR (or LPCTSTR) type variable must be used to pass into the function URLDownloadToFile(). With the x86 compiler, an LPCWSTR (or LPCTSTR) type variable must be used for that function instead.

The pseudo conclusion I can come to is this then... While I was able to assign the values of a wstring to an LPCTSTR variable (to pass for the URLDownloadToFile() function) in the x86 compiler, the x64 compiler wouldn't accept the Wide formatting in that variable. Instead it had to use an LPCSTR variable with a regular string's formatted version.

Thanks goes to gpotw for setting me on the right track to figure this out (when I compiled using an LPCWSTR variable instead, it gave a compiler error saying it needed an LPCSTR variable and so I connected the rest of the dots).
closed account (1vRz3TCk)
The pseudo conclusion I can come to is this then... While I was able to assign the values of a wstring to an LPCTSTR variable (to pass for the URLDownloadToFile() function) in the x86 compiler, the x64 compiler wouldn't accept the Wide formatting in that variable. Instead it had to use an LPCSTR variable with a regular string's formatted version.

That would suggest that you have one build set to use UNICODE and the other as ANSI.
Topic archived. No new replies allowed.