/// <summary> /// Initializes a new instance of the <see cref="WmiObject"/> class. /// </summary> /// <param name="wmiObject">The native <see cref="IWbemClassObject"/> object.</param> #endregion internal WmiObject(IWbemClassObject wmiObject) { if (wmiObject == null) { throw new ArgumentNullException(MethodBase.GetCurrentMethod().GetParameters().First().Name); } this.wbemClassObject = wmiObject; }
/// <summary> /// Gets the names of all properties in the object. /// </summary> /// <param name="this">The <see cref="IWbemClassObject"/> object which will be used to execute the method.</param> /// <param name="flags">Combination of flags. (see <see url="http://msdn.microsoft.com/en-us/library/windows/desktop/aa391447(v=vs.85).aspx"/>)</param> /// <returns>The names of all properties.</returns> #endregion internal static IEnumerable <string> GetNames(this IWbemClassObject @this, WbemConditionFlag flags = WbemConditionFlag.WBEM_FLAG_NONSYSTEM_ONLY) { string[] names = null; HResult hr = @this.GetNames(null, flags, null, out names); if (hr.Failed) { throw (Exception)hr; } return(names); }
/// <summary> /// Retrieves the next object in the enumeration starting from the current position. /// </summary> /// <param name="this">The <see cref="WmiLight.Wbem.IWbemClassObjectEnumerator"/> which will be moved.</param> /// <returns>The next object in the enumeration.</returns> #endregion internal static IWbemClassObject Next(this IWbemClassObjectEnumerator @this) { IWbemClassObject current = null; uint returnedCount; HResult hresult = @this.Next(Timeout.Infinite, 1, out current, out returnedCount); if (hresult.Failed) { throw (Exception)hresult; } return(current); }
/// <summary> /// Gets a particular property value. /// </summary> /// <param name="this">The <see cref="IWbemClassObject"/> object which will be used to execute the method.</param> /// <param name="propertyName">Name of the desired property.</param> /// <returns>The correct type and value for the qualifier.</returns> #endregion internal static object Get(this IWbemClassObject @this, string propertyName) { object value; CimType type; int flavor; HResult hresult = @this.Get(propertyName, 0, out value, out type, out flavor); if (hresult.Failed) { throw (Exception)hresult; } return(value == DBNull.Value ? null : value); }
/// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns> #endregion public bool MoveNext() { if (this.disposed) { throw new ObjectDisposedException(typeof(WmiObjectEnumerator).FullName); } IWbemClassObject currentWmiObject = this.wbemClassObjectEnumerator.Next(); if (currentWmiObject != null) { this.Current = new WmiObject(currentWmiObject); return(true); } return(false); }
int IWbemServices_Old.GetObjectAsync_([In][MarshalAs(UnmanagedType.BStr)] string strObjectPath, [In] Int32 lFlags, [In][MarshalAs(UnmanagedType.Interface)] IWbemContext pCtx, [In][MarshalAs(UnmanagedType.Interface)] IWbemObjectSink pResponseHandler) { // pResponseHandler.SetStatus(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, null); // Marshal.ReleaseComObject(pResponseHandler); // return (int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND); Match match = Regex.Match(strObjectPath.ToLower(CultureInfo.InvariantCulture), "(.*?)\\.instanceid=\"(.*?)\",processid=\"(.*?)\""); if (match.Success == false) { pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); } string className = match.Groups[1].Value; string instanceId = match.Groups[2].Value; string processId = match.Groups[3].Value; if (Instrumentation.ProcessIdentity != processId) { pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); } int id = ((IConvertible)instanceId).ToInt32((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int32))); Object theObject = null; try { InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1); theObject = InstrumentedAssembly.mapIDToPublishedObject[id.ToString((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int32)))]; } finally { InstrumentedAssembly.readerWriterLock.ReleaseReaderLock(); } if (theObject != null) { Type converterType = (Type)instrumentedAssembly.mapTypeToConverter[theObject.GetType()]; if (converterType != null) { Object converter = Activator.CreateInstance(converterType); ConvertToWMI func = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), converter, "ToWMI"); // // Regression: Reuters VSQFE#: 750, PS#141144 [marioh] // GetObjectAsync was missed. Again, here we have to call ToWMI before retrieving the pointer to the object // since we clone a new one during the ToWMI call. The code below was simply moved from a location further down // lock (theObject) func(theObject); // // END: Regression: Reuters VSQFE#: 750, PS#141144 [marioh] IntPtr[] objs = new IntPtr[] { (IntPtr)converter.GetType().GetField("instWbemObjectAccessIP").GetValue(converter) }; Marshal.AddRef(objs[0]); IWbemClassObjectFreeThreaded inst = new IWbemClassObjectFreeThreaded(objs[0]); Object o = id; inst.Put_("InstanceId", 0, ref o, 0); o = Instrumentation.ProcessIdentity; inst.Put_("ProcessId", 0, ref o, 0); // ConvertFuncToWMI func = (ConvertFuncToWMI)InstrumentedAssembly.mapTypeToToWMIFunc[h.Target.GetType()]; // // Reuters VSQFE#: 750, PS#141144 [marioh] // The commented out code was moved up before accessing the object pointer in order to get the // newly cloned one. // // lock(theObject) // func(theObject); pResponseHandler.Indicate_(1, objs); pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return(0); } } pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); #if xxx IWbemClassObject classObj = null; IWbemCallResult result = null; pNamespace.GetObject("TestInstance", 0, pCtx, ref classObj, ref result); IWbemClassObject inst; classObj.SpawnInstance(0, out inst); TestInstance testInstance = (TestInstance)mapNameToTestInstance[match.Groups[1].Value]; Object o = (object)testInstance.name; inst.Put("name", 0, ref o, 0); o = (object)testInstance.value; inst.Put("value", 0, ref o, 0); pResponseHandler.Indicate(1, new IWbemClassObject[] { inst }); pResponseHandler.SetStatus_(0, 0, IntPtr.Zero, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); #endif }
int IWbemServices_Old.GetObjectAsync_([In][MarshalAs(UnmanagedType.BStr)] string strObjectPath, [In] Int32 lFlags, [In][MarshalAs(UnmanagedType.Interface)] IWbemContext pCtx, [In][MarshalAs(UnmanagedType.Interface)] IWbemObjectSink pResponseHandler) { // pResponseHandler.SetStatus(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, null); // Marshal.ReleaseComObject(pResponseHandler); // return (int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND); Match match = Regex.Match(strObjectPath.ToLower(CultureInfo.InvariantCulture), "(.*?)\\.instanceid=\"(.*?)\",processid=\"(.*?)\""); if (match.Success == false) { pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); } string className = match.Groups[1].Value; string instanceId = match.Groups[2].Value; string processId = match.Groups[3].Value; if (Instrumentation.ProcessIdentity != processId) { pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); } int id = ((IConvertible)instanceId).ToInt32(null); Object theObject = null; try { InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1); theObject = InstrumentedAssembly.mapIDToPublishedObject[id.ToString()]; } finally { InstrumentedAssembly.readerWriterLock.ReleaseReaderLock(); } if (theObject != null) { Type converterType = (Type)instrumentedAssembly.mapTypeToConverter[theObject.GetType()]; if (converterType != null) { Object converter = Activator.CreateInstance(converterType); ConvertToWMI func = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), converter, "ToWMI"); IntPtr[] objs = new IntPtr[] { (IntPtr)converter.GetType().GetField("instWbemObjectAccessIP").GetValue(converter) }; Marshal.AddRef(objs[0]); IWbemClassObjectFreeThreaded inst = new IWbemClassObjectFreeThreaded(objs[0]); Object o = id; inst.Put_("InstanceId", 0, ref o, 0); o = Instrumentation.ProcessIdentity; inst.Put_("ProcessId", 0, ref o, 0); // ConvertFuncToWMI func = (ConvertFuncToWMI)InstrumentedAssembly.mapTypeToToWMIFunc[h.Target.GetType()]; lock (theObject) func(theObject); pResponseHandler.Indicate_(1, objs); pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return(0); } } pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); #if xxx IWbemClassObject classObj = null; IWbemCallResult result = null; pNamespace.GetObject("TestInstance", 0, pCtx, ref classObj, ref result); IWbemClassObject inst; classObj.SpawnInstance(0, out inst); TestInstance testInstance = (TestInstance)mapNameToTestInstance[match.Groups[1].Value]; Object o = (object)testInstance.name; inst.Put("name", 0, ref o, 0); o = (object)testInstance.value; inst.Put("value", 0, ref o, 0); pResponseHandler.Indicate(1, new IWbemClassObject[] { inst }); pResponseHandler.SetStatus_(0, 0, IntPtr.Zero, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); #endif }
int IWbemServices_Old.GetObjectAsync_([In][MarshalAs(UnmanagedType.BStr)] string strObjectPath, [In] Int32 lFlags, [In][MarshalAs(UnmanagedType.Interface)] IWbemContext pCtx, [In][MarshalAs(UnmanagedType.Interface)] IWbemObjectSink pResponseHandler) { // pResponseHandler.SetStatus(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, null); // Marshal.ReleaseComObject(pResponseHandler); // return (int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND); Match match = Regex.Match(strObjectPath.ToLower(), "(.*?)\\.instanceid=\"(.*?)\",processid=\"(.*?)\""); if (match.Success == false) { pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); } string className = match.Groups[1].Value; string instanceId = match.Groups[2].Value; string processId = match.Groups[3].Value; if (Instrumentation.ProcessIdentity != processId) { pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); } int id = ((IConvertible)instanceId).ToInt32(null); if (InstrumentedAssembly.mapIDToRef.ContainsKey(id)) { GCHandle h = (GCHandle)InstrumentedAssembly.mapIDToRef[id]; if (h.IsAllocated) { IWbemClassObjectFreeThreaded classObj; pNamespace.GetObject_(h.Target.GetType().Name, 0, pCtx, out classObj, IntPtr.Zero); IWbemClassObjectFreeThreaded inst; classObj.SpawnInstance_(0, out inst); Object o = h.GetHashCode(); inst.Put_("InstanceId", 0, ref o, 0); o = Instrumentation.ProcessIdentity; inst.Put_("ProcessId", 0, ref o, 0); ConvertFuncToWMI func = (ConvertFuncToWMI)InstrumentedAssembly.mapTypeToToWMIFunc[h.Target.GetType()]; // func(h.Target, inst); // pResponseHandler.Indicate_(1, ref inst); pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return(0); } } pResponseHandler.SetStatus_(0, (int)tag_WBEMSTATUS.WBEM_E_NOT_FOUND, null, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); return((int)(tag_WBEMSTATUS.WBEM_E_NOT_FOUND)); #if xxx IWbemClassObject classObj = null; IWbemCallResult result = null; pNamespace.GetObject("TestInstance", 0, pCtx, ref classObj, ref result); IWbemClassObject inst; classObj.SpawnInstance(0, out inst); TestInstance testInstance = (TestInstance)mapNameToTestInstance[match.Groups[1].Value]; Object o = (object)testInstance.name; inst.Put("name", 0, ref o, 0); o = (object)testInstance.value; inst.Put("value", 0, ref o, 0); pResponseHandler.Indicate(1, new IWbemClassObject[] { inst }); pResponseHandler.SetStatus_(0, 0, IntPtr.Zero, IntPtr.Zero); Marshal.ReleaseComObject(pResponseHandler); #endif }