3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: InternalResources
12 ===========================================================*/
15 using System.Security;
17 using Marshal=System.Runtime.InteropServices.Marshal;
18 using Microsoft.Win32;
20 namespace System.IO.Ports
22 internal static class InternalResources
24 // Beginning of static Error methods
26 internal static void EndOfFile()
28 throw new EndOfStreamException(SR.GetString(SR.IO_EOF_ReadBeyondEOF));
33 [SecuritySafeCritical]
35 internal static String GetMessage(int errorCode)
37 StringBuilder sb = new StringBuilder(512);
38 int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
39 NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
40 IntPtr.Zero, (uint) errorCode, 0, sb, sb.Capacity, null);
43 // result is the # of characters copied to the StringBuilder on NT,
44 // but on Win9x, it appears to be the number of MBCS bytes.
45 // Just give up and return the String as-is...
46 String s = sb.ToString();
51 return SR.GetString(SR.IO_UnknownError, errorCode);
56 internal static void FileNotOpen()
58 throw new ObjectDisposedException(null, SR.GetString(SR.Port_not_open));
61 internal static void WrongAsyncResult()
63 throw new ArgumentException(SR.GetString(SR.Arg_WrongAsyncResult));
66 internal static void EndReadCalledTwice()
68 // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
69 throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndReadCalledMultiple));
72 internal static void EndWriteCalledTwice()
74 // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
75 throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndWriteCalledMultiple));
80 [SecuritySafeCritical]
82 internal static void WinIOError()
84 int errorCode = Marshal.GetLastWin32Error();
85 WinIOError(errorCode, String.Empty);
89 [SecuritySafeCritical]
91 internal static void WinIOError(string str)
93 int errorCode = Marshal.GetLastWin32Error();
94 WinIOError(errorCode, str);
97 // After calling GetLastWin32Error(), it clears the last error field,
98 // so you must save the HResult and pass it to this method. This method
99 // will determine the appropriate exception to throw dependent on your
100 // error, and depending on the error, insert a string into the message
101 // gotten from the ResourceManager.
102 internal static void WinIOError(int errorCode, String str)
106 case NativeMethods.ERROR_FILE_NOT_FOUND:
107 case NativeMethods.ERROR_PATH_NOT_FOUND:
109 throw new IOException(SR.GetString(SR.IO_PortNotFound));
111 throw new IOException(SR.GetString(SR.IO_PortNotFoundFileName, str));
113 case NativeMethods.ERROR_ACCESS_DENIED:
115 throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_NoPathName));
117 throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_Path, str));
119 case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
120 throw new PathTooLongException(SR.GetString(SR.IO_PathTooLong));
122 case NativeMethods.ERROR_SHARING_VIOLATION:
125 throw new IOException(SR.GetString(SR.IO_SharingViolation_NoFileName));
127 throw new IOException(SR.GetString(SR.IO_SharingViolation_File, str));
130 throw new IOException(GetMessage(errorCode), MakeHRFromErrorCode(errorCode));
134 // Use this to translate error codes like the above into HRESULTs like
135 // 0x80070006 for ERROR_INVALID_HANDLE
136 internal static int MakeHRFromErrorCode(int errorCode)
138 return unchecked(((int)0x80070000) | errorCode);