How do I create an SQL query which returns processes with visual representation?

54 views Asked by At

I want to create a query which gets all active processes with visual representation. The active part is already done, I'm still confused on the visual representation though. It uses Managementobject's id to find the process and then checks if the MainWindowHandle isn't intptr.Zero (no visual representation).

Something like this:

ManagmentObjectSearcher search = new("SELECT * FROM Win32_Process" +
  "WHERE ExecutionState < 4 OR ExecutionState = 9");
ManagementObjectCollection collection = search.Get();
foreach (ManagementObject obj in collection.Cast<ManagementObject>())
{
    bool succes = int.TryParse(obj["ProcessId"].ToString(),out int Id);
    if (!succes) continue;
    Process process = Process.GetProcessById(Id);
    if (process.MainWindowHandle == intptr.zero) continue;
    //Rest of code to handle processes with visual representation...
}

Now I was wondering, to reduce the amount of iterations, can we add a check in the query that checks if it has visual representation? Like this:

SELECT * FROM Win32_Process
WHERE (ExecutionState < 4 OR ExecutionState = 9) AND MainWindowHandle IS NOT NULL

but I'm not sure if you need to use numbers instead:

SELECT * FROM Win32_Process
WHERE (ExecutionState < 4 OR ExecutionState = 9) AND NOT MainWindowHandle = 0

I looked at the documentation and I didn't even find the MainWindowHandle property, so now I'm wondering if I can even use that.

I tried looking for other resources, expecting that one would mention 'MainWindowHandle' but no.

0

There are 0 answers