Mike McCracken
asked on
C++ - Convert a wString to char *
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I probably just had it coded wrong.
mlmcc
mlmcc
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::ConvertoA NSI()'.
ASKER
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
mlmcc
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
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
ASKER
I thought I had closed this.
Sorry for the delay
mlmcc
Sorry for the delay
mlmcc