locked
WMI active process name RRS feed

  • Question

  • I can use the following WMI code to get the names of all the running processes on a 64-bit version of Windows using a 32-bit application.  How do I expand this to get the name of the current active process (for the active window)?  I can't use the various Process functions and Win32 API functions to get the current active process because they won't work from a 32-bit application looking at 64-bit processes and hence I am using WMI.

    Also where can I find a list of valid strings for the GetPropertyValue method?

    Thanks

    ManagementClass processClass =
                    new ManagementClass("Win32_Process");
    ManagementObjectCollection classObjects = processClass.GetInstances();
    
    foreach (ManagementObject classObject in
                    classObjects)
    {
       richTextBox1.AppendText(
       classObject.GetPropertyValue(
          "Name").ToString  () + "\r\n");
    }



    Friday, April 12, 2013 3:39 PM

Answers

All replies

  • The problem is enumerating the 64-bit processes with a 32-bit applications when using the System.Diagnostics namespace, so you do this with WMI as you have done.  You can get all the various property names for your particular management class using the code below and you can find the active process name with the same code (see comment).

    Use GetWindowThreadProcessID (GetForegroundWindow(), out pid) (PInvoke) to get the process ID of the active window

    ManagementClass processClass = new ManagementClass("Win32_Process"); ManagementObjectCollection classObjects = processClass.GetInstances(); PropertyDataCollection properties = processClass.Properties; foreach (PropertyData property in properties) { //print out the name of all the property Strings richTextBox1.AppendText(property.Name + "\r\n"); } foreach (ManagementObject classObject in classObjects) { //output the name of the current active window executable if (classObject.GetPropertyValue("ProcessID") .ToString ().Equals(pid.ToString())) { richTextBox1.AppendText( classObject.GetPropertyValue("Name").ToString() + " "

    + classObject.GetPropertyValue("ProcessID").ToString() + "\r\n"); }

    You can get the full executable path using "ExecutablePath" in stead of "Name"



    • Proposed as answer by CoolDadTx Friday, April 12, 2013 5:44 PM
    • Edited by scocia888 Friday, April 12, 2013 5:52 PM
    Friday, April 12, 2013 5:40 PM
  • That's it thanks!
    • Marked as answer by monkey8888z Friday, April 12, 2013 6:07 PM
    Friday, April 12, 2013 6:07 PM