// //IEnumerable methods // //**************************************** //GetEnumerator //**************************************** /// <summary> /// <para>Returns the enumerator for the collection.</para> /// </summary> /// <returns> /// An <see cref='System.Collections.IEnumerator'/>that can be used to iterate through the /// collection. /// </returns> public ManagementObjectEnumerator GetEnumerator() { if (isDisposed) { throw new ObjectDisposedException(name); } // Unless this is the first enumerator, we have // to clone. This may throw if we are non-rewindable. lock (firstEnum) { if (firstEnum.firstEnum) { firstEnum.firstEnum = false; return(new ManagementObjectEnumerator(this, enumWbem)); } else { IEnumWbemClassObject enumWbemClone = null; int status = (int)ManagementStatus.NoError; try { status = enumWbem.Clone_(out enumWbemClone); scope.GetSecurityHandler().Secure(enumWbemClone); if ((status & 0x80000000) == 0) { //since the original enumerator might not be reset, we need //to reset the new one. status = enumWbemClone.Reset_(); } } catch (Exception e) { ManagementException.ThrowWithExtendedInfo(e); } if ((status & 0xfffff000) == 0x80041000) { // BUGBUG : release callResult. ManagementException.ThrowWithExtendedInfo((ManagementStatus)status); } else if ((status & 0x80000000) != 0) { // BUGBUG : release callResult. Marshal.ThrowExceptionForHR(status); } return(new ManagementObjectEnumerator(this, enumWbemClone)); } } }
//**************************************** //Reset //**************************************** /// <summary> /// <para>Resets the enumerator to the beginning of the collection.</para> /// </summary> public void Reset() { if (isDisposed) { throw new ObjectDisposedException(name); } //If the collection is not rewindable you can't do this if (!collectionObject.options.Rewindable) { throw new InvalidOperationException(); } else { //Reset the WMI enumerator SecurityHandler securityHandler = collectionObject.scope.GetSecurityHandler(); int status = (int)ManagementStatus.NoError; try { status = enumWbem.Reset_(); } 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); } //Flush the current enumeration cache for (int i = (cacheIndex >= 0 ? cacheIndex : 0); i < cachedCount; i++) { Marshal.ReleaseComObject((IWbemClassObject_DoNotMarshal)(Marshal.GetObjectForIUnknown(cachedObjects[i]))); } cachedCount = 0; cacheIndex = -1; atEndOfCollection = false; } }