I have the following piece of code:

void *body = "testcode";

wchar_t  *d_Body = SysAllocStringLen(NULL,255);


But when I convert d_Body again to char * and print its value to file,the value printed is junk.

Can I solve the above problem?

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.

sizeof (body) is sizeof (void*).  You really want to pass the length of the string in, don't you?

Use char* to define the string, and pas strlen(body) instead.  

Its one obvious thing that's wrong anyway. . .

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
Make sure you are printing/writing the text with the correct functions.

You would use printf() for ASCII chars, but wprintf for wide chars.

A better way to do your code is to avoid the separate function call altogether and define your code using the standard Windows macro _T.

WCHAR* body = _T("testcode");

If you compile your program with Unicode enabled (define _UNICODE), this will make sure that body is a wide string already.

Also, if you do not object to using MFC, Microsoft has a very convenient macro called A2W() which converts an ASCII string (not just literal stringS) to wide chars. It's useful as long as you don't need to use the wide string outside of the current scope (e.g., outside the current function).

Finally, though this may not apply to simple strings, you can't always convert a wide string to an ASCII string (because most wide characters can't be represented in ASCII, e.g. Chinese characters).
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.