How can I find the correct Windows system code to use in my application?

I am writing a C # .NET 2.0 application in which when it is expected that a message will be received through SerialPort. If the frame is not received (i.e., timeout) or is defined as invalid, I need to set the error code with SetLastError. Windows has many error codes. Is there a simple tool or link to help narrow down the correct error code to use?

ADDITIONAL INFORMATION

Throwing an exception and handling it above is my preference, it is not an option in this case, because the application that I am updating is not intended to use such a useful function.

+3
source share
4 answers

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

UPDATE: The link below is dead. I'm not sure that the file is still available for download, but all the definitions of Windows system errors can be found at this link.

This file can be found on the Microsoft website (although it surprises me a little that it dates back to 2003 - it might be worth hunting for a later version).

But if you get (or want to install) Win32 error codes, this will be the definition of the definition.

0
source

Unfortunately, this did not work for me, but it worked fine for me by pasting all the code so that it can be copied to 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;
    }
}
+3
source
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);
+2

You can find their list here:

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

Then just search for "Serial Number" and use what works best for your mistake.

0
source

Source: https://habr.com/ru/post/1767205/


All Articles