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
m_psp.dwFlags |= PSP_USETITLE
-----------
Ajay Kalra
ajay...@yahoo.com
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
ajay...@yahoo.com
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
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...
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
>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
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
Tanks anyway
Joao Rego
>.
>
>> 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.
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
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
I agree.
--------
Ajay Kalra
ajay...@yahoo.com