CString Stupidity
-
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 -
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 carrieAre 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
-
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 carrieare 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
-
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 carrieDid 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)
-
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 carrieTry LockBuffer/UnlockBuffer for obtaining a non-const LPSTR Concussus surgo. When struck I rise.
-
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
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 :)
-
Try LockBuffer/UnlockBuffer for obtaining a non-const LPSTR Concussus surgo. When struck I rise.
-
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 :)
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
-
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)
Your code sample will crash if used in a UNICODE build.
new
will allocate a certain number of bytes, not charactersTCHAR* 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!
-
Your code sample will crash if used in a UNICODE build.
new
will allocate a certain number of bytes, not charactersTCHAR* 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!
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 notstrcpy
!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)
-
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 notstrcpy
!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)
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
-
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
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!