.Net框架中是否存在系统错误代码的枚举?

时间:2011-08-08 16:02:43

标签: .net pinvoke

我有一个库函数,它返回GetLastError代码(things like these)。我需要将它们与特定错误进行比较,例如ERROR_INVALID_HANDLE。但是我自己定义常量并不舒服。所以问题是,为此目的是否有预定义的枚举?

6 个答案:

答案 0 :(得分:8)

不,你必须自己制作。

答案 1 :(得分:3)

我为此发布了NuGet package

Install-Package BetterWin32Errors

首先为库添加一个using:

using BetterWin32Errors;

然后你可以像这样使用它:

if (!SomeWin32ApiCall(...))
{
    var error = Win32Exception.GetLastWin32Error();
    if (error == Win32Error.ERROR_FILE_NOT_FOUND)
    {
        // ...do something...
    }
    else if (error == Win32Error.ERROR_PATH_NOT_FOUND)
    {
        // ...do something else...
    }
    else
    {
        throw new Win32Exception(error);
    }
}

有关如何使用该库的更多示例,请参阅the site

答案 2 :(得分:1)

您可以将katmassage添加的http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx代码复制到您自己的类SystemErrorCodes。 它包含从0到499的代码。这是一个很好的启动器。如果有人已经有一个包含所有代码的类,那么他的代码将会受到赞赏。

答案 3 :(得分:0)

Win32Exception类可用于此目的。

    /// <summary>
    /// Setting the system Date and Time
    /// </summary>
    /// <param name="dateAndTime">The date and time settings</param>
    /// <returns>Operation success</returns>
    public static bool SetSystemTimeAndDate(DateTime dateAndTime)
    {
        var newDateAndTime = new Win32SystemTimeStruct(dateAndTime);

        var ret = Win32ApiStub.SetSystemTime(ref newDateAndTime);
        if(!ret)
            ThrowExceptionForHr(Marshal.GetLastWin32Error());
        return ret;
    }

    /// <summary>
    /// This function checks an error code and throws a nice exception if the code is signifying an error.
    /// Do not confuse this with <see cref="Marshal.ThrowExceptionForHR"/> which gets error information out of the context of the current thread.
    /// </summary>
    /// <param name="nativeErrorCode">The HRESULT error code</param>
    public static void ThrowExceptionForHr(int nativeErrorCode)
    {
        if (nativeErrorCode != 0)
            throw new Win32Exception(nativeErrorCode);
    }

答案 4 :(得分:0)

在pinvoke上有公开的课程。我没有尝试在此处发布12.000行。

下载:WINERROR

using System;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Microsoft.Win32.Interop
{
    public class ResultWin32
    {
        public static string GetErrorName(int result)
        {
            FieldInfo[] fields = typeof(ResultWin32).GetFields();
            foreach (FieldInfo fi in fields)
                if ((int)fi.GetValue(null) == result)
                    return fi.Name;
            return String.Empty;
        }

        /// <summary>
        /// The operation completed successfully.
        /// </summary>
        public const int ERROR_SUCCESS = 0;
        /// <summary>
        /// Incorrect function.
        /// </summary>
        public const int ERROR_INVALID_FUNCTION = 1;
        /// <summary>
        /// The system cannot find the file specified.
        /// </summary>
        public const int ERROR_FILE_NOT_FOUND = 2;
        /// <summary>
        /// The system cannot find the path specified.
        /// </summary>



        // !! Don't use this cutout
        // get the whole file at: http://www.pinvoke.net/default.aspx/Constants/WINERROR.html



        public const int COMADMIN_E_SAFERINVALID = (int)(0x80110822 - 0x100000000);
        /// <summary>
        /// The specified user cannot write to the system registry
        /// </summary>
        public const int COMADMIN_E_REGISTRY_ACCESSDENIED = (int)(0x80110823 - 0x100000000);
        /// <summary>
        /// No information avialable.
        /// </summary>
        public const int COMADMIN_E_PARTITIONS_DISABLED = (int)(0x80110824 - 0x100000000);
        /// <summary>
        /// Failed to open a file.
        /// </summary>
        public const int NS_E_FILE_OPEN_FAILED = (int)(0xC00D001DL - 0x100000000);

        public static bool Succeeded(int result)
        {
            return result >= 0;
        }

        public static bool Failed(int result)
        {
            return result < 0;
        }
    }
}

答案 5 :(得分:0)

我的回答最初针对的是 here 所描述的错误,但您的回答似乎只是其中的一个子集。

这里的所有答案都谈到必须手动完成工作。我就是这样做的(删除了上述链接并使用这些值生成了一个枚举类)。

但是 - 令我绝望的是 - 我发现您可以简单地执行 throw new Win32Exception(errorCode) 并获得匹配的错误消息文本。

了解Win32Exception here