4

I'm using the win32 api for C in my program to read from a serial port, it seems to be pretty low level stuff. Assuming that there is no better way of reading from a serial port, the CreateFile function involves a LPCWSTR argument, I've read and it looks like LPCWSTR is a wchar_t type. Firstly, I don't really understand the difference between wchar and char, I've read stuff about ansi and unicode, but I don't really know how it applies to my situation.

My program uses a main function, not wmain, and needs to get an argument from the command line and store it in a wchar_t variable. Now I know I could do this if I just made the string up on the spot;

wchar_t variable[1024];
swprintf(variable,1024,L"%s",L"randomstringETC");

Because it looks like the L converts char arrays to wchar arrays. However it does not work when I do;

wchar_t variable[1024];
swprintf(variable,1024,L"%s",Largv[1]);

obviously because it's a syntax error. I guess my question is, is there an easy way to convert normal strings to wchar_t strings?

Or is there a way to avoid this Unicode stuff completely and read from serial another way using C on windows..

Michael
  • 5,994
  • 7
  • 44
  • 56

3 Answers3

4

There is no winapi function named CreateFile. There's CreateFileW and CreateFileA. CreateFile is a macro that maps to one of these real function depending on whether the _UNICODE macro is defined. CreateFileW takes an LPCWSTR (aka const wchar_t*), CreateFileA takes an LPCSTR (aka const char*).

If you are not ready yet to move to Unicode then simply use the CreateFileA() function explicitly. Or change the project setting: Project + Properties, General, Character Set. There's a non-zero cost, the underlying operating system is entirely Unicode based. So CreateFileA() goes through a translation layer that turns the const char* into a const wchar_t* according to the current system code page.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Oh wow nice! I've already got it working with the unicode createfile though so I don't think its worth changing. Although it is the only part of my program that uses wchars. Do you think changing it to createfileA would be a good idea in terms of code neatness/uniformity – Michael Sep 07 '11 at 12:33
  • No, not really. Not using Unicode on an operating system that's entirely Unicode in a language you'd normally select for speed doesn't make a lot of sense to me. Nevertheless, the macro hoopla was explicitly meant to keep old C programs running. Some emphasis on "old". – Hans Passant Sep 07 '11 at 12:57
  • “If you are not ready yet to move to Unicode”—then you should consider inventing a time machine and travel 20 years backwards in time ;-) – Philipp Sep 10 '11 at 12:33
1

MultiByteToWideChar can be used to map from ANSI to UNICODE. To do your swprintf call, you need to define an array of wchar like this:

WCHAR lala[256] = {0};
swprintf(lala, _countof(lala), L"%s", Largv[1]);

It is possible to avoid unicode by compiling your application against a multibyte character set but it's bad practice to do this unless you're doing so for legacy reasons. Windows will need to convert it back to unicode at some point eventually anyway because that is the encoding of the underlying OS.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • Sorry, I wrote the code down wrong, should be variable[1024]; Anyway, the Largv[1] doesn't work like myforwik has explained. – Michael Sep 07 '11 at 12:04
  • You named it Largv so I assumed it was already unicode. You can convert with either the function he specified or MultiByteToWideChar – Mike Kwan Sep 07 '11 at 12:17
1

The L thing is only for string literals.

You need to convert argv string (presumably unsigned char) to wchar by using something like the winapi mbstowcs() function.

Curtis
  • 101,612
  • 66
  • 270
  • 352
myforwik
  • 34
  • 1
  • 3
    far easier to use a wide char main function – David Heffernan Sep 07 '11 at 12:31
  • 1
    @David: Not only easier, but also correct. If you don’t use `wmain`, you have to fetch the command line with `GetCommandLineW`—getting the arguments from `main` is not correct since these aren’t Unicode strings. – Philipp Sep 10 '11 at 12:32