CString to const char*
-
Is your build a UNICODE one? Then you will have to convert, not just cast your CString data. See [WideCharToMultiByte function (stringapiset.h) | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte) or ATL macros like W2A
-
I have tried this also:
const char\* chTemp = (const char\*)sTemp.GetBuffer(0); sTemp.ReleaseBuffer(); TRACE(">>>>>>>>>>>>>>>>>>>%s\\n", chTemp);
the project is not multi-byte ... the same result, just one letter (first letter) from the string ...
_Flaviu wrote:
the project is not multi-byte
Is it "not multi-byte" or "multi-byte"?
-
_Flaviu wrote:
the project is not multi-byte
Is it "not multi-byte" or "multi-byte"?
-
Is your build a UNICODE one? Then you will have to convert, not just cast your CString data. See [WideCharToMultiByte function (stringapiset.h) | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte) or ATL macros like W2A
-
Sorry, is multi-byte .. .anyway, if the project is unicode or is multi-byte, is the same thing.
Try these tests:
CStringA ansiText("TestA");
LPCSTR ansi = (LPCSTR)ansiText;
TRACE("%s\n", ansi);CString someText("Test");
ansi = (LPCSTR)someText;
TRACE("%s\n", ansi);What is the result?
-
Try these tests:
CStringA ansiText("TestA");
LPCSTR ansi = (LPCSTR)ansiText;
TRACE("%s\n", ansi);CString someText("Test");
ansi = (LPCSTR)someText;
TRACE("%s\n", ansi);What is the result?
with the first try:
CStringA ansiText("TestA"); LPCSTR ansi = (LPCSTR)ansiText; TRACE("%s\\n", ansi);
the result is TestA for the second try, I got an error:
1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
at
CString someText("Test"); ansi = (LPCSTR)someText; // <-- here is the error TRACE("%s\\n", ansi);
The project is unicode.
-
with the first try:
CStringA ansiText("TestA"); LPCSTR ansi = (LPCSTR)ansiText; TRACE("%s\\n", ansi);
the result is TestA for the second try, I got an error:
1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
at
CString someText("Test"); ansi = (LPCSTR)someText; // <-- here is the error TRACE("%s\\n", ansi);
The project is unicode.
_Flaviu wrote:
for the second try, I got an error:
1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
at
CString someText("Test");
ansi = (LPCSTR)someText; // <-- here is the error
TRACE("%s\n", ansi);The project is unicode.
Of course you get the error since the CString contains the wide char text! Either change the build to be MBCS/ANSI or convert CString to wchar_t* (or use _T() macro).
-
Is obviously something in project settings, because I have tried this code in a new test project and goes fine as multi-byte project ... as unicode, the same result.
-
_Flaviu wrote:
for the second try, I got an error:
1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
at
CString someText("Test");
ansi = (LPCSTR)someText; // <-- here is the error
TRACE("%s\n", ansi);The project is unicode.
Of course you get the error since the CString contains the wide char text! Either change the build to be MBCS/ANSI or convert CString to wchar_t* (or use _T() macro).
-
I have tried this also:
const char\* chTemp = (const char\*)sTemp.GetBuffer(0); sTemp.ReleaseBuffer(); TRACE(">>>>>>>>>>>>>>>>>>>%s\\n", chTemp);
the project is not multi-byte ... the same result, just one letter (first letter) from the string ...
_Flaviu wrote:
TRACE(">>>>>>>>>>>>>>>>>>>%s\n", chTemp);
What if you try:
TRACE(">>>>>>>>>>>>>>>>>>>%S\n", chTemp); // capital S
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles