1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Runtime.InteropServices; 6 using System.Runtime.Serialization; 7 using System.Security.Permissions; 8 9 namespace System.Management 10 { 11 12 /// <summary> 13 /// <para>Represents the enumeration of all WMI error codes that are currently defined.</para> 14 /// </summary> 15 public enum ManagementStatus 16 { 17 /// <summary> 18 /// The operation was successful. 19 /// </summary> 20 NoError = 0, 21 /// <summary> 22 /// <para> This value is returned when no more objects 23 /// are available, the number of objects returned is less than the number requested, 24 /// or at the end of an enumeration. It is also returned when the method is called 25 /// with a value of 0 for the <paramref name="uCount"/> parameter.</para> 26 /// </summary> 27 False = 1, 28 /// <summary> 29 /// <para>An overridden property was deleted. This value is 30 /// returned to signal that the original, non-overridden value has been restored as a 31 /// result of the deletion.</para> 32 /// </summary> 33 ResetToDefault = 0x40002, 34 /// <summary> 35 /// <para> The compared items (such as objects and classes) 36 /// are not identical.</para> 37 /// </summary> 38 Different = 0x40003, 39 /// <summary> 40 /// <para> A call timed out. This is not an 41 /// error condition; therefore, some results may have been returned.</para> 42 /// </summary> 43 Timedout = 0x40004, 44 /// <summary> 45 /// <para> No more data is available from the enumeration; the 46 /// user should terminate the enumeration. </para> 47 /// </summary> 48 NoMoreData = 0x40005, 49 /// <summary> 50 /// <para> The operation was 51 /// canceled.</para> 52 /// </summary> 53 OperationCanceled = 0x40006, 54 /// <summary> 55 /// <para>A request is still in progress; however, the results are not 56 /// yet available.</para> 57 /// </summary> 58 Pending = 0x40007, 59 /// <summary> 60 /// <para> More than one copy of the same object was detected in 61 /// the result set of an enumeration. </para> 62 /// </summary> 63 DuplicateObjects = 0x40008, 64 /// <summary> 65 /// <para>The user did not receive all of the requested objects 66 /// because of inaccessible resources (other than security violations).</para> 67 /// </summary> 68 PartialResults = 0x40010, 69 /// <summary> 70 /// <para>The call failed.</para> 71 /// </summary> 72 Failed = unchecked((int)0x80041001), 73 /// <summary> 74 /// <para> The object could not be found. </para> 75 /// </summary> 76 NotFound = unchecked((int)0x80041002), 77 /// <summary> 78 /// The current user does not have permission to perform the 79 /// action. 80 /// </summary> 81 AccessDenied = unchecked((int)0x80041003), 82 /// <summary> 83 /// <para> The provider failed after 84 /// initialization. </para> 85 /// </summary> 86 ProviderFailure = unchecked((int)0x80041004), 87 /// <summary> 88 /// A type mismatch occurred. 89 /// </summary> 90 TypeMismatch = unchecked((int)0x80041005), 91 /// <summary> 92 /// There was not enough memory for the operation. 93 /// </summary> 94 OutOfMemory = unchecked((int)0x80041006), 95 /// <summary> 96 /// <para>The context object is not valid.</para> 97 /// </summary> 98 InvalidContext = unchecked((int)0x80041007), 99 /// <summary> 100 /// <para> One of the parameters to the call is not correct. 101 /// </para> 102 /// </summary> 103 InvalidParameter = unchecked((int)0x80041008), 104 /// <summary> 105 /// <para> The resource, typically a remote server, is not 106 /// currently available. </para> 107 /// </summary> 108 NotAvailable = unchecked((int)0x80041009), 109 /// <summary> 110 /// <para>An internal, critical, and unexpected error occurred. 111 /// Report this error to Microsoft Product Support Services.</para> 112 /// </summary> 113 CriticalError = unchecked((int)0x8004100A), 114 /// <summary> 115 /// <para>One or more network packets were corrupted during a remote session.</para> 116 /// </summary> 117 InvalidStream = unchecked((int)0x8004100B), 118 /// <summary> 119 /// <para> The feature or operation is not supported. </para> 120 /// </summary> 121 NotSupported = unchecked((int)0x8004100C), 122 /// <summary> 123 /// The specified base class is not valid. 124 /// </summary> 125 InvalidSuperclass = unchecked((int)0x8004100D), 126 /// <summary> 127 /// <para> The specified namespace could not be found. </para> 128 /// </summary> 129 InvalidNamespace = unchecked((int)0x8004100E), 130 /// <summary> 131 /// The specified instance is not valid. 132 /// </summary> 133 InvalidObject = unchecked((int)0x8004100F), 134 /// <summary> 135 /// The specified class is not valid. 136 /// </summary> 137 InvalidClass = unchecked((int)0x80041010), 138 /// <summary> 139 /// A provider referenced in the schema does not have a 140 /// corresponding registration. 141 /// </summary> 142 ProviderNotFound = unchecked((int)0x80041011), 143 /// <summary> 144 /// A provider referenced in the schema has an incorrect or 145 /// incomplete registration. 146 /// </summary> 147 InvalidProviderRegistration = unchecked((int)0x80041012), 148 /// <summary> 149 /// COM cannot locate a provider referenced in the schema. 150 /// </summary> 151 ProviderLoadFailure = unchecked((int)0x80041013), 152 /// <summary> 153 /// A component, such as a provider, failed to initialize for internal reasons. 154 /// </summary> 155 InitializationFailure = unchecked((int)0x80041014), 156 /// <summary> 157 /// A networking error that prevents normal operation has 158 /// occurred. 159 /// </summary> 160 TransportFailure = unchecked((int)0x80041015), 161 /// <summary> 162 /// <para> The requested operation is not valid. This error usually 163 /// applies to invalid attempts to delete classes or properties. </para> 164 /// </summary> 165 InvalidOperation = unchecked((int)0x80041016), 166 /// <summary> 167 /// The query was not syntactically valid. 168 /// </summary> 169 InvalidQuery = unchecked((int)0x80041017), 170 /// <summary> 171 /// <para>The requested query language is not supported.</para> 172 /// </summary> 173 InvalidQueryType = unchecked((int)0x80041018), 174 /// <summary> 175 /// <para>In a put operation, the <see langword='wbemChangeFlagCreateOnly'/> 176 /// flag was specified, but the instance already exists.</para> 177 /// </summary> 178 AlreadyExists = unchecked((int)0x80041019), 179 /// <summary> 180 /// <para>The add operation cannot be performed on the qualifier 181 /// because the owning object does not permit overrides.</para> 182 /// </summary> 183 OverrideNotAllowed = unchecked((int)0x8004101A), 184 /// <summary> 185 /// <para> The user attempted to delete a qualifier that was not 186 /// owned. The qualifier was inherited from a parent class. </para> 187 /// </summary> 188 PropagatedQualifier = unchecked((int)0x8004101B), 189 /// <summary> 190 /// <para> The user attempted to delete a property that was not 191 /// owned. The property was inherited from a parent class. </para> 192 /// </summary> 193 PropagatedProperty = unchecked((int)0x8004101C), 194 /// <summary> 195 /// The client made an unexpected and illegal sequence of 196 /// calls. 197 /// </summary> 198 Unexpected = unchecked((int)0x8004101D), 199 /// <summary> 200 /// <para>The user requested an illegal operation, such as 201 /// spawning a class from an instance.</para> 202 /// </summary> 203 IllegalOperation = unchecked((int)0x8004101E), 204 /// <summary> 205 /// <para> There was an illegal attempt to specify a key qualifier 206 /// on a property that cannot be a key. The keys are specified in the class 207 /// definition for an object and cannot be altered on a per-instance basis.</para> 208 /// </summary> 209 CannotBeKey = unchecked((int)0x8004101F), 210 /// <summary> 211 /// <para>The current object is not a valid class definition. 212 /// Either it is incomplete, or it has not been registered with WMI using 213 /// <see cref='System.Management.ManagementObject.Put'/>().</para> 214 /// </summary> 215 IncompleteClass = unchecked((int)0x80041020), 216 /// <summary> 217 /// Reserved for future use. 218 /// </summary> 219 InvalidSyntax = unchecked((int)0x80041021), 220 /// <summary> 221 /// Reserved for future use. 222 /// </summary> 223 NondecoratedObject = unchecked((int)0x80041022), 224 /// <summary> 225 /// <para>The property that you are attempting to modify is read-only.</para> 226 /// </summary> 227 ReadOnly = unchecked((int)0x80041023), 228 /// <summary> 229 /// <para> The provider cannot perform the requested operation, such 230 /// as requesting a query that is too complex, retrieving an instance, creating or 231 /// updating a class, deleting a class, or enumerating a class. </para> 232 /// </summary> 233 ProviderNotCapable = unchecked((int)0x80041024), 234 /// <summary> 235 /// <para>An attempt was made to make a change that would 236 /// invalidate a derived class.</para> 237 /// </summary> 238 ClassHasChildren = unchecked((int)0x80041025), 239 /// <summary> 240 /// <para> An attempt has been made to delete or modify a class that 241 /// has instances. </para> 242 /// </summary> 243 ClassHasInstances = unchecked((int)0x80041026), 244 /// <summary> 245 /// Reserved for future use. 246 /// </summary> 247 QueryNotImplemented = unchecked((int)0x80041027), 248 /// <summary> 249 /// <para> A value of null was specified for a property that may 250 /// not be null, such as one that is marked by a <see langword='Key'/>, <see langword='Indexed'/>, or 251 /// <see langword='Not_Null'/> qualifier.</para> 252 /// </summary> 253 IllegalNull = unchecked((int)0x80041028), 254 /// <summary> 255 /// <para> The value provided for a qualifier was not a 256 /// legal qualifier type.</para> 257 /// </summary> 258 InvalidQualifierType = unchecked((int)0x80041029), 259 /// <summary> 260 /// The CIM type specified for a property is not valid. 261 /// </summary> 262 InvalidPropertyType = unchecked((int)0x8004102A), 263 /// <summary> 264 /// <para> The request was made with an out-of-range value, or is 265 /// incompatible with the type. </para> 266 /// </summary> 267 ValueOutOfRange = unchecked((int)0x8004102B), 268 /// <summary> 269 /// <para>An illegal attempt was made to make a class singleton, 270 /// such as when the class is derived from a non-singleton class.</para> 271 /// </summary> 272 CannotBeSingleton = unchecked((int)0x8004102C), 273 /// <summary> 274 /// The CIM type specified is not valid. 275 /// </summary> 276 InvalidCimType = unchecked((int)0x8004102D), 277 /// <summary> 278 /// The requested method is not available. 279 /// </summary> 280 InvalidMethod = unchecked((int)0x8004102E), 281 /// <summary> 282 /// <para> The parameters provided for the method are not valid. 283 /// </para> 284 /// </summary> 285 InvalidMethodParameters = unchecked((int)0x8004102F), 286 /// <summary> 287 /// There was an attempt to get qualifiers on a system 288 /// property. 289 /// </summary> 290 SystemProperty = unchecked((int)0x80041030), 291 /// <summary> 292 /// The property type is not recognized. 293 /// </summary> 294 InvalidProperty = unchecked((int)0x80041031), 295 /// <summary> 296 /// <para> An asynchronous process has been canceled internally or 297 /// by the user. Note that because of the timing and nature of the asynchronous 298 /// operation, the operation may not have been truly canceled. </para> 299 /// </summary> 300 CallCanceled = unchecked((int)0x80041032), 301 /// <summary> 302 /// <para>The user has requested an operation while WMI is in the 303 /// process of quitting.</para> 304 /// </summary> 305 ShuttingDown = unchecked((int)0x80041033), 306 /// <summary> 307 /// <para> An attempt was made to reuse an existing method name from 308 /// a base class, and the signatures did not match. </para> 309 /// </summary> 310 PropagatedMethod = unchecked((int)0x80041034), 311 /// <summary> 312 /// <para> One or more parameter values, such as a query text, is 313 /// too complex or unsupported. WMI is requested to retry the operation 314 /// with simpler parameters. </para> 315 /// </summary> 316 UnsupportedParameter = unchecked((int)0x80041035), 317 /// <summary> 318 /// A parameter was missing from the method call. 319 /// </summary> 320 MissingParameterID = unchecked((int)0x80041036), 321 /// <summary> 322 /// A method parameter has an invalid <see langword='ID'/> qualifier. 323 /// </summary> 324 InvalidParameterID = unchecked((int)0x80041037), 325 /// <summary> 326 /// <para> One or more of the method parameters have <see langword='ID'/> 327 /// qualifiers that are out of sequence. </para> 328 /// </summary> 329 NonconsecutiveParameterIDs = unchecked((int)0x80041038), 330 /// <summary> 331 /// <para> The return value for a method has an <see langword='ID'/> qualifier. 332 /// </para> 333 /// </summary> 334 ParameterIDOnRetval = unchecked((int)0x80041039), 335 /// <summary> 336 /// The specified object path was invalid. 337 /// </summary> 338 InvalidObjectPath = unchecked((int)0x8004103A), 339 /// <summary> 340 /// <para> There is not enough free disk space to continue the 341 /// operation. </para> 342 /// </summary> 343 OutOfDiskSpace = unchecked((int)0x8004103B), 344 /// <summary> 345 /// <para> The supplied buffer was too small to hold all the objects 346 /// in the enumerator or to read a string property. </para> 347 /// </summary> 348 BufferTooSmall = unchecked((int)0x8004103C), 349 /// <summary> 350 /// The provider does not support the requested put 351 /// operation. 352 /// </summary> 353 UnsupportedPutExtension = unchecked((int)0x8004103D), 354 /// <summary> 355 /// <para> An object with an incorrect type or version was 356 /// encountered during marshaling. </para> 357 /// </summary> 358 UnknownObjectType = unchecked((int)0x8004103E), 359 /// <summary> 360 /// <para> A packet with an incorrect type or version was 361 /// encountered during marshaling. </para> 362 /// </summary> 363 UnknownPacketType = unchecked((int)0x8004103F), 364 /// <summary> 365 /// The packet has an unsupported version. 366 /// </summary> 367 MarshalVersionMismatch = unchecked((int)0x80041040), 368 /// <summary> 369 /// <para>The packet is corrupted.</para> 370 /// </summary> 371 MarshalInvalidSignature = unchecked((int)0x80041041), 372 /// <summary> 373 /// An attempt has been made to mismatch qualifiers, such as 374 /// putting [key] on an object instead of a property. 375 /// </summary> 376 InvalidQualifier = unchecked((int)0x80041042), 377 /// <summary> 378 /// A duplicate parameter has been declared in a CIM method. 379 /// </summary> 380 InvalidDuplicateParameter = unchecked((int)0x80041043), 381 /// <summary> 382 /// <para> Reserved for future use. </para> 383 /// </summary> 384 TooMuchData = unchecked((int)0x80041044), 385 /// <summary> 386 /// <para>The delivery of an event has failed. The provider may 387 /// choose to re-raise the event.</para> 388 /// </summary> 389 ServerTooBusy = unchecked((int)0x80041045), 390 /// <summary> 391 /// The specified flavor was invalid. 392 /// </summary> 393 InvalidFlavor = unchecked((int)0x80041046), 394 /// <summary> 395 /// <para> An attempt has been made to create a reference that is 396 /// circular (for example, deriving a class from itself). </para> 397 /// </summary> 398 CircularReference = unchecked((int)0x80041047), 399 /// <summary> 400 /// The specified class is not supported. 401 /// </summary> 402 UnsupportedClassUpdate = unchecked((int)0x80041048), 403 /// <summary> 404 /// <para> An attempt was made to change a key when instances or derived 405 /// classes are already using the key. </para> 406 /// </summary> 407 CannotChangeKeyInheritance = unchecked((int)0x80041049), 408 /// <summary> 409 /// <para> An attempt was made to change an index when instances or derived 410 /// classes are already using the index. </para> 411 /// </summary> 412 CannotChangeIndexInheritance = unchecked((int)0x80041050), 413 /// <summary> 414 /// <para> An attempt was made to create more properties than the 415 /// current version of the class supports. </para> 416 /// </summary> 417 TooManyProperties = unchecked((int)0x80041051), 418 /// <summary> 419 /// <para> A property was redefined with a conflicting type in a 420 /// derived class. </para> 421 /// </summary> 422 UpdateTypeMismatch = unchecked((int)0x80041052), 423 /// <summary> 424 /// <para> An attempt was made in a derived class to override a 425 /// non-overrideable qualifier. </para> 426 /// </summary> 427 UpdateOverrideNotAllowed = unchecked((int)0x80041053), 428 /// <summary> 429 /// <para> A method was redeclared with a conflicting signature in a 430 /// derived class. </para> 431 /// </summary> 432 UpdatePropagatedMethod = unchecked((int)0x80041054), 433 /// <summary> 434 /// An attempt was made to execute a method not marked with 435 /// [implemented] in any relevant class. 436 /// </summary> 437 MethodNotImplemented = unchecked((int)0x80041055), 438 /// <summary> 439 /// <para> An attempt was made to execute a method marked with 440 /// [disabled]. </para> 441 /// </summary> 442 MethodDisabled = unchecked((int)0x80041056), 443 /// <summary> 444 /// <para> The refresher is busy with another operation. </para> 445 /// </summary> 446 RefresherBusy = unchecked((int)0x80041057), 447 /// <summary> 448 /// <para> The filtering query is syntactically invalid. </para> 449 /// </summary> 450 UnparsableQuery = unchecked((int)0x80041058), 451 /// <summary> 452 /// The FROM clause of a filtering query references a class 453 /// that is not an event class. 454 /// </summary> 455 NotEventClass = unchecked((int)0x80041059), 456 /// <summary> 457 /// A GROUP BY clause was used without the corresponding 458 /// GROUP WITHIN clause. 459 /// </summary> 460 MissingGroupWithin = unchecked((int)0x8004105A), 461 /// <summary> 462 /// A GROUP BY clause was used. Aggregation on all properties 463 /// is not supported. 464 /// </summary> 465 MissingAggregationList = unchecked((int)0x8004105B), 466 /// <summary> 467 /// <para> Dot notation was used on a property that is not an 468 /// embedded object. </para> 469 /// </summary> 470 PropertyNotAnObject = unchecked((int)0x8004105C), 471 /// <summary> 472 /// A GROUP BY clause references a property that is an 473 /// embedded object without using dot notation. 474 /// </summary> 475 AggregatingByObject = unchecked((int)0x8004105D), 476 /// <summary> 477 /// An event provider registration query 478 /// (<see langword='__EventProviderRegistration'/>) did not specify the classes for which 479 /// events were provided. 480 /// </summary> 481 UninterpretableProviderQuery = unchecked((int)0x8004105F), 482 /// <summary> 483 /// <para> An request was made to back up or restore the repository 484 /// while WinMgmt.exe was using it. </para> 485 /// </summary> 486 BackupRestoreWinmgmtRunning = unchecked((int)0x80041060), 487 /// <summary> 488 /// <para> The asynchronous delivery queue overflowed from the 489 /// event consumer being too slow. </para> 490 /// </summary> 491 QueueOverflow = unchecked((int)0x80041061), 492 /// <summary> 493 /// The operation failed because the client did not have the 494 /// necessary security privilege. 495 /// </summary> 496 PrivilegeNotHeld = unchecked((int)0x80041062), 497 /// <summary> 498 /// <para>The operator is not valid for this property type.</para> 499 /// </summary> 500 InvalidOperator = unchecked((int)0x80041063), 501 /// <summary> 502 /// <para> The user specified a username, password, or authority on a 503 /// local connection. The user must use an empty user name and password and rely on 504 /// default security. </para> 505 /// </summary> 506 LocalCredentials = unchecked((int)0x80041064), 507 /// <summary> 508 /// <para> The class was made abstract when its base class is not 509 /// abstract. </para> 510 /// </summary> 511 CannotBeAbstract = unchecked((int)0x80041065), 512 /// <summary> 513 /// <para> An amended object was used in a put operation without the 514 /// WBEM_FLAG_USE_AMENDED_QUALIFIERS flag being specified. </para> 515 /// </summary> 516 AmendedObject = unchecked((int)0x80041066), 517 /// <summary> 518 /// The client was not retrieving objects quickly enough from 519 /// an enumeration. 520 /// </summary> 521 ClientTooSlow = unchecked((int)0x80041067), 522 523 /// <summary> 524 /// <para> The provider registration overlaps with the system event 525 /// domain. </para> 526 /// </summary> 527 RegistrationTooBroad = unchecked((int)0x80042001), 528 /// <summary> 529 /// <para> A WITHIN clause was not used in this query. </para> 530 /// </summary> 531 RegistrationTooPrecise = unchecked((int)0x80042002) 532 } 533 534 /// <summary> 535 /// <para> Represents management exceptions.</para> 536 /// </summary> 537 /// <example> 538 /// <code lang='C#'>using System; 539 /// using System.Management; 540 /// 541 /// // This sample demonstrates how to display error 542 /// // information stored in a ManagementException object. 543 /// class Sample_ManagementException 544 /// { 545 /// public static int Main(string[] args) 546 /// { 547 /// try 548 /// { 549 /// ManagementObject disk = 550 /// new ManagementObject("Win32_LogicalDisk.DeviceID='BAD:'"); 551 /// disk.Get(); // throws ManagementException 552 /// Console.WriteLine("This shouldn't be displayed."); 553 /// } 554 /// catch (ManagementException e) 555 /// { 556 /// Console.WriteLine("ErrorCode " + e.ErrorCode); 557 /// Console.WriteLine("Message " + e.Message); 558 /// Console.WriteLine("Source " + e.Source); 559 /// if (e.ErrorInformation) //extended error object 560 /// Console.WriteLine("Extended Description : " + e.ErrorInformation["Description"]); 561 /// } 562 /// return 0; 563 /// } 564 /// } 565 /// </code> 566 /// <code lang='VB'>Imports System 567 /// Imports System.Management 568 /// 569 /// ' This sample demonstrates how to display error 570 /// ' information stored in a ManagementException object. 571 /// Class Sample_ManagementException 572 /// Overloads Public Shared Function Main(args() As String) As Integer 573 /// Try 574 /// Dim disk As New ManagementObject("Win32_LogicalDisk.DeviceID='BAD:'") 575 /// disk.Get() ' throws ManagementException 576 /// Console.WriteLine("This shouldn't be displayed.") 577 /// Catch e As ManagementException 578 /// Console.WriteLine("ErrorCode " & e.ErrorCode) 579 /// Console.WriteLine("Message " & e.Message) 580 /// Console.WriteLine("Source " & e.Source) 581 /// If e.ErrorInformation != Nothing Then 'extended error object 582 /// Console.WriteLine("Extended Description : " & e.ErrorInformation("Description")) 583 /// End If 584 /// End Try 585 /// Return 0 586 /// End Function 587 /// End Class 588 /// </code> 589 /// </example> 590 [Serializable] 591 public class ManagementException : SystemException 592 { 593 private ManagementBaseObject errorObject = null; 594 private ManagementStatus errorCode = 0; 595 ThrowWithExtendedInfo(ManagementStatus errorCode)596 internal static void ThrowWithExtendedInfo(ManagementStatus errorCode) 597 { 598 ManagementBaseObject errObj = null; 599 string msg = null; 600 601 //Try to get extended error info first, and save in errorObject member 602 IWbemClassObjectFreeThreaded obj = WbemErrorInfo.GetErrorInfo(); 603 if (obj != null) 604 errObj = new ManagementBaseObject(obj); 605 606 //If the error code is not a WMI one and there's an extended error object available, stick the message 607 //from the extended error object in. 608 if (((msg = GetMessage(errorCode)) == null) && (errObj != null)) 609 try 610 { 611 msg = (string)errObj["Description"]; 612 } 613 catch {} 614 615 throw new ManagementException(errorCode, msg, errObj); 616 } 617 618 ThrowWithExtendedInfo(Exception e)619 internal static void ThrowWithExtendedInfo(Exception e) 620 { 621 ManagementBaseObject errObj = null; 622 string msg = null; 623 624 //Try to get extended error info first, and save in errorObject member 625 IWbemClassObjectFreeThreaded obj = WbemErrorInfo.GetErrorInfo(); 626 if (obj != null) 627 errObj = new ManagementBaseObject(obj); 628 629 //If the error code is not a WMI one and there's an extended error object available, stick the message 630 //from the extended error object in. 631 if (((msg = GetMessage(e)) == null) && (errObj != null)) 632 try 633 { 634 msg = (string)errObj["Description"]; 635 } 636 catch {} 637 638 throw new ManagementException(e, msg, errObj); 639 } 640 641 ManagementException(ManagementStatus errorCode, string msg, ManagementBaseObject errObj)642 internal ManagementException(ManagementStatus errorCode, string msg, ManagementBaseObject errObj) : base (msg) 643 { 644 this.errorCode = errorCode; 645 this.errorObject = errObj; 646 } 647 ManagementException(Exception e, string msg, ManagementBaseObject errObj)648 internal ManagementException(Exception e, string msg, ManagementBaseObject errObj) : base (msg, e) 649 { 650 try 651 { 652 if (e is ManagementException) 653 { 654 errorCode = ((ManagementException)e).ErrorCode; 655 656 // May/may not have extended error info. 657 // 658 if (errorObject != null) 659 errorObject = (ManagementBaseObject)((ManagementException)e).errorObject.Clone(); 660 else 661 errorObject = null; 662 } 663 else if (e is COMException) 664 errorCode = (ManagementStatus)((COMException)e).ErrorCode; 665 else 666 errorCode = (ManagementStatus)this.HResult; 667 } 668 catch {} 669 } 670 671 /// <summary> 672 /// <para>Initializes a new instance of the <see cref='System.Management.ManagementException'/> class that is serializable.</para> 673 /// </summary> 674 /// <param name='info'>The <see cref='System.Runtime.Serialization.SerializationInfo'/> to populate with data.</param> 675 /// <param name='context'>The destination (see <see cref='System.Runtime.Serialization.StreamingContext'/> ) for this serialization.</param> ManagementException(SerializationInfo info, StreamingContext context)676 protected ManagementException(SerializationInfo info, StreamingContext context) : base(info, context) 677 { 678 errorCode = (ManagementStatus)info.GetValue("errorCode", typeof(ManagementStatus)); 679 errorObject = info.GetValue("errorObject", typeof(ManagementBaseObject)) as ManagementBaseObject; 680 } 681 /// <summary> 682 /// <para>Initializes a new instance of the <see cref='System.Management.ManagementException'/> class</para> 683 /// </summary> ManagementException()684 public ManagementException():this(ManagementStatus.Failed, "", null) 685 { 686 687 } 688 689 690 /// <summary> 691 /// <para>Initializes a new instance of the <see cref='System.Management.ManagementException'/> 692 /// class with a specified error message.</para> 693 /// <param name='message'>The message that describes the error.</param> 694 /// </summary> ManagementException(string message)695 public ManagementException(string message):this(ManagementStatus.Failed, message, null) 696 { 697 698 } 699 700 /// <summary> 701 /// <para>Initializes a empty new instance of the <see cref='System.Management.ManagementException'/> class </para> 702 /// <param name='message'>The message that describes the error.</param> 703 /// <param name='innerException'>The exception that is the cause of the current exception. If the innerException 704 /// parameter is not a null reference (Nothing in Visual Basic), the current exception is raised in a catch 705 /// block that handles the inner exception.</param> 706 /// </summary> ManagementException(string message,Exception innerException)707 public ManagementException(string message,Exception innerException):this(innerException, message, null) 708 { 709 // if the exception passed is not a ManagementException, then initialize the ErrorCode to Failed 710 if (!(innerException is ManagementException)) 711 errorCode = ManagementStatus.Failed; 712 } 713 714 /// <summary> 715 /// <para>Populates the <see cref='System.Runtime.Serialization.SerializationInfo'/> object with the data needed to 716 /// serialize the <see cref='System.Management.ManagementException'/> 717 /// object.</para> 718 /// </summary> 719 /// <param name='info'>The <see cref='System.Runtime.Serialization.SerializationInfo'/> to populate with data.</param> 720 /// <param name='context'>The destination (see <see cref='System.Runtime.Serialization.StreamingContext'/> ) for this serialization.</param> 721 GetObjectData(SerializationInfo info, StreamingContext context)722 public override void GetObjectData(SerializationInfo info, StreamingContext context) 723 { 724 base.GetObjectData(info, context); 725 info.AddValue("errorCode", errorCode); 726 info.AddValue("errorObject", errorObject); 727 } 728 GetMessage(Exception e)729 private static string GetMessage(Exception e) 730 { 731 string msg = null; 732 733 if (e is COMException) 734 { 735 // Try and get WMI error message. If not use the one in 736 // the exception 737 msg = GetMessage ((ManagementStatus)((COMException)e).ErrorCode); 738 } 739 740 if (null == msg) 741 msg = e.Message; 742 743 return msg; 744 } 745 GetMessage(ManagementStatus errorCode)746 private static string GetMessage(ManagementStatus errorCode) 747 { 748 string msg = null; 749 IWbemStatusCodeText statusCode = null; 750 int hr; 751 752 statusCode = (IWbemStatusCodeText) new WbemStatusCodeText(); 753 if (statusCode != null) 754 { 755 try { 756 hr = statusCode.GetErrorCodeText_((int)errorCode, 0, 1, out msg); 757 758 // Just in case it didn't like the flag=1, try it again 759 // with flag=0. 760 if (hr != 0) 761 hr = statusCode.GetErrorCodeText_((int)errorCode, 0, 0, out msg); 762 } 763 catch {} 764 } 765 766 return msg; 767 } 768 769 /// <summary> 770 /// <para>Gets the extended error object provided by WMI.</para> 771 /// </summary> 772 /// <value> 773 /// <para>A <see cref='System.Management.ManagementBaseObject'/> representing the 774 /// extended error object provided by WMI, if available; <see langword='null'/> 775 /// otherwise.</para> 776 /// </value> 777 public ManagementBaseObject ErrorInformation 778 { 779 get 780 { return errorObject; } 781 } 782 783 /// <summary> 784 /// <para>Gets the error code reported by WMI, which caused this exception.</para> 785 /// </summary> 786 /// <value> 787 /// A <see cref='System.Management.ManagementStatus'/> value representing the error code returned by 788 /// the WMI operation. 789 /// </value> 790 public ManagementStatus ErrorCode 791 { 792 get 793 { return errorCode; } 794 } 795 796 } 797 } 798