Click here to Skip to main content
15,895,777 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 129.2K   2.6K   174  
Access detailed information about the current process the easiest way.
using System;
using System.Windows.Forms;
using ProSysLib;

namespace ProcessInfo
{
    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)
        {
			// Need to set default values for windows filters:
			IsVisibleFlag.SelectedIndex = 0;
			IsEnabledFlag.SelectedIndex = 0;
			IsTopLevelFlag.SelectedIndex = 0;
			HasTextFlag.SelectedIndex = 0;

			// Populate all lists:
            PopulateStatic();
            PopulateDynamic();
			PopulateMemory();
			PopulateIOCounters();
			PopulateEnvironment();
			PopulateCommands();
            PopulateWindows();
            PopulateThreads();
			PopulateModules();
			PopulatePrivileges();
        }

        private void PopulateStatic()
        {
            staticList.Items.Clear();
            staticList.Items.Add("Application Name").SubItems.Add(sys.Process.FileName);
            staticList.Items.Add("Application Path").SubItems.Add(sys.Process.FilePath);
            staticList.Items.Add("Application Directory").SubItems.Add(sys.Process.FileDir);
            staticList.Items.Add("Process Created").SubItems.Add(sys.Process.Created.ToString());
            staticList.Items.Add("Process ID").SubItems.Add(sys.Process.ProcessID.ToString());
            staticList.Items.Add("Process is 64-bit").SubItems.Add(sys.Process.Is64Bit.ToString());
            staticList.Items.Add("File Version").SubItems.Add(sys.Process.Version.FileVersion.Text);
            staticList.Items.Add("Product Version").SubItems.Add(sys.Process.Version.ProductVersion.Text);
        }

        private void PopulateDynamic()
        {
            dynamicList.Items.Clear();
            dynamicList.Items.Add("Current Directory").SubItems.Add(sys.Process.CurrentDir);
            dynamicList.Items.Add("Affinity Mask").SubItems.Add(sys.Process.AffinityMask.ToString());
			dynamicList.Items.Add("Priority").SubItems.Add(sys.Process.Priority.ToString());
            dynamicList.Items.Add("Handle Count").SubItems.Add(sys.Process.HandleCount.ToString());
            dynamicList.Items.Add("GDI Object Count").SubItems.Add(sys.Process.GDICount.ToString());
            dynamicList.Items.Add("USER Object Count").SubItems.Add(sys.Process.USERCount.ToString());
            dynamicList.Items.Add("Is Debugged").SubItems.Add(sys.Process.IsDebugged.ToString());
            dynamicList.Items.Add("Shutdown Level").SubItems.Add(sys.Process.ShutdownLevel.ToString());
            dynamicList.Items.Add("Shutdown Flags").SubItems.Add(sys.Process.ShutdownFlags.ToString());
        }

        private void PopulateMemory()
        {
            memoryList.Items.Clear();
            memoryList.Items.Add("PageFaultCount").SubItems.Add(sys.Process.Memory.PageFaultCount.ToString());
            memoryList.Items.Add("PageFaultDelta").SubItems.Add(sys.Process.Memory.PageFaultDelta.ToString());
            memoryList.Items.Add("PeakWorkingSetSize").SubItems.Add(sys.Process.Memory.PeakWorkingSetSize.ToString() + " KB");
            memoryList.Items.Add("WorkingSetSize").SubItems.Add(sys.Process.Memory.WorkingSetSize.ToString() + " KB");
            memoryList.Items.Add("QuotaPeakPagedPoolUsage").SubItems.Add(sys.Process.Memory.QuotaPeakPagedPoolUsage.ToString() + " KB");
			memoryList.Items.Add("QuotaPagedPoolUsage").SubItems.Add(sys.Process.Memory.QuotaPagedPoolUsage.ToString() + " KB");
			memoryList.Items.Add("QuotaPeakNonPagedPoolUsage").SubItems.Add(sys.Process.Memory.QuotaPeakNonPagedPoolUsage.ToString() + " KB");
			memoryList.Items.Add("QuotaNonPagedPoolUsage").SubItems.Add(sys.Process.Memory.QuotaNonPagedPoolUsage.ToString() + " KB");
			memoryList.Items.Add("PagefileUsage").SubItems.Add(sys.Process.Memory.PagefileUsage.ToString() + " KB");
			memoryList.Items.Add("PeakPagefileUsage").SubItems.Add(sys.Process.Memory.PeakPagefileUsage.ToString() + " KB");
			memoryList.Items.Add("PrivateUsage").SubItems.Add(sys.Process.Memory.PrivateUsage.ToString() + " KB");
        }

		private void PopulateIOCounters()
		{
			IOCountersList.Items.Clear();
			IOCountersList.Items.Add("ReadOperationCount").SubItems.Add(sys.Process.IO.ReadOperationCount.ToString());
			IOCountersList.Items.Add("WriteOperationCount").SubItems.Add(sys.Process.IO.WriteOperationCount.ToString());
			IOCountersList.Items.Add("OtherOperationCount").SubItems.Add(sys.Process.IO.OtherOperationCount.ToString());
			IOCountersList.Items.Add("ReadTransferCount").SubItems.Add(sys.Process.IO.ReadTransferCount.ToString());
			IOCountersList.Items.Add("WriteTransferCount").SubItems.Add(sys.Process.IO.WriteTransferCount.ToString());
			IOCountersList.Items.Add("OtherTransferCount").SubItems.Add(sys.Process.IO.OtherTransferCount.ToString());
		}

		private void PopulateEnvironment()
		{
			environmentList.Items.Clear();
			foreach (PSLEnvironmentVar var in sys.Process.EnvVars)
				environmentList.Items.Add(var.Name).SubItems.Add(var.Value);
		}

		private void PopulateCommands()
		{
			commandsList.Items.Clear();
			foreach (PSLCmdParam cmd in sys.Process.Commands)
			{
				ListViewItem item = commandsList.Items.Add(cmd.Command);
				item.SubItems.Add(cmd.Name);
				item.SubItems.Add(cmd.Value);
			}
		}

        private void PopulateWindows()
        {
            windowsList.Items.Clear();
            PSLWindowsFilter Filter = sys.Process.Windows.Filter;
            Filter.Visible = (PSLFilterFlag)IsVisibleFlag.SelectedIndex;
            Filter.Enabled = (PSLFilterFlag)IsEnabledFlag.SelectedIndex;
            Filter.TopLevel = (PSLFilterFlag)IsTopLevelFlag.SelectedIndex;
			Filter.HasText = (PSLFilterFlag)HasTextFlag.SelectedIndex;
            foreach (PSLWindow wnd in sys.Process.Windows)
            {
                ListViewItem item = windowsList.Items.Add(String.Format("{0:X}", wnd.Handle));
                item.SubItems.Add(wnd.ThreadID.ToString());
                item.SubItems.Add(wnd.ClassName);
                item.SubItems.Add(wnd.Text);
                item.SubItems.Add(wnd.Enabled.ToString());
                item.SubItems.Add(wnd.IsVisible.ToString());
                item.SubItems.Add(wnd.IsChild.ToString());
                item.SubItems.Add(wnd.IsTopLevel.ToString());
                item.SubItems.Add(wnd.IsBridge.ToString());
                item.SubItems.Add(wnd.Style.ToString());
                item.SubItems.Add(wnd.StyleEx.ToString());
                item.SubItems.Add(wnd.UserData.ToString());
            }
        }

        private void PopulateThreads()
        {
            threadsList.Items.Clear();
            foreach (PSLThread t in sys.Process.Threads)
            {
                ListViewItem item = threadsList.Items.Add(t.ThreadID.ToString());
                item.SubItems.Add(t.Created.ToString());
                item.SubItems.Add(t.IsCurrent.ToString());
                item.SubItems.Add(t.Priority.ToString());
                item.SubItems.Add(t.PriorityBoost.ToString());
				item.SubItems.Add(t.AffinityMask.ToString());
            }
        }

		private void PopulateModules()
		{
			modulesList.Items.Clear();
			foreach (PSLModule m in sys.Process.Modules)
			{
				ListViewItem item = modulesList.Items.Add(m.FileName);
				item.SubItems.Add(m.FileDir);
				item.SubItems.Add(m.Version.FileVersion.Text);
				item.SubItems.Add(m.Version.ProductVersion.Text);
			}
		}

		private void PopulatePrivileges()
		{
			privilegesList.Items.Clear();
			foreach (PSLPrivilege p in sys.Security.Privileges)
			{
				ListViewItem item = privilegesList.Items.Add(p.LUID.ToString());
				item.SubItems.Add(p.Name);
				item.SubItems.Add(p.Enabled.ToString());
				item.SubItems.Add(p.Default.ToString());
				item.SubItems.Add(p.Removed.ToString());
				item.SubItems.Add(p.UsedForAccess.ToString());		
			}
		}

        private void btnUpdateWindows_Click(object sender, EventArgs e)
        {
            sys.Process.Windows.Update();
            PopulateWindows();
        }

        private void btnUpdateThreads_Click(object sender, EventArgs e)
        {
            sys.Process.Threads.Update();
            PopulateThreads();
        }

        private void IsVisibleFlag_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateWindows();
        }

        private void IsEnabledFlag_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateWindows();
        }

        private void IsTopLevelFlag_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateWindows();
        }

		private void btnUpdateCnt_Click(object sender, EventArgs e)
		{
			PopulateIOCounters();
		}

		private void btnUpdateDyn_Click(object sender, EventArgs e)
		{
			PopulateDynamic();
		}

		private void btnUpdateMem_Click(object sender, EventArgs e)
		{
			PopulateMemory();
		}

		private void btnUpdateEnv_Click(object sender, EventArgs e)
		{
			sys.Process.EnvVars.Update();
			PopulateEnvironment();
		}

		private void btnUpdatePriv_Click(object sender, EventArgs e)
		{
			sys.Security.Privileges.Update();
			PopulatePrivileges();
		}

		private void btnUpdateMod_Click(object sender, EventArgs e)
		{
			sys.Process.Modules.Update();
			PopulateModules();
		}

		private void HasTextFlag_SelectedIndexChanged(object sender, EventArgs e)
		{
			PopulateWindows();
		}
    }
}

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