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) { IWbemClassObjectFreeThreaded classObj; pNamespace.GetObject_(strFilter, 0, pCtx, out classObj, IntPtr.Zero); IWbemClassObjectFreeThreaded inst; classObj.SpawnInstance_(0, out inst); foreach (GCHandle h in InstrumentedAssembly.mapIDToRef.Values) { if (h.IsAllocated && h.Target.GetType().Name == strFilter) { 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); }
public TypeInfo(EventSource source, SchemaNaming naming, Type t) { this.toWMI = (ConvertFuncToWMI)InstrumentedAssembly.mapTypeToToWMIFunc[t]; this.toNET = (ConvertFuncToNET)InstrumentedAssembly.mapTypeToToNETFunc[t]; this.t = t; isSTA = Thread.CurrentThread.ApartmentState == ApartmentState.STA; ManagementClass eventClass = new ManagementClass(naming.NamespaceName + ":" + ManagedNameAttribute.GetMemberName(t)); PropertyInfo prop = typeof(ManagementBaseObject).GetProperty("WmiObject", BindingFlags.Instance | BindingFlags.NonPublic); ManagementObject evt = eventClass.CreateInstance(); obj = (IWbemClassObject_DoNotMarshal)prop.GetValue(evt, null); this.source = source; SetBatchSize(batchSize); IWbemClassObject_DoNotMarshal obj2; obj.Clone_(out obj2); xoa1[0] = (IWbemObjectAccess)obj2; oa1test = new ClassObjectArray(xoa1); writeDWORD1 = new WriteDWORD(xoa1[0].WriteDWORD_); writeQWORD1 = new WriteQWORD(xoa1[0].WriteQWORD_); #if xxx string code = CodeSpit.Spit(t, xoa1[0]); CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add(t.Assembly.Location); parameters.ReferencedAssemblies.Add(typeof(WriteDWORD).Assembly.Location); parameters.ReferencedAssemblies.Add(typeof(Event).Assembly.Location); CompilerResults results = compiler.CompileAssemblyFromSource(parameters, code); foreach (CompilerError err in results.Errors) { Console.WriteLine(err.ToString()); } Type dynType = results.CompiledAssembly.GetType("Hack"); MethodInfo doit = dynType.GetMethod("Func"); managedToIWbem = (ManagedToIWbem)Delegate.CreateDelegate(typeof(ManagedToIWbem), doit); #endif }
public static Hashtable mapTypeToToNETFunc = new Hashtable(); //TODO LOCK THIS public InstrumentedAssembly(Assembly assembly, SchemaNaming naming) { this.naming = naming; CSharpCodeProvider provider = new CSharpCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.ReferencedAssemblies.Add(assembly.Location); parameters.ReferencedAssemblies.Add(typeof(BaseEvent).Assembly.Location); parameters.ReferencedAssemblies.Add(typeof(System.ComponentModel.Component).Assembly.Location); CompilerResults results = compiler.CompileAssemblyFromSource(parameters, naming.Code); foreach (CompilerError err in results.Errors) { Console.WriteLine(err.ToString()); } Type dynType = results.CompiledAssembly.GetType("WMINET_Converter"); Type[] types = (Type[])dynType.GetField("netTypes").GetValue(null); MethodInfo[] toWMIs = (MethodInfo[])dynType.GetField("toWMIMethods").GetValue(null); MethodInfo[] toNETs = (MethodInfo[])dynType.GetField("toNETMethods").GetValue(null); Object theOne = dynType.GetField("theOne").GetValue(null); for (int i = 0; i < types.Length; i++) { ConvertFuncToWMI toWMI = (ConvertFuncToWMI)Delegate.CreateDelegate(typeof(ConvertFuncToWMI), theOne, toWMIs[i].Name); ConvertFuncToNET toNET = (ConvertFuncToNET)Delegate.CreateDelegate(typeof(ConvertFuncToNET), theOne, toNETs[i].Name); mapTypeToToWMIFunc.Add(types[i], toWMI); mapTypeToToNETFunc.Add(types[i], toNET); } // TODO: Is STA/MTA all we have to worry about? if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) { // We are on an STA thread. Create the event source on an MTA Thread thread = new Thread(new ThreadStart(InitEventSource)); thread.ApartmentState = ApartmentState.MTA; thread.Start(); thread.Join(); } else { InitEventSource(); } }
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 }