Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Article

COM+ Running Process

Rate me:
Please Sign up or sign in to vote.
4.79/5 (10 votes)
28 Jul 20053 min read 97.3K   1.9K   20   15
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:

C#
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.

C#
/// 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:

C#
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:

C#
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.

C#
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


Written By
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

 
Questioncan u please give us the code for fetching list of COM + components Pin
subhodeep banerjee30-May-18 0:42
subhodeep banerjee30-May-18 0:42 
GeneralMy vote of 5 Pin
kevin.dmonte15-Apr-11 21:12
kevin.dmonte15-Apr-11 21:12 
GeneralCOm and COM+ Pin
saurabh.surana30-Mar-10 23:14
saurabh.surana30-Mar-10 23:14 
GeneralWorks fine Pin
aalday14-Oct-06 2:31
aalday14-Oct-06 2:31 
GeneralError Pin
Sachs14-Feb-06 8:54
Sachs14-Feb-06 8:54 
GeneralRe: Error Pin
M_Rizwan14-Feb-06 18:20
M_Rizwan14-Feb-06 18:20 
GeneralRe: Error Pin
simpleperson28-Feb-06 8:03
simpleperson28-Feb-06 8:03 
GeneralRe: Error Pin
M_Rizwan1-Mar-06 17:40
M_Rizwan1-Mar-06 17:40 
GeneralRe: Error Pin
simpleperson4-Mar-06 3:33
simpleperson4-Mar-06 3:33 
GeneralRe: Error Pin
Andrzej Hamkalo7-Jul-06 2:25
Andrzej Hamkalo7-Jul-06 2:25 
GeneralCongrates Pin
Babarsaeed3-Aug-05 19:49
Babarsaeed3-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 Pin
Babarsaeed3-Aug-05 19:40
Babarsaeed3-Aug-05 19:40 
GeneralNice work u did Riz Pin
yasirqau31-Jul-05 20:54
yasirqau31-Jul-05 20:54 
QuestionLinks? Pin
fwsouthern28-Jul-05 5:44
fwsouthern28-Jul-05 5:44 
AnswerRe: Links? Pin
M_Rizwan29-Jul-05 3:14
M_Rizwan29-Jul-05 3:14 

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

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