Solved

Watch to see what processes are spawned by another process

Posted on 2006-06-29
9
404 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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