views:

79

answers:

2

Hi, I have read few articles about different Windows C entry pooints, wmain and WinMain.

So, if I am correct, these are added to C language compilers for Windows OS. But, how are implemented?

For example, wmain gets Unicode as argv[], but its Os that sends these arguments to program, so is there any special field in the .exe file entry which says to windows to pass arguemnts as unicode? Thanks.

+1  A: 

First: a pedantic rant: wmain certainly does not get Unicode arguments. Unicode is defined independently of any particular encoding. wmain gets arguments in a 16 bit character encoding of Unicode, UTF-16 at a guess. I've just checked Microsoft's documentation on wmain and the links from it and it is clear that Microsoft had no clue about what Unicode is when they wrote it.

Anyway, the entry point of a program is defined by the linker. A C program always has a certain amount of prologue code that runs before main/wmain/WinMain/wWinMain. If one of the wide versions of main is used, the prologue code converts the characters in the environment from whatever character set they are in to the wide character version.

JeremyP
Windows NT started off internally using UCS-2, but modern versions use (mostly) UTF-16, which might explain why the documentation isn't explicitly clear. Also, it's unlikely that the prologue code converts from the local encoding to UTF-16 since that would mean that UTF-16 arguments supplied by the caller would pass through an intermediate conversion to and from the local encoding, and since Windows doesn't support UTF-8 as a local encoding, that would be lossy. Therefore I believe it's the other way around.
jamesdlin
@jamesdlin: I was reading the docs as written by Microsoft. I imagine their character handling code is clever enough that if the character set was already UTF-16, no conversion wouild apply.
JeremyP
Why the down vote?
JeremyP
@JeremyP: How would the prologue code know whether the character is UTF-16 unless it got UTF-16 data initially?
jamesdlin
+1  A: 

Modern versions of Windows internally use UTF-16. Therefore, when you launch an executable, all command line arguments likely are passed as UTF-16 from the onset, and the runtime library linked into the launched application either passes the arguments through unscathed (if using wmain) or converts them to the local code encoding automatically (if using main). (Specifically this would be done by wmainCRTStartup/mainCRTStartup which are the actual entry points used for console Windows applications.)

jamesdlin