Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Convert LPTSTR to string class in VC++

How to Convert LPTSTR to string class in VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jahnson K
    wrote on last edited by
    #1

    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

    M A 2 Replies Last reply
    0
    • J Jahnson K

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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."

      A 1 Reply Last reply
      0
      • J Jahnson K

        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

        A Offline
        A Offline
        awah
        wrote on last edited by
        #3

        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, type MyString = "this is my string" if you want to pass your CString into a LPTSTR field, use MyString.GetBuffer() switch to CString today!

        1 Reply Last reply
        0
        • M Mark Salsbery

          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."

          A Offline
          A Offline
          Arman S
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • A Arman S

            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

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Thanks for the info :) Mark

            "Go that way, really fast. If something gets in your way, turn."

            J 1 Reply Last reply
            0
            • M Mark Salsbery

              Thanks for the info :) Mark

              "Go that way, really fast. If something gets in your way, turn."

              J Offline
              J Offline
              Jahnson K
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • J Jahnson K

                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

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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."

                J 1 Reply Last reply
                0
                • M Mark Salsbery

                  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."

                  J Offline
                  J Offline
                  Jahnson K
                  wrote on last edited by
                  #8

                  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

                  M 1 Reply Last reply
                  0
                  • J Jahnson K

                    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

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups