wstring to LPCTSTR
-
Hey all. I had a function that simply returned a LPCTSTR like
LPCTSTR getSomething() { return L"Hello There"; }
Using that inGetClassInfoEx
,CreateWindowEx
, etc worked fine. Though I tried to change it to a wstring like sowstring getSomething() { return L"Hello There"; }
orconst wstring getSomething() { return L"Hello There"; }
both compiled etc, though I always got memory access errors. I am just wondering why this is so, and what I can do to get around it. MainlyWNDCLASSEX.lpszMenuName = getSomething().c_str()
would fall over. Any ideas on how to do this correctlly? I did try casting it as a LPCTSTR as well with no luck. Thanks all -
Hey all. I had a function that simply returned a LPCTSTR like
LPCTSTR getSomething() { return L"Hello There"; }
Using that inGetClassInfoEx
,CreateWindowEx
, etc worked fine. Though I tried to change it to a wstring like sowstring getSomething() { return L"Hello There"; }
orconst wstring getSomething() { return L"Hello There"; }
both compiled etc, though I always got memory access errors. I am just wondering why this is so, and what I can do to get around it. MainlyWNDCLASSEX.lpszMenuName = getSomething().c_str()
would fall over. Any ideas on how to do this correctlly? I did try casting it as a LPCTSTR as well with no luck. Thanks allL"Hello There"; The above line says "Hello There" is wide character string, this means each character takes two bytes internally. LPCTSTR should be pointed only to multibyte character strings (normal string where one character uses one byte internally). So if you want to change a wide char string to multibyte char string you have to use any of the functions like wcstombs/WideCharToMultiByte ...
suhredayan
-- modified at 11:46 Tuesday 15th November, 2005 -
L"Hello There"; The above line says "Hello There" is wide character string, this means each character takes two bytes internally. LPCTSTR should be pointed only to multibyte character strings (normal string where one character uses one byte internally). So if you want to change a wide char string to multibyte char string you have to use any of the functions like wcstombs/WideCharToMultiByte ...
suhredayan
-- modified at 11:46 Tuesday 15th November, 2005 -
but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise
sunit5 wrote:
but LPCTSTR is compatible to both Unicode as well as ANSI
yes, but not the two ones at the same time... if
UNICODE
is not defined,LPCTSTR
equals to aLPCSTR
(which is aconct char*
) and so, writing a wstring in it is a mistake !!
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise
sunit5 wrote:
but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise
Sunit5, You are right. Thanks for correcting.
suhredayan
There is no spoon. -
sunit5 wrote:
but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise
Sunit5, You are right. Thanks for correcting.
suhredayan
There is no spoon.Unicode is defined, so the methods are expecting LPCWSTR not LPCSTR and the LPCTSTR is typedef of LPCWSTR. So going from wstring to LPCTSTR with unicode defined should be fine, right?
-
Hello. This article might help you: http://www.codeproject.com/string/cppstringguide2.asp[^]
Thanks, that looks like it may help. I'll have a read tonight
-
Unicode is defined, so the methods are expecting LPCWSTR not LPCSTR and the LPCTSTR is typedef of LPCWSTR. So going from wstring to LPCTSTR with unicode defined should be fine, right?
WNDCLASSEX.lpszMenuName = getSomething().c_str();
in the above line wstring returned is destroyed as soon as the execution passes to the next line. So the pointer to the wstring is invalid after that. But the WNDCLASSEX unaware of this still try to access this pointer. Also from the function getSomething()use the following syntax to return:return wstring(L"my wstring");
suhredayan
There is no spoon. -
WNDCLASSEX.lpszMenuName = getSomething().c_str();
in the above line wstring returned is destroyed as soon as the execution passes to the next line. So the pointer to the wstring is invalid after that. But the WNDCLASSEX unaware of this still try to access this pointer. Also from the function getSomething()use the following syntax to return:return wstring(L"my wstring");
suhredayan
There is no spoon. -
wstring wstrMenuName=getSomething().c_str();
WNDCLASSEX.lpszMenuName=(LPCTSTR)wstrMenuName;
//remember dont call the destructor of wstrMenuName till the work is not finishedThank you guys that worked great. Thanks all