Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Watch to see what processes are spawned by another process

Posted on 2006-06-29
9
Medium Priority
?
419 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
topdog770 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

927 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