C++ - Convert a wString to char *

Mike McCracken
Mike McCracken used Ask the Experts™
on
I am trying to write a C++ library (dll) that will be called by the WebI reporting tool.  

The parameters passed from WebI are of type wString.

I need to pass these to another function that takes char * as the parameters.

How do I convert a wString to a char *?

mlmcc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
evilrixSenior Software Engineer (Avast)
Commented:
There are two issues. Firstly, wString (not a standard type, so most likely a typedef or custom class)  is almost certainly a wide char type string (based on wchar_t). This will need to be narrowed to a char. Your second problem is one of encoding. What is the encoding format of the original string? Uft16? What encoding format does your function expect? Utf8? ANSI? There isn't enough info in the question to provide a real answer.
jkr
Top Expert 2012

Commented:
Assuming that you are on windows (since you mentioned 'DLL') and taking in mind what evilrix wrote about character sets, 'WideCharToMultiByte()' (https://msdn.microsoft.com/en-us/library/windows/desktop/dd374130%28v=vs.85%29.aspx) should be the most flexible way to approach that, since its parameters allow to control that. Scroll down for an example or check the section 'String Handling' in http://www.codeproject.com/Articles/633/Introduction-to-COM-What-It-Is-and-How-to-Use-It
Senior Consultant
Most Valuable Expert 2011
Top Expert 2013
Commented:
We tried the 'WideCharToMultiByte but couldn't make it work.

This does seem to work

std::wstring valPar0(param0);
const wchar_t *str = valPar0.c_str();
size_t size = wcslen(str)*2 + 2;
char * StartPoint= new char[size];
size_t c_size;
wcstombs_s(&c_size, StartPoint, size, str, size);

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

jkr
Top Expert 2012
Commented:
That's a bit odd, since 'wcstombs_s()' is a wrapper around 'WideCharToMultiByte()' providing the most common parameters. Or, as in the Codeproject article, using

// Assuming we already have a Unicode string wszSomeString...
char szANSIString [MAX_PATH];

    WideCharToMultiByte ( CP_ACP,                // ANSI code page
                          WC_COMPOSITECHECK,     // Check for accented characters
                          wszSomeString,         // Source Unicode string
                          -1,                    // -1 means string is zero-terminated
                          szANSIString,          // Destination char string
                          sizeof(szANSIString),  // Size of buffer
                          NULL,                  // No default character
                          NULL );                // Don't care about this flag

Open in new window


is equivalent to

wcstombs ( szANSIString, wszSomeString, sizeof(szANSIString) );

Open in new window

Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Author

Commented:
I probably just had it coded wrong.  

mlmcc
jkr
Top Expert 2012

Commented:
Maybe... I'd just consider using the 1st way if you would encounter different encodings in the future. Maybe you'd just roll your own conversion layer that then can host either 'wcstombs*()' or 'WideCharToMultiByte()', depending on your future requirement. Just Something like 'MyProjectUtils::ConvertoANSI()'.
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Author

Commented:
Hopefully this is a one time thing for a special requirement.  I am a report writer who happens to be able to write code.  The last serious programming I did was in 2001 in VB6.

mlmcc
Top Expert 2016

Commented:
a little remark apart from this question:

if using visual c++ compiler for the dll you may consider to passing std::wstring to the dll and handle the strings in the dll. most report libraries for windows can handle wide strings (utf16/ms unicode) same as (or even better than) ansi strings. also if you want to return strings from dll, you could avoid a reverse conversion (with possible issues, for example if utf-8 characters are involved).

Sara
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Author

Commented:
I thought I had closed this.

Sorry for the delay

mlmcc

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial