C# 从其十六进制数获取 Windows 系统错误代码标题/描述
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1650838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the Windows System Error Code title/description from its hex number
提问by Malfist
I'm messing around with some windows functions using p/invoke. Occasionally, I get an error code that is not ERROR_SUCCESS (such an odd name).
我正在使用 p/invoke 处理一些 Windows 函数。有时,我会收到一个不是 ERROR_SUCCESS(这样一个奇怪的名字)的错误代码。
Is there a way to look these up within the program? Forexample, if I get error 1017. Can I tell the user
有没有办法在程序中查找这些?例如,如果我收到错误 1017。我可以告诉用户吗?
The system has attempted to load or restore a file into the registry, but the specified file is not in a registry file format. (ERROR_NOT_REGISTRY_FILE: 0x3F9)
系统已尝试将文件加载或还原到注册表中,但指定的文件不是注册表文件格式。(ERROR_NOT_REGISTRY_FILE: 0x3F9)
Instead of
代替
Error Code: 1017
错误代码:1017
采纳答案by Nick Meyer
I'm not sure if there's a niifty .NET wrapper, but you could call the FormatMessage API using P/Invoke.
我不确定是否有一个漂亮的 .NET 包装器,但您可以使用 P/Invoke 调用 FormatMessage API。
See this answerfor how it would normally be called from native code. Though the question refers to grabbing error codes from HRESULTs, the answer also applies for retreiving codes from the regular OS error codes coming from GetLastError/GetLastWin32Error).
请参阅此答案以了解通常如何从本机代码调用它。虽然问题是指从 HRESULT 中获取错误代码,但答案也适用于从来自 GetLastError/GetLastWin32Error 的常规操作系统错误代码中检索代码。
EDIT: Thanks Malfist for pointing me to pinvoke.net, which includes alternative, managed API:
编辑:感谢 Malfist 将我指向 pinvoke.net,其中包括替代的托管 API:
using System.ComponentModel;
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Console.WriteLine(errorMessage);
回答by Jon Norton
Yes there's a function that does that but I don't remember what it is. In the mean time, you can use the error lookup tool (Tools->Error Lookup) to see what a particular code means from within Visual Studio.
是的,有一个函数可以做到这一点,但我不记得它是什么。同时,您可以使用错误查找工具(工具->错误查找)从 Visual Studio 中查看特定代码的含义。
回答by David A. Gray
I landed on this page while in search of a managed alternative to calling FormatMessage through P/Invoke.
我在寻找通过 P/Invoke 调用 FormatMessage 的托管替代方案时登陆此页面。
As others have said, there is no way to get those capitalized, underscored names, short of looking them up in winerror.h, which I have seen reproduced online in various places where I landed in the course of searching for information about resolving specific status codes. A quick Google search, for winerror.h, itself, uncovered a page, at Rensselaer Polytechnic Instutute, where someone has helpfully extracted the #define statements from it.
正如其他人所说,没有办法得到那些大写的下划线名称,除非在 winerror.h 中查找它们,我在搜索有关解决特定状态的信息的过程中在不同的地方看到了在线复制代码。在 Google 上快速搜索 winerror.h 本身,发现了Rensselaer Polytechnic Instutute 的一个页面,有人从中提取了#define 语句,很有帮助。
Looking at it gave me an idea; I think there may be a way to get there, working from the source code of winerror.h, which I have, as part of the Windows Platform SDK that ships with every recent version of Microsoft Visual Studio.
看着它给了我一个想法;我认为可能有一种方法可以到达那里,从 winerror.h 的源代码开始工作,我拥有它,作为 Windows 平台 SDK 的一部分,它随每个最新版本的 Microsoft Visual Studio 一起提供。
Right now, I am in the middle of sorting out a pressing issue in the .NET assembly that brought me to this page. Then, I'll see what I can cobble together; this kind of challenge is right up my alley, and somebody threw down a gauntlet.
现在,我正在解决 .NET 程序集中的一个紧迫问题,该问题将我带到了此页面。然后,我会看看我能拼凑出什么;这种挑战就在我的胡同里,有人扔了一个手套。
回答by flapster
You could take the defines from winerror.h at Rensselaer Polytechnic Institute, and put them into an Enum:
您可以从Rensselaer Polytechnic Institute 的winerror.h 中获取定义,并将它们放入 Enum 中:
public enum Win32ErrorCode : long
{
ERROR_SUCCESS = 0L,
NO_ERROR = 0L,
ERROR_INVALID_FUNCTION = 1L,
ERROR_FILE_NOT_FOUND = 2L,
ERROR_PATH_NOT_FOUND = 3L,
ERROR_TOO_MANY_OPEN_FILES = 4L,
ERROR_ACCESS_DENIED = 5L,
etc.
}
Then if your error code is in a variable error_codeyou would use :
然后,如果您的错误代码在变量error_code 中,您将使用:
Enum.GetName(typeof(Win32ErrorCode), error_code);