ms-win32

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Win32 Discipline

Win32 规范准则

Invoke for ANY code touching Windows APIs:
*W
/
*A
functions from kernel32/user32/advapi32/ole32/shell32 and friends, COM interfaces, handles (
HANDLE
,
HWND
,
HDC
), registry, services, or shell — in any language, however trivial the snippet.
任何涉及Windows API的代码都需遵循此准则:包括kernel32/user32/advapi32/ole32/shell32等库中的
*W
/
*A
函数、COM接口、句柄(
HANDLE
HWND
HDC
)、注册表、服务或Shell相关代码——无论使用何种语言,即使是简单的代码片段也不例外。

Non-negotiables

不可协商的规则

  1. Unicode
    W
    APIs only, never
    A
    .
    Text is UTF-16 or UTF-8 only; never
    CP_ACP
    or any ANSI/narrow encoding for durable or protocol text.
  2. Bytes are not UTF-16 code units. Honor exact length contracts and terminators; never assume null termination when a length is supplied.
  3. Check documented failure values.
    FAILED
    /
    SUCCEEDED
    for
    HRESULT
    ;
    ERROR_SUCCESS
    for
    LSTATUS
    ; capture
    GetLastError()
    only where documented — many calls invalidate it.
  4. RAII ownership with matching release for handles, buffers, and COM allocations; never release borrowed or pseudo-handles. Initialize structure size/version fields (
    cbSize
    etc.) as documented; use pointer-sized types (
    DWORD_PTR
    ,
    INT_PTR
    ) without truncation.
When a rule here conflicts with an API's documented contract, the documented contract wins — note the deviation.
  1. 仅使用Unicode
    W
    API,绝对禁止使用
    A
    版本。
    文本仅采用UTF-16或UTF-8编码;对于持久化或协议文本,绝不能使用
    CP_ACP
    或任何ANSI/窄编码。
  2. 字节不等同于UTF-16代码单元。 严格遵守精确的长度约定和终止符;当提供长度时,绝不要假设存在空终止符。
  3. 检查文档化的失败值。
    HRESULT
    使用
    FAILED
    /
    SUCCEEDED
    宏;对
    LSTATUS
    检查
    ERROR_SUCCESS
    ;仅在文档明确说明的情况下捕获
    GetLastError()
    ——许多调用会使该值失效。
  4. 对句柄、缓冲区和COM分配采用RAII所有权机制并匹配释放操作;绝不要释放借用的句柄或伪句柄。 按照文档要求初始化结构体的大小/版本字段(如
    cbSize
    等);使用指针大小的类型(
    DWORD_PTR
    INT_PTR
    ),避免截断。
如果此处的规则与API的文档化约定冲突,以文档化约定为准——请注明偏差情况。