Avatar of Mike McCracken
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
C++

Avatar of undefined
Last Comment
Mike McCracken

8/22/2022 - Mon
SOLUTION
evilrix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jkr

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
Mike McCracken

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mike McCracken

ASKER
I probably just had it coded wrong.  

mlmcc
jkr

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()'.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Mike McCracken

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
sarabande

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 McCracken

ASKER
I thought I had closed this.

Sorry for the delay

mlmcc
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.