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
LVL 101
mlmccAsked:
Who is Participating?
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.
0
jkrCommented:
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
0
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

0

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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

jkrCommented:
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

0
mlmccAuthor Commented:
I probably just had it coded wrong.  

mlmcc
0
jkrCommented:
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()'.
0
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.

mlmcc
0
sarabandeCommented:
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
0
mlmccAuthor Commented:
I thought I had closed this.

Sorry for the delay

mlmcc
0
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
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.