/mcs/class/referencesource/System/sys/system/IO/ports/InternalResources.cs

https://github.com/kumpera/mono · C# · 148 lines · 104 code · 18 blank · 26 comment · 10 complexity · ca18a1e45d854500aa25a4665fe370f7 MD5 · raw file

  1. // ==++==
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // ==--==
  6. /*============================================================
  7. **
  8. ** Class: InternalResources
  9. **
  10. ** Date: August 2002
  11. **
  12. ===========================================================*/
  13. using System.IO;
  14. using System.Security;
  15. using System.Text;
  16. using Marshal=System.Runtime.InteropServices.Marshal;
  17. using Microsoft.Win32;
  18. namespace System.IO.Ports
  19. {
  20. internal static class InternalResources
  21. {
  22. // Beginning of static Error methods
  23. #if !FEATURE_NETCORE
  24. internal static void EndOfFile()
  25. {
  26. throw new EndOfStreamException(SR.GetString(SR.IO_EOF_ReadBeyondEOF));
  27. }
  28. #endif
  29. #if FEATURE_NETCORE
  30. [SecuritySafeCritical]
  31. #endif
  32. internal static String GetMessage(int errorCode)
  33. {
  34. #if !MONO
  35. StringBuilder sb = new StringBuilder(512);
  36. int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
  37. NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
  38. IntPtr.Zero, (uint) errorCode, 0, sb, sb.Capacity, null);
  39. if (result != 0)
  40. {
  41. // result is the # of characters copied to the StringBuilder on NT,
  42. // but on Win9x, it appears to be the number of MBCS bytes.
  43. // Just give up and return the String as-is...
  44. String s = sb.ToString();
  45. return s;
  46. }
  47. else
  48. #endif
  49. {
  50. return SR.GetString(SR.IO_UnknownError, errorCode);
  51. }
  52. }
  53. #if !FEATURE_NETCORE
  54. internal static void FileNotOpen()
  55. {
  56. throw new ObjectDisposedException(null, SR.GetString(SR.Port_not_open));
  57. }
  58. internal static void WrongAsyncResult()
  59. {
  60. throw new ArgumentException(SR.GetString(SR.Arg_WrongAsyncResult));
  61. }
  62. internal static void EndReadCalledTwice()
  63. {
  64. // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
  65. throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndReadCalledMultiple));
  66. }
  67. internal static void EndWriteCalledTwice()
  68. {
  69. // Should ideally be InvalidOperationExc but we can't maintain parity with Stream and SerialStream without some work
  70. throw new ArgumentException(SR.GetString(SR.InvalidOperation_EndWriteCalledMultiple));
  71. }
  72. #endif
  73. #if !MONO
  74. #if FEATURE_NETCORE
  75. [SecuritySafeCritical]
  76. #endif
  77. internal static void WinIOError()
  78. {
  79. int errorCode = Marshal.GetLastWin32Error();
  80. WinIOError(errorCode, String.Empty);
  81. }
  82. #if FEATURE_NETCORE
  83. [SecuritySafeCritical]
  84. #endif
  85. internal static void WinIOError(string str)
  86. {
  87. int errorCode = Marshal.GetLastWin32Error();
  88. WinIOError(errorCode, str);
  89. }
  90. #endif
  91. // After calling GetLastWin32Error(), it clears the last error field,
  92. // so you must save the HResult and pass it to this method. This method
  93. // will determine the appropriate exception to throw dependent on your
  94. // error, and depending on the error, insert a string into the message
  95. // gotten from the ResourceManager.
  96. internal static void WinIOError(int errorCode, String str)
  97. {
  98. switch (errorCode)
  99. {
  100. case NativeMethods.ERROR_FILE_NOT_FOUND:
  101. case NativeMethods.ERROR_PATH_NOT_FOUND:
  102. if (str.Length == 0)
  103. throw new IOException(SR.GetString(SR.IO_PortNotFound));
  104. else
  105. throw new IOException(SR.GetString(SR.IO_PortNotFoundFileName, str));
  106. case NativeMethods.ERROR_ACCESS_DENIED:
  107. if (str.Length == 0)
  108. throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_NoPathName));
  109. else
  110. throw new UnauthorizedAccessException(SR.GetString(SR.UnauthorizedAccess_IODenied_Path, str));
  111. case NativeMethods.ERROR_FILENAME_EXCED_RANGE:
  112. throw new PathTooLongException(SR.GetString(SR.IO_PathTooLong));
  113. case NativeMethods.ERROR_SHARING_VIOLATION:
  114. // error message.
  115. if (str.Length == 0)
  116. throw new IOException(SR.GetString(SR.IO_SharingViolation_NoFileName));
  117. else
  118. throw new IOException(SR.GetString(SR.IO_SharingViolation_File, str));
  119. default:
  120. throw new IOException(GetMessage(errorCode), MakeHRFromErrorCode(errorCode));
  121. }
  122. }
  123. // Use this to translate error codes like the above into HRESULTs like
  124. // 0x80070006 for ERROR_INVALID_HANDLE
  125. internal static int MakeHRFromErrorCode(int errorCode)
  126. {
  127. return unchecked(((int)0x80070000) | errorCode);
  128. }
  129. }
  130. }