Click here to Skip to main content
15,848,391 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hye all,

How do I pass unicode sring parameter to my function? Was there any conversion needed?
So I have to pass the unicode string for example Run(L"Query")..
Then in my implementation code i define like this

void Run(wchar_t *message)
{
  DWORD cbRequest, cbWritten;
  cbRequest=sizeof(message);

  WriteFile(
        hPipe,                      // Handle of the pipe
        message,                  // Message to be written
        cbRequest,                  // Number of bytes to write
        &cbWritten,                 // Number of bytes written
        NULL                        // Not overlapped
      ));
}

but when i do debugging, the message writen was only one namely 'Q'..
How do i correct this?
Posted
Updated 3-Nov-10 1:24am
v3

You must use wcslen() function to get string length in this case instead of sizeof.

cbRequest = wcslen(message);
 
Share this answer
 
Comments
Pdaus 3-Nov-10 7:18am    
I only get "Que"..doesn't work
voloda2 3-Nov-10 7:48am    
Sorry - you should have: cbRequest = wcslen(message) * sizeof(wchar_t) because unicode characters are stored as multiple bytes.
Unicode characters are represented in C++ by the wchar_t character type. Unicode string literals are denoted by prefixing them with an L, because L is the first letter of Unicode, or wide, er, okay.

So when you want to declare a C style zero terminated string of unicode characters you use:

wchar_t *wide_text = L"I am wide text, feel my girth!";

and when you want to declare a function that takes this type of string you'd prototype it something like:

void display_message( const wchar_t *text_to_display );

However this being C++ why not use std::wstring for all your Unicode text needs?

Cheers,

Ash

PS: As Volodya said...

Your problem here is the sizeof - you telling it to use the size of a pointer and not the size of the character array you're passing into the function. You can use something like wcslen but had you used std::wstring instead the code would have been simpler.
 
Share this answer
 
v2
Be more specific about the problem.

Basically you can use LPCWSTR type under Windows or std::wstring using STL.
 
Share this answer
 
v2
Be specific...
What i understand is that your function takes ascii value as a argument and you want to pass unicode string ??
If so than use API WideCharToMultiByte.

cbRequest= (wcslen(message)+1)*sizeof(wchar_t *);
 
Share this answer
 
v2
hello,
After doing some trial and error, I found the solution but don't know this is acceptable or not..:~ unless it works. :rolleyes:

cbRequest= wcslen(message)*2
The concept:
wcslen(message) will calculate the lenght of string (1 for 1 char). Coz I was using the unicode, so multiply by 2, will produce wide character (16bit), so i can sent the exact bytes to be written... ;P

Keep looking for the proper solution... :doh:
 
Share this answer
 
v2
Comments
voloda2 3-Nov-10 8:04am    
See my comment in Answer 3. Your solution is basically correct, but multiplication should be obtained using sizeof operator
ShilpiP 3-Nov-10 8:23am    
Nops this is not the acceptable solution
Please try this cbRequest= (wcslen(message)+1)*sizeof(wchar_t *);
Emilio Garavaglia 6-Nov-10 16:59pm    
Shilpi, that should be sizeof(wchar_t), not sizeof(wchar_t*).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900