Solved

Best way to convert a CString to LPSTR

Posted on 2003-11-02
7
1,940 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

756 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