Solved

Best way to convert a CString to LPSTR

Posted on 2003-11-02
7
2,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 clear a vector as well as how to detect empty vectors in C++.

626 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