Howler72

Hello All,

I have been working on a application that will prompt a user to create a directroy on the file system. If I hard code the parameters the project will work. However when I try to cast a string variable to LPCTSTR I get an error. Here is a snippet of my code:

Code Snippet

string userDirectory;

cout<<"Hello!\n";

cout<<"Please enter the fully qualified path of the directory you would like to create: ";

cin>>userDirectory;

LPCTSTR dir = (LPCTSTR(userDirectory));

Anyone know of a way to convert a string user input variable to a LPCTSTR type

Kind Regards,

O



Re: Visual C++ General Convert string to LPCTSTR

einaros

If you need a LPCTSTR, you should create a generic "tstring" type, which resolves to wstring for UNICODE builds and string for multibyte. If you what you need is really just a LPCSTR (in other words; you're compiling for multibyte only), use the string's .c_str() member function.





Re: Visual C++ General Convert string to LPCTSTR

Giovanni Dicanio

Hi,

you may use the ATL conversion helper, like CA2CT (convert from ANSI to LPCTSTR), like so:

Code Snippet

string userDirectory;

...

// Convert to LPCTSTR

CA2CT dir( userDirectory.c_str() );

// Now you can use "dir"

// whenver a LPCTSTR is required.

Giovanni





Re: Visual C++ General Convert string to LPCTSTR

Giovanni Dicanio

Note that you should #include <atlconv.h> to use the above helper.

G.