const WCHAR *filename question

Started by
8 comments, last by jflanglois 19 years, 7 months ago
This is a pretty dumb question, but I'm just getting back into coding after a long break and I can't remember how to get this to work. I have a function prototype: HRESULT myfunc(const WCHAR *filename); But for the life of me, I can't remember how to feed the filename to it. I've tried myfunc("file.txt"); but that doesn't work. I've also tried to declare a WCHAR* and assign it a string value, but I can't seem to get that to do it either... Like this: WCHAR *myfile; myfile="file.txt"; Anyway, how would I pass this function "file.txt" as the filename to use? Thanks
Advertisement
hmmm, I don't use unicode too much but wouldn't passing into the function like:
myfunc(L"file.txt");

work?

-TOmcAT
-Keith
Hrm, that does prevent the error, but I don't understand the L part. What does that do? Also, I'm still not sure how to assign a value to a WCHAR.
Like
WCHAR myword="somewords";
I know that doesn't work, but I don't know how to do it...
WCHAR is a wide char (i.e. unicode character), so you need to assign it to a unicode string. The following:

WCHAR *str = "foo";

...will give an error because you're assigning an ASCII string to a char (they're different types / different sizes)

In order to assign to a unicode string, you use the prefix "L" before the string:

WCHAR *str = L"foo";

Similarly, when passing a string constant to a function you need to do the same thing, for the same reasons:

myfunc(L"file.txt");


-Alias
WCHAR stands for Wide CHARacter and is coded in 16 bits so you need to add the L before your string to tell the compiler to code it to 16 bits not 8 bits as a normal char would use.

hope that helps !
Matt
Matt
Well, WCHAR myword="somewords"; will not work for 2 reasons, WCHAR can only hold one 16-bit character not a string, and "somewords" is a string of 8-bit characters. So what you need to do is:
WCHAR myword[]=L"somewords";

*Note: The L (or l) prefix is needed for multi-byte characters.

-TOmcAT
-Keith
ok, awesome! That makes a lot more sense then.
A quick note that will make your code work on both ANSI and UNICODE platforms is to use the TCHAR macros provided by windows to write portable string code. TCHAR is expanded to WCHAR if Unicode and _Unicode is defined and to CHAR if not. All of the string functions (strlen, strcpy, strcat, etc.) have corresponding wide character implementations (wcslen, wcscpy, wcscat, etc.) but you can write code that works with both using the functions _tcslen, _tcscpy, _tcscat, which will expand to the appropriate function based on preprocessor defines.

Magius
Thanks, that helps too. I have one more question about it though. Suppose I have a Unicode string. WCHAR mystr[]="mystring"; How would I display that in a messagebox?
When I put:
MessageBox(hWnd,(LPSTR)mystr,"Box Title",MB_OK);
It only shows the first letter...
As far as I know, you shouldn't have to convert it to LPSTR. IIRC MessageBox resolves to MessageBoxW (which it the UNICODE version of MessageBox) when UNICODE is defined. The second argument should be LPCWSTR.

[edit] and the third argument should be preceeded by an L, because it is wide character as well.

This topic is closed to new replies.

Advertisement