For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > How to modify CString in the debugger for VC6.0?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to modify CString in the debugger for VC6.0?
malhenry

2005-05-31, 9:00 pm

How can a CString variable be modified in the debugger with VC 6.0?

For instance I want to change "xxx" to "" so that I can test a certain code
path.

Thanks.
Simon Trew

2005-06-01, 4:00 am

"malhenry" <malhenry@discussions.microsoft.com> wrote in message
news:C91DC683-74A5-4734-B8B7-8039874F9FCD@microsoft.com...
> How can a CString variable be modified in the debugger with VC 6.0?
>
> For instance I want to change "xxx" to "" so that I can test a certain
> code
> path.


Give up. It's almost always impossible to change a value in the debugger,
even though it tantalises you with an edit box. Modify the memory bytes
through the memory window (Edit|Debug Windows>|Memory), by changing the
first character to 0.

S.


Alex Blekhman

2005-06-01, 9:09 am

Simon Trew wrote:
>
> Modify the memory bytes through the memory
> window (Edit|Debug Windows>|Memory), by changing the
> first character to 0.


I believe you'll need to update CString's internal
CStringData.nDataLength member, as well, to avoid possible
crashes.


malhenry

2005-06-01, 4:03 pm

Here is how I did it:
View | Debug Windows | Memory
Copy the address of my CString variable (csParam) into the memory address
window
Deleting the first byte in the memory window changes it to hex 00.
Here is the Watch window before change:
csParam = {"NOK"}
m_pchData = 0x00ab5064 "NOK"
78 'N'
Here is the Watch window AFTER changing memory:
csParam = {""}
m_pchData = 0x00ab5064 ""
0 ''
I could not find any reference to CStringData.nDataLength member in the
watch window.
Thanks, for your help.

"Alex Blekhman" wrote:

> Simon Trew wrote:
>
> I believe you'll need to update CString's internal
> CStringData.nDataLength member, as well, to avoid possible
> crashes.
>
>
>

Alex Blekhman

2005-06-01, 4:03 pm

malhenry wrote:
> I could not find any reference to CStringData.nDataLength
> member in the watch window.


It won't appear in watch window, since there is no such
member in CString. CStringData is a class that maintains
string length, buffer length and reference counting. Several
CString instances can share single CStringData instance.
CStringData is allocated before actual string buffer, so
memory layout is like that:

0x00200000 +------------------+ <-- CStringData
| long nRefs |
0x00200004 +------------------+
| int nDataLength |
0x00200008 +------------------+
| int nAllocLength |
0x0020000C +------------------+ <-- CString.m_pchData
| LPTSTR m_pchData |
| ... |
| ... |
+------------------+

So, to get correct CStringData instance you need to subtract
12 from CString.m_pchData address (write it in watch
window):

(CStringData*)(strHello.m_pchData - 12)

Warning: If you see that CStringData.nRefs is more than 1,
then you cannot change string buffer pointed by
CString.m_pchData because some other CString instance(s)
share(s) string buffer with current instance. By changing it
you will change string for them all.


Simon Trew

2005-06-02, 4:02 am

"Alex Blekhman" <tkfx.N05P4M@yahoo.com> wrote in message
news:eLJAxBoZFHA.3152@TK2MSFTNGP14.phx.gbl...
>
> I believe you'll need to update CString's internal
> CStringData.nDataLength member, as well, to avoid possible
> crashes.


You're right, I'd lapsed to thinking it was a vanilla C string.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com