The Labs \ Source Viewer \ SSCLI \ System.IO.Ports \ InternalResources

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

Developer Fusion