What is the difference between the `A` and `W` functions in the Win32 API?
The A
functions use Ansi (not ASCII) strings as input and output, and the W
functions use Unicode string instead (UCS-2 on NT4 and earlier, UTF-16 on W2K and later). Refer to MSDN for more details.
Admin
Updated on June 18, 2022Comments
-
Admin almost 2 years
What is the difference in calling the Win32 API function that have an
A
character appended to the end as opposed to theW
character.I know it means
ASCII
andWIDE CHARACTER
or Unicode, but what is the difference in the output or the input?For example, If I call GetDefaultCommConfigA, will it fill my COMMCONFIG structure with ASCII strings instead of WCHAR strings? (Or vice-versa for GetDefaultCommConfigW)
In other words, how do I know what Encoding the string is in, ASCII or UNICODE, it must be by the version of the function I call
A
orW
? Correct?I have found this question, but I don't think it answers my question.
-
BrendanMcK over 12 years+1 - also another thing to keep in mind is that all the structures that are passed as parameters to these also come in A and W versions; so for example RegisterClassExA takes a WNDCLASSEXA which uses ANSI LPCSTRs, and while the W versions use LPCWSTRs - this prevents you from passing WCHARs to the A version, and vice versa - if you try, you'll get a compiler error about the types not matching - a fairly common beginner error is to use a cast to make the error "go away" instead of changing the code to use the right type without a cast.
-
yu quan about 4 yearsHow does the function without "A" or "W" postfix work? Say "GetVolumeInformation" function, does it call the GetVolumeInformationA or GetVolumeInformationW?
-
Remy Lebeau about 4 years@yuquan kind of. The non-A/W functions are implemented as preprocessor macros that map to the appropriate A/W functions. They do not call the A/W functions, but rather are replaced with them during the preprocessor stage, depending on whether
UNICODE
is defined or not. So, for example, ifUNICODE
is defined, a call toGetVolumeInformation()
is replaced with a call toGetVolumeInformationW()
, otherwise it is replaced with a call toGetVolumeInformationA()
. -
Mark Ransom almost 4 yearsThat macro substitution "feature" is a real pain. If you have your own class function called
GetVolumeInformation
it will be silently renamed toGetVolumeInformationA
orGetVolumeInformationW
. Using the IDE to look up the definition will take you to the macro definition, not the function. -
Remy Lebeau almost 4 years@MarkRansom Yup, and all because Microsoft won't get with the times and support C++ properly, such as by using function overloads and/or template functions rather than the preprocessor.
-
Mark Ransom almost 4 yearsTo be fair the Win32 API still has to work from C, especially back when wide-char support was first introduced, so a C++ only solution wasn't possible. But at this point there's no excuse for not creating something better and giving you a way of opting out of the macros.
-
Remy Lebeau almost 4 years@MarkRansom "the Win32 API still has to work from C" - yes, but that doesn't mean it can't use proper wrappers for C++. It already does that for COM interfaces, so there is no reason why it can't do that for functions, too.
-
Mark Ransom almost 4 yearsI think we're in agreement.
-
Shayan over 3 yearsWhat are ExA and ExW functions? What's the difference between these all?
-
Remy Lebeau over 3 years@Shayan The link in my answer, and earlier comments above, explain the
A
/W
semantics. Those apply toExA
/ExW
functions as well, which are usually just "Ex"-tended versions of earlierA
/W
functions.