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

前端 未结 4 1048
遇见更好的自我
遇见更好的自我 2020-12-21 05:57

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 i

4条回答
  •  心在旅途
    2020-12-21 06:49

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

提交回复
热议问题