Пример #1
0
        internal string SetNamespacePath(string nsPath, out bool bChange)
        {
            int namespaceCount_ = 0;

            bChange = false;
            if (!ManagementPath.IsValidNamespaceSyntax(nsPath))
            {
                ManagementException.ThrowWithExtendedInfo(ManagementStatus.InvalidNamespace);
            }
            IWbemPath wbemPath = this.CreateWbemPath(nsPath);

            if (this.wmiPath != null)
            {
                if (this.isWbemPathShared)
                {
                    this.wmiPath          = this.CreateWbemPath(this.GetWbemPath());
                    this.isWbemPathShared = false;
                }
            }
            else
            {
                this.wmiPath = this.CreateWbemPath("");
            }
            string namespacePath = ManagementPath.GetNamespacePath(this.wmiPath, 16);
            string str           = ManagementPath.GetNamespacePath(wbemPath, 16);

            if (string.Compare(namespacePath, str, StringComparison.OrdinalIgnoreCase) != 0)
            {
                this.wmiPath.RemoveAllNamespaces_();
                bChange = true;
                uint num = 0;
                namespaceCount_ = wbemPath.GetNamespaceCount_(out num);
                if (namespaceCount_ >= 0)
                {
                    for (uint i = 0; i < num; i++)
                    {
                        int num1 = 0;
                        namespaceCount_ = wbemPath.GetNamespaceAt_(i, out num1, null);
                        if (namespaceCount_ < 0)
                        {
                            break;
                        }
                        string str1 = new string ('0', num1 - 1);
                        namespaceCount_ = wbemPath.GetNamespaceAt_(i, out num1, str1);
                        if (namespaceCount_ < 0)
                        {
                            break;
                        }
                        namespaceCount_ = this.wmiPath.SetNamespaceAt_(i, str1);
                        if (namespaceCount_ < 0)
                        {
                            break;
                        }
                    }
                }
            }
            if (namespaceCount_ >= 0 && nsPath.Length > 1 && (nsPath [0] == '\\' && nsPath [1] == '\\' || nsPath [0] == '/' && nsPath [1] == '/'))
            {
                int num2 = 0;
                namespaceCount_ = wbemPath.GetServer_(out num2, null);
                if (namespaceCount_ < 0 || num2 <= 0)
                {
                    if (namespaceCount_ == -2147217399)
                    {
                        namespaceCount_ = 0;
                    }
                }
                else
                {
                    string str2 = new string ('0', num2 - 1);
                    namespaceCount_ = wbemPath.GetServer_(out num2, str2);
                    if (namespaceCount_ >= 0)
                    {
                        num2            = 0;
                        namespaceCount_ = this.wmiPath.GetServer_(out num2, null);
                        if (namespaceCount_ < 0)
                        {
                            if (namespaceCount_ == -2147217399)
                            {
                                namespaceCount_ = this.wmiPath.SetServer_(str2);
                                if (namespaceCount_ >= 0)
                                {
                                    bChange = true;
                                }
                            }
                        }
                        else
                        {
                            string str3 = new string ('0', num2 - 1);
                            namespaceCount_ = this.wmiPath.GetServer_(out num2, str3);
                            if (namespaceCount_ >= 0 && string.Compare(str3, str2, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                namespaceCount_ = this.wmiPath.SetServer_(str2);
                            }
                        }
                    }
                }
            }
            else if (namespaceCount_ <= 0 && nsPath.Length > 1)
            {
                this.wmiPath.SetNamespaceAt_(0, nsPath);
                uint nsCount = 0;
                namespaceCount_ = (int)nsCount;
                this.wmiPath.GetNamespaceCount_(out nsCount);
                str = nsPath;
            }

            if (namespaceCount_ < 0)
            {
                if (((long)namespaceCount_ & (long)-4096) != (long)-2147217408)
                {
                    Marshal.ThrowExceptionForHR(namespaceCount_);
                }
                else
                {
                    ManagementException.ThrowWithExtendedInfo((ManagementStatus)namespaceCount_);
                }
            }
            return(str);
        }
Пример #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");
			}
		}
Пример #3
0
 internal string GetNamespacePath(int flags)
 {
     return(ManagementPath.GetNamespacePath(this.wmiPath, flags));
 }
Пример #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;
		}
Пример #5
0
		private void ManagementObjectCTOR(ManagementScope scope, ManagementPath path, ObjectGetOptions options)
		{
			string empty = string.Empty;
			if (path != null && !path.IsEmpty)
			{
				if (!(base.GetType() == typeof(ManagementObject)) || !path.IsClass)
				{
					if (!(base.GetType() == typeof(ManagementClass)) || !path.IsInstance)
					{
						empty = path.GetNamespacePath(8);
						if (scope != null && scope.Path.NamespacePath.Length > 0)
						{
							path = new ManagementPath(path.RelativePath);
							path.NamespacePath = scope.Path.GetNamespacePath(8);
						}
						if (path.IsClass || path.IsInstance)
						{
							this.path = ManagementPath._Clone(path, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
						}
						else
						{
							this.path = ManagementPath._Clone(null, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
						}
					}
					else
					{
						throw new ArgumentOutOfRangeException("path");
					}
				}
				else
				{
					throw new ArgumentOutOfRangeException("path");
				}
			}
			if (options != null)
			{
				this.options = ObjectGetOptions._Clone(options, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
			}
			if (scope == null)
			{
				if (empty.Length > 0)
				{
					this.scope = new ManagementScope(empty);
					this.scope.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange);
				}
			}
			else
			{
				this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(this.HandleIdentifierChange));
			}
			this.IdentifierChanged += new IdentifierChangedEventHandler(this.HandleIdentifierChange);
			this.putButNotGot = false;
		}
        void ManagementObjectCTOR(ManagementScope scope, ManagementPath path, ObjectGetOptions options)
        {
            // We may use this to set the scope path
            string nsPath = String.Empty;

            if ((null != path) && !path.IsEmpty)
            {
                //If this is a ManagementObject then the path has to be an instance,
                // and if this is a ManagementClass the path has to be a class.
                if (GetType() == typeof(ManagementObject) && path.IsClass)
                    throw new ArgumentOutOfRangeException("path");
                else if (GetType() == typeof(ManagementClass) && path.IsInstance)
                    throw new ArgumentOutOfRangeException("path");

                // Save the namespace path portion of the path (if any) in case
                // we don't have a scope
                nsPath = path.GetNamespacePath((int)tag_WBEM_GET_TEXT_FLAGS.WBEMPATH_GET_SERVER_AND_NAMESPACE_ONLY);

                if ((null != scope) && (scope.Path.NamespacePath.Length>0))
                {
                    // If the scope has a path too, the namespace portion of
                    // scope.path takes precedence over what is specified in path
                    path = new ManagementPath(path.RelativePath);
                    path.NamespacePath = scope.Path.GetNamespacePath((int)tag_WBEM_GET_TEXT_FLAGS.WBEMPATH_GET_SERVER_AND_NAMESPACE_ONLY);
                }

                // If the supplied path is a class or instance use it, otherwise
                // leave it empty
                if (path.IsClass || path.IsInstance)
                    this.path = ManagementPath._Clone(path, new IdentifierChangedEventHandler(HandleIdentifierChange));

                else
                    this.path = ManagementPath._Clone(null, new IdentifierChangedEventHandler(HandleIdentifierChange));
            }

            if (null != options)
                this.options = ObjectGetOptions._Clone(options, new IdentifierChangedEventHandler(HandleIdentifierChange));

            if (null != scope)
                this.scope = ManagementScope._Clone(scope, new IdentifierChangedEventHandler(HandleIdentifierChange));
            else
            {
                // Use the path if possible, otherwise let it default
                if (nsPath.Length>0)
                {
                    this.scope = new ManagementScope(nsPath);
                    this.scope.IdentifierChanged += new IdentifierChangedEventHandler(HandleIdentifierChange);
                }
            }

            //register for identifier change events
            IdentifierChanged += new IdentifierChangedEventHandler(HandleIdentifierChange);
            // ***
            // *    Changed isBound flag to wbemObject==null check.
            // *    isBound = false;
            // ***
            putButNotGot = false;

        }
        /// <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;
        }