private void Initialize() { //If the query is not set yet we can't do it if (null == query) { throw new InvalidOperationException(); } if (null == options) { Options = new EventWatcherOptions(); } //If we're not connected yet, this is the time to do it... lock (this) { if (null == scope) { Scope = new ManagementScope(); } if (null == cachedObjects) { cachedObjects = new IWbemClassObjectFreeThreaded[options.BlockSize]; } } lock (scope) { scope.Initialize(); } }
private void Initialize() { //If the query is not set yet we can't do it if (null == query) { throw new InvalidOperationException(); } //If we're not connected yet, this is the time to do it... #pragma warning disable CA2002 lock (this) #pragma warning restore CA2002 { if (null == scope) { scope = ManagementScope._Clone(null); } } lock (scope) { if (!scope.IsConnected) { scope.Initialize(); } } }
private void Initialize() { //If the query is not set yet we can't do it if (null == query) { throw new InvalidOperationException(); } //If we're not connected yet, this is the time to do it... lock (this) { if (null == scope) { Scope = new ManagementScope(); } } lock (scope) { scope.Initialize(); } }
public void CopyTo(ManagementOperationObserver watcher, ManagementPath path, PutOptions options) { int num; PutOptions putOption; if (watcher != null) { this.Initialize(false); ManagementScope managementScope = new ManagementScope(path, this.scope); managementScope.Initialize(); if (options != null) { putOption = (PutOptions)options.Clone(); } else { putOption = new PutOptions(); } PutOptions putOption1 = putOption; if (watcher.HaveListenersForProgress) { putOption1.SendStatus = true; } WmiEventSink newPutSink = watcher.GetNewPutSink(managementScope, putOption1.Context, path.GetNamespacePath(8), base.ClassName); IWbemServices wbemServices = managementScope.GetIWbemServices(); SecurityHandler securityHandler = managementScope.GetSecurityHandler(); if (!base.IsClass) { num = managementScope.GetSecuredIWbemServicesHandler(wbemServices).PutInstanceAsync_(base.wbemObject, putOption1.Flags, putOption1.GetContext(), newPutSink.Stub); } else { num = managementScope.GetSecuredIWbemServicesHandler(wbemServices).PutClassAsync_(base.wbemObject, putOption1.Flags, putOption1.GetContext(), newPutSink.Stub); } if (securityHandler != null) { securityHandler.Reset(); } if (num < 0) { watcher.RemoveSink(newPutSink); if (((long)num & (long)-4096) != (long)-2147217408) { Marshal.ThrowExceptionForHR(num); } else { ManagementException.ThrowWithExtendedInfo((ManagementStatus)num); return; } } return; } else { throw new ArgumentNullException("watcher"); } }
public ManagementPath CopyTo(ManagementPath path, PutOptions options) { int callStatus_; int num = 0; PutOptions putOption; this.Initialize(false); ManagementScope managementScope = new ManagementScope(path, this.scope); managementScope.Initialize(); if (options != null) { putOption = options; } else { putOption = new PutOptions(); } PutOptions putOption1 = putOption; IWbemServices wbemServices = managementScope.GetIWbemServices(); ManagementPath namespacePath = null; IntPtr zero = IntPtr.Zero; IntPtr intPtr = IntPtr.Zero; IWbemCallResult objectForIUnknown = null; SecurityHandler securityHandler = null; try { securityHandler = managementScope.GetSecurityHandler(); zero = Marshal.AllocHGlobal(IntPtr.Size); Marshal.WriteIntPtr(zero, IntPtr.Zero); if (!base.IsClass) { callStatus_ = this.scope.GetSecuredIWbemServicesHandler(wbemServices).PutInstance_(base.wbemObject, putOption1.Flags | 16, putOption1.GetContext(), zero); } else { callStatus_ = this.scope.GetSecuredIWbemServicesHandler(wbemServices).PutClass_(base.wbemObject, putOption1.Flags | 16, putOption1.GetContext(), zero); } intPtr = Marshal.ReadIntPtr(zero); objectForIUnknown = (IWbemCallResult)Marshal.GetObjectForIUnknown(intPtr); callStatus_ = objectForIUnknown.GetCallStatus_(-1, out num); if (callStatus_ >= 0) { callStatus_ = num; } if (callStatus_ < 0) { if (((long)callStatus_ & (long)-4096) != (long)-2147217408) { Marshal.ThrowExceptionForHR(callStatus_); } else { ManagementException.ThrowWithExtendedInfo((ManagementStatus)callStatus_); } } namespacePath = this.GetPath(objectForIUnknown); namespacePath.NamespacePath = path.GetNamespacePath(8); } finally { if (securityHandler != null) { securityHandler.Reset(); } if (zero != IntPtr.Zero) { Marshal.FreeHGlobal(zero); } if (intPtr != IntPtr.Zero) { Marshal.Release(intPtr); } if (objectForIUnknown != null) { Marshal.ReleaseComObject(objectForIUnknown); } } return namespacePath; }
/// <summary> /// <para>Copies the object to a different location, asynchronously.</para> /// </summary> /// <param name='watcher'>The object that will receive the results of the operation.</param> /// <param name='path'>The path to which the object should be copied.</param> /// <param name='options'>The options for how the object should be put.</param> public void CopyTo(ManagementOperationObserver watcher, ManagementPath path, PutOptions options) { if (null == watcher) throw new ArgumentNullException("watcher"); else { Initialize ( false ) ; ManagementScope destinationScope = null; destinationScope = new ManagementScope(path, scope); destinationScope.Initialize(); PutOptions o = (null != options) ? (PutOptions) options.Clone() : new PutOptions(); // If someone has registered for progress, make sure we flag it if (watcher.HaveListenersForProgress) o.SendStatus = true; WmiEventSink sink = watcher.GetNewPutSink(destinationScope, o.Context, path.GetNamespacePath((int)tag_WBEM_GET_TEXT_FLAGS.WBEMPATH_GET_SERVER_AND_NAMESPACE_ONLY), ClassName); IWbemServices destWbemServices = destinationScope.GetIWbemServices(); SecurityHandler securityHandler = null; int status = (int)ManagementStatus.NoError; securityHandler = destinationScope.GetSecurityHandler(); if (IsClass) { status = destinationScope.GetSecuredIWbemServicesHandler( destWbemServices ).PutClassAsync_( wbemObject, o.Flags, o.GetContext(), sink.Stub); } else { status = destinationScope.GetSecuredIWbemServicesHandler( destWbemServices ).PutInstanceAsync_( wbemObject, o.Flags, o.GetContext(), sink.Stub); } if (securityHandler != null) securityHandler.Reset(); if (status < 0) { watcher.RemoveSink(sink); if ((status & 0xfffff000) == 0x80041000) ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); else Marshal.ThrowExceptionForHR(status); } } }
/// <summary> /// <para>Copies the object to a different location.</para> /// </summary> /// <param name='path'>The <see cref='System.Management.ManagementPath'/> to which the object should be copied.</param> /// <param name='options'>The options for how the object should be put.</param> /// <returns> /// The new path of the copied object. /// </returns> public ManagementPath CopyTo(ManagementPath path, PutOptions options) { Initialize ( false ) ; ManagementScope destinationScope = null; // Build a scope for our target destination destinationScope = new ManagementScope(path, scope); destinationScope.Initialize(); PutOptions o = (null != options) ? options : new PutOptions(); IWbemServices wbemServices = destinationScope.GetIWbemServices(); ManagementPath newPath = null; // // TO-DO : This code is almost identical to Put - should consolidate. // // Must do this convoluted allocation since the IWbemServices ref IWbemCallResult // has been redefined to be an IntPtr. Due to the fact that it wasn't possible to // pass NULL for the optional argument. // IntPtr ppwbemCallResult = IntPtr.Zero; IntPtr pwbemCallResult = IntPtr.Zero; IWbemCallResult wbemCallResult = null; SecurityHandler securityHandler = null; int status = (int)ManagementStatus.NoError; try { securityHandler = destinationScope.GetSecurityHandler(); ppwbemCallResult = Marshal.AllocHGlobal(IntPtr.Size); Marshal.WriteIntPtr(ppwbemCallResult, IntPtr.Zero); // Init to NULL. if (IsClass) { status = scope.GetSecuredIWbemServicesHandler( wbemServices ).PutClass_( wbemObject, o.Flags | (int)tag_WBEM_GENERIC_FLAG_TYPE.WBEM_FLAG_RETURN_IMMEDIATELY, o.GetContext(), ppwbemCallResult); } else { status = scope.GetSecuredIWbemServicesHandler( wbemServices ).PutInstance_( wbemObject, o.Flags | (int)tag_WBEM_GENERIC_FLAG_TYPE.WBEM_FLAG_RETURN_IMMEDIATELY, o.GetContext(), ppwbemCallResult); } // Keep this statement here; otherwise, there'll be a leak in error cases. pwbemCallResult = Marshal.ReadIntPtr(ppwbemCallResult); //Use the CallResult to retrieve the resulting object path wbemCallResult = (IWbemCallResult)Marshal.GetObjectForIUnknown(pwbemCallResult); int hr; status = wbemCallResult.GetCallStatus_((int)tag_WBEM_TIMEOUT_TYPE.WBEM_INFINITE, out hr); if (status >= 0) status = hr; if (status < 0) { if ((status & 0xfffff000) == 0x80041000) ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); else Marshal.ThrowExceptionForHR(status); } newPath = GetPath(wbemCallResult); newPath.NamespacePath = path.GetNamespacePath((int)tag_WBEM_GET_TEXT_FLAGS.WBEMPATH_GET_SERVER_AND_NAMESPACE_ONLY); } finally { if (securityHandler != null) securityHandler.Reset(); if (ppwbemCallResult != IntPtr.Zero) // Cleanup from allocations above. Marshal.FreeHGlobal(ppwbemCallResult); if (pwbemCallResult != IntPtr.Zero) Marshal.Release(pwbemCallResult); if (wbemCallResult != null) Marshal.ReleaseComObject(wbemCallResult); } return newPath; }