如何查找正确的 Windows 系统错误代码以在我的应用程序中使用?

发布于 2024-09-25 03:05:11 字数 292 浏览 7 评论 0原文

我正在编写一个 C# .NET 2.0 应用程序,其中预计将通过 SerialPort 接收消息。如果未收到帧(即超时)或确定帧无效,我需要使用 SetLastError 设置错误代码。 Windows 有大量错误代码。是否有一个简单的工具或参考来帮助缩小要使用的正确错误代码的范围?

附加信息

虽然我更喜欢抛出异常并在堆栈的更高层处理它,但在这种情况下这不是一个选项,因为我正在更新的应用程序并非设计为采用利用这样一个有用的功能。

I am writing a C# .NET 2.0 application wherein when a message is expected to be received via the SerialPort. If the frame is not received (i.e. times out) or it is determined to be invalid, I need to set an error code using SetLastError. Windows has a plethora of error codes. Is there a simple tool or reference to help narrow down the proper error code to use?

ADDITIONAL INFO

While throwing an exception and handling it higher up the stack is my preference, that is not an option in this case because the application I am updating was not designed to take advantage of such a useful feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

八巷 2024-10-02 03:05:11

不幸的是,上面的方法对我不起作用,但这对我来说非常有效,粘贴整个代码,以便可以直接在 C# 中复制粘贴

public static class WinErrors
{
    /// <summary>
    /// Gets a user friendly string message for a system error code
    /// </summary>
    /// <param name="errorCode">System error code</param>
    /// <returns>Error string</returns>
    public static string GetSystemMessage(uint errorCode)
    {
        var exception = new Win32Exception((int)errorCode);
        return exception.Message;
    }
}

Unfortunately the above didn't work for me, however this worked perfectly for me, pasting the whole code so it can be copy pasted directly in C#

public static class WinErrors
{
    /// <summary>
    /// Gets a user friendly string message for a system error code
    /// </summary>
    /// <param name="errorCode">System error code</param>
    /// <returns>Error string</returns>
    public static string GetSystemMessage(uint errorCode)
    {
        var exception = new Win32Exception((int)errorCode);
        return exception.Message;
    }
}
想你的星星会说话 2024-10-02 03:05:11
using System.Runtime.InteropServices;       // DllImport

public static string GetSystemMessage(int errorCode) {
int capacity = 512;
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
StringBuilder sb = new StringBuilder(capacity);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, errorCode, 0,
    sb, sb.Capacity, IntPtr.Zero);
int i = sb.Length;
if (i>0 && sb[i - 1] == 10) i--;
if (i>0 && sb[i - 1] == 13) i--;
sb.Length = i;
return sb.ToString();
}

[DllImport("kernel32.dll")]
public static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId,
    int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr Arguments);
using System.Runtime.InteropServices;       // DllImport

public static string GetSystemMessage(int errorCode) {
int capacity = 512;
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
StringBuilder sb = new StringBuilder(capacity);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, errorCode, 0,
    sb, sb.Capacity, IntPtr.Zero);
int i = sb.Length;
if (i>0 && sb[i - 1] == 10) i--;
if (i>0 && sb[i - 1] == 13) i--;
sb.Length = i;
return sb.ToString();
}

[DllImport("kernel32.dll")]
public static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId,
    int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr Arguments);
七七 2024-10-02 03:05:11

在“美好的过去”(C 和 C++)中,可能的 Windows 错误列表是在 winerror.h 中定义的

更新: 下面的链接已失效。不确定该文件是否仍然可供下载,但所有 Windows 系统错误代码定义可以在此链接中找到。

该文件可以在 Microsoft 网站 上找到(尽管它让我有点惊讶)它的历史可以追溯到 2003 年 - 可能值得寻找更新的版本)。

但是,如果您收到(或想要设置)Win32 错误代码,则可以在此处找到定义。

in the "good old days" (C and C++), the list of possible Windows errors was defined in winerror.h

UPDATE: Link below is dead. Not sure if the file is still available for download, but all the Windows System Error Code definitions can be found at this link.

This file can be found on Microsoft's site (although it surprises me a little that it is dated as far back as 2003 - might be worth hunting for a more recent version).

But if you're getting (or wanting to set) Win32 error codes, this'll be where the definition is found.

花期渐远 2024-10-02 03:05:11

您可以在这里找到所有这些内容的列表:

http://en.kioskea .net/faq/2347-error-codes-in-windows

然后只需搜索“串行”并使用最适合您的错误的一个

You can find a list of them all here:

http://en.kioskea.net/faq/2347-error-codes-in-windows

Then just do a search for 'Serial' and use whichever one best fits your error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文