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 a string from CString to LPTSTR

How to convert a string from CString to LPTSTR

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
12 Posts 6 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.
  • C Offline
    C Offline
    cauvang
    wrote on last edited by
    #1

    I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot

    R T R 3 Replies Last reply
    0
    • C cauvang

      I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      In case you can do with a LPCTSTR (which in most cases is safer), you can easily convert a CString to a LPCTSTR, the CString class provides an operator CString::operator LPCTSTR to achieve that.

      CString MyCString;
      LPCTSTR mystring=MyCString.operator LPCTSTR();

      To force the conversion to a non constant string, use the GetBuffer and ReleaseBuffer functions. ~RaGE();

      C 1 Reply Last reply
      0
      • C cauvang

        I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        CString already provides such an operator...

        CString str = "Hello you";
        char* pszTxt = (LPTSTR)str;


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        C 1 Reply Last reply
        0
        • T toxcct

          CString already provides such an operator...

          CString str = "Hello you";
          char* pszTxt = (LPTSTR)str;


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          C Offline
          C Offline
          cauvang
          wrote on last edited by
          #4

          Thanks for your answering, but when I try doing the following code LPTSTR CMainDlg::XuLyChuoi() { char *tmp_sz = new char[1000]; ::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000); CString s = tmp_sz; int index = 1; while ((index=s.Find("x", index)) != -1) { CString s1 = s[index-1]; s1+= s[index]; CString s2 = s[index-1]; s2+= "*"; s2+= s[index]; s.Replace(s1, s2); } tmp_sz = (LPTSTR)s; return tmp_sz; } I have the error: error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. - 1 error(s), 0 warning(s) Can you explain?Thank you.

          T I 2 Replies Last reply
          0
          • C cauvang

            Thanks for your answering, but when I try doing the following code LPTSTR CMainDlg::XuLyChuoi() { char *tmp_sz = new char[1000]; ::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000); CString s = tmp_sz; int index = 1; while ((index=s.Find("x", index)) != -1) { CString s1 = s[index-1]; s1+= s[index]; CString s2 = s[index-1]; s2+= "*"; s2+= s[index]; s.Replace(s1, s2); } tmp_sz = (LPTSTR)s; return tmp_sz; } I have the error: error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. - 1 error(s), 0 warning(s) Can you explain?Thank you.

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            put aroud your literal string some _T() because you seem to use UNICODE... and also declare tmp_sz as a TCHAR*, not a char*


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            C 1 Reply Last reply
            0
            • T toxcct

              put aroud your literal string some _T() because you seem to use UNICODE... and also declare tmp_sz as a TCHAR*, not a char*


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              C Offline
              C Offline
              cauvang
              wrote on last edited by
              #6

              thanks again, but it still give me that error.

              T 1 Reply Last reply
              0
              • C cauvang

                Thanks for your answering, but when I try doing the following code LPTSTR CMainDlg::XuLyChuoi() { char *tmp_sz = new char[1000]; ::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000); CString s = tmp_sz; int index = 1; while ((index=s.Find("x", index)) != -1) { CString s1 = s[index-1]; s1+= s[index]; CString s2 = s[index-1]; s2+= "*"; s2+= s[index]; s.Replace(s1, s2); } tmp_sz = (LPTSTR)s; return tmp_sz; } I have the error: error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. - 1 error(s), 0 warning(s) Can you explain?Thank you.

                I Offline
                I Offline
                icabod
                wrote on last edited by
                #7

                Not relating to the use of strings or conversion etc, but it looks to me like you have a chance of memory leakage here. You allocate memory for the string on the heap, then copy this into a CString. Later on you change the pointer to point to the LPTSTR of the CString (I think it should maybe be LPCTSTR, as casting a CString will probably only be safe if cast to a const - which may explain the error you're getting). This means that you lose the memory you allocated at the start of the function, as nothing's pointing to it any more. Also, you're returning a pointer to something which is about to be deleted (the CString will be deleted when you exit the function). I think what you actually should be doing (tho' feel free to correct me) is to copy the contents of the CString into the address pointed to by tmp_sz, and then return the tmp_sz pointer. Just remember that you need to later on delete the pointer that you're returning. So your last bit of code would do something like:

                strcpy(tmp_sz, (LPCTSTR)s, s.GetLength());
                return tmp_sz;

                After making sure that the length of CString s is not > 1000 (including the NULL terminator). Does that make sense? I'm speed-typing coz it's lunchtime and I'm hungry.

                T 1 Reply Last reply
                0
                • R Rage

                  In case you can do with a LPCTSTR (which in most cases is safer), you can easily convert a CString to a LPCTSTR, the CString class provides an operator CString::operator LPCTSTR to achieve that.

                  CString MyCString;
                  LPCTSTR mystring=MyCString.operator LPCTSTR();

                  To force the conversion to a non constant string, use the GetBuffer and ReleaseBuffer functions. ~RaGE();

                  C Offline
                  C Offline
                  cauvang
                  wrote on last edited by
                  #8

                  Thanks for your helping me. But I don't want to return a LPTSTR, not a LPCTSTR.Anyway, thanks you a lot.

                  1 Reply Last reply
                  0
                  • C cauvang

                    thanks again, but it still give me that error.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    LPTSTR CMainDlg::XuLyChuoi() {
                    TCHAR* tmp_sz = new TCHAR[1000];
                    ::GetWindowText(GetDlgItem(IDC_EDIT1), tmp_sz, 1000*sizeof(TCHAR));
                    CString s = tmp_sz;
                    int index = 1;
                    while ((index = s.Find(_T("x"), index)) != -1) {
                    CString s1 = s[index-1];
                    s1 += s[index];
                    CString s2 = s[index-1];
                    s2 += _T("*");
                    s2 += s[index];
                    s.Replace(s1, s2);
                    }
                    ::_tcscpy(tmp_sz, (LPTSTR)s); // Notice that you cannot affect a C-style string with the operator = !!!!

                    return tmp\_sz;
                    

                    }


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    1 Reply Last reply
                    0
                    • C cauvang

                      I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot

                      R Offline
                      R Offline
                      Roger Stoltz
                      wrote on last edited by
                      #10

                      cauvang wrote: convert a string from CString(MFC) to LPTSTR Are you certain that your CString is an MFC CString? I ask this because in the more detailed answer your gave toxcct here[^] looks like your using a CString defined in a namespace called 'WTL' which could indicate that it's all about the WTL version of CString you are using. I don't know anything about WTL so I won't be able to guide you there. Check this out. If you're not using the MFC version our answers won't make sense. Perhaps you even should post your question in the WTL forum. -- Roger

                      C 1 Reply Last reply
                      0
                      • I icabod

                        Not relating to the use of strings or conversion etc, but it looks to me like you have a chance of memory leakage here. You allocate memory for the string on the heap, then copy this into a CString. Later on you change the pointer to point to the LPTSTR of the CString (I think it should maybe be LPCTSTR, as casting a CString will probably only be safe if cast to a const - which may explain the error you're getting). This means that you lose the memory you allocated at the start of the function, as nothing's pointing to it any more. Also, you're returning a pointer to something which is about to be deleted (the CString will be deleted when you exit the function). I think what you actually should be doing (tho' feel free to correct me) is to copy the contents of the CString into the address pointed to by tmp_sz, and then return the tmp_sz pointer. Just remember that you need to later on delete the pointer that you're returning. So your last bit of code would do something like:

                        strcpy(tmp_sz, (LPCTSTR)s, s.GetLength());
                        return tmp_sz;

                        After making sure that the length of CString s is not > 1000 (including the NULL terminator). Does that make sense? I'm speed-typing coz it's lunchtime and I'm hungry.

                        T Offline
                        T Offline
                        Tim Smith
                        wrote on last edited by
                        #11

                        Doing the following would work. strncpy (tmp_sz, s, 1000); tmp_sz [999] = 0; // strncpy doesn't place a null on a full buffer return tmp_sz; Tim Smith I'm going to patent thought. I have yet to see any prior art.

                        1 Reply Last reply
                        0
                        • R Roger Stoltz

                          cauvang wrote: convert a string from CString(MFC) to LPTSTR Are you certain that your CString is an MFC CString? I ask this because in the more detailed answer your gave toxcct here[^] looks like your using a CString defined in a namespace called 'WTL' which could indicate that it's all about the WTL version of CString you are using. I don't know anything about WTL so I won't be able to guide you there. Check this out. If you're not using the MFC version our answers won't make sense. Perhaps you even should post your question in the WTL forum. -- Roger

                          C Offline
                          C Offline
                          cauvang
                          wrote on last edited by
                          #12

                          I'm really sorry! I want to convert a string from CString(MFC) to LPCTSTR but the example is really stupid( it was written in WTL !!!). Now I wonder how to use a WTL class ( written in WTL,used WTL CString ) in a MFC application. The class is so important and i need it in my MFC project.However,it was written in WTL using WTL CString.Help me!!

                          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