internal void Initialize() { //If the path is not set yet we can't do it if (null == path) { throw new InvalidOperationException(); } /* * If we're not connected yet, this is the time to do it... We lock * the state to prevent 2 threads simultaneously doing the same * connection. To avoid taking the lock unnecessarily we examine * isConnected first */ if (!IsConnected) { lock (this) { if (!IsConnected) { IWbemLocator loc = (IWbemLocator) new WbemLocator(); if (null == options) { Options = new ConnectionOptions(); } string nsPath = path.NamespacePath; // If no namespace specified, fill in the default one if ((null == nsPath) || (0 == nsPath.Length)) { // NB: we use a special method to set the namespace // path here as we do NOT want to trigger an // IdentifierChanged event as a result of this set path.SetNamespacePath(ManagementPath.DefaultPath.Path); } // If we have privileges to enable, now is the time SecurityHandler securityHandler = GetSecurityHandler(); int status = (int)ManagementStatus.NoError; try { status = loc.ConnectServer_( path.NamespacePath, options.Username, options.GetPassword(), options.Locale, options.Flags, options.Authority, options.GetContext(), out wbemServices); //Set security on services pointer Secure(wbemServices); } catch (Exception e) { // BUGBUG : securityHandler.Reset()? ManagementException.ThrowWithExtendedInfo(e); } finally { securityHandler.Reset(); } if ((status & 0xfffff000) == 0x80041000) { ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else if ((status & 0x80000000) != 0) { Marshal.ThrowExceptionForHR(status); } } } } }