How do I convert from CString
to const char*
in my Unicode MFC application?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- How to know full paths to DLL's from .csproj f
- Importing NuGet references through a local project
相关文章
- How to show location of errors, references to memb
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- How to track MongoDB requests from a console appli
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
Generic Conversion Macros (TN059 Other Considerations section is important):
I recommendo to you use TtoC from ConvUnicode.h
It is a macro to do conversions per Unicode
I had a similar problem. I had a
char*
buffer with the .so name in it.I could not convert the
char*
variable toLPCTSTR
. Here's how I got around it...Note: This answer predates the Unicode requirement; see the comments.
Just cast it:
It works because CString has a cast operator to do exactly this.
Using TCHAR makes your code Unicode-independent; if you're not concerned about Unicode you can simply use
char
instead ofTCHAR
.If your CString is Unicode, you'll need to do a conversion to multi-byte characters. Fortunately there is a version of CString which will do this automatically.
To convert a
TCHAR
CString to ASCII, use theCT2A
macro - this will also allow you to convert the string to UTF8 (or any other Windows code page):There is also a macro to convert from ASCII -> Unicode (
CA2T
) and you can use these in ATL/WTL apps as long as you have VS2003 or greater.See the MSDN for more info.