Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / C#
Article

Accessing Process Information Using the Win32 API

Rate me:
Please Sign up or sign in to vote.
4.71/5 (15 votes)
8 Nov 2006GPL3 83.6K   2.4K   25   9
An article on accessing process information using the Win32 API.

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.

C#
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");

}
C#
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)


Written By
Architect
Pakistan Pakistan
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.

Comments and Discussions

 
GeneralA suggestion Pin
Mohamad K. Ayyash5-Sep-07 3:51
Mohamad K. Ayyash5-Sep-07 3:51 
GeneralRe: A suggestion Pin
Waheed Iqbal5-Sep-07 9:29
Waheed Iqbal5-Sep-07 9:29 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.