Solved

Watch to see what processes are spawned by another process

Posted on 2006-06-29
9
406 Views
Last Modified: 2010-05-18
Hello,

I have a requirement to track what programs are opened by another process.  The process is third party, so I cannot change it.  I am looking to attach some sort of tracer to the first process so that I can see if it spawns other processes and add them to a list.  Consider the following:

Program Z runs a launcher (Process A)
Process A launches Process B
Process A launches Process C
Program Z must maintain a list of the handles of Process B, and Process C, and must know when they are closed.

The business requirement for this project is that our program, Program Z, invokes a launcher (Process A) for other programs.  We need to know when the other programs are closed, and so we are trying to track the handles for the additional processes which are spawned by Process A.

Is it possible to figure out what processes are spawned from a 3rd party process, and to find out when the new processes are closed?

Thanks!
0
Comment
Question by:TLevin10
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:topdog770
ID: 17010293
Are you using VS2003 or VS2005?
0
 

Author Comment

by:TLevin10
ID: 17010383
VS2003
0
 

Author Comment

by:TLevin10
ID: 17010515
I am thinking that maybe what I am looking for is similar to "spying" on Process A, like I would do with Spy++?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Accepted Solution

by:
topdog770 earned 500 total points
ID: 17010582
Here's a very basic code snippet that I had laying around... was trying to create a bit nicer example, but ran out of time, sorry for delay.

The performance counter must be enabled on the computer for the GetProcesses function to work.

Here's the key..            
============================================
      Process[] p = System.Diagnostics.Process.GetProcesses();
      foreach (Process process in p)
               {
                     // do something with each process
               }
============================================



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;  
using System.Diagnostics;


namespace ProcessView
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.ListView ProcessListView;
            private System.Windows.Forms.Label ProcessCountLabel;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.ProcessListView = new System.Windows.Forms.ListView();
                  this.ProcessCountLabel = new System.Windows.Forms.Label();
                  this.SuspendLayout();
                  //
                  // ProcessListView
                  //
                  this.ProcessListView.Location = new System.Drawing.Point(8, 56);
                  this.ProcessListView.Name = "ProcessListView";
                  this.ProcessListView.Size = new System.Drawing.Size(272, 208);
                  this.ProcessListView.TabIndex = 0;
                  //
                  // ProcessCountLabel
                  //
                  this.ProcessCountLabel.Location = new System.Drawing.Point(176, 16);
                  this.ProcessCountLabel.Name = "ProcessCountLabel";
                  this.ProcessCountLabel.TabIndex = 1;
                  this.ProcessCountLabel.Text = "label1";
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Controls.Add(this.ProcessCountLabel);
                  this.Controls.Add(this.ProcessListView);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void ShowProcessInfo()
            {
                  Process[] p = System.Diagnostics.Process.GetProcesses();
                  ProcessListView.Items.Clear();
                  foreach (Process process in p)
                  {
                        ListViewItem lvi = new ListViewItem(
                              process.ProcessName);
                        lvi.SubItems.Add(process.Id.ToString());
                        try
                        {
                              if (process.MainModule != null)
                              {
                                    lvi.SubItems.Add(
                                          process.MainModule.ModuleName);
                              }
                              else lvi.SubItems.Add("-");
                        }
                        catch (Exception ex)
                        {
                              // for example, access denied
                              lvi.SubItems.Add(ex.Message);
                        }
                        lvi.SubItems.Add(
                              (process.PagedMemorySize/1024)+" KB");
                        ProcessListView.Items.Add(lvi);
                  }
                  ProcessCountLabel.Text =      p.Length.ToString();
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  ShowProcessInfo();
            }
      }
}
0
 

Author Comment

by:TLevin10
ID: 17010868
Very interesting - so I can loop through all the processes and find the one I'm looking for.  However, I have run into a small problem...

The following code should illustrate:

public void WatchProcesses()
{
      Process[] procCollection = Process.GetProcessesByName("spawnedProcess");

      foreach (Process p in procCollection)
            p.Exited +=new EventHandler(p_Exited);
}

private void p_Exited(object sender, EventArgs e)
{
      this.OnExit();
}

When I launch the "launcher" process (Process A) I attach to its exit event, which runs "WatchProcesses()".  Since the launcher just launches the new Processes and exits, this is the correct time to find the processes it launched.  Since I know the name of the launched processes, I can bind to their exit events (as in the same code).  However, when I exit the launched processes, no event is fired.  Why is this?

P.S. - The Processes (B and C) which are launched by Process A are not managed processes.  Can I still bind to their exit events? How would I do this for VB applications (thats what processes B and C are)?

0
 

Author Comment

by:TLevin10
ID: 17010872
Note on the P.S - I mean VB6, not VB.NET...
0
 

Author Comment

by:TLevin10
ID: 17010903
NEVERMIND! I figured it out - the VB processes don't normally have "enableRaisingEvents" set to true, so I had to set this first.

Now its working - thanks!
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17010909
change this code to

 foreach (Process p in procCollection)
{
          p.EnableRaisingEvents;  
          p.Exited +=new EventHandler(p_Exited);
}
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17010917
Np, glad it's working!

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET (2008) - Refactoring Question 2 24
Where did System.Data.Objects go? 2 40
Hey!!!! 1 29
jquery + C#.net radiobuttonlist 3 30
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question