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

Answers were Sorted based on User's Feedback



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

Answer / manoj

GetBuffer

Is This Answer Correct ?    8 Yes 2 No

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

Answer / 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

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

Answer / prajeesh prabhakar

char* pString = reinterpret_cast<LPSTR>(
csString.GetBuffer( csString.GetLength()));
csString.ReleaseBuffer();

Is This Answer Correct ?    1 Yes 0 No

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

Answer / ratnesh sachan

We can convert a CString variable to char* or LPTSTR by
using the GetBuffer.

Example
// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
char *pStr;
pStr = s.GetBuffer( 10 );
strcpy( pStr, "Hello" ); // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif

Is This Answer Correct ?    3 Yes 3 No

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

Answer / billy howell

Use the _stprintf_s function. It's much simpler.

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More MFC Interview Questions

In MFC By Using Communication Between Dialog Boxes,How Can We Send Message Of one Dialog Box To Another Dialog.?

3 Answers   Financial Technologies,


If there is more than 100 control in a window how we can change the Taborder of a controls

4 Answers   Satyam,


Explain the flow of SDI application?

8 Answers   Mphasis,


What is the difference between ASSERT and VERIFY?

2 Answers  


How many types of dialog box are their

10 Answers   Honeywell,






Does the application have more than one object? If Yes, Briefly explain.

2 Answers   Soltius Infotech,


What types of threads are supported by MFC framework?

2 Answers  


What is the use of CObject::Dump function ?

4 Answers  


Q1. A. What is unary operator? List out the different operators involved in the unary operator. B. What is an adjust field format flag? Q2. A. Distinguish between a # include and #define. B. Can a list of string be stored within a two dimensional array? Q3. A. Explain how a pointer to function can be declared in C++? B. List the merits and demerits of declaring a nested class in C++? Q4. A. What are the syntactic rules to be avoid ambiguity in multiple inheritence? B. Explain the operation of overloading of an assignment operator. Q5. A. Explain how the virtual base class is different from the conventional base classes of the opps. B. Explain how an exception handler is defined and invoked in a Program. Q6. A. What is a binary file? List the merits and demerits of the binary file usagein C++. B. Write short notes on Text Manipulation Routines. C. Write bites in Turbo c++ Header ("Include") Files.

3 Answers   ABC, HCL, Infosys,


What is Thread ?(VC++)What is the difference between Cmutex and Csemaphone?

5 Answers   Atos Origin,


what is functioning of DIalodDataXchange ..?

0 Answers  


what is message Testing ?

1 Answers   L&T,


Categories