Sign in to follow this  
Ranthalion

const WCHAR *filename question

Recommended Posts

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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this