Solved

Best way to convert a CString to LPSTR

Posted on 2003-11-02
7
1,797 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

11 Experts available now in Live!

Get 1:1 Help Now