73

What is the difference in calling the Win32 API function that have an A character appended to the end as opposed to the W character.

I know it means ASCII and WIDE 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 or W? Correct?

I have found this question, but I don't think it answers my question.

0

1 Answer 1

85

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.

10
  • 15
    +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.
    – BrendanMcK
    Sep 15, 2011 at 2:43
  • 6
    @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, if UNICODE is defined, a call to GetVolumeInformation() is replaced with a call to GetVolumeInformationW(), otherwise it is replaced with a call to GetVolumeInformationA(). Mar 30, 2020 at 4:03
  • 2
    That macro substitution "feature" is a real pain. If you have your own class function called GetVolumeInformation it will be silently renamed to GetVolumeInformationA or GetVolumeInformationW. Using the IDE to look up the definition will take you to the macro definition, not the function. May 28, 2020 at 21:07
  • 2
    @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. May 28, 2020 at 21:36
  • 2
    @Shayan The link in my answer, and earlier comments above, explain the A/W semantics. Those apply to ExA/ExW functions as well, which are usually just "Ex"-tended versions of earlier A/W functions. Oct 19, 2020 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.