How do I look up the proper Windows System Error Code to use in my application?

烂漫一生 提交于 2019-11-29 15:28:25
PeteH

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.

rboy

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;
    }
}
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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!