简体   繁体   English

WinAPI Unicode 和 ANSI 函数

[英]WinAPI Unicode and ANSI functions

Most of WinAPI calls have Unicode and ANSI function call大多数 WinAPI 调用都有UnicodeANSI函数调用

For examble:例如:

function MessageBoxA(hWnd: HWND; lpText, lpCaption: LPCSTR; uType: UINT): Integer; stdcall;external user32;

function MessageBoxW(hWnd: HWND; lpText, lpCaption: LPCWSTR; uType: UINT): Integer; stdcall; external user32;

When should i use the ANSI function rather than calling the Unicode function ?我什么时候应该使用 ANSI 函数而不是调用 Unicode 函数?

Just as (rare) exceptions to the posted comments/answers...就像发布的评论/答案的(罕见)例外一样......

One may choose to use the ANSI calls in cases where UTF-8 is expected and supported.在需要和支持 UTF-8 的情况下,可以选择使用 ANSI 调用。 For an example, WriteConsoleA'ing UTF-8 strings in a console set to use a TT font and running under chcp 65001.例如,在控制台中写入 UTF-8 字符串以使用 TT 字体并在 chcp 65001 下运行。

Another oddball exception is functions that are primarily implemented as ANSI, where the Unicode "W" variant simply converts to a narrow string in the active codepage and calls the "A" counterpart.另一个奇怪的例外是主要作为 ANSI 实现的函数,其中 Unicode“W”变体简单地转换为活动代码页中的窄字符串并调用“A”对应物。 For such a function, and when a narrow string is available, calling the "A" variant directly saves a redundant double conversion.对于这样的函数,当窄字符串可用时,直接调用“A”变体可以节省多余的双转换。 Case in point is OutputDebugString, which fell into this category until Windows 10 (I just noticed https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362.aspx which mentions that a call to WaitForDebugEventEx - only available since Windows 10 - enables true Unicode output for OutputDebugStringW).一个典型的例子是 OutputDebugString,它在 Windows 10 之前属于这个类别(我刚刚注意到https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362.aspx ,其中提到对 WaitForDebugEventEx 的调用 - 仅自 Windows 10 起可用 - 为 OutputDebugStringW 启用真正的 Unicode 输出)。

Then there are APIs which, even though dealing with strings, are natively ANSI.还有一些 API,尽管处理字符串,但本质上是 ANSI。 For example GetProcAddress only exists in the ANSI variant which takes a LPCSTR argument, since names in the export tables are narrow strings.例如 GetProcAddress 仅存在于采用 LPCSTR 参数的 ANSI 变体中,因为导出表中的名称是窄字符串。

That said, by an large most string-related APIs are natively Unicode and one is encouraged use the "W" variants.也就是说,大多数与字符串相关的 API 都是原生 Unicode,并且鼓励使用“W”变体。 Not all the newer APIs even have an "A" variant any longer (eg CommandLineToArgvW ).并非所有较新的 API 都不再具有“A”变体(例如CommandLineToArgvW )。 From the horses's mouth https://msdn.microsoft.com/en-us/library/windows/desktop/ff381407.aspx :从马嘴https://msdn.microsoft.com/en-us/library/windows/desktop/ff381407.aspx

Windows natively supports Unicode strings for UI elements, file names, and so forth. Windows 本身支持 UI 元素、文件名等的 Unicode 字符串。 Unicode is the preferred character encoding, because it supports all character sets and languages. Unicode 是首选的字符编码,因为它支持所有字符集和语言。 Windows represents Unicode characters using UTF-16 encoding, in which each character is encoded as a 16-bit value. Windows 使用 UTF-16 编码表示 Unicode 字符,其中每个字符都编码为 16 位值。 UTF-16 characters are called wide characters, to distinguish them from 8-bit ANSI characters. UTF-16 字符称为宽字符,以区别于 8 位 ANSI 字符。

[...] When Microsoft introduced Unicode support to Windows, it eased the transition by providing two parallel sets of APIs, one for ANSI strings and the other for Unicode strings. [...] 当 Microsoft 向 Windows 引入 Unicode 支持时,它通过提供两组并行的 API 来简化转换,一组用于 ANSI 字符串,另一组用于 Unicode 字符串。

[...] Internally, the ANSI version translates the string to Unicode. [...] 在内部,ANSI 版本将字符串转换为 Unicode。 The Windows headers also define a macro that resolves to the Unicode version when the preprocessor symbol UNICODE is defined or the ANSI version otherwise. Windows 标头还定义了一个宏,该宏在定义预处理器符号 UNICODE 时解析为 Unicode 版本,否则解析为 ANSI 版本。

[...] Most newer APIs in Windows have just a Unicode version, with no corresponding ANSI version. [...] Windows 中大多数较新的 API 只有一个 Unicode 版本,没有相应的 ANSI 版本。

[ NOTE ] The post was edited to add the last two paragraphs. [注意]该帖子已被编辑以添加最后两段。

The simplest rule to follow is this: Only use the ANSI variants on systems that do not have the Unicode variant.要遵循的最简单规则是:仅在没有 Unicode 变体的系统上使用 ANSI 变体。 That is on Windows 95, 98 and ME, which are the versions of Windows that do not support Unicode.这是在 Windows 95、98 和 ME 上,它们是不支持 Unicode 的 Windows 版本。

These days, it is exceptionally unlikely that you will be targeting such versions, and so in all probability you should always just use the Unicode variants.如今,您极不可能以此类版本为目标,因此您应该始终只使用 Unicode 变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM