/mcs/class/System/System.ComponentModel/Win32Exception.cs

https://github.com/letssellsomebananas/mono · C# · 115 lines · 73 code · 11 blank · 31 comment · 2 complexity · adc44ae5ed632115184fc508f2d0f052 MD5 · raw file

  1. //
  2. // System.ComponentModel.Win32Exception.cs
  3. //
  4. // Author:
  5. // Dick Porter (dick@ximian.com)
  6. //
  7. // (C) 2002 Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Security;
  30. using System.Security.Permissions;
  31. using System.Runtime.InteropServices;
  32. using System.Runtime.Serialization;
  33. using System.Collections;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. namespace System.ComponentModel
  37. {
  38. [Serializable, SuppressUnmanagedCodeSecurity]
  39. public class Win32Exception : ExternalException
  40. {
  41. private int native_error_code;
  42. // [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  43. #if TARGET_JVM
  44. [MonoNotSupported("")]
  45. #endif
  46. public Win32Exception ()
  47. : base (W32ErrorMessage (Marshal.GetLastWin32Error ()))
  48. {
  49. native_error_code = Marshal.GetLastWin32Error ();
  50. }
  51. // [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  52. public Win32Exception (int error)
  53. : base (W32ErrorMessage (error))
  54. {
  55. native_error_code = error;
  56. }
  57. // [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  58. public Win32Exception (int error, string message)
  59. : base (message)
  60. {
  61. native_error_code = error;
  62. }
  63. #if NET_2_0
  64. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  65. #if TARGET_JVM
  66. [MonoNotSupported ("")]
  67. #endif
  68. public Win32Exception (string message)
  69. : base (message)
  70. {
  71. native_error_code = Marshal.GetLastWin32Error ();
  72. }
  73. #if TARGET_JVM
  74. [MonoNotSupported ("")]
  75. #endif
  76. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  77. public Win32Exception (string message, Exception innerException)
  78. : base (message, innerException)
  79. {
  80. native_error_code = Marshal.GetLastWin32Error ();
  81. }
  82. #endif
  83. protected Win32Exception(SerializationInfo info,
  84. StreamingContext context)
  85. : base (info, context) {
  86. native_error_code = info.GetInt32 ("NativeErrorCode");
  87. }
  88. public int NativeErrorCode {
  89. get {
  90. return(native_error_code);
  91. }
  92. }
  93. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  94. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  95. {
  96. if (info == null)
  97. throw new ArgumentNullException ("info");
  98. info.AddValue ("NativeErrorCode", native_error_code);
  99. base.GetObjectData (info, context);
  100. }
  101. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  102. internal static extern string W32ErrorMessage (int error_code);
  103. }
  104. }