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); }
internal void SetNamespacePath(string nsPath) { int status = (int)ManagementStatus.NoError; ManagementPath newPath = new ManagementPath(nsPath); if (null == newPath.wmiPath) { newPath.wmiPath = (IWbemPath) new WbemDefPath(); } // Remove all existing namespaces from the current path if (null != wmiPath) { status = wmiPath.RemoveAllNamespaces_(); } else { wmiPath = (IWbemPath) new WbemDefPath(); } // Add the new ones in uint nCount = 0; status = newPath.wmiPath.GetNamespaceCount_(out nCount); if (status >= 0) { for (uint i = 0; i < nCount; i++) { uint uLen = 0; status = newPath.wmiPath.GetNamespaceAt_(i, ref uLen, null); if (status >= 0) { string nSpace = new String('0', (int)uLen - 1); status = newPath.wmiPath.GetNamespaceAt_(i, ref uLen, nSpace); if (status >= 0) { status = wmiPath.SetNamespaceAt_(i, nSpace); if (status < 0) { break; } } else { break; } } else { break; } } } // // Update Server property if specified in the namespace. // if (status >= 0 && nCount > 0) // Successfully set namespace. { uint bufLen = 0; status = newPath.wmiPath.GetServer_(ref bufLen, null); if (status >= 0 && bufLen > 0) { String server = new String('0', (int)bufLen - 1); status = newPath.wmiPath.GetServer_(ref bufLen, server); if (status >= 0) // Can't use property set since it will do a get. { status = wmiPath.SetServer_(server); } } } if (status < 0) { if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else { Marshal.ThrowExceptionForHR(status); } } }