title: Get items from "Windows Task Manager" process list c#
body: <p>I'm trying to get all the items from "Windows Task Manager" process list and add them to a listbox in my application. I have found code to get the handle of the process list on task manger. And I have code to delete an item from the list using its index (Its a good way to test that I have the right handle). But I need c# code to get the total number of items in the process list and to get items by there indexes. Ill settle for simply getting all the items in order and adding them to my listbox.</p>
  
  <p>Edit: Thank you for your comments, I'll explain more...
  I'm not trying to just list the processes, Otherwise I would use: System.Diagnostics.Process.GetProcesses();
  I want to get the processes sorted in the way which they are sorted in task manger.
  Task manger has many ways of sorting its processes. It provides about 31 different ways to do it. For example: Image Name, PID, Username, CPU usage ect. My objective is to get the processes in the order which they are sorted in Task Manger.</p>
  
  <pre><code>    static Int32 LVM_FIRST = 4096;
      static Int32 LVM_DELETEITEM = (LVM_FIRST + 8);
      static Int32 LVM_SORTITEMS = (LVM_FIRST + 48);
  
      [DllImport("user32.dll", EntryPoint = "FindWindowA")]
      private static extern Int32 apiFindWindow(string lpClassName, string lpWindowName);
  
      [DllImport("user32.dll", EntryPoint = "FindWindowExA")]
      private static extern Int32 apiFindWindowEx(Int32 hWnd1, Int32 hWnd2, string lpsz1, string lpsz2);
  
      [DllImport("user32.dll", EntryPoint = "SendMessageA")]
      private static extern Int32 apiSendMessage(Int32 hWnd, Int32 wMsg, Int32 wParam, string lParam);
  
      [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
      private static extern Int32 apiGetDesktopWindow();
  
  
      void GetItems()
      {
          Int32 lhWndParent = apiFindWindow(null, "Windows Task Manager");
          Int32 lhWndProcessList = 0;
          Int32 lhWndDialog = 0;
  
          for (int i = 1; (i &lt;= 7); i++)
          {
              // Loop through all seven child windows, for handle to the listview
              lhWndDialog = apiFindWindowEx(lhWndParent, lhWndDialog, null, null);
              if ((lhWndProcessList == 0))
              {
                  lhWndProcessList = apiFindWindowEx(lhWndDialog, 0, "SysListView32", "Processes");
              }
          }
  
  
          /* This code removes the first item in the Task Manager Processes list:
           * apiSendMessage(lhWndProcessList, LVM_DELETEITEM, 0, "0");
           * note that the 3rd paramiter: 0, is the index of the item to delete.
           * I put it here in comments because I thought there would be
           * something similar to get the name*/
  
          listBox1.Items.Clear();
  
          int TotalItemCount = /*Total item count in Task Manager Processes list*/;
          for (int i = 0; i &lt; TotalItemCount; i++)
          {
              listBox1.Items.Add(/*Get item  in Task Manager Processes list by index: i*/)
  
          }
  
      }
  </code></pre>