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 *?

LVL 102
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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
mlmccAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

mlmccAuthor Commented:
I probably just had it coded wrong.  

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()'.
mlmccAuthor 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.

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).

mlmccAuthor Commented:
I thought I had closed this.

Sorry for the delay

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.