-
Notifications
You must be signed in to change notification settings - Fork 18
Windows:优先使用原生API而非Win32 #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cers000
wants to merge
1
commit into
zigcc:main
Choose a base branch
from
cers000:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| --- | ||
| .title = "Windows:优先使用原生API而非Win32", | ||
| .date = @date("2026-2-13T20:11:51+0800"), | ||
| .author = "cers", | ||
| .layout = "post.shtml", | ||
| --- | ||
|
|
||
| ## **摘要** | ||
|
|
||
| 这篇发布于2026年2月5日的 Zig 语言 Issues(编号 #31131),标题为 “Windows: Prefer the Native API over Win32”,详细阐述了 Zig 项目在 Windows 平台上的一项重要技术策略调整:即优先使用更底层的 Native API(由 ntdll.dll 导出),而不是传统的 Win32 API(由 kernel32.dll 等导出)。 | ||
|
|
||
| ## **1\. 核心主张:为何要“弃用”Win32,转向Native?** | ||
|
|
||
| 作者并非 Zig 核心成员,但深度参与了相关工作。他提出,现代 Windows 的 Win32 API 本质上是构建于 Native API 之上的一个“子系统”。Native API 是内核直接提供的、更接近底层的接口。Zig 选择优先使用 Native API,主要基于以下**四大优势**: | ||
|
|
||
| 1. **性能**:绕过 Win32 这一软件层,直接与内核交互,减少调用开销。 | ||
| 2. **能力**:Native API 暴露了更多 Win32 未提供的功能和信息(例如,更丰富的文件变化通知 `NtNotifyChangeDirectoryFileEx`)。 | ||
| 3. **依赖**:减少对 `kernel32.dll` 等子系统 DLL 的依赖,有助于生成更小、更精简的可执行文件,并能在 Windows 启动早期运行。 | ||
| 4. **灵活性与错误处理**: | ||
| * Native API 直接返回丰富的 `NTSTATUS` 错误码,而 Win32 通常只返回 `BOOL`,失败后还需调用 `GetLastError` 查询,多了一层转换。 | ||
| * 使用的数据类型(如时间 `LARGE_INTEGER`、权限掩码 `ACCESS_MASK`)更现代、更统一,更易于与 Zig 的语言特性(如打包结构体)结合。 | ||
|
|
||
| ### **2\. 潜在风险与挑战** | ||
|
|
||
| 作者也坦诚地列出了使用 Native API 的**固有风险**,这也是为什么这是一个需要审慎决策的策略: | ||
|
|
||
| 1. **缺乏文档**:Native API 大部分未公开,学习和使用难度远高于有完善文档的 Win32 API。 | ||
| 2. **变更风险**:作为“内部实现细节”,微软理论上可以随时修改它而不做通知。不过作者指出,实践中这类破坏性变更非常罕见,因为微软自身的大量工具也依赖它。 | ||
| 3. **兼容性问题**:可能影响程序在**旧版Windows**(Zig官方只支持Win10/11及对应服务器版)或 **Wine** 上的运行。 | ||
| 4. **安全软件误报**:调用底层、非标准API的行为可能更容易被安全软件标记为可疑。 | ||
|
|
||
| ### **3\. 具体实践:哪些该换,哪些该留?** | ||
|
|
||
| 该策略并非一刀切。作者详细划分了替换的界限: | ||
|
|
||
| | 类别 | 示例 | 说明 | | ||
| | :--- | :--- | :--- | | ||
| | **可替换的(“公平游戏”)** | **ABI兼容的转发器**(如 `ReleaseSRWLockExclusive` 直接转发给 `RtlReleaseSRWLockExclusive`)、**简单包装器**(如 `CopySid`)、**组合API**(如 `CreateIoCompletionPort`) | 这些Win32函数本质上是Native API的简单封装,可以直接替换以获得更清晰的错误处理和性能提升。 | | ||
| | **过于复杂,暂不替换** | **控制台API**(已在讨论过程中被重写)、**Winsock API**、**动态库加载**、**进程创建**(`NtCreateUserProcess` 因需与 `csrss.exe` 复杂交互而异常脆弱) | 这些领域要么实现过于复杂,要么与Windows子系统的深层机制绑定,使用Native API的收益远低于风险和复杂度。 | | ||
|
|
||
| ### **4\. 对常见质疑的回应** | ||
|
|
||
| 议题最后,作者以问答形式回应了社区可能存在的疑虑,展现了Zig团队务实的态度: | ||
|
|
||
| * **问:如果我直接用了 `std.os.windows.kernel32` 里的函数,它们被移除了怎么办?** | ||
| * **答:** 会编译报错。建议需要稳定Win32调用的用户,将函数定义拷贝到自己项目中,或使用专门的绑定库(如 `zigwin32`)。 | ||
| * **问:这会影响旧版Windows兼容性吗?** | ||
| * **答:** 会的。但Zig标准库的目标平台是基于**开发者能支持的最新稳定版本**(Win10/11)。追求极致向后兼容的用户应直接调用Win32。 | ||
| * **问:在Wine上出问题了怎么办?** | ||
| * **答:** 我们视其为Wine的bug。除非严重影响CI测试,否则不会主动为Wine添加特殊工作区。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * **问:微软更改Native API怎么办?** | ||
| * **答:** 首先,Native API的设计(如信息类枚举+可变长结构)本身就具备很强的扩展性,旧API极少被移除,只会增加新版本。其次,如果有变更,Zig标准库会负责处理,用户代码无需感知。 | ||
|
|
||
| ## **5\. 总结** | ||
|
|
||
| 这篇Issues不仅仅是一个技术讨论,它清晰地阐述了 Zig 在系统编程语言定位下的一个具体决策:**为了追求更高的性能、更强的能力和更简洁的代码,宁愿承担使用未充分文档API的风险,也要尽可能地接近操作系统底层**。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| 它展示了Zig团队在**理想目标**(性能、控制力)与**现实挑战**(兼容性、稳定性、文档缺失)之间所做的精细权衡。对于Zig开发者而言,这意味着在Windows平台上编写代码时,可以期待标准库提供更底层、更强大的能力,但也需要理解这种选择所带来的边界(如官方支持的Windows版本范围)。 | ||
|
|
||
| #### **Works cited** | ||
|
|
||
| 1. Windows: Prefer the Native API over Win32 · Issue \#31131 · ziglang/zig \- Codeberg, accessed 2026年2月6日, [https://codeberg.org/ziglang/zig/issues/31131](https://codeberg.org/ziglang/zig/issues/31131) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
标题(h2, h3, h4)默认已经有加粗或放大的样式,在 Markdown 源文件中再用
**...**对标题文字进行加粗是多余的。为了保持 Markdown 源码的简洁性,建议移除标题中的**。这个建议也适用于文中的所有标题(如第12、23、32、41、54、60行)。