int WbemNative.IWbemServices.PutInstanceAsync(
            WbemNative.IWbemClassObject wbemObject,
            Int32 lFlags,
            WbemNative.IWbemContext wbemContext,
            WbemNative.IWbemObjectSink wbemSink
            )
        {
            if (wbemObject == null || wbemContext == null || wbemSink == null || this.wbemServices == null)
            {
                return((int)WbemNative.WbemStatus.WBEM_E_INVALID_PARAMETER);
            }

            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
            {
                try
                {
                    object val   = null;
                    int    type  = 0;
                    int    favor = 0;
                    WbemException.ThrowIfFail(wbemObject.Get("__CLASS", 0, ref val, ref type, ref favor));
                    string className = (string)val;
                    ServiceModelActivity.Start(activity, SR.GetString(SR.WmiPutInstance, string.IsNullOrEmpty(className) ? string.Empty : className), ActivityType.WmiPutInstance);

                    ParameterContext parms        = new ParameterContext(className, this.wbemServices, wbemContext, wbemSink);
                    WbemInstance     wbemInstance = new WbemInstance(parms, wbemObject);
                    IWmiProvider     wmiProvider  = this.GetProvider(parms.ClassName);
                    if (wmiProvider.PutInstance(new InstanceContext(wbemInstance)))
                    {
                        wbemInstance.Indicate();
                    }

                    WbemException.ThrowIfFail(wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                                                 (int)WbemNative.WbemStatus.WBEM_S_NO_ERROR, null, null));
                }
                catch (WbemException e)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi, (uint)System.Runtime.Diagnostics.EventLogEventId.WmiPutInstanceFailed,
                                                        TraceUtility.CreateSourceString(this), e.ToString());
                    wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                       e.ErrorCode, null, null);
                    return(e.ErrorCode);
                }
#pragma warning suppress 56500 // covered by FxCOP
                catch (Exception e)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi, (uint)System.Runtime.Diagnostics.EventLogEventId.WmiPutInstanceFailed,
                                                        TraceUtility.CreateSourceString(this), e.ToString());
                    wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                       (int)WbemNative.WbemStatus.WBEM_E_FAILED, null, null);
                    return((int)WbemNative.WbemStatus.WBEM_E_FAILED);
                }
                finally
                {
                    Marshal.ReleaseComObject(wbemSink);
                }
            }
            return((int)WbemNative.WbemStatus.WBEM_S_NO_ERROR);
        }
        int WbemNative.IWbemServices.GetObjectAsync(
            string objectPath,
            Int32 flags,
            WbemNative.IWbemContext wbemContext,
            WbemNative.IWbemObjectSink wbemSink)
        {
            if (wbemContext == null || wbemSink == null || this.wbemServices == null)
            {
                return((int)WbemNative.WbemStatus.WBEM_E_INVALID_PARAMETER);
            }

            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ?
                                                   ServiceModelActivity.CreateActivity(true, SR.GetString(SR.WmiGetObject, string.IsNullOrEmpty(objectPath) ? string.Empty : objectPath), ActivityType.WmiGetObject) : null)
            {
                try
                {
                    ObjectPathRegex  objPathRegex = new ObjectPathRegex(objectPath);
                    ParameterContext parms        = new ParameterContext(objPathRegex.ClassName, this.wbemServices, wbemContext, wbemSink);
                    WbemInstance     wbemInstance = new WbemInstance(parms, objPathRegex);
                    IWmiProvider     wmiProvider  = this.GetProvider(parms.ClassName);
                    if (wmiProvider.GetInstance(new InstanceContext(wbemInstance)))
                    {
                        wbemInstance.Indicate();
                    }

                    WbemException.ThrowIfFail(wbemSink.SetStatus(
                                                  (int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                                  (int)WbemNative.WbemStatus.WBEM_S_NO_ERROR,
                                                  null,
                                                  null));
                }
                catch (WbemException e)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi, (uint)System.Runtime.Diagnostics.EventLogEventId.WmiGetObjectFailed,
                                                        TraceUtility.CreateSourceString(this), e.ToString());
                    wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                       e.ErrorCode, null, null);
                    return(e.ErrorCode);
                }
#pragma warning suppress 56500 // covered by FxCOP
                catch (Exception e)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi, (uint)System.Runtime.Diagnostics.EventLogEventId.WmiGetObjectFailed,
                                                        TraceUtility.CreateSourceString(this), e.ToString());
                    wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                       (int)WbemNative.WbemStatus.WBEM_E_FAILED, null, null);
                    return((int)WbemNative.WbemStatus.WBEM_E_FAILED);
                }
                finally
                {
                    Marshal.ReleaseComObject(wbemSink);
                }
            }
            return((int)WbemNative.WbemStatus.WBEM_S_NO_ERROR);
        }
 public void Register(string className, IWmiProvider wmiProvider)
 {
     lock (this.syncRoot)
     {
         if (!this.initialized)
         {
             this.Initialize();
         }
         this.wmiProviders.Add(className, wmiProvider);
     }
 }
        int WbemNative.IWbemServices.CreateInstanceEnumAsync(
            string className,
            Int32 flags,
            WbemNative.IWbemContext wbemContext,
            WbemNative.IWbemObjectSink wbemSink)
        {
            if (wbemContext == null || wbemSink == null || this.wbemServices == null)
            {
                return((int)WbemNative.WbemStatus.WBEM_E_INVALID_PARAMETER);
            }

            try
            {
                ParameterContext parms       = new ParameterContext(className, this.wbemServices, wbemContext, wbemSink);
                IWmiProvider     wmiProvider = this.GetProvider(parms.ClassName);
                wmiProvider.EnumInstances(new InstancesContext(parms));

                WbemException.ThrowIfFail(wbemSink.SetStatus(
                                              (int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                              (int)WbemNative.WbemStatus.WBEM_S_NO_ERROR,
                                              null,
                                              null));
            }
            catch (WbemException e)
            {
                wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                   e.ErrorCode, null, null);
                DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                    (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi,
                                                    (uint)System.Runtime.Diagnostics.EventLogEventId.WmiCreateInstanceFailed,
                                                    className,
                                                    e.ToString());
                return(e.ErrorCode);
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            {
                DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                    (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi,
                                                    (uint)System.Runtime.Diagnostics.EventLogEventId.WmiCreateInstanceFailed,
                                                    className,
                                                    e.ToString());
                wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                   (int)WbemNative.WbemStatus.WBEM_E_FAILED, null, null);
                return((int)WbemNative.WbemStatus.WBEM_E_FAILED);
            }
            finally
            {
                Marshal.ReleaseComObject(wbemSink);
            }
            return((int)WbemNative.WbemStatus.WBEM_S_NO_ERROR);
        }
 public void Register(string className, IWmiProvider wmiProvider)
 {
     lock (this.syncRoot)
     {
         if (!this.initialized)
         {
             this.Initialize();
         }
         this.wmiProviders.Add(className, wmiProvider);
     }
 }
        int WbemNative.IWbemServices.ExecMethodAsync(
            string objectPath,
            string methodName,
            Int32 flags,
            WbemNative.IWbemContext wbemContext,
            WbemNative.IWbemClassObject wbemInParams,
            WbemNative.IWbemObjectSink wbemSink)
        {
            if (wbemContext == null || wbemInParams == null || wbemSink == null || this.wbemServices == null)
            {
                return((int)WbemNative.WbemStatus.WBEM_E_INVALID_PARAMETER);
            }

            int result = (int)WbemNative.WbemStatus.WBEM_S_NO_ERROR;

            try
            {
                ObjectPathRegex  objPathRegex = new ObjectPathRegex(objectPath);
                ParameterContext parms        = new ParameterContext(objPathRegex.ClassName, this.wbemServices, wbemContext, wbemSink);
                WbemInstance     wbemInstance = new WbemInstance(parms, objPathRegex);

                MethodContext methodContext = new MethodContext(parms, methodName, wbemInParams, wbemInstance);
                IWmiProvider  wmiProvider   = this.GetProvider(parms.ClassName);
                if (!wmiProvider.InvokeMethod(methodContext))
                {
                    result = (int)WbemNative.WbemStatus.WBEM_E_NOT_FOUND;
                }

                WbemException.ThrowIfFail(wbemSink.SetStatus(
                                              (int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                              (int)result,
                                              null,
                                              null));
            }
            catch (WbemException e)
            {
                DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                    (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi,
                                                    (uint)System.Runtime.Diagnostics.EventLogEventId.WmiExecMethodFailed,
                                                    e.ToString());
                result = e.ErrorCode;
                wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                   result, null, null);
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            {
                DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                    (ushort)System.Runtime.Diagnostics.EventLogCategory.Wmi,
                                                    (uint)System.Runtime.Diagnostics.EventLogEventId.WmiExecMethodFailed,
                                                    e.ToString());
                result = (int)WbemNative.WbemStatus.WBEM_E_FAILED;
                wbemSink.SetStatus((int)WbemNative.tag_WBEM_STATUS_TYPE.WBEM_STATUS_COMPLETE,
                                   (int)result, null, null);
            }
            finally
            {
                Marshal.ReleaseComObject(wbemSink);
            }
            return(result);
        }