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.

Re: How to modify CString in the debugger for VC6.0? by Simon

Simon
Tue May 31 18:27:24 CDT 2005

"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.



Re: How to modify CString in the debugger for VC6.0? by Alex

Alex
Wed Jun 01 03:03:45 CDT 2005

Simon Trew wrote:
>> How can a CString variable be modified in the debugger
>> with VC 6.0?
>
> 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.



Re: How to modify CString in the debugger for VC6.0? by malhenry

malhenry
Wed Jun 01 09:09:42 CDT 2005

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:
> >> How can a CString variable be modified in the debugger
> >> with VC 6.0?
> >
> > 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.
>
>
>

Re: How to modify CString in the debugger for VC6.0? by Alex

Alex
Wed Jun 01 10:47:08 CDT 2005

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.



Re: How to modify CString in the debugger for VC6.0? by Simon

Simon
Wed Jun 01 18:26:01 CDT 2005

"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.