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. CString to LPCTSTR

CString to LPCTSTR

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
8 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.
  • J Offline
    J Offline
    jerome_data
    wrote on last edited by
    #1

    How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS

    C T J 3 Replies Last reply
    0
    • J jerome_data

      How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      GetBuffer and ReleaseBuffer. Christian Graus - Microsoft MVP - C++

      J 1 Reply Last reply
      0
      • C Christian Graus

        GetBuffer and ReleaseBuffer. Christian Graus - Microsoft MVP - C++

        J Offline
        J Offline
        jerome_data
        wrote on last edited by
        #3

        Sorry but Getbuffer doesn't works with pFromFolder SHFileOperation return a error. WORK CopyFolder("C:\\directoy\\sub","D:\\directoy\\sub"); DON'T WORK CopyFolder(str.Getbuffer(size),"D:\\directoy\\sub"); CopyFolder((LPCTSTR)str,D:\\directoy\\sub"); BOOL CDialog::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); }

        1 Reply Last reply
        0
        • J jerome_data

          How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS

          T Offline
          T Offline
          Tom Archer
          wrote on last edited by
          #4

          http://www.codeproject.com/string/cppstringguide2.asp?df=100&forumid=11477&exp=0&select=971864[^] Tom Archer - Visual C++ MVP Archer Consulting Group.com

          J 1 Reply Last reply
          0
          • T Tom Archer

            http://www.codeproject.com/string/cppstringguide2.asp?df=100&forumid=11477&exp=0&select=971864[^] Tom Archer - Visual C++ MVP Archer Consulting Group.com

            J Offline
            J Offline
            jerome_data
            wrote on last edited by
            #5

            Yes i'll read this article but the conversion is good in DEBUG mode but the function (SHFileOperation) always returns an error!! Strange Thanks for help

            1 Reply Last reply
            0
            • J jerome_data

              How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS

              J Offline
              J Offline
              Jose Lamas Rios
              wrote on last edited by
              #6

              jerome_data wrote: How to convert CString to LPCTSTR. CString already converts automatically to LPCTSTR (through a cast operator). You don't need to do anything special. jerome_data wrote: But pFromFolder don't works with type cast (LPCTSTR)CString What exactly do you mean? What's your code and what's the error? The following fragment should compile and run as expected:

              CString sFromFolder = "C:\\directory\\subdirectory";
                 CString sToFolder = "C:\\directory\\OtherSubdirectory";
                 if (!CDialogMaintenance::CopyFolder(sFromFolder, sToFolder))
                 {
                    // handle error;
                 }

              [UPDATE] If that doesn't work, maybe you have wrong definitions for Unicode/MBCS? You should have one of the following combinations: a) Unicode project: UNICODE defined, _UNICODE defined, _MBCS not defined b) MBCS project: UNICODE not defined, _UNICODE not defined, _MBCS defined c) No setting: UNICODE not defined, _UNICODE not defined, _MBCS not defined Any other combination may cause problems. [END UPDATE] That being said, the documentation for SHFILEOPSTRUCT[^] says the following about both pFrom and pTo: "Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of [pFrom or pTo]" I suggest you modify CopyFolder to something like this:

              static LPCTSTR GetBufferWithExtraNull(CString s)
              {
                 int len = s.GetLength();
               
                 // make sure the buffer has space for an additional char
                 LPCTSTR p = s.GetBuffer(len+1+1);
               
                 // append the required additional NULL
                 p[len+1] = '\0'
               
                 // note there's no ReleaseBuffer()
                 return p;
              }
               
              BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder)
              {
              &nbs

              J 1 Reply Last reply
              0
              • J Jose Lamas Rios

                jerome_data wrote: How to convert CString to LPCTSTR. CString already converts automatically to LPCTSTR (through a cast operator). You don't need to do anything special. jerome_data wrote: But pFromFolder don't works with type cast (LPCTSTR)CString What exactly do you mean? What's your code and what's the error? The following fragment should compile and run as expected:

                CString sFromFolder = "C:\\directory\\subdirectory";
                   CString sToFolder = "C:\\directory\\OtherSubdirectory";
                   if (!CDialogMaintenance::CopyFolder(sFromFolder, sToFolder))
                   {
                      // handle error;
                   }

                [UPDATE] If that doesn't work, maybe you have wrong definitions for Unicode/MBCS? You should have one of the following combinations: a) Unicode project: UNICODE defined, _UNICODE defined, _MBCS not defined b) MBCS project: UNICODE not defined, _UNICODE not defined, _MBCS defined c) No setting: UNICODE not defined, _UNICODE not defined, _MBCS not defined Any other combination may cause problems. [END UPDATE] That being said, the documentation for SHFILEOPSTRUCT[^] says the following about both pFrom and pTo: "Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of [pFrom or pTo]" I suggest you modify CopyFolder to something like this:

                static LPCTSTR GetBufferWithExtraNull(CString s)
                {
                   int len = s.GetLength();
                 
                   // make sure the buffer has space for an additional char
                   LPCTSTR p = s.GetBuffer(len+1+1);
                 
                   // append the required additional NULL
                   p[len+1] = '\0'
                 
                   // note there's no ReleaseBuffer()
                   return p;
                }
                 
                BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder)
                {
                &nbs

                J Offline
                J Offline
                John M Drescher
                wrote on last edited by
                #7

                Jose Lamas Rios wrote: An additional NULL character must be appended to the end of the final name I know for a fact that this is the problem. Without double terminating the string unpredictable results will occur... John

                D 1 Reply Last reply
                0
                • J John M Drescher

                  Jose Lamas Rios wrote: An additional NULL character must be appended to the end of the final name I know for a fact that this is the problem. Without double terminating the string unpredictable results will occur... John

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  John M. Drescher wrote: I know for a fact that this is the problem. Without double terminating the string unpredictable results will occur... I've been bit by this more than once.


                  "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                  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