Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello, i wrote this loop to get all processes by using Process.GetProcesses()

C#
foreach (var currentProcess in Process.GetProcesses())
{

    bool is32bit = false;
    IntPtr hprocess = OpenProcess((PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_VM_WRITE), false, currentProcess.Id);
    IsWow64Process(hprocess, out is32bit);

    if (hprocess != IntPtr.Zero)
    {
        if (is32bit = false)
        {
            Console.WriteLine("{0}: {1} - 64 BIT", hprocess, currentProcess.ProcessName);
        }
        else
        {
            Console.WriteLine("{0}: {1} - 32 BIT", hprocess, currentProcess.ProcessName);
        }
    }


    CloseHandle(hprocess);
}


I am facing some issues...

1) I get 0 instead of a handle IntPtr on many processes
2) There where i don't get 0, i get the same handle for more than one process (the sample output below)
3) Only 32bit processes are beign enumerated unfortunately (the sample output below)


This is the sample output. ANY help would be appreciated!!!

556: putty - 32 BIT<br />
556: vmware-unity-helper - 32 BIT<br />
556: Greenshot - 32 BIT<br />
556: RAVCpl64 - 32 BIT<br />
556: notepad - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: vmplayer - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
556: MOM - 32 BIT<br />
556: chrome - 32 BIT<br />
556: chrome - 32 BIT<br />
564: TeamViewer - 32 BIT<br />
564: chrome - 32 BIT<br />
568: calc - 32 BIT<br />
568: explorer - 32 BIT<br />
568: conhost - 32 BIT<br />
568: taskhostex - 32 BIT<br />
568: chrome - 32 BIT<br />
568: xampp-control - 32 BIT<br />
568: chrome - 32 BIT<br />
568: conhost - 32 BIT<br />
568: chrome - 32 BIT<br />
568: notepad++ - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: AdobeIPCBroker - 32 BIT<br />
568: chrome - 32 BIT<br />
568: conhost - 32 BIT<br />
568: IntelliTrace - 32 BIT<br />
568: chrome - 32 BIT<br />
568: bdwtxag - 32 BIT<br />
568: chrome - 32 BIT<br />
568: chrome - 32 BIT<br />
568: MSBuild - 32 BIT<br />
568: CCC - 32 BIT<br />
568: vprintproxy - 32 BIT<br />
568: chrome - 32 BIT<br />
568: putty - 32 BIT<br />
568: chrome - 32 BIT
Posted
Comments
Sergey Alexandrovich Kryukov 17-Dec-14 14:52pm    
How about just thinking for a minute? :-)
—SA

1 solution

First of all
C#
if (is32bit = false)

is both buggy (should be '=='; comparison, not assignment you wrote) and silly, because is32bit is already Boolean. Should be:
C#
if (is32bit)
    // ...

Also, you should not open the process to get a handle. Instead, use currentProcess.Handle:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.handle%28v=vs.110%29.aspx[^].

Probably you need to show both Handle and Id, or, even better, Id only, because Handle is nearly useless to from the user's standpoint. Only the Id is good for using in different processes; this is the process-independent (system-wide) ID of a process. Handles of the same process obtained in the address spaces of different application domains will be different numerical values.

—SA
 
Share this answer
 
v5
Comments
Konstantinos Karakatsoulis 17-Dec-14 15:11pm    
Problem #3 solved with what you suggested, i still get the same Handle from ALL processes :/

Oh and... currentProcess.Handle throws an error btw that's why i didn't use it :/
Sergey Alexandrovich Kryukov 17-Dec-14 17:44pm    
This is another reason to show only IDs, not Handles. Handles are useless to show. Run it "As Administrator" to see the handles. Also, sandwich each process.Handle call in try-catch.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900