Example #1
0
        int IWbemServices_Old.GetObjectAsync_([In, MarshalAs(UnmanagedType.BStr)] string strObjectPath, [In] int lFlags, [In, MarshalAs(UnmanagedType.Interface)] IWbemContext pCtx, [In, MarshalAs(UnmanagedType.Interface)] IWbemObjectSink pResponseHandler)
        {
            Match match = Regex.Match(strObjectPath.ToLower(CultureInfo.InvariantCulture), "(.*?)\\.instanceid=\"(.*?)\",processid=\"(.*?)\"");

            if (!match.Success)
            {
                pResponseHandler.SetStatus_(0, -2147217406, null, IntPtr.Zero);
                Marshal.ReleaseComObject(pResponseHandler);
                return(-2147217406);
            }
            string text1 = match.Groups[1].Value;
            string str   = match.Groups[2].Value;
            string str2  = match.Groups[3].Value;

            if (System.Management.Instrumentation.Instrumentation.ProcessIdentity != str2)
            {
                pResponseHandler.SetStatus_(0, -2147217406, null, IntPtr.Zero);
                Marshal.ReleaseComObject(pResponseHandler);
                return(-2147217406);
            }
            int    num  = ((IConvertible)str).ToInt32((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(int)));
            object obj2 = null;

            try
            {
                InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1);
                obj2 = InstrumentedAssembly.mapIDToPublishedObject[num.ToString((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(int)))];
            }
            finally
            {
                InstrumentedAssembly.readerWriterLock.ReleaseReaderLock();
            }
            if (obj2 != null)
            {
                Type type = (Type)this.instrumentedAssembly.mapTypeToConverter[obj2.GetType()];
                if (type != null)
                {
                    object       target = Activator.CreateInstance(type);
                    ConvertToWMI owmi   = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), target, "ToWMI");
                    lock (obj2)
                    {
                        owmi(obj2);
                    }
                    IntPtr[] apObjArray = new IntPtr[] { (IntPtr)target.GetType().GetField("instWbemObjectAccessIP").GetValue(target) };
                    Marshal.AddRef(apObjArray[0]);
                    IWbemClassObjectFreeThreaded threaded = new IWbemClassObjectFreeThreaded(apObjArray[0]);
                    object pVal = num;
                    threaded.Put_("InstanceId", 0, ref pVal, 0);
                    pVal = System.Management.Instrumentation.Instrumentation.ProcessIdentity;
                    threaded.Put_("ProcessId", 0, ref pVal, 0);
                    pResponseHandler.Indicate_(1, apObjArray);
                    pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero);
                    Marshal.ReleaseComObject(pResponseHandler);
                    return(0);
                }
            }
            pResponseHandler.SetStatus_(0, -2147217406, null, IntPtr.Zero);
            Marshal.ReleaseComObject(pResponseHandler);
            return(-2147217406);
        }
Example #2
0
        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);
        }
Example #3
0
        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
        }
Example #4
0
        int System.Management.IWbemServices_Old.GetObjectAsync_(string strObjectPath, int lFlags, IWbemContext pCtx, IWbemObjectSink pResponseHandler)
        {
            Match match = Regex.Match(strObjectPath.ToLower(CultureInfo.InvariantCulture), "(.*?)\\.instanceid=\"(.*?)\",processid=\"(.*?)\"");

            if (match.Success)
            {
                //match.Groups[1].Value;
                string value = match.Groups[2].Value;
                string str   = match.Groups[3].Value;
                if (Instrumentation.ProcessIdentity == str)
                {
                    int    num  = ((IConvertible)value).ToInt32((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(int)));
                    object item = null;
                    try
                    {
                        InstrumentedAssembly.readerWriterLock.AcquireReaderLock(-1);
                        item = InstrumentedAssembly.mapIDToPublishedObject[num.ToString((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(int)))];
                    }
                    finally
                    {
                        InstrumentedAssembly.readerWriterLock.ReleaseReaderLock();
                    }
                    if (item != null)
                    {
                        Type type = (Type)this.instrumentedAssembly.mapTypeToConverter[item.GetType()];
                        if (type != null)
                        {
                            object       obj          = Activator.CreateInstance(type);
                            ConvertToWMI convertToWMI = (ConvertToWMI)Delegate.CreateDelegate(typeof(ConvertToWMI), obj, "ToWMI");
                            lock (item)
                            {
                                convertToWMI(item);
                            }
                            IntPtr[] intPtrArray = new IntPtr[1];
                            intPtrArray[0] = (IntPtr)obj.GetType().GetField("instWbemObjectAccessIP").GetValue(obj);
                            IntPtr[] intPtrArray1 = intPtrArray;
                            Marshal.AddRef(intPtrArray1[0]);
                            IWbemClassObjectFreeThreaded wbemClassObjectFreeThreaded = new IWbemClassObjectFreeThreaded(intPtrArray1[0]);
                            object processIdentity = num;
                            wbemClassObjectFreeThreaded.Put_("InstanceId", 0, ref processIdentity, 0);
                            processIdentity = Instrumentation.ProcessIdentity;
                            wbemClassObjectFreeThreaded.Put_("ProcessId", 0, ref processIdentity, 0);
                            pResponseHandler.Indicate_(1, intPtrArray1);
                            pResponseHandler.SetStatus_(0, 0, null, IntPtr.Zero);
                            Marshal.ReleaseComObject(pResponseHandler);
                            return(0);
                        }
                    }
                    pResponseHandler.SetStatus_(0, -2147217406, null, IntPtr.Zero);
                    Marshal.ReleaseComObject(pResponseHandler);
                    return(-2147217406);
                }
                else
                {
                    pResponseHandler.SetStatus_(0, -2147217406, null, IntPtr.Zero);
                    Marshal.ReleaseComObject(pResponseHandler);
                    return(-2147217406);
                }
            }
            else
            {
                pResponseHandler.SetStatus_(0, -2147217406, null, IntPtr.Zero);
                Marshal.ReleaseComObject(pResponseHandler);
                return(-2147217406);
            }
        }
Example #5
0
        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);
        }
Example #6
0
        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
        }
Example #7
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);
 }
        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.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
        }