How to convert a CString variable to char* or LPTSTR?
Answers were Sorted based on User's Feedback
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 |
Answer / prajeesh prabhakar
char* pString = reinterpret_cast<LPSTR>(
csString.GetBuffer( csString.GetLength()));
csString.ReleaseBuffer();
Is This Answer Correct ? | 1 Yes | 0 No |
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 |
Answer / billy howell
Use the _stprintf_s function. It's much simpler.
Is This Answer Correct ? | 1 Yes | 1 No |
What is document-view architecture ? Give me one real time example for SDI ?
What is CALLBACK? How it work? what is the advantage of CALLBACK, please explain with an example
What view class enables you to use an edit control as a view?
How can i implement the dynamic menus in MFC plz give the code
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 both base and derived class have the constructors if i create an object for derive class which class constructor is executed first
what is the use of CCmdTarget?
What are the special requirements for dialog box resources used in a form view?
List out the parameters of WinMain Function.
1.Get string1,string2,string3 1.add string1 and 2,string1 and 3. 2.replace vowels with T 3.count number of T. 4.remove T 5.COPY string1 to stringf. 6.restore string1. print the following 1.Number of T 2.StringF 3.string1+string2+string3 Use pointers and functions
1)How to load an icon on a button as Dynamically ?
What is #progma and where it is used?