Convert CString to char*

Posted on 2007-10-10
Medium Priority
Last Modified: 2013-11-20
How to convert CString to char *
Question by:xtran888
  • 2
LVL 46

Accepted Solution

Kent Olsen earned 150 total points
ID: 20051858
Hi xtran888,

If all you want to do is get to the underlying string (and perhaps modify it) use the GetBuffer() method.  Here's a quick example.

CString s(_T("File.ext"));
LPTSTR p = s.GetBuffer();
LPTSTR dot = strchr(p, '.'); // OK, should have used s.Find...
if(p != NULL)
    *p = _T('\0');

Good Luck,

Expert Comment

ID: 20052711
If you only need read-only access to the string, you can cast the CString to LPCSTR, if your environment is setup for ANSI (multi-byte) strings.

CString sTest("test");
const char *p = (LPCSTR)sTest;

If your build environment is set for Unicode, you'll get a type cast error because then the CString will compile to Unicode, and so you would need to add code to convert the string at runtime.
LVL 39

Expert Comment

ID: 20054786
>>>> const char *p = (LPCSTR)sTest;

CString has a built-in cast operator for LPCSTR (== const char*). So you need no cast:

    CString s = "Hello";
    const char* psz = s;  // no cast needed

That means wherever a const char* (LPCSTR) was used you may use a CString instead.

Regards, Alex

LVL 39

Expert Comment

ID: 20054809
>>> If your build environment is set for Unicode, you'll get a
>>> type cast error because then the CString will compile to Unicode
Using the TCHAR and LPCTSTR will solve that either for UNICODE or non-UNICODE.

  CString s = _T("Hello");   // makes either LPCSTR or LPCWSTR on right side
  LPTSTR p = new TCHAR[20];   // left side is either char* or wchar_t*
                                                 // TCHAR is either char or wchar_t
  _tcscpy(p, s);   // here you don't need to cast to LPCTSTR as it is built-in
                          // _tcscopy makes strcpy in case of non-UNICODE and wcscpy for UNICODE

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

864 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