Click here to Skip to main content
15,914,820 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to convert a string from CString to LPTSTR Pin
Rage11-Oct-05 22:18
professionalRage11-Oct-05 22:18 
GeneralRe: How to convert a string from CString to LPTSTR Pin
cauvang12-Oct-05 1:15
cauvang12-Oct-05 1:15 
AnswerRe: How to convert a string from CString to LPTSTR Pin
toxcct11-Oct-05 22:25
toxcct11-Oct-05 22:25 
GeneralRe: How to convert a string from CString to LPTSTR Pin
cauvang12-Oct-05 0:43
cauvang12-Oct-05 0:43 
GeneralRe: How to convert a string from CString to LPTSTR Pin
toxcct12-Oct-05 0:54
toxcct12-Oct-05 0:54 
GeneralRe: How to convert a string from CString to LPTSTR Pin
cauvang12-Oct-05 1:07
cauvang12-Oct-05 1:07 
AnswerRe: How to convert a string from CString to LPTSTR Pin
toxcct12-Oct-05 1:45
toxcct12-Oct-05 1:45 
GeneralRe: How to convert a string from CString to LPTSTR Pin
icabod12-Oct-05 1:12
icabod12-Oct-05 1:12 
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.
GeneralRe: How to convert a string from CString to LPTSTR Pin
Tim Smith12-Oct-05 4:03
Tim Smith12-Oct-05 4:03 
AnswerRe: How to convert a string from CString to LPTSTR Pin
Roger Stoltz12-Oct-05 1:53
Roger Stoltz12-Oct-05 1:53 
GeneralRe: How to convert a string from CString to LPTSTR Pin
cauvang12-Oct-05 15:21
cauvang12-Oct-05 15:21 
Questionfile handling Pin
karmendra_js11-Oct-05 21:56
karmendra_js11-Oct-05 21:56 
AnswerRe: file handling Pin
Bob Stanneveld11-Oct-05 22:53
Bob Stanneveld11-Oct-05 22:53 
GeneralRe: file handling Pin
karmendra_js11-Oct-05 23:07
karmendra_js11-Oct-05 23:07 
GeneralRe: file handling Pin
Bob Stanneveld12-Oct-05 6:14
Bob Stanneveld12-Oct-05 6:14 
AnswerRe: file handling Pin
krmed12-Oct-05 2:45
krmed12-Oct-05 2:45 
QuestionNot been able to return value from a function in a DLL Pin
Amarelia11-Oct-05 21:42
Amarelia11-Oct-05 21:42 
AnswerRe: Not been able to return value from a function in a DLL Pin
Prakash Nadar11-Oct-05 22:59
Prakash Nadar11-Oct-05 22:59 
Questionhow to do a priority queue???? Pin
Nadzna11-Oct-05 21:26
Nadzna11-Oct-05 21:26 
AnswerRe: how to do a priority queue???? Pin
Roger Stoltz11-Oct-05 23:44
Roger Stoltz11-Oct-05 23:44 
GeneralRe: how to do a priority queue???? Pin
John M. Drescher12-Oct-05 2:15
John M. Drescher12-Oct-05 2:15 
GeneralRe: how to do a priority queue???? Pin
David Crow12-Oct-05 3:02
David Crow12-Oct-05 3:02 
QuestionNew build configuration ???? Pin
xSoptik11-Oct-05 20:43
xSoptik11-Oct-05 20:43 
AnswerRe: New build configuration ???? Pin
kakan11-Oct-05 21:24
professionalkakan11-Oct-05 21:24 
GeneralRe: New build configuration ???? Pin
xSoptik11-Oct-05 21:40
xSoptik11-Oct-05 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.