How to convert a CString variable to char* or LPTSTR?

Answer Posted / jones

there are two ways
I) Use CString::GetBuffer(). It can be used in a manner
similar to this:
// prototype of a function that takes a LPTSTR parameter
// presented for argument's sake.
void test_func ( LPTSTR lpszString, int length );

CString string;
test_func ( string.GetBuffer ( 50 ), 50 );
string.ReleaseBuffer ( );
Forgetting to call CString::ReleaseBuffer() can cause
problems very difficult to debug as it releases the lock on
CString's inner buffer.
One thing to keep in mind about CString::GetBuffer() is
that it returns a TCHAR* value (or LPTSTR, it's the same),
so it is subject to the same ANSI/MBCS Vs. UNICODE
convertions as most other Win32 APIs. It also means that if
you're compiling a unicode version of your application, and
specifically need a char* from your CString instance,
you'll have to use a separate buffer of the appropriate
type, and then make the convertion to unicode using one of
the available API's before asigning it's value to the
CString instance. The same goes if you're doing the exact
opposite: getting a WCHAR* out of a CString, while
compiling in MBCS mode.
ii) Use a temporary variable. For example:

char temp[256];
CString string;

test_func ( temp, 256 );
string = temp;

Is This Answer Correct ?    7 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do I create a dialog box in mfc?

790


List out the basic features of MFC.

14383


what is the meaning of constant FILE EXCEEDS LENGTH LIMIT while loading from ps file to vsam in the jcl?

3563


what message is sent to an application when the user presses the primary button?

1590


plzz tell me what we can do and dont in tally ERP & sap business one?

2079


What does mfc stand for?

1199


I want recent paper pattern for HP company?

2011


What is mfc class hierarchy?

802


How to handle dynamic menus in mfc? What happens when client calls cocreateinstance?

920


what is functioning of DIalodDataXchange ..?

1366


Do you have an idea about MFC?

1178


how to repaint when working with horizontal scroll bar

1713


What is the difference between Struts and JSF? Pls list some most suitable differences.

3023


2.create for 10 batch: Employee_Number Employee_name Employee_Dateofjoining Employee_address Employee_salary 1.select the employee name who deriving more than 10 thousand salary and joined before august 08. use structure and pointers

2002