how to convert _T() to string and vice versa

Oct 1, 2010 at 3:34pm
Hi,
I am trying to open a url when users click on the window.

ShellExecute(NULL,_T("open"),_T("http://www.google.com"),NULL,NULL, SW_SHOWNORMAL);

this works perfectly fine....
but what i am trying to do is, that i want to take input from the user and then append this input to the url...
basically

string fileName = "";
cout<<"enter file name: ";
cin>>fileName;
then i want to to append this file name to the url.
so i have tried this but not working.
string url = "http://www.google.com";
url = url + fileName;
and i have passed this to the above ShellExecute function.
ShellExecute(NULL,_T("open"),url,NULL,NULL, SW_SHOWNORMAL);

Can any one tell me why? and can any one tell me how to append values or convert string to _TCHAR * variable.
i know the url is suppose to be a _TCHAR type variable but i do not know how to convert it or append a string to it.

Thanking in anticipation.
Regards,
Hajidon
Oct 1, 2010 at 3:46pm
To convert std::string to char*, use http://www.cplusplus.com/reference/string/string/c_str/
Oct 1, 2010 at 3:55pm
This is different from char * this is _TCHAR * which take unicode.
while char is takes ascii.
i do not think it will work.....
Oct 1, 2010 at 4:03pm
There's std::wstring for that.
Last edited on Oct 1, 2010 at 4:04pm
Oct 1, 2010 at 5:30pm
"open" is a (const) char*. _T is there to make it unicode (if I understand correctly..). So _T(url.c_str()) should work.
Oct 1, 2010 at 6:55pm
I don't think sticking _T in front of a char* does anything.
_T is there for string literals (but I'm going to check)

ShellExecute is one of thoese windows functions that maps to a UNICODE version ShellExecuteW or an standard ascii version ShellExecuteA of the function
depending on wheter _UNICODE macro is defined.

If you are stuck with char* then use ShellExecuteA
ShellExecuteA(NULL, "open",url.c_str() ,NULL,NULL, SW_SHOWNORMAL);
Last edited on Oct 1, 2010 at 6:56pm
Oct 1, 2010 at 7:04pm
_T (and, preferably, TEXT) adds an L in front of a string literal, and only a string literal, to make it a wide string #ifdef UNICODE. Otherwise, it does nothing.
Topic archived. No new replies allowed.