예제 #1
0
        internal string SetNamespacePath(string nsPath, out bool bChange)
        {
            int       status     = (int)ManagementStatus.NoError;
            string    nsOrg      = null;
            string    nsNew      = null;
            IWbemPath wmiPathTmp = null;

            bChange = false;

            Debug.Assert(nsPath != null);

            //Do some validation on the path to make sure it is a valid namespace path (at least syntactically)
            if (!IsValidNamespaceSyntax(nsPath))
            {
                ManagementException.ThrowWithExtendedInfo((ManagementStatus)tag_WBEMSTATUS.WBEM_E_INVALID_NAMESPACE);
            }

            wmiPathTmp = CreateWbemPath(nsPath);
            if (wmiPath == null)
            {
                wmiPath = this.CreateWbemPath("");
            }
            else if (isWbemPathShared)
            {
                // Check if this IWbemPath is shared among multiple managed objects.
                // With this write, it will have to maintain its own copy.
                wmiPath          = CreateWbemPath(this.GetWbemPath());
                isWbemPathShared = false;
            }

            nsOrg = GetNamespacePath(wmiPath,
                                     (int)tag_WBEM_GET_TEXT_FLAGS.WBEMPATH_GET_NAMESPACE_ONLY);
            nsNew = GetNamespacePath(wmiPathTmp,
                                     (int)tag_WBEM_GET_TEXT_FLAGS.WBEMPATH_GET_NAMESPACE_ONLY);

            if (!string.Equals(nsOrg, nsNew, StringComparison.OrdinalIgnoreCase))
            {
                wmiPath.RemoveAllNamespaces_();                                 // Out with the old... Ignore status code.

                // Add the new ones in
                bChange = true;                                                 // Now dirty from above.
                uint nCount = 0;
                status = wmiPathTmp.GetNamespaceCount_(out nCount);

                if (status >= 0)
                {
                    for (uint i = 0; i < nCount; i++)
                    {
                        uint uLen = 0;
                        status = wmiPathTmp.GetNamespaceAt_(i, ref uLen, null);

                        if (status >= 0)
                        {
                            char[] space = new char[(int)uLen];
                            status = wmiPathTmp.GetNamespaceAt_(i, ref uLen, space);
                            if (status >= 0)
                            {
                                status = wmiPath.SetNamespaceAt_(i, space);

                                if (status < 0)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                ;
            }            // Continue on. Could have different server name, same ns specified.

            //
            // Update Server property if specified in the namespace.
            // eg: "\\MyServer\root\cimv2".
            //
            if (status >= 0 && nsPath.Length > 1 &&
                (nsPath[0] == '\\' && nsPath[1] == '\\' ||
                 nsPath[0] == '/' && nsPath[1] == '/'))
            {
                uint uLen = 0;
                status = wmiPathTmp.GetServer_(ref uLen, null);

                if (status >= 0 && uLen > 0)
                {
                    char[] newServerChars = new char[(int)uLen];
                    status = wmiPathTmp.GetServer_(ref uLen, newServerChars);
                    string serverNew = new string(newServerChars, 0, Array.IndexOf(newServerChars, '\0'));

                    if (status >= 0)
                    {
                        // Compare server name on this object, if specified, to the caller's.
                        //     Update this object if different or unspecified.
                        uLen   = 0;
                        status = wmiPath.GetServer_(ref uLen, null);            // NB: Cannot use property get since it may throw.

                        if (status >= 0)
                        {
                            char[] orgServerChars = new char[(int)uLen];
                            status = wmiPath.GetServer_(ref uLen, orgServerChars);
                            string serverOrg = new string(orgServerChars, 0, Array.IndexOf(orgServerChars, '\0'));

                            if (status >= 0 && !string.Equals(serverOrg, serverNew, StringComparison.OrdinalIgnoreCase))
                            {
                                status = wmiPath.SetServer_(serverNew);
                            }
                        }
                        else if (status == (int)tag_WBEMSTATUS.WBEM_E_NOT_AVAILABLE)
                        {
                            status = wmiPath.SetServer_(serverNew);
                            if (status >= 0)
                            {
                                bChange = true;
                            }
                        }
                    }
                }
                else if (status == (int)tag_WBEMSTATUS.WBEM_E_NOT_AVAILABLE)    // No caller-supplied server name;
                {
                    status = (int)ManagementStatus.NoError;                     // Ignore error.
                }
            }

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

            return(nsNew);
        }
예제 #2
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);
        }
        internal string SetNamespacePath(string nsPath, out bool bChange)
        {
            int       errorCode = 0;
            string    strA      = null;
            string    strB      = null;
            IWbemPath wbemPath  = null;

            bChange = false;
            if (!IsValidNamespaceSyntax(nsPath))
            {
                ManagementException.ThrowWithExtendedInfo(ManagementStatus.InvalidNamespace);
            }
            wbemPath = this.CreateWbemPath(nsPath);
            if (this.wmiPath == null)
            {
                this.wmiPath = this.CreateWbemPath("");
            }
            else if (this.isWbemPathShared)
            {
                this.wmiPath          = this.CreateWbemPath(this.GetWbemPath());
                this.isWbemPathShared = false;
            }
            strA = GetNamespacePath(this.wmiPath, 0x10);
            strB = GetNamespacePath(wbemPath, 0x10);
            if (string.Compare(strA, strB, StringComparison.OrdinalIgnoreCase) != 0)
            {
                this.wmiPath.RemoveAllNamespaces_();
                bChange = true;
                uint puCount = 0;
                errorCode = wbemPath.GetNamespaceCount_(out puCount);
                if (errorCode >= 0)
                {
                    for (uint i = 0; i < puCount; i++)
                    {
                        uint puNameBufLength = 0;
                        errorCode = wbemPath.GetNamespaceAt_(i, ref puNameBufLength, null);
                        if (errorCode < 0)
                        {
                            break;
                        }
                        string pName = new string('0', ((int)puNameBufLength) - 1);
                        errorCode = wbemPath.GetNamespaceAt_(i, ref puNameBufLength, pName);
                        if (errorCode < 0)
                        {
                            break;
                        }
                        errorCode = this.wmiPath.SetNamespaceAt_(i, pName);
                        if (errorCode < 0)
                        {
                            break;
                        }
                    }
                }
            }
            if (((errorCode >= 0) && (nsPath.Length > 1)) && (((nsPath[0] == '\\') && (nsPath[1] == '\\')) || ((nsPath[0] == '/') && (nsPath[1] == '/'))))
            {
                uint num5 = 0;
                errorCode = wbemPath.GetServer_(ref num5, null);
                if ((errorCode >= 0) && (num5 > 0))
                {
                    string str4 = new string('0', ((int)num5) - 1);
                    errorCode = wbemPath.GetServer_(ref num5, str4);
                    if (errorCode >= 0)
                    {
                        num5      = 0;
                        errorCode = this.wmiPath.GetServer_(ref num5, null);
                        if (errorCode >= 0)
                        {
                            string str5 = new string('0', ((int)num5) - 1);
                            errorCode = this.wmiPath.GetServer_(ref num5, str5);
                            if ((errorCode >= 0) && (string.Compare(str5, str4, StringComparison.OrdinalIgnoreCase) != 0))
                            {
                                errorCode = this.wmiPath.SetServer_(str4);
                            }
                        }
                        else if (errorCode == -2147217399)
                        {
                            errorCode = this.wmiPath.SetServer_(str4);
                            if (errorCode >= 0)
                            {
                                bChange = true;
                            }
                        }
                    }
                }
                else if (errorCode == -2147217399)
                {
                    errorCode = 0;
                }
            }
            if (errorCode < 0)
            {
                if ((errorCode & 0xfffff000L) == 0x80041000L)
                {
                    ManagementException.ThrowWithExtendedInfo((ManagementStatus)errorCode);
                    return(strB);
                }
                Marshal.ThrowExceptionForHR(errorCode);
            }
            return(strB);
        }