You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

哪个Windows API可返回NTSTATUS代码对应的消息文本?

NTSTATUS代码转消息文本的Windows API方案
  • FormatMessage确实无法直接解析NTSTATUS代码(包括HRESULT_FROM_NT转换后的结果),因为系统默认的消息资源不包含NTSTATUS的定义。
  • 可以使用原生NT API RtlGetNtStatusMessage,它专门用于将NTSTATUS代码转换为对应的描述文本。
  • 调用示例(C语言):
#include <ntstatus.h>
#include <ntdll.h>
#include <wchar.h>

void PrintNtStatusMessage(NTSTATUS status) {
    WCHAR msgBuffer[512];
    ULONG msgLength = 0;
    NTSTATUS ret = RtlGetNtStatusMessage(status, msgBuffer, _countof(msgBuffer), &msgLength);
    if (NT_SUCCESS(ret)) {
        wprintf(L"NTSTATUS 0x%08X: %s\n", status, msgBuffer);
    } else {
        wprintf(L"Failed to get message for NTSTATUS 0x%08X\n", status);
    }
}
  • 注意事项:
    • 该API从Windows Vista开始提供,不支持更早的系统版本。
    • 需要链接ntdll.lib,或者通过GetModuleHandle+GetProcAddress动态加载ntdll.dll中的RtlGetNtStatusMessage函数,避免静态依赖。

内容的提问来源于stack exchange,提问作者peterchen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:52:02