Click here to Skip to main content
Click here to Skip to main content

COM+ Running Process

, 28 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Monitoring COM+ running applications.

Running COM+ Process (Application snapshot)

Introduction

Following is an application for those who want to know, what is the COM+ application name behind a specific process ID (i.e. dllhost.exe process) and for those who want to monitor memory used by these COM+ components.

Background

There are two ways that COM+ can host .NET components: Library applications and Server applications. A library application runs the component in the caller process while a server application runs the component in a dedicate process that COM+ creates, called Dllhost.exe. Server application runs in a separate process affecting the overall performance of the application that needs to cross the process boundary to call component services.

Many organizations and individuals want to monitor their COM+ applications, like what is the COM+ application name behind a process ID and how much memory is being used by a COM+ component. This inspired me to make a simple application that possess the above features mentioned.

Using the code

The first step is to add reference to the following namespaces:

using System;
using System.Drawing;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using COMSVCSLib; // Path = C:\Windows\System32\comsvcs.dll
using System.Runtime.InteropServices;

Whenever you press "Get Application(s)" button, inside the btnGetApplications_Click(object sender, System.EventArgs e) function, the GetCOMPlusApplicationsList(); function is called. This is the main function that gets the list of all running COM+ applications and also gets the associated process ID.

/// This function retrieves all the running COM+ commponent services 
/// and also retrieves their application ID.
public  GetCOMPlusApplicationsList()
{
    // MtsGrp interface provides methods
    // for enumeration through running packages.
    COMSVCSLib.MtsGrp GrpObj = null;
    
    Type dcomType = Type.GetTypeFromProgID("mts.MtsGrp");
    
    // Creating new instance of MtsGrp component.
    Object dcomObj = Activator.CreateInstance( dcomType );

    GrpObj = (COMSVCSLib.MtsGrp) dcomObj;
    object obj = null;
    
    COMSVCSLib.COMEvents eventObj = null;
    
    for (int i = 0 ;i < GrpObj.Count ; ++i)
    {
        GrpObj.Item (i, out obj);
        eventObj = (COMSVCSLib.COMEvents) obj;
        
        // Add information to list view.
        AddtoListView(eventObj.GetProcessID(),eventObj.PackageName);
        
        // Release the resources held by object.
        Marshal.ReleaseComObject(obj); 
        obj = null;
        Marshal.ReleaseComObject(eventObj); 
        eventObj = null;
    }
    Marshal.ReleaseComObject( dcomObj ); 
    dcomObj = null;
    return;
}

In GetCOMPlusApplicationsList() function, first of all, it creates a reference object of interface COMSVCSLib.MtsGrp which is used to enumerate through the running packages. Later you need to get the type associated with the specified program identifier (progID). In this case, "mts.MtsGrp" progID is specified to get the type associated with this component. The next step is to create an instance of this type object by calling the .NET class function Activator.CreateInstance( dcomType ).

Now you get the MtsGrp object. You can get each instance by using the GrpObj.Item (i, out obj) function, and type cast it to COMEvents object like this:

eventObj = (COMSVCSLib.COMEvents) obj;

Next step is calling the function eventObj.GetProcessID() and eventObj.PackageName function and properties respectively to get the process ID and application name of the running COM+ application. After that AddtoListView function is called which adds the process ID and application name to the ListView. After that you need to call the Marshal.ReleaseComObject(obj) function defined in the System.Runtime.InteropServices.Marshal to release the memory resources held by this object. See what is happening in AddtoListView function:

private void AddtoListView(int pid, string appName)
{
    listItem = this.comlistView.Items.Add(pid.ToString());
    listItem.SubItems.Add(appName);
}

When you get the COM+ applications, then you can get the memory used by these COM+ applications. For this, you can select the COM+ application you want to know about and after that press "Get Memory" button which in response shows you the memory usage in terms of bytes. Now let's see what happens in the Click event of the "Get Memory" button.

private void getMemoryButton_Click(object sender, System.EventArgs e)
{
    // Getting process id from pid text box.
    string pid = this.pidTextBox.Text ;
    if(pid =="")
    {
        MessageBox.Show("Please enter a process Id!", "Process ID");
        return;
    }

    // Get all the process of the system.
    Process []getProcess = Process.GetProcesses();

    // Iterate through these process to find the specific process.
    foreach(Process pro in getProcess)
    {
        if(pro.Id.ToString() == pid)
        {
            // getting physical memory used by this process.
            this.phyMemTextBox.Text = pro.WorkingSet.ToString() + " bytes"; 

            // getting virtual memory used by this process.
            this.virTextBox.Text = pro.VirtualMemorySize.ToString() + " bytes";
            return;
        }
    }

    MessageBox.Show("Invalid process Id","Invalid ID");
}

Points of Interest

This was the first assignment given to me in my software house Ultimus. First I tried hard to get the running COM+ packages, but later when I explored a lot, this solution stuck into my mind and I finally came with this idea. Before that I never got familiar with COM+ components. But this application could be very useful for those organizations which usually use COM+ packages.

History

  • 28 July 2005: Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

M_Rizwan
Web Developer
Pakistan Pakistan
Muhammad Rizwan is a Software engineer. He did his M.Sc. in Computer Science from Quaid-i-Azam university Islamabad, Pakistan.
He has almost One year experience working in .NET technologies. He feels free to work in Visual C#.NET, Visual C++ and in Java as well. You can reach him at nice_rizwan4u@hotmail.com.
 
Thanks,
Rizwan

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberkevin.dmonte15-Apr-11 21:12 
GeneralCOm and COM+ Pinmembersaurabh.surana30-Mar-10 23:14 
GeneralWorks fine Pinmemberaalday14-Oct-06 2:31 
GeneralError PinmemberSachs14-Feb-06 8:54 
GeneralRe: Error PinmemberM_Rizwan14-Feb-06 18:20 
GeneralRe: Error Pinmembersimpleperson28-Feb-06 8:03 
GeneralRe: Error PinmemberM_Rizwan1-Mar-06 17:40 
GeneralRe: Error Pinmembersimpleperson4-Mar-06 3:33 
GeneralRe: Error PinmemberAndrzej Hamkalo7-Jul-06 2:25 
GeneralCongrates PinmemberBabarsaeed3-Aug-05 19:49 
hmmmmmmmmmm..........
 
have red your article , and i find it best nd nice,
 
keep the moral high..
 

wish u all da best!
 
Allah hafiz
 
Babar
GeneralCongrates PinmemberBabarsaeed3-Aug-05 19:40 
GeneralNice work u did Riz Pinmemberyasirqau31-Jul-05 20:54 
QuestionLinks? Pinmemberfwsouthern28-Jul-05 5:44 
AnswerRe: Links? PinmemberM_Rizwan29-Jul-05 3:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 28 Jul 2005
Article Copyright 2005 by M_Rizwan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid