How to Convert LPTSTR to string class in VC++
-
Hi All, Can any one tell me how to convert LPRSTR data type to string date type in VC++. I am doing in the following way. LPTSTR testLpt; // this I am getting from some method string testStr = string(testLpt); I am getting type cast error. Can any tell me how to do this? Thanks in advance. Regards, JK
-
Hi All, Can any one tell me how to convert LPRSTR data type to string date type in VC++. I am doing in the following way. LPTSTR testLpt; // this I am getting from some method string testStr = string(testLpt); I am getting type cast error. Can any tell me how to do this? Thanks in advance. Regards, JK
Sounds like your LPTSTR is Unicode (wchar_t). Try a wstring instead. You also don't need to construct a temporary string and copy it to another string. You can construct it in one step: LPTSTR testLpt; // this I am getting from some method wstring testStr(testLpt);
"Go that way, really fast. If something gets in your way, turn."
-
Hi All, Can any one tell me how to convert LPRSTR data type to string date type in VC++. I am doing in the following way. LPTSTR testLpt; // this I am getting from some method string testStr = string(testLpt); I am getting type cast error. Can any tell me how to do this? Thanks in advance. Regards, JK
i am very bad in strings and LPTSTR all that crap so i switched to using the class CString simply type
CString MyString;
if you want to put something into your Cstring, typeMyString = "this is my string"
if you want to pass your CString into a LPTSTR field, use MyString.GetBuffer() switch to CString today! -
Sounds like your LPTSTR is Unicode (wchar_t). Try a wstring instead. You also don't need to construct a temporary string and copy it to another string. You can construct it in one step: LPTSTR testLpt; // this I am getting from some method wstring testStr(testLpt);
"Go that way, really fast. If something gets in your way, turn."
LPTSTR can be either Unicode or not depending on whether UNICODE is defined. Using std::wstring is ok if you are sure LPTSTR is always unicode. But in this case it would be more correct to have LPWSTR. What to do in current case, is to use std::basic_string<TCHAR>: typedef std::basic_string<TCHAR> tstring; Now, conversions between LPTSTR and tstring will work regardless of being unicode or not.
-- ===== Arman
-
LPTSTR can be either Unicode or not depending on whether UNICODE is defined. Using std::wstring is ok if you are sure LPTSTR is always unicode. But in this case it would be more correct to have LPWSTR. What to do in current case, is to use std::basic_string<TCHAR>: typedef std::basic_string<TCHAR> tstring; Now, conversions between LPTSTR and tstring will work regardless of being unicode or not.
-- ===== Arman
Thanks for the info :) Mark
"Go that way, really fast. If something gets in your way, turn."
-
Thanks for the info :) Mark
"Go that way, really fast. If something gets in your way, turn."
Hi Arman and Mark, Thanks a lot. Its working fine with wstring for me.There are no compilation errors now. But I am having one more problem. I am using text resource file in my VC++ workspace. I have included *.rc and *.h file. but I am not able to get the handle to resource file. Following is the sample code. TCHAR sResName[5] = _T("#103"); TCHAR sRestype[12] = _T("MY_RESOURCE"); HRSRC hres = FindResource(NULL, sResName, sRestype); HGLOBAL hbytes = LoadResource(NULL, hres); LPVOID pdata = LockResource(hbytes); LPBYTE sData = (LPBYTE)pdata; LPTSTR sXml = (LPTSTR)sData; wstring strXml(sXml); In the above code i am not getting hres pointer. The address of hres is null. Do u have any idea why I am not getting the pointer to resource file using FindResource(). I have included the resource file in the header files folder. Is there any better way of doing the same? Thanks in advance JK
-
Hi Arman and Mark, Thanks a lot. Its working fine with wstring for me.There are no compilation errors now. But I am having one more problem. I am using text resource file in my VC++ workspace. I have included *.rc and *.h file. but I am not able to get the handle to resource file. Following is the sample code. TCHAR sResName[5] = _T("#103"); TCHAR sRestype[12] = _T("MY_RESOURCE"); HRSRC hres = FindResource(NULL, sResName, sRestype); HGLOBAL hbytes = LoadResource(NULL, hres); LPVOID pdata = LockResource(hbytes); LPBYTE sData = (LPBYTE)pdata; LPTSTR sXml = (LPTSTR)sData; wstring strXml(sXml); In the above code i am not getting hres pointer. The address of hres is null. Do u have any idea why I am not getting the pointer to resource file using FindResource(). I have included the resource file in the header files folder. Is there any better way of doing the same? Thanks in advance JK
2 reasons I see for FindResource to fail - 1) A resource of type "MY_RESOURCE" with an integer ID of 103 isn't in the exe's resources. 2) The resource isn't compiled/linked into the exe file (since you've used NULL for hModule param) Mark
"Go that way, really fast. If something gets in your way, turn."
-
2 reasons I see for FindResource to fail - 1) A resource of type "MY_RESOURCE" with an integer ID of 103 isn't in the exe's resources. 2) The resource isn't compiled/linked into the exe file (since you've used NULL for hModule param) Mark
"Go that way, really fast. If something gets in your way, turn."
Hi Mark, Thanks for the reply. I have checked the ID number its correct. And I have included resource file in the workspace and I have included resource header in my *.cpp. I could complie successfully. When I debug and see the return type of findresource() it is NULL. Do u any idea how to fix this. Thanks in advance. JK
-
Hi Mark, Thanks for the reply. I have checked the ID number its correct. And I have included resource file in the workspace and I have included resource header in my *.cpp. I could complie successfully. When I debug and see the return type of findresource() it is NULL. Do u any idea how to fix this. Thanks in advance. JK
It looks like it should work. Without seeing the resource script I can't imagine. You could try AfxGetInstanceHandle() instead of NULL for the module handle. Also make sure the type is a string and not a macro for an integer. That's all I can think of off hand... Mark
This episode brought to you by the number 5