Solved

Best way to convert a CString to LPSTR

Posted on 2003-11-02
7
1,896 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
C language IDE – Compilers installation 14 74
Setting nameservers after res_init fails doing res_query 2 109
Beginner to Unreal Engine 4 5 94
How do i run a c++ file? 15 46
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

840 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