int System.Management.IWbemServices_Old.CreateInstanceEnumAsync_(string strFilter, int lFlags, IWbemContext pCtx, IWbemObjectSink pResponseHandler) { Type type = null; int num; try { EventSource.preventShutdownLock.AcquireReaderLock(-1); if (EventSource.shutdownInProgress == 0) { int tickCount = Environment.TickCount + 100; foreach (Type key in this.instrumentedAssembly.mapTypeToConverter.Keys) { if (string.Compare(ManagedNameAttribute.GetMemberName(key), strFilter, StringComparison.Ordinal) != 0) { continue; } type = key; break; } if (null != type) { int num1 = 64; IntPtr[] value = new IntPtr[num1]; IntPtr[] intPtrArray = new IntPtr[num1]; ConvertToWMI[] convertToWMIArray = new ConvertToWMI[num1]; IWbemClassObjectFreeThreaded[] wbemClassObjectFreeThreaded = new IWbemClassObjectFreeThreaded[num1]; int num2 = 0; int V_8 = 0; object processIdentity = Instrumentation.ProcessIdentity; try { InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1); foreach (DictionaryEntry dictionaryEntry in InstrumentedAssembly.mapIDToPublishedObject) { if (EventSource.shutdownInProgress == 0) { if (type != dictionaryEntry.Value.GetType()) { continue; } if (convertToWMIArray[num2] != null) { lock (dictionaryEntry.Value) { convertToWMIArray[num2](dictionaryEntry.Value); } value[num2] = (IntPtr)convertToWMIArray[num2].Target.GetType().GetField("instWbemObjectAccessIP").GetValue(convertToWMIArray[num2].Target); Marshal.AddRef(value[num2]); wbemClassObjectFreeThreaded[num2] = new IWbemClassObjectFreeThreaded(value[num2]); wbemClassObjectFreeThreaded[num2].Put_("ProcessId", 0, ref processIdentity, 0); if (num2 == 0) { int V_15; WmiNetUtilsHelper.GetPropertyHandle_f27(27, wbemClassObjectFreeThreaded[num2], "InstanceId", out V_15, out V_8); } } else { object obj = Activator.CreateInstance((Type)this.instrumentedAssembly.mapTypeToConverter[type]); convertToWMIArray[num2] = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), obj, "ToWMI"); lock (dictionaryEntry.Value) { convertToWMIArray[num2](dictionaryEntry.Value); } value[num2] = (IntPtr)obj.GetType().GetField("instWbemObjectAccessIP").GetValue(obj); Marshal.AddRef(value[num2]); wbemClassObjectFreeThreaded[num2] = new IWbemClassObjectFreeThreaded(value[num2]); wbemClassObjectFreeThreaded[num2].Put_("ProcessId", 0, ref processIdentity, 0); if (num2 == 0) { int V_13; WmiNetUtilsHelper.GetPropertyHandle_f27(27, wbemClassObjectFreeThreaded[num2], "InstanceId", out V_13, out V_8); } } string str = (string)dictionaryEntry.Key; WmiNetUtilsHelper.WritePropertyValue_f28(28, wbemClassObjectFreeThreaded[num2], V_8, (str.Length + 1) * 2, str); num2++; if (num2 != num1 && Environment.TickCount < tickCount) { continue; } for (int i = 0; i < num2; i++) { WmiNetUtilsHelper.Clone_f(12, value[i], out intPtrArray[i]); } int num3 = pResponseHandler.Indicate_(num2, intPtrArray); for (int j = 0; j < num2; j++) { Marshal.Release(intPtrArray[j]); } if (num3 == 0) { num2 = 0; tickCount = Environment.TickCount + 100; } else { num = 0; return(num); } } else { num = 0; return(num); } } } finally { InstrumentedAssembly.readerWriterLock.ReleaseReaderLock(); } if (num2 > 0) { for (int k = 0; k < num2; k++) { WmiNetUtilsHelper.Clone_f(12, value[k], out intPtrArray[k]); } pResponseHandler.Indicate_(num2, intPtrArray); for (int l = 0; l < num2; l++) { Marshal.Release(intPtrArray[l]); } } return(0); } else { num = 0; } } else { num = 0; } } finally { pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); EventSource.preventShutdownLock.ReleaseReaderLock(); } return(num); }
int IWbemServices_Old.CreateInstanceEnumAsync_([In][MarshalAs(UnmanagedType.BStr)] string strFilter, [In] Int32 lFlags, [In][MarshalAs(UnmanagedType.Interface)] IWbemContext pCtx, [In][MarshalAs(UnmanagedType.Interface)] IWbemObjectSink pResponseHandler) { try { // Make sure we do not let a shutdown kill this thread preventShutdownLock.AcquireReaderLock(-1); // If we are already shutting down, don't do anything if (shutdownInProgress != 0) { return(0); } // If batching takes longer than 1/10 of a second, we want to stop // and flush the batch uint timeLimitForBatchFlush = (uint)Environment.TickCount + 100; // Find the managed type that is being requested by this call Type managedType = null; foreach (Type type in instrumentedAssembly.mapTypeToConverter.Keys) { if (0 == String.Compare(ManagedNameAttribute.GetMemberName(type), strFilter, StringComparison.Ordinal)) { managedType = type; break; } } // If we do not support the requested type, just exit if (null == managedType) { return(0); } // size for batching int batchSize = 64; // Array of IWbemClassObject IntPtrs for batching IntPtr[] objs = new IntPtr[batchSize]; IntPtr[] objsClone = new IntPtr[batchSize]; // Array of converter methods for batching ConvertToWMI[] funcs = new ConvertToWMI[batchSize]; // Array of IWbemClassObjectFreeThreaded instances for batching IWbemClassObjectFreeThreaded[] insts = new IWbemClassObjectFreeThreaded[batchSize]; // IWbemObjectAccess handle for 'InstanceId' int handleInstanceId = 0; // Current number of objects in batch int count = 0; // The process identity string Object processIdentity = Instrumentation.ProcessIdentity; // Walk all published instances try { // Don't let anyone add entries to the dictionary while we are enumerating it. // Other people can read from the dictionary InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1); foreach (DictionaryEntry entry in InstrumentedAssembly.mapIDToPublishedObject) { // If the process is going away, stop indicating more instances if (shutdownInProgress != 0) { return(0); } // Is this object the type requested by this query? if (managedType != entry.Value.GetType()) { continue; } // Initialize this batch entry if necessary. Each element of the batch arrays // are initialized 'just in time'. If we have less than batchSize entries total // (or it takes too long to batch), we do not unnecessarily allocate the batch entry if (funcs[count] == null) { Object converter = Activator.CreateInstance((Type)instrumentedAssembly.mapTypeToConverter[managedType]); funcs[count] = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), converter, "ToWMI"); // // Reuters VSQFE#: 750 [marioh] // In order for the instance data batching logic to work properly, we HAVE TO convert the .NET // objects to WMI objects before we update the batching pointers since the ToWMI method in the generated // code will Spawn new instances. // lock (entry.Value) funcs[count](entry.Value); objs[count] = (IntPtr)converter.GetType().GetField("instWbemObjectAccessIP").GetValue(converter); Marshal.AddRef(objs[count]); insts[count] = new IWbemClassObjectFreeThreaded(objs[count]); insts[count].Put_("ProcessId", 0, ref processIdentity, 0); int cimType; if (count == 0) { WmiNetUtilsHelper.GetPropertyHandle_f27(27, insts[count], "InstanceId", out cimType, out handleInstanceId); } } else { // // Reuters VSQFE#: 750 [marioh] // If we end up re-using an existing delegate from the batch array, we still have to convert // the .NET to WMI objects and update the instance pointers. // // Copy the managed instance information into the IWbemClassObject' // We're using a batch, therefore we have to convert them. lock (entry.Value) funcs[count](entry.Value); objs[count] = (IntPtr)funcs[count].Target.GetType().GetField("instWbemObjectAccessIP").GetValue(funcs[count].Target); // // We have to AddRef the interface pointer due to the IWbemClassObjectFreeThreaded not addreffing // but releasing in the destructor. Great huh? // Marshal.AddRef(objs[count]); insts[count] = new IWbemClassObjectFreeThreaded(objs[count]); insts[count].Put_("ProcessId", 0, ref processIdentity, 0); int cimType; if (count == 0) { WmiNetUtilsHelper.GetPropertyHandle_f27(27, insts[count], "InstanceId", out cimType, out handleInstanceId); } } // We have an instance to publish. Store the instance ID in 'InstanceId' string instanceId = (string)entry.Key; WmiNetUtilsHelper.WritePropertyValue_f28(28, insts[count], handleInstanceId, (instanceId.Length + 1) * 2, instanceId); // // Copy the managed instance information into the IWbemClassObject' // lock(entry.Value) // funcs[count](entry.Value); // Increment the batch counter count++; // If we've reached batchSize, or if we've gone longer than 1/10th second since // an Indicate, flush the batch if (count == batchSize || ((uint)Environment.TickCount) >= timeLimitForBatchFlush) { // Do the Indicate to WMI // NOTE: On WinXP, we cannot control whether the implementation of // Indicate will immediately send the objects to WMI, or if it will // batch them. If it batches them, we cannot reuse them in a future // call to Indicate, or change them in any way after the call to // Indicate. Because of this, we will always 'Clone' the objects // just before calling Indicate. The performance is negligable, even // on Windows 2000, which can handle about 200,000 Clones per second // on a 1 GHz machine. for (int i = 0; i < count; i++) { WmiNetUtilsHelper.Clone_f(12, objs[i], out objsClone[i]); } int hr = pResponseHandler.Indicate_(count, objsClone); for (int i = 0; i < count; i++) { Marshal.Release(objsClone[i]); } // If hr is not S_OK, we stop the enumeration. This can happen if the // client cancels the call. if (hr != 0) { return(0); } // Reset the batch counter count = 0; // Reset the time limit for another 1/10th second in the future timeLimitForBatchFlush = (uint)Environment.TickCount + 100; } } } finally { InstrumentedAssembly.readerWriterLock.ReleaseReaderLock(); } if (count > 0) { // NOTE: On WinXP, we cannot control whether the implementation of // Indicate will immediately send the objects to WMI, or if it will // batch them. If it batches them, we cannot reuse them in a future // call to Indicate, or change them in any way after the call to // Indicate. Because of this, we will always 'Clone' the objects // just before calling Indicate. The performance is negligable, even // on Windows 2000, which can handle about 200,000 Clones per second // on a 1 GHz machine. for (int i = 0; i < count; i++) { WmiNetUtilsHelper.Clone_f(12, objs[i], out objsClone[i]); } pResponseHandler.Indicate_(count, objsClone); for (int i = 0; i < count; i++) { Marshal.Release(objsClone[i]); } } } finally { pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); preventShutdownLock.ReleaseReaderLock(); } return(0); }
int IWbemServices_Old.CreateInstanceEnumAsync_([In, MarshalAs(UnmanagedType.BStr)] string strFilter, [In] int lFlags, [In, MarshalAs(UnmanagedType.Interface)] IWbemContext pCtx, [In, MarshalAs(UnmanagedType.Interface)] IWbemObjectSink pResponseHandler) { try { preventShutdownLock.AcquireReaderLock(-1); if (shutdownInProgress != 0) { return(0); } uint num = (uint)(Environment.TickCount + 100); Type type = null; foreach (Type type2 in this.instrumentedAssembly.mapTypeToConverter.Keys) { if (string.Compare(ManagedNameAttribute.GetMemberName(type2), strFilter, StringComparison.Ordinal) == 0) { type = type2; break; } } if (null == type) { return(0); } int num2 = 0x40; IntPtr[] ptrArray = new IntPtr[num2]; IntPtr[] apObjArray = new IntPtr[num2]; ConvertToWMI[] owmiArray = new ConvertToWMI[num2]; IWbemClassObjectFreeThreaded[] threadedArray = new IWbemClassObjectFreeThreaded[num2]; int plHandle = 0; int index = 0; object processIdentity = System.Management.Instrumentation.Instrumentation.ProcessIdentity; try { InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1); foreach (DictionaryEntry entry in InstrumentedAssembly.mapIDToPublishedObject) { if (shutdownInProgress != 0) { return(0); } if (type == entry.Value.GetType()) { if (owmiArray[index] == null) { object target = Activator.CreateInstance((Type)this.instrumentedAssembly.mapTypeToConverter[type]); owmiArray[index] = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), target, "ToWMI"); lock (entry.Value) { owmiArray[index](entry.Value); } ptrArray[index] = (IntPtr)target.GetType().GetField("instWbemObjectAccessIP").GetValue(target); Marshal.AddRef(ptrArray[index]); threadedArray[index] = new IWbemClassObjectFreeThreaded(ptrArray[index]); threadedArray[index].Put_("ProcessId", 0, ref processIdentity, 0); if (index == 0) { int num5; WmiNetUtilsHelper.GetPropertyHandle_f27(0x1b, (IntPtr)threadedArray[index], "InstanceId", out num5, out plHandle); } } else { lock (entry.Value) { owmiArray[index](entry.Value); } ptrArray[index] = (IntPtr)owmiArray[index].Target.GetType().GetField("instWbemObjectAccessIP").GetValue(owmiArray[index].Target); Marshal.AddRef(ptrArray[index]); threadedArray[index] = new IWbemClassObjectFreeThreaded(ptrArray[index]); threadedArray[index].Put_("ProcessId", 0, ref processIdentity, 0); if (index == 0) { int num6; WmiNetUtilsHelper.GetPropertyHandle_f27(0x1b, (IntPtr)threadedArray[index], "InstanceId", out num6, out plHandle); } } string key = (string)entry.Key; WmiNetUtilsHelper.WritePropertyValue_f28(0x1c, (IntPtr)threadedArray[index], plHandle, (key.Length + 1) * 2, key); index++; if ((index == num2) || (Environment.TickCount >= num)) { for (int i = 0; i < index; i++) { WmiNetUtilsHelper.Clone_f(12, ptrArray[i], out apObjArray[i]); } int num8 = pResponseHandler.Indicate_(index, apObjArray); for (int j = 0; j < index; j++) { Marshal.Release(apObjArray[j]); } if (num8 != 0) { return(0); } index = 0; num = (uint)(Environment.TickCount + 100); } } } } finally { InstrumentedAssembly.readerWriterLock.ReleaseReaderLock(); } if (index > 0) { for (int k = 0; k < index; k++) { WmiNetUtilsHelper.Clone_f(12, ptrArray[k], out apObjArray[k]); } pResponseHandler.Indicate_(index, apObjArray); for (int m = 0; m < index; m++) { Marshal.Release(apObjArray[m]); } } } finally { pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); preventShutdownLock.ReleaseReaderLock(); } return(0); }