Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to copy a CString to const char*

44 views
Skip to first unread message

Joao Rego

unread,
Mar 29, 2005, 9:44:04 AM3/29/05
to
How do I copy a variable of type CString to another of
type const char*? This happens when alter CPropertyPage
Titles

CString myStr;
CPropertyPage myPage1, myPage2;

myStr = "SOMTHING_1";
myPage1.m_psp.pszTitle = myStr;

myStr = "SOMTHING_2";
myPage2.m_psp.pszTitle = myStr;

The Title of myPage2 is OK
The Title of myPage1 is dirt

I think I need to copy the value to m_psp.pszTitle each
time. How do I copy that?

THKS,

Joao Rego

Ajay Kalra

unread,
Mar 29, 2005, 9:56:56 AM3/29/05
to
I think you need to turn on the following flag as well:

m_psp.dwFlags |= PSP_USETITLE

-----------
Ajay Kalra
ajay...@yahoo.com

Joao Rego

unread,
Mar 29, 2005, 10:09:22 AM3/29/05
to
>.
>

Yes, I have already that.

The problem is const char* is a pointer and when I assign
the CString to the pointer it does not copy the content
of CString.
Tks
Joao

Ajay Kalra

unread,
Mar 29, 2005, 10:42:42 AM3/29/05
to
When you do as shown in your code, you are actually using
CString::operator LPCTSTR, which should be OK for you unless you are in
UNICODE and myPage1.m_psp.pszTitle does not support UNICODE.

-----
Ajay Kalra
ajay...@yahoo.com

anon...@discussions.microsoft.com

unread,
Mar 29, 2005, 11:30:11 AM3/29/05
to
>.

Yes, it works ok, but the second time I make the assign
the first one myPage1.m_psp.pszTitle has garbage.
... And if I do it again the 3th is OK and the 1st and
2nd has garbage... only the last CPropertyPage gets the
title OK, the (n-1) first ones have garbage... I hope I
can make my self clear.

THKS
Joao Rego

Alexander Grigoriev

unread,
Mar 29, 2005, 12:21:03 PM3/29/05
to
Use two different CString objects for Page1 and Page2.

When you assign a "string" to CString, a buffer is allocated and the string
contents is copied there. When you assign another "string" to the same
CString, the original buffer may get reallocated, thus its contents becomes
invalid.

If you assign CString to a LPCTSTR (implicitly invoking operator
LPCTSTR()), you cannot modify CString object as long as you need that
LPCTSTR pointer be valid.

"Joao Rego" <anon...@discussions.microsoft.com> wrote in message
news:0c3901c5346d$c0f24670$a501...@phx.gbl...

Ajay Kalra

unread,
Mar 29, 2005, 12:27:56 PM3/29/05
to
> When you assign a "string" to CString, a buffer is allocated and the
string
> contents is copied there. When you assign another "string" to the
same
> CString, the original buffer may get reallocated, thus its contents
becomes
> invalid.

I thought of that however the contents of CString are copied over when
it is assigned: myPage1.m_psp.pszTitle = myStr; If myStr goes out of
scope or is reassigned, it should still work.

------------
Ajay Kalra
ajay...@yahoo.com

Tim Slattery

unread,
Mar 29, 2005, 12:29:49 PM3/29/05
to
<anon...@discussions.microsoft.com> wrote:

>Yes, it works ok, but the second time I make the assign
>the first one myPage1.m_psp.pszTitle has garbage.
>... And if I do it again the 3th is OK and the 1st and
>2nd has garbage... only the last CPropertyPage gets the
>title OK, the (n-1) first ones have garbage... I hope I
>can make my self clear.

It may have to do with the scope of your CString objects. If the
CString is a dynamic object within a small method in which you assign
its value to the title pointer, what happens when that method ends?
The CString is deconstructed, and its memory is freed to be used by
anything else that needs it. But the title pointer (pszTitle) is still
pointing to that memory, expecting it to contain a character string.

So maybe you need to allocate some space and copy the contents of the
CString to that, and assign that pointer to pszTitle. Just don't
forget to free that space when you no longer need it.

--
Tim Slattery
MS MVP(DTS)
Slatt...@bls.gov

Ajay Kalra

unread,
Mar 29, 2005, 12:36:41 PM3/29/05
to
> It may have to do with the scope of your CString objects.

See my response to Alexander. It does not sound right. I would have
guessed that contents of CString get copied over when it is assigned.
CString can then be destroyed without any side effect.

-------
Ajay Kalra
ajay...@yahoo.com

Joao Rego

unread,
Mar 29, 2005, 12:48:14 PM3/29/05
to
Thanks... that was the solution I found... "different
CString objects "... not good style (I have 10
CPropertyPage!!!)... but it works.

Tanks anyway

Joao Rego

>.
>

Tim Slattery

unread,
Mar 29, 2005, 4:03:46 PM3/29/05
to
"Ajay Kalra" <ajay...@yahoo.com> wrote:

>> It may have to do with the scope of your CString objects.
>
>See my response to Alexander. It does not sound right. I would have
>guessed that contents of CString get copied over when it is assigned.
>CString can then be destroyed without any side effect.

You're not copying, you're just doing an assignment, right?

In that case, the (LPCSTR) overloaded operator of CString gets
invoked. That simply returns a pointer to the character string within
the CString object. Nothing is copied.

Ajay Kalra

unread,
Mar 29, 2005, 4:17:37 PM3/29/05
to
> Nothing is copied.

I am guessing that is correct. It just points to a location in the
memory. As the new CString object is allocated, this memory is freed
and new allocation is elesewhere. This sounds unusual to me as you need
to carry a string object with each of the titles to ensure it does not
go out of scope.

--------
Ajay Kalra
ajay...@yahoo.com

Joseph M. Newcomer

unread,
Mar 31, 2005, 1:15:44 PM3/31/05
to
That is indeed what is happening. Note that this is not the same as calling an API call
that makes a copy.

What I don't understand is the need to assign a literal to a CString!

CString s = _T("Something");
myPage2.m_psp.pszTitle = s;

This introduces a completely unnecessary intermediate copy!

The correct solution is

myPage2.m_psp.pszTitle = _T("Something");

at which point the pszTitle string points to a literal at a fixed location in memory! Then
there is no problem!
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Ajay Kalra

unread,
Mar 31, 2005, 1:36:24 PM3/31/05
to
> The correct solution is
> myPage2.m_psp.pszTitle = _T("Something");

I agree.

--------
Ajay Kalra
ajay...@yahoo.com

0 new messages