Retrieving an Error Code
As with all applications, WMI receives error codes from the Windows operating system.
When you receive an error code, you have the following options:
Look at the event log.
WMI sends error messages to the Event Log service that checks the event logs to help determine the cause of an error. You can use the classes that the Event Log provider supports to access event logs programmatically.
Retrieve the error code normally.
WMI supports the standard techniques to retrieve error codes, which are COM error codes for C++, and native error objects, such as Err Object (VBScript), or SWbemLastError if the provider supplies error information.
For more information, see Manipulating Class and Instance Information.
The following sections are discussed in this topic:
If a call to WMI through the Scripting API for WMI causes an error, you have the following options to access the error information:
- Use native error mechanisms of the scripting language, for example, in VBScript use Err Object (VBScript) to support error handling.
- Create an SWbemLastError object to get an error report.
The following script shows use of the native Err Object (VBScript). When an incorrect value for the process handle is given, an error is generated.
On Error Resume Next Set objProcess = GetObject( _ "winmgmts:root\cimv2:Win32_Process.Handle='one'") Wscript.Echo Err.Number
The following table lists the properties of Err Object (VBScript).
Localized, human-readable description of the error.
HRESULT returned by the Scripting API for WMI.
Identifies the object that raised the error.
The following script shows use of an SWbemLastError object to obtain detailed error information. Note that not all providers supply information to SWbemLastError. For more information about error codes in script, see WbemErrorEnum.
On Error Resume Next Set obj = GetObject("winmgmts:root\cimv2:Win32_Process.Handle='one'") Set LastError = createobject("wbemscripting.swbemlasterror") Wscript.Echo "Operation = " & LastError.operation & VBCRLF & "ParameterInfo = " _ & LastError.ParameterInfo & VBCRLF & "ProviderName = " & LastError.ProviderName
A WMI client application can receive either COM-specific or WMI-specific errors. The COM errors conform to the structure of COM error codes. All WMI interfaces can return a COM-specific error except the IWbemContext, IWbemClassObject, and IWbemQualifierSet interfaces. For more information about COM error codes, see Error Handling. The reference pages of the WMI interfaces list the appropriate WMI error codes in the Error Codes section.
A client application must follow the COM standards for checking status and error return codes. The main difference you must choose is whether you wish to retrieve the error code from a synchronous, semisynchronous, or asynchronous call.
To access synchronous and semisynchronous error messages using C++
Retrieve the error information with a call to the GetErrorInfo COM function.
Examine the returned COM error object with a call to the IErrorInterface::QueryInterface method.
WMI does not deliver the error object through GetErrorInfo for an asynchronous call because there is no way to know when, or on which thread the asynchronous call occurred. Therefore, your code can only handle specific errors or pass the call failure through COM.
To access asynchronous error messages using C++
Retrieve the COM error object from your implementation of IWbemObjectSink::SetStatus.
The following pseudocode shows a typical error handling implementation for a client application.