If the prototypes of DispatchMessageA and DispatchMessageW are identical, why have both?

Date:November 1, 2018 / year-entry #246
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20181101-00/?p=100105
Comments:    0
Summary:Because there's still a character set dependency.

There are a number of functions, mostly in the window manager, which have both ANSI and Unicode variants, even though the prototypes are identical.

LRESULT WINAPI DispatchMessageW(const MSG*);
LRESULT WINAPI DispatchMessageA(const MSG*);

BOOL WINAPI TranslateMessageW(const MSG*);
BOOL WINAPI TranslateMessageA(const MSG*);

int WINAPI TranslateAcceleratorW(HWND, HACCEL, LPMSG);
int WINAPI TranslateAcceleratorA(HWND, HACCEL, LPMSG);

HACCEL WINAPI CreateAcceleratorTableW(LPACCEL, int);
HACCEL WINAPI CreateAcceleratorTableA(LPACCEL, int);

Why can't these pairs of functions be combined into a single function? Clearly there's no CHAR/WCHAR mismatch, seeing as the parameters are identical.

While it's true that there is no type mismatch, there is still a character set dependency.

For the MSG-based functions, the system needs to know whether the message was obtained via Get­MessageW/Peek­MessageW or via Get­MessageA/Peek­MessageA. If the message is WM_CHAR, then the meaning of the WPARAM changes depending on the character set of the function that obtained the MSG. If you used Get­MessageW/Peek­MessageW, then the WPARAM is a Unicode code unit, but if you used Get­MessageA/Peek­MessageA, then it's an ANSI code unit.

The case of Create­Accelerator­Table is more subtle. Even though the same ACCEL structure is used for both ANSI and Unicode, the meaning of one of the fields changes:

typedef struct tagACCEL {
  BYTE fVirt;
  WORD key;
  WORD cmd;
} ACCEL, *LPACCEL;

If the FVIRTKEY flag is not set in the fVirt member, then the key member contains a character code, and that's the place where a character set dependency sneaks in.



*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index