Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / VBScript

ProSysLib: Dissecting the Process

Rate me:
Please Sign up or sign in to vote.
4.84/5 (69 votes)
22 Nov 2010CPOL12 min read 128.9K   2.6K   174  
Access detailed information about the current process the easiest way.
using System;
using System.Windows.Forms;
using ProSysLib;

namespace ProcessViewer
{
    public partial class MainForm : Form
    {
		// Initializing ProSysLib root namespace;
		PSLSystem sys = ProSysLib.PSL.CreatePSLSystem();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            /////////////////////////////////////////////////////
            // Required for 32-bit processes on 32-bit OS only:
            //
            // We need to enable privilege "SeDebugPrivilege" on older systems
			// in order to get access to advanced details about processes:
			PSLPrivilege p = sys.Security.Privileges.Find("SeDebugPrivilege");
			if(p != null)
				p.Enabled = true;

            PopulateList(); // Populating the list of processes;
        }

        private void PopulateList()
        {
            ProcessList.Items.Clear(); // Remove all items;
            foreach (PSLProcess p in sys.Software.Processes) // Go through all processes;
            {
                ListViewItem item = ProcessList.Items.Add(p.ProcessID.ToString());

                string sProcessName = "";
                if (p.ProcessID == 0) // System Idle Process
                    sProcessName = "System Idle Process";
                else
                {
                    sProcessName = p.FileName;
                    if (sys.Software.OS.Is64Bit && p.Is64Bit == false)  // If OS is 64-bit, and the found process
                        sProcessName += " *32";                         // is not 64-bit, we add " *32" like in TaskManager;
                }

                // Adding some of the available process details...
                //
                // NOTE: p.FilePath for 64-bit processes will always be
                // empty for a 32-bit client running on a 64-bit system,
                // simply because Windows prohibits 32-bit processes to
                // have any access to 64-bit-specific folders;

                item.SubItems.Add(sProcessName);
                item.SubItems.Add(p.FilePath); 
                item.SubItems.Add(p.UserName);
                item.SubItems.Add(p.ThreadCount.ToString());
                item.SubItems.Add(p.HandleCount.ToString());

                item.Tag = p; // Associate each list item with the process object;
            }
            btnKill.Enabled = false;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCurrentUser_CheckedChanged(object sender, EventArgs e)
        {
            // Switch to enumerating processes for the current user only;
            sys.Software.Processes.EnumUserType = PSLEnumUserType.enumForCurrentUser;
            sys.Software.Processes.Update();
            PopulateList();
        }

        private void btnAllUsers_CheckedChanged(object sender, EventArgs e)
        {
            // Switch to enumerating processes for all users;
            sys.Software.Processes.EnumUserType = PSLEnumUserType.enumForAllUsers;
            sys.Software.Processes.Update();
            PopulateList();
        }

        private void btnKill_Click(object sender, EventArgs e)
        {
            PSLProcess p = (PSLProcess)ProcessList.SelectedItems[0].Tag;
            if (p.Kill())
            {
                MessageBox.Show("Process " + p.FileName + " was sucessfully killed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                sys.Software.Processes.Update(); // Need to update, as at least one process is gone;
                PopulateList(); // Re-populating the list of processes;
            }
            else
                MessageBox.Show("Failed to kill process " + p.FileName, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            sys.Software.Processes.Update(); // Re-read list of processes in the system;
            PopulateList(); // Re-populate the list of processes;
        }

        private void ProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            btnKill.Enabled = ProcessList.SelectedItems.Count > 0;
        }
    }
}

namespace ProSysLib
{
	// Neutral (32/64-bit) loader for ProSysLib
	// that does not use Windows Registry;
	static class PSL
	{
		// Creates a new instance of ProSysLib
		public static PSLSystem CreatePSLSystem()
		{
			if (IntPtr.Size == 8) // 64-bit
				return PSL64.CreatePSLSystem();
			else // 32-bit;
				return PSL32.CreatePSLSystem();
		}

		// Accessor to 32-bit version of ProSysLib
		static class PSL32
		{
			// Replace it with your own relative path ;)
			[System.Runtime.InteropServices.DllImport("..\\..\\..\\..\\..\\Bin\\PSL32v0.9.dll")]
			public extern static PSLSystem CreatePSLSystem();
		}

		// Accessor to 64-bit version of ProSysLib
		static class PSL64
		{
			// Replace it with your own relative path ;)
			[System.Runtime.InteropServices.DllImport("..\\..\\..\\..\\..\\Bin\\PSL64v0.9.dll")]
			public extern static PSLSystem CreatePSLSystem();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Sibedge IT
Ireland Ireland
My online CV: cv.vitalytomilov.com

Comments and Discussions