Solved

Best way to convert a CString to LPSTR

Posted on 2003-11-02
7
1,824 Views
Last Modified: 2010-05-18
I have a Treeview control where I am adding a hierarchy of text items.
To insert items I use the TV_INSERTSTRUCT and the TreeCtrlItem.item.pszText to set text.
Since item.pszText needs a LPSTR I can either set it without using a variable, like 'TreeCtrlItem.item.pszText = "testItem";' but if I want to use a CString I have to convert that CString to LPSTR before using it.
What is the best way to do this?

I am currently using:
CString testString = "Cardboard";
int length = testString .GetLength();
char *cptr = testString .GetBuffer(length);
TreeCtrlItem.item.pszText = cptr;

Any other better ways?
0
Comment
Question by:Trickster
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9666915
Why not using

CString testString = "Cardboard";
TreeCtrlItem.item.pszText = (LPCTSTR) testString;

?

0
 

Author Comment

by:Trickster
ID: 9667021
I tried that but got the message that TreeCtrlItem.item.pszText won't accept a LPCTSTR string - only LPSTR. I read in the help files that I can convert from LPCTSTR to LPSTR but that would mean two different conversions/casts and I don't know if that is a good choice.. That's one of the reasons I asked this question here.. :-)

// T
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 100 total points
ID: 9667070
Trickster:

> Since item.pszText needs a LPSTR I can either set it without using a variable,
> like 'TreeCtrlItem.item.pszText = "testItem";' but if I want to use a CString
> I have to convert that CString to LPSTR before using it. What is the best way
> to do this?

Try this:
     CString testString = "Cardboard";
     TreeCtrlItem.item.pszText = testString.GetBuffer();

     // Do something with the struct

Then do this:
     testString.ReleaseBuffer();

Hope that helps,
Dex*
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
freewell earned 100 total points
ID: 9669082
t is alright to do the multiple castings. The first casting is to get the internal String character buffer pointer and the second casting is just to please the compiler.

TreeCtrlItem.item.pszText = (LPTSTR)(LPCTSTR)testString;

The TreeCtrlItem.item.pszText is an input parameter and it should be declared as const. As you are see in the CTreeCtrl::InsertItem(UNIT nMask, LPCTSTR lpszItem, ...).

There is inconsistency of the CONST-ness of the ealier Microsoft API.
0
 

Expert Comment

by:jbuote1
ID: 9674083
Why not something as simple as:

strcpy((char*)TreeCtrlItem.item.pszText , (LPCSTR)testString);

Obviously casting the TreeCtrlItem... to whatever it is...
char, wchar, tchar  so on....
0
 

Expert Comment

by:jbuote1
ID: 9674088
Rather cast testString to LPCTSTR as previously noted...
Both seem to work.
0
 

Author Comment

by:Trickster
ID: 9678127
Both Dextar and Freewell came up with a good answer so I doubled the points and then split them between the two..

// Trickster
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now