What's the difference between these Windows API signatures in Delphi?

前端 未结 2 2014
南旧
南旧 2021-01-05 18:27

Looking Windows.pas in Delphi, I see that there are several signatures as LoadLibrary (A) or (W) for loading a specific module. What are the differences between

2条回答
  •  萌比男神i
    2021-01-05 18:47

    The Windows API provides either ANSI strings (A) or Unicode strings (W). Interally, the Windows API has both available. However, Delphi is defaulted to either one or the other, depending on the version of Delphi. Many other Windows languages do this too. The language uses either ANSI or Unicode strings as a default.

    In Delphi versions prior to 2009, the ANSI API calls were used, suffixed with an A. That was when Delphi primarily used ANSI strings. As of Delphi 2009 and above, Unicode has been enforced. That also made the default API calls to Unicode, suffixed with a W. Unicode has always been supported in Delphi, but as of 2009 it's been enforced as the default, favored over ANSI. In those older versions, functions such as LoadLibrary mapped to the ANSI version LoadLibraryA.

    The particular API call you're referring to LoadLibrary is available as either LoadLibraryA or LoadLibraryW. Windows.h also provides a generic LoadLibrary function, which internally uses the preferred Unicode version. The A and W difference provides developers the option for backwards compatibility, as with many of Microsoft's products. If the language is primarily ANSI Strings, you can explicitly use Unicode. Or, if the language is primarily Unicode, you can explicitly use ANSI.

    Long story short, at one point Windows itself made the switch from ANSI strings to Unicode strings as default. However still provided backwards compatibility. Later Delphi versions have been changed to use whatever the preferred defaults are - in this case they're Unicode.

    To summarize:

    • LoadLibraryA - Loads library via ANSI strings
    • LoadLibraryW - Loads library via Unicode strings
    • LoadLibrary - Loads library using preferred default (Unicode Strings)
    • Ancient versions of Windows used only ANSI strings before introducing Unicode
    • Windows provided backwards compatibility by defaulting to ANSI versions - but eventually switched defaults to Unicode (not sure at which version)

    You can learn more about Microsoft's introduction of Unicode here as well as here.

提交回复
热议问题