1
votes

I am building my application in UNICODE mode and want convert CString to LPWSTR.Basically i have one one class containing CString as member variable like,

class MyClass
{
   CString TreeNodeName;
}

And i want to use following structure for inserting item into tree control,

TVINSERTSTRUCT tvInsert;
tvInsert.hParent = ParentNode;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.lParam = (long)ClassObject;
tvInsert.item.pszText = ClassObject->TreeNodeName;  //Need this conversion

Please help me how to convert CString TreeNodeName; to tvInsert.item.pszText ?

1
I know you might not know the answer to this, but what encoding are you using for the text in your CString? You see the question is unanswerable unless you know that piece of information. If you don't know then perhaps you could explain where you get the value of TreeNodeName from. - john
@john thanks for quick reply basically i am storing some values from XML files to TReeNodeName which is normal ASCII encoding - user2417685
Seems I was under a misapprehension, since you are in Unicode mode you can just assign directly as Jonathan Potter says. I was thinking that some kind of char set translation was necessary. - john

1 Answers

4
votes

Unless I've misunderstood the question, all you should have to do is cast the CString to a LPCTSTR to use it with Windows API functions. See here for a description.

Because the TVITEM::pszText member is a LPTSTR you will need to cast again to non-const, however this should be safe for actions like TVM_INSERTITEM as the string you supply is not modified.

tvInsert.item.pszText = (LPTSTR)(LPCTSTR)ClassObject->TreeNodeName;