Esempio n. 1
0
        //********************************************
        //Get()
        //********************************************
        /// <overload>
        ///    Invokes the specified WMI query and returns the resulting collection.
        /// </overload>
        /// <summary>
        ///    <para>Invokes the specified WMI query and returns the
        ///       resulting collection.</para>
        /// </summary>
        /// <returns>
        /// <para>A <see cref='System.Management.ManagementObjectCollection'/> containing the objects that match the
        ///    specified query.</para>
        /// </returns>
        public ManagementObjectCollection Get()
        {
            Initialize();
            IEnumWbemClassObject ew = null;
            SecurityHandler      securityHandler = scope.GetSecurityHandler();
            EnumerationOptions   enumOptions     = (EnumerationOptions)options.Clone();

            int status = (int)ManagementStatus.NoError;

            try
            {
                //If this is a simple SelectQuery (className only), and the enumerateDeep is set, we have
                //to find out whether this is a class enumeration or instance enumeration and call CreateInstanceEnum/
                //CreateClassEnum appropriately, because with ExecQuery we can't do a deep enumeration.
                if ((query.GetType() == typeof(SelectQuery)) &&
                    (((SelectQuery)query).Condition == null) &&
                    (((SelectQuery)query).SelectedProperties == null) &&
                    (options.EnumerateDeep == true))
                {
                    //Need to make sure that we're not passing invalid flags to enumeration APIs.
                    //The only flags not valid for enumerations are EnsureLocatable & PrototypeOnly.
                    enumOptions.EnsureLocatable = false; enumOptions.PrototypeOnly = false;

                    if (((SelectQuery)query).IsSchemaQuery == false) //deep instance enumeration
                    {
                        status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices()).CreateInstanceEnum_(
                            ((SelectQuery)query).ClassName,
                            enumOptions.Flags,
                            enumOptions.GetContext(),
                            ref ew);
                    }
                    else //deep class enumeration
                    {
                        status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices()).CreateClassEnum_(((SelectQuery)query).ClassName,
                                                                                                                 enumOptions.Flags,
                                                                                                                 enumOptions.GetContext(),
                                                                                                                 ref ew);
                    }
                }
                else //we can use ExecQuery
                {
                    //Make sure the EnumerateDeep flag bit is turned off because it's invalid for queries
                    enumOptions.EnumerateDeep = true;
                    status = scope.GetSecuredIWbemServicesHandler(scope.GetIWbemServices()).ExecQuery_(
                        query.QueryLanguage,
                        query.QueryString,
                        enumOptions.Flags,
                        enumOptions.GetContext(),
                        ref ew);
                }
            }
            catch (COMException e)
            {
                ManagementException.ThrowWithExtendedInfo(e);
            }
            finally
            {
                securityHandler.Reset();
            }

            if ((status & 0xfffff000) == 0x80041000)
            {
                ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
            }
            else if ((status & 0x80000000) != 0)
            {
                Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
            }

            //Create a new collection object for the results

            return(new ManagementObjectCollection(scope, options, ew));
        }//Get()
Esempio n. 2
0
		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");
			}
		}
        /// <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);
                }
            }
        }
Esempio n. 4
0
		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.</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;
        }