| [1551] | 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| [1566] | 25 | using System.Runtime.InteropServices; |
|---|
| [1576] | 26 | using System.IO; |
|---|
| [1705] | 27 | using System.ComponentModel; |
|---|
| [1551] | 28 | |
|---|
| 29 | namespace Eraser.Util |
|---|
| 30 | { |
|---|
| [1566] | 31 | public static class Win32ErrorCode |
|---|
| [1551] | 32 | { |
|---|
| [1566] | 33 | /// <summary> |
|---|
| [1872] | 34 | /// Gets the error message associated with the provided system error code. |
|---|
| 35 | /// </summary> |
|---|
| 36 | /// <param name="code">The system error code which should have the message queried.</param> |
|---|
| 37 | /// <returns>The string describing the error with the given error code.</returns> |
|---|
| 38 | public static string GetSystemErrorMessage(int code) |
|---|
| 39 | { |
|---|
| 40 | return new System.ComponentModel.Win32Exception(code).Message; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /// <summary> |
|---|
| [1566] | 44 | /// Converts a Win32 Error code to a HRESULT. |
|---|
| 45 | /// </summary> |
|---|
| 46 | /// <param name="errorCode">The error code to convert.</param> |
|---|
| 47 | /// <returns>A HRESULT value representing the error code.</returns> |
|---|
| [1576] | 48 | private static int GetHRForWin32Error(int errorCode) |
|---|
| [1566] | 49 | { |
|---|
| 50 | const uint FACILITY_WIN32 = 7; |
|---|
| 51 | return errorCode <= 0 ? errorCode : |
|---|
| 52 | (int)((((uint)errorCode) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /// <summary> |
|---|
| 56 | /// Gets a Exception for the given Win32 error code. |
|---|
| 57 | /// </summary> |
|---|
| 58 | /// <param name="errorCode">The error code.</param> |
|---|
| 59 | /// <returns>An exception object representing the error code.</returns> |
|---|
| 60 | internal static Exception GetExceptionForWin32Error(int errorCode) |
|---|
| 61 | { |
|---|
| [1576] | 62 | switch (errorCode) |
|---|
| 63 | { |
|---|
| 64 | case NoError: return null; |
|---|
| [2165] | 65 | case DeviceNotExist: |
|---|
| [2140] | 66 | case NotReady: return new IOException(); |
|---|
| [2079] | 67 | case RequestAborted: return new OperationCanceledException(); |
|---|
| [1872] | 68 | case SharingViolation: return new SharingViolationException(); |
|---|
| [1705] | 69 | } |
|---|
| [1576] | 70 | |
|---|
| [1705] | 71 | int HR = GetHRForWin32Error(errorCode); |
|---|
| 72 | Exception exception = Marshal.GetExceptionForHR(HR); |
|---|
| 73 | if (exception.GetType() == typeof(COMException)) |
|---|
| 74 | throw new Win32Exception(errorCode); |
|---|
| 75 | else |
|---|
| 76 | throw exception; |
|---|
| [1566] | 77 | } |
|---|
| 78 | |
|---|
| 79 | public const int NoError = Success; |
|---|
| 80 | public const int Success = 0; |
|---|
| 81 | public const int InvalidFunction = 1; |
|---|
| 82 | public const int FileNotFound = 2; |
|---|
| 83 | public const int PathNotFound = 3; |
|---|
| 84 | public const int AccessDenied = 5; |
|---|
| 85 | public const int NoMoreFiles = 18; |
|---|
| 86 | public const int NotReady = 21; |
|---|
| 87 | public const int SharingViolation = 32; |
|---|
| [2165] | 88 | public const int DeviceNotExist = 55; |
|---|
| [1566] | 89 | public const int InvalidParameter = 87; |
|---|
| [2150] | 90 | public const int DiskFull = 112; |
|---|
| [2174] | 91 | public const int InsufficientBuffer = 122; |
|---|
| [1566] | 92 | public const int MoreData = 234; |
|---|
| [1701] | 93 | public const int NoMoreItems = 259; |
|---|
| [1566] | 94 | public const int UnrecognizedVolume = 1005; |
|---|
| [1701] | 95 | public const int BadDevice = 1200; |
|---|
| [2079] | 96 | public const int RequestAborted = 1235; |
|---|
| [1566] | 97 | public const int NotAReparsePoint = 4390; |
|---|
| [1551] | 98 | } |
|---|
| 99 | } |
|---|