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)
38 StringBuilder sb = new StringBuilder(512);
39 int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
40 NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
41 IntPtr.Zero, (uint) errorCode, 0, sb, sb.Capacity, null);
44 // result is the # of characters copied to the StringBuilder on NT,
45 // but on Win9x, it appears to be the number of MBCS bytes.
46 // Just give up and return the String as-is...
47 String s = sb.ToString();
53 return SR.GetString(SR.IO_UnknownError, errorCode);
58 internal static void FileNotOpen()
60 throw new ObjectDisposedException(null, SR.GetString(SR.Port_not_open));
63 internal static void WrongAsyncResult()
65 throw new ArgumentException(SR.GetString(SR.Arg_WrongAsyncResult));
68 internal static void EndReadCalledTwice()
70 // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
71 throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndReadCalledMultiple));
74 internal static void EndWriteCalledTwice()
76 // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
77 throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndWriteCalledMultiple));
82 [SecuritySafeCritical]
84 internal static void WinIOError()
86 int errorCode = Marshal.GetLastWin32Error();
87 WinIOError(errorCode, String.Empty);
91 [SecuritySafeCritical]
93 internal static void WinIOError(string str)
95 int errorCode = Marshal.GetLastWin32Error();
96 WinIOError(errorCode, str);
99 // After calling GetLastWin32Error(), it clears the last error field,
100 // so you must save the HResult and pass it to this method. This method
101 // will determine the appropriate exception to throw dependent on your
102 // error, and depending on the error, insert a string into the message
103 // gotten from the ResourceManager.
104 internal static void WinIOError(int errorCode, String str)
108 case NativeMethods.ERROR_FILE_NOT_FOUND:
109 case NativeMethods.ERROR_PATH_NOT_FOUND:
111 throw new IOException(SR.GetString(SR.IO_PortNotFound));
113 throw new IOException(SR.GetString(SR.IO_PortNotFoundFileName, str));
115 case NativeMethods.ERROR_ACCESS_DENIED:
117 throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_NoPathName));
119 throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_Path, str));
121 case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
122 throw new PathTooLongException(SR.GetString(SR.IO_PathTooLong));
124 case NativeMethods.ERROR_SHARING_VIOLATION:
127 throw new IOException(SR.GetString(SR.IO_SharingViolation_NoFileName));
129 throw new IOException(SR.GetString(SR.IO_SharingViolation_File, str));
132 throw new IOException(GetMessage(errorCode), MakeHRFromErrorCode(errorCode));
136 // Use this to translate error codes like the above into HRESULTs like
137 // 0x80070006 for ERROR_INVALID_HANDLE
138 internal static int MakeHRFromErrorCode(int errorCode)
140 return unchecked(((int)0x80070000) | errorCode);