Click here to Skip to main content
Click here to Skip to main content

Accessing Process Information Using the Win32 API

By , 8 Nov 2006
 

Sample Image

Introduction

Calling System.Diagonistics.Process.GetProcesses() on a hyper-threading (possibly true multi-proc) computer running Windows 2000 fails with: "Couldn't get process information from remote machine ---> System.ArgumentOutOfRangeException: Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks." The same error can occur if your registry key HKEY_LOCAL_MACHINE.Software.Microsoft.WindowNT.CurrentVersion.PerfLib is corrupted. The solution for this problem is to get process  information using win32 API.

Using the code

This Win32 API implementation project consists of one key file: Processes.cs which implements the Win32 functions for Process List, Process IDs, and killing a process by process ID. An example project is also included in the source code that demonstrates the basic usage of the Process class.

Simply build and run the project, you will see a Windows form that has a listbox loading all system processes. You can select any process and just click the KILL button, and the selected process will be killed by using the Win32 API implementation.

private void btnKill_Click(object sender, System.EventArgs e)
{
    string strProcessName=
      System.IO.Path.GetFileNameWithoutExtension(
      lstProcesses.SelectedItem.ToString());
    uint processId=Convert.ToUInt32(
      Win32Processes.Processes.GetProcessByName(
      strProcessName)[0].ToString());
    if(Win32Processes.Processes.Kill(processId))
    {
        MessageBox.Show("Process is killed successfully");
        LoadProcesses();
    }
    else
        MessageBox.Show("Process cannot be Killed");

}
public static bool Kill(uint uiProcessId)
{
    System.IntPtr handler= 
       OpenProcess(PROCESS_KILLRIGHTS,false,uiProcessId); 
    bool b=Win32Processes.Processes.KillProcess(handler);
    Win32Processes.Processes.CloseHandle(handler);
    return b; 
}

History

  • Initial release - 8th November 2006.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Waheed Iqbal
Architect
Pakistan Pakistan
Member
Waheed is a student of Master leading to PhD (computer sciences) at AIT (http://www.ait.asia). His area of research is High performance computing(HPC). Prior to this he was attached with Compass partners Inc. and Mazik Pakistan as a Software Engineer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank you very muchmemberkasparovthe217 Feb '09 - 8:39 
Thanx ! That was exactly what i was lookin' for ! Big Grin | :-D
GeneralThanks!memberequi4y3q420 Aug '08 - 0:18 
The managed version is insufficient because it doesn't update it's snapshot. The API is just what I needed, thanks.
GeneralGreat!....memberMember 297619318 Jun '08 - 19:14 
Hello Waheed Iqbal,
The tutorial you have given is great and is so helpful to me.
I have searched a lot to kill process by name but i had never found it.
Thanks.
GeneralProcess hangsmemberRajeshTech14 Oct '07 - 19:26 
Is there any way i can know whether any process hangs or not using C#.net ? or i need to know how do we check the process hangs status using System.Diagnostic namespace ?
 

 
Rajesh Agarawal
GeneralA suggestionmemberMohamad K Ayash5 Sep '07 - 3:51 
Very Good Work work but let me suggset to you regarding shape(not content) to create instead of a listbox a listview item that lists in a well organized manner the process name, ID, mainwindowcaption, icon, number of threads, priority,...etc.(You may not need to use win32 api though, but you may).
 
Best Wishes...
 
To follow the path, Walk with the MASTER, See through the MASTER, Be the MASTER!

GeneralRe: A suggestionmemberWaheed Iqbal5 Sep '07 - 9:29 
Ayesh!
 
Thanks for the nice suggestion but I just explained the contents and I just used a small form to explain the contents. I explained a typical problem that u may receive while u use .net calls to access process information.

 
Waheed Iqbal
 
Software Engineer
MCP C#.Net

GeneralThank you so muchmembersoniavdas2 Jul '07 - 9:03 
This was very useful. My app was developed using VS 2003 and it was throwing exception in Vista with "couldn't get process information on remote machine" and in XP "performamce counter is disabled". It is working after I used your solution.
GeneralAccessing Process Information Using the Win32 APImemberSquall_aow10 Dec '06 - 16:23 
Hi, Waheed
 
This code works fine on me.
Good job man! Thanks very much..
 
Best Regards,
Victor.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 8 Nov 2006
Article Copyright 2006 by Waheed Iqbal
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid