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 Stupidity

CString Stupidity

Scheduled Pinned Locked Moved C / C++ / MFC
debugging
12 Posts 7 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
    carrie
    wrote on last edited by
    #1

    I'm tearing my hair out here through frustration because I know its probably the stupidest of problems but whatever way I try I can't get it fixed. Basically I've taken a CString from the CFileDialogs GetPathName function, I'm needing to turn that into a char*, or more exact a PSTR but I think they're the same, but I keep getting Access Violations whichever way I try this. Just now my code is char* szPath = new char[pathToExe.GetLength()+1]; strcpy(szPath,(LPCTSTR)pathToExe); TRACE("szPath = %s\n",szPath); I've also tried using the CString.GetBuffer, without the (LPCTSTR), but no luck there either, how the damnation do I do this :) Please save my sanity and not laugh too hard at the newbie :) Thanks fellas carrie

    M C E D 4 Replies Last reply
    0
    • C carrie

      I'm tearing my hair out here through frustration because I know its probably the stupidest of problems but whatever way I try I can't get it fixed. Basically I've taken a CString from the CFileDialogs GetPathName function, I'm needing to turn that into a char*, or more exact a PSTR but I think they're the same, but I keep getting Access Violations whichever way I try this. Just now my code is char* szPath = new char[pathToExe.GetLength()+1]; strcpy(szPath,(LPCTSTR)pathToExe); TRACE("szPath = %s\n",szPath); I've also tried using the CString.GetBuffer, without the (LPCTSTR), but no luck there either, how the damnation do I do this :) Please save my sanity and not laugh too hard at the newbie :) Thanks fellas carrie

      M Offline
      M Offline
      Mustafa Demirhan
      wrote on last edited by
      #2

      Are you sure that the error is in these lines? It seems to be correct. It may be because allocation of char* on the heap fails (just a guess). Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix

      They say I'm lazy but it takes all my time

      C 1 Reply Last reply
      0
      • C carrie

        I'm tearing my hair out here through frustration because I know its probably the stupidest of problems but whatever way I try I can't get it fixed. Basically I've taken a CString from the CFileDialogs GetPathName function, I'm needing to turn that into a char*, or more exact a PSTR but I think they're the same, but I keep getting Access Violations whichever way I try this. Just now my code is char* szPath = new char[pathToExe.GetLength()+1]; strcpy(szPath,(LPCTSTR)pathToExe); TRACE("szPath = %s\n",szPath); I've also tried using the CString.GetBuffer, without the (LPCTSTR), but no luck there either, how the damnation do I do this :) Please save my sanity and not laugh too hard at the newbie :) Thanks fellas carrie

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        are you doing a Unicode build? -c


        Though the cough, hough and hiccough so unsought would plough me through, enough that I o'er life's dark lough my thorough course pursue. --Stuart Kidd

        image effects!

        1 Reply Last reply
        0
        • C carrie

          I'm tearing my hair out here through frustration because I know its probably the stupidest of problems but whatever way I try I can't get it fixed. Basically I've taken a CString from the CFileDialogs GetPathName function, I'm needing to turn that into a char*, or more exact a PSTR but I think they're the same, but I keep getting Access Violations whichever way I try this. Just now my code is char* szPath = new char[pathToExe.GetLength()+1]; strcpy(szPath,(LPCTSTR)pathToExe); TRACE("szPath = %s\n",szPath); I've also tried using the CString.GetBuffer, without the (LPCTSTR), but no luck there either, how the damnation do I do this :) Please save my sanity and not laugh too hard at the newbie :) Thanks fellas carrie

          E Offline
          E Offline
          Ernest Laurentin
          wrote on last edited by
          #4

          Did you try this too? if you are compiling ASCII version, your code should work.

          TCHAR* szPath = new TCHAR[pathToExe.GetLength()+1];
          lstrcpy(szPath, pathToExe);
          TRACE("szPath = %s\n",szPath);

          VOTD: 23 "And this is his command: to believe in the name of his Son, Jesus Christ, and to love one another as he commanded us." - 1 John 3:23 (NIV)

          P 1 Reply Last reply
          0
          • C carrie

            I'm tearing my hair out here through frustration because I know its probably the stupidest of problems but whatever way I try I can't get it fixed. Basically I've taken a CString from the CFileDialogs GetPathName function, I'm needing to turn that into a char*, or more exact a PSTR but I think they're the same, but I keep getting Access Violations whichever way I try this. Just now my code is char* szPath = new char[pathToExe.GetLength()+1]; strcpy(szPath,(LPCTSTR)pathToExe); TRACE("szPath = %s\n",szPath); I've also tried using the CString.GetBuffer, without the (LPCTSTR), but no luck there either, how the damnation do I do this :) Please save my sanity and not laugh too hard at the newbie :) Thanks fellas carrie

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #5

            Try LockBuffer/UnlockBuffer for obtaining a non-const LPSTR Concussus surgo. When struck I rise.

            C 1 Reply Last reply
            0
            • M Mustafa Demirhan

              Are you sure that the error is in these lines? It seems to be correct. It may be because allocation of char* on the heap fails (just a guess). Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix

              They say I'm lazy but it takes all my time

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

              d'oh, you're right, and thanks for the quick reply because you just made me set a breakpoint and saw that it wasn't that that was the problem. I'm trying to use the WinSpy32 code to make something of my own and didn't spot it doing something in the background. Sorry to be an idiot, 4 years of a computing degree, 3 of them in C & C++ and I can't even do the basics sometimes :) Thanks again :)

              M 1 Reply Last reply
              0
              • D Daniel Turini

                Try LockBuffer/UnlockBuffer for obtaining a non-const LPSTR Concussus surgo. When struck I rise.

                C Offline
                C Offline
                carrie
                wrote on last edited by
                #7

                damn you guys are megafast tonight :) So fast that when I was replying another 3 turned up :p Thats impressive, thanks to you all for your help, time to try to decipher some more code :)

                1 Reply Last reply
                0
                • C carrie

                  d'oh, you're right, and thanks for the quick reply because you just made me set a breakpoint and saw that it wasn't that that was the problem. I'm trying to use the WinSpy32 code to make something of my own and didn't spot it doing something in the background. Sorry to be an idiot, 4 years of a computing degree, 3 of them in C & C++ and I can't even do the basics sometimes :) Thanks again :)

                  M Offline
                  M Offline
                  Mustafa Demirhan
                  wrote on last edited by
                  #8

                  carrie wrote: Sorry to be an idiot, 4 years of a computing degree, 3 of them in C & C++ and I can't even do the basics sometimes This happens sometimes. Once, in Linux, in a simuation program i was stuck with an error for a whole day. Then I started to blame the simulation program. But it turned out that I was trying to divide an integer by zero :rolleyes: Now I blame linux, because instead of telling me that there is a divide by zero error, it just gave me that famous error report "segmentation fault". Nothing more :wtf: Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix

                  They say I'm lazy but it takes all my time

                  1 Reply Last reply
                  0
                  • E Ernest Laurentin

                    Did you try this too? if you are compiling ASCII version, your code should work.

                    TCHAR* szPath = new TCHAR[pathToExe.GetLength()+1];
                    lstrcpy(szPath, pathToExe);
                    TRACE("szPath = %s\n",szPath);

                    VOTD: 23 "And this is his command: to believe in the name of his Son, Jesus Christ, and to love one another as he commanded us." - 1 John 3:23 (NIV)

                    P Offline
                    P Offline
                    PJ Arends
                    wrote on last edited by
                    #9

                    Your code sample will crash if used in a UNICODE build. new will allocate a certain number of bytes, not characters

                    TCHAR* szPath = new TCHAR[ (pathToExe.GetLength() + 1) * sizeof(TCHAR) ];


                    CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

                    E 1 Reply Last reply
                    0
                    • P PJ Arends

                      Your code sample will crash if used in a UNICODE build. new will allocate a certain number of bytes, not characters

                      TCHAR* szPath = new TCHAR[ (pathToExe.GetLength() + 1) * sizeof(TCHAR) ];


                      CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

                      E Offline
                      E Offline
                      Ernest Laurentin
                      wrote on last edited by
                      #10

                      Hmmm... I was surprised by your comment since you are allocating twice memory than necessary. sizeof(TCHAR)==1 for ASCII and 2 for _UNICODE build. I tried it with VS .NET and it works fine. It should work also with VS6. :confused::confused: BTW, I used lstrcpy in my example not strcpy!

                      VOTD: 23 "And this is his command: to believe in the name of his Son, Jesus Christ, and to love one another as he commanded us." - 1 John 3:23 (NIV)

                      T 1 Reply Last reply
                      0
                      • E Ernest Laurentin

                        Hmmm... I was surprised by your comment since you are allocating twice memory than necessary. sizeof(TCHAR)==1 for ASCII and 2 for _UNICODE build. I tried it with VS .NET and it works fine. It should work also with VS6. :confused::confused: BTW, I used lstrcpy in my example not strcpy!

                        VOTD: 23 "And this is his command: to believe in the name of his Son, Jesus Christ, and to love one another as he commanded us." - 1 John 3:23 (NIV)

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

                        You are right, PJ lost his mind there for a moment. He is thinking malloc and not new. Tim Smith "Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution." Hoare - 1980 ACM Turing Award Lecture

                        P 1 Reply Last reply
                        0
                        • T Tim Smith

                          You are right, PJ lost his mind there for a moment. He is thinking malloc and not new. Tim Smith "Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution." Hoare - 1980 ACM Turing Award Lecture

                          P Offline
                          P Offline
                          PJ Arends
                          wrote on last edited by
                          #12

                          No, I did not loose my mind. But now that you point it out, and I think about it, I will admit that I was wrong. No excuse can be made for ignorance. :rolleyes:


                          CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

                          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