1

just need a little explanation please.

I am referencing question here about listening to processes. I am also getting the "access denied" error as mentioned in the comments but not sure how to run the WMI service as administrator for the example. Someone please point me in the right direction.

Here is what I have so far.

startWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived += new EventArrivedEventHandler(ProcessEvent);
startWatch.Start();

private void ProcessEvent(object sender, EventArrivedEventArgs e)
{...}
Community
  • 1
  • 1
Chef Pharaoh
  • 2,387
  • 3
  • 27
  • 38
  • check out this site.. also make sure you actually have WMI installed on your machine.. I am not an admin on my machine and I use WMI all the time. I will send you an example in one second http://msdn.microsoft.com/en-us/library/windows/desktop/aa394594(v=vs.85).aspx I will post a working code example in 30 seconds – MethodMan Dec 29 '11 at 21:28
  • How do you solved the admin rights problem? – GreenEyedAndy Sep 01 '16 at 09:30
  • @GreenEyedAndy Take a look at the answer I accepted. When changing my code to be similar to that answer I think I found I didn't need to bother with admin rights. Worst case, you can always change the permissions for launching your app to require admin rights or use run as. – Chef Pharaoh Sep 01 '16 at 23:39

1 Answers1

1
ManagementScope theScope = new ManagementScope("\\\\ComputerName\\root\\cimv2");
ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_ProcessStartTrace");
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
ManagementObjectCollection theCollection = theSearcher.Get();
foreach (ManagementObject theCurObject in theCollection)
{
  MessageBox.Show(theCurObject["whatever properties you are looking for"].ToString());
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Awesome, thanks. Also I ended up using a separate thread and didn't need the admin rights as you mentioned when I revised. Thanks for the link and help :) – Chef Pharaoh Dec 29 '11 at 21:46
  • @MethodMan: How to do the same in `C++`? – Jackzz Feb 25 '15 at 11:58
  • @Jackz this is C# example if you need to know how to do the same thing in `C++` I would suggest looking at your development tool and seeing if it supports `ManagementObjectSearcher` as well as `System.Management` here is a MSDN link select the `C++ tab` or do a google search https://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 – MethodMan Feb 25 '15 at 13:42
  • @MethodMan: I am using `VS2010` for development. Don't think that it supports `System.Management`. – Jackzz Feb 26 '15 at 04:17
  • @Jackz have you checked msdn documentation.. I think you need to do a bit more research - https://msdn.microsoft.com/en-us/library/system.management(v=vs.90).aspx for c++ check here - https://msdn.microsoft.com/en-us/library/system.management.managementclass(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 – MethodMan Feb 26 '15 at 14:16