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. Help Experts...CString to LPCSTR conversion

Help Experts...CString to LPCSTR conversion

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestionannouncement
13 Posts 8 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.
  • R rohit dhamija 0

    Good Morning, I am developing an application in MFC VC++ Version 6.0 for Windows 2000 I need some help. How can we convert CSTring to LPCSTR I have a function in which I neet to pass LPCSTR type agument. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(str)---??? } myfunc(LPCSTR str) { .... .... ... } Waiting for a positive response. Rohit

    M Offline
    M Offline
    Michael Dunn
    wrote on last edited by
    #2

    See The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^] --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Ericahist updated Aug 30!

    1 Reply Last reply
    0
    • R rohit dhamija 0

      Good Morning, I am developing an application in MFC VC++ Version 6.0 for Windows 2000 I need some help. How can we convert CSTring to LPCSTR I have a function in which I neet to pass LPCSTR type agument. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(str)---??? } myfunc(LPCSTR str) { .... .... ... } Waiting for a positive response. Rohit

      B Offline
      B Offline
      BhaskarBora
      wrote on last edited by
      #3

      here's the solution.. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(LPCSTR(str))---??? } myfunc(LPCSTR str) { .... .... ... } ___________________________ When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new."

      R 1 Reply Last reply
      0
      • B BhaskarBora

        here's the solution.. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(LPCSTR(str))---??? } myfunc(LPCSTR str) { .... .... ... } ___________________________ When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new."

        R Offline
        R Offline
        rohit dhamija 0
        wrote on last edited by
        #4

        hi, my exact code is: myprogram() { CString EZBuf; EZBuf.Format("%sEZBuf",HardDiskLetters[iCounter]); //where HardDiskLetters[iCounter] = c:\\ DelTree((LPCSTR)EZBuf); } DelTree(LPCSTR path) { ... } //////////////////////////////// Deltree function is used to remove the directory I tried your method but it didnot worked. On other hand if i pass c:\EZBuf in the function then the program works fine ///////////////////////////////// Can anybody tell where the problem is ?? Rohit

        B 1 Reply Last reply
        0
        • R rohit dhamija 0

          hi, my exact code is: myprogram() { CString EZBuf; EZBuf.Format("%sEZBuf",HardDiskLetters[iCounter]); //where HardDiskLetters[iCounter] = c:\\ DelTree((LPCSTR)EZBuf); } DelTree(LPCSTR path) { ... } //////////////////////////////// Deltree function is used to remove the directory I tried your method but it didnot worked. On other hand if i pass c:\EZBuf in the function then the program works fine ///////////////////////////////// Can anybody tell where the problem is ?? Rohit

          B Offline
          B Offline
          BhaskarBora
          wrote on last edited by
          #5

          check out ur HardDiskLetters.. i sur harddisk letter is "C:" hten u need to add '\\' in ur code EZBuf.Format("%s\\EZBuf",HardDiskLetters[iCounter]); in any case, final string in 'EZBuf' should be "C:\dir_name" try it out.. Bhaskar ___________________________ When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new."

          J R 2 Replies Last reply
          0
          • B BhaskarBora

            check out ur HardDiskLetters.. i sur harddisk letter is "C:" hten u need to add '\\' in ur code EZBuf.Format("%s\\EZBuf",HardDiskLetters[iCounter]); in any case, final string in 'EZBuf' should be "C:\dir_name" try it out.. Bhaskar ___________________________ When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new."

            J Offline
            J Offline
            J Dunlap
            wrote on last edited by
            #6

            BhaskarBora wrote: When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new." LOL! True!

            "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
            "You must be the change you wish to see in the world." - Mahatma Gandhi

            1 Reply Last reply
            0
            • B BhaskarBora

              check out ur HardDiskLetters.. i sur harddisk letter is "C:" hten u need to add '\\' in ur code EZBuf.Format("%s\\EZBuf",HardDiskLetters[iCounter]); in any case, final string in 'EZBuf' should be "C:\dir_name" try it out.. Bhaskar ___________________________ When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new."

              R Offline
              R Offline
              rohit dhamija 0
              wrote on last edited by
              #7

              hi:doh:, I tried both ways but it didnot worked. void myfun() { CString str; str.Format("%sEZBuf",HardDiskLetters[iCounter]); DelTree(str); //doesnot works DelTree((LPCSTR)str); // doesnot works DelTree("c:\\EZBuf"); //Works } DWORD DelTree(LPCSTR pszBase) { SHFILEOPSTRUCT sFileOp; ZeroMemory(&sFileOp, sizeof(SHFILEOPSTRUCT)); sFileOp.wFunc = FO_DELETE; sFileOp.pFrom = pszBase; sFileOp.fFlags = FOF_SILENT|FOF_NOCONFIRMATION|FOF_NOERRORUI; SHFileOperation(&sFileOp) ; return 0; }

              C 1 Reply Last reply
              0
              • R rohit dhamija 0

                hi:doh:, I tried both ways but it didnot worked. void myfun() { CString str; str.Format("%sEZBuf",HardDiskLetters[iCounter]); DelTree(str); //doesnot works DelTree((LPCSTR)str); // doesnot works DelTree("c:\\EZBuf"); //Works } DWORD DelTree(LPCSTR pszBase) { SHFILEOPSTRUCT sFileOp; ZeroMemory(&sFileOp, sizeof(SHFILEOPSTRUCT)); sFileOp.wFunc = FO_DELETE; sFileOp.pFrom = pszBase; sFileOp.fFlags = FOF_SILENT|FOF_NOCONFIRMATION|FOF_NOERRORUI; SHFileOperation(&sFileOp) ; return 0; }

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

                your problem is that - in 'sFileOp.pFrom' the list of names must be double null-terminated!!! with: str.Format("%sEZBuf%c",HardDiskLetters[iCounter],'\0'); your function will work fine. CC.

                R 1 Reply Last reply
                0
                • C chifor

                  your problem is that - in 'sFileOp.pFrom' the list of names must be double null-terminated!!! with: str.Format("%sEZBuf%c",HardDiskLetters[iCounter],'\0'); your function will work fine. CC.

                  R Offline
                  R Offline
                  rohit dhamija 0
                  wrote on last edited by
                  #9

                  Great brain.. Constantin. Thanks a lot. :-D

                  1 Reply Last reply
                  0
                  • R rohit dhamija 0

                    Good Morning, I am developing an application in MFC VC++ Version 6.0 for Windows 2000 I need some help. How can we convert CSTring to LPCSTR I have a function in which I neet to pass LPCSTR type agument. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(str)---??? } myfunc(LPCSTR str) { .... .... ... } Waiting for a positive response. Rohit

                    B Offline
                    B Offline
                    Braulio Dez
                    wrote on last edited by
                    #10

                    Hi, I think just with a casting could be enough ? CString Mystring; myfunc((LPCSTR) Mystring); If not, try this: CString MyString; myfunc(MyString.GetBuffer(0)); MyString.ReleaseBuffer(); HTH Braulio

                    1 Reply Last reply
                    0
                    • R rohit dhamija 0

                      Good Morning, I am developing an application in MFC VC++ Version 6.0 for Windows 2000 I need some help. How can we convert CSTring to LPCSTR I have a function in which I neet to pass LPCSTR type agument. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(str)---??? } myfunc(LPCSTR str) { .... .... ... } Waiting for a positive response. Rohit

                      M Offline
                      M Offline
                      Maverick
                      wrote on last edited by
                      #11

                      try using the Cstring::operator LPCSTR(); i.e. yourfunc(str.operator LPCSTR()); yourfunc(LPCSTR str) { ... ... }

                      1 Reply Last reply
                      0
                      • R rohit dhamija 0

                        Good Morning, I am developing an application in MFC VC++ Version 6.0 for Windows 2000 I need some help. How can we convert CSTring to LPCSTR I have a function in which I neet to pass LPCSTR type agument. myprogram() { CString str; // //how to convert this str to LPCSTR // myfunc(str)---??? } myfunc(LPCSTR str) { .... .... ... } Waiting for a positive response. Rohit

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        It would be nice if your explanation could describe whether this error was a syntax or semantic error. From your description, most people with a good english understanding would have thought that "does not work", implies a syntax compiler error, however, from constantin's solution I can see you actually meant "does not work" in a semantic sense where the function does not operate how you expect it to. It would be great if people could do this in their questions to stop nutters like me barking up the wrong tree, racking my brains for a solution that doesn't exist :mad:. Alan.

                        R 1 Reply Last reply
                        0
                        • L Lost User

                          It would be nice if your explanation could describe whether this error was a syntax or semantic error. From your description, most people with a good english understanding would have thought that "does not work", implies a syntax compiler error, however, from constantin's solution I can see you actually meant "does not work" in a semantic sense where the function does not operate how you expect it to. It would be great if people could do this in their questions to stop nutters like me barking up the wrong tree, racking my brains for a solution that doesn't exist :mad:. Alan.

                          R Offline
                          R Offline
                          rohit dhamija 0
                          wrote on last edited by
                          #13

                          Dear :mad:Alan, Will definately take care from now onwards, Rohit:-D

                          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