Com Plus Info v1.0
Following is a class I wrote more then a year ago when someone wanted to know what is the COM+ application name behind a specific process id, all he was able to get was 'dllhost.exe' and he wanted needed the specific COM+ application name.
Using the helped class you'll be able to get the PID of an Application Name or the Application Name of a specific PID plus the ability to get all COM+ applications currently running.
Today someone asked me for the same feature... i had to dig a little and i found it... But now i have decided to publish it so it wont get lost again.
If you find this class useful leave me a note as its nice to hear someone else beside me is using the code in my blog :-)
Update: To use COMSVCSLib namespace you need to add a reference to c:\windows dir\system32\comsvsc.dll
// ComPlus Info v1.0
// (c) Ohad Israeli
//
using System;
using System.Collections;
using COMSVCSLib;
using System.Runtime.InteropServices;
namespace
ComInfo{
public class ComPlusHelper
{
public ComPlusHelper()
{} public static ArrayList GetApplicationList()
{
COMSVCSLib.MtsGrp GrpObj = null;
Type dcomType = Type.GetTypeFromProgID("mts.MtsGrp");
Object dcomObj = Activator.CreateInstance( dcomType );
GrpObj = (COMSVCSLib.MtsGrp) dcomObj;
object obj = null;
COMSVCSLib.COMEvents eventObj = null;
ArrayList arr=new ArrayList(GrpObj.Count);
for (int i = 0 ;i < GrpObj.Count ; ++i)
{
GrpObj.Item (i, out obj);
eventObj = (COMSVCSLib.COMEvents) obj;
arr.Add(eventObj.GetProcessID()+" : "+eventObj.PackageName);
Marshal.ReleaseComObject(obj);
obj = null;
Marshal.ReleaseComObject(eventObj);
eventObj = null;
}
Marshal.ReleaseComObject( dcomObj );
dcomObj = null;
return arr;
}
public static int GetPidByName(string strComPlusProgName)
{
int returnVal = 0;
COMSVCSLib.MtsGrp GrpObj = null;
Type dcomType = Type.GetTypeFromProgID("mts.MtsGrp");
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;
if (eventObj.PackageName.ToUpper()==strComPlusProgName.ToUpper())
returnVal=eventObj.GetProcessID();
Marshal.ReleaseComObject(obj);
obj = null;
Marshal.ReleaseComObject(eventObj);
eventObj = null;
}
Marshal.ReleaseComObject( dcomObj );
dcomObj = null;
return returnVal;
}
public static string GetNameByPid(int intPid)
{
string returnVal = "";
COMSVCSLib.MtsGrp GrpObj = null;
Type dcomType = Type.GetTypeFromProgID("mts.MtsGrp");
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;
if (eventObj.GetProcessID()==intPid)
returnVal=eventObj.PackageName;
Marshal.ReleaseComObject(obj);
obj = null;
Marshal.ReleaseComObject(eventObj);
eventObj = null;
}
Marshal.ReleaseComObject( dcomObj );
dcomObj = null;
return returnVal;
}
}
}
// Usage:
string strComPlusId=ComPlusHelper.GetPidByName(strComPlusName).ToString();
string strComPlusName=ComPlusHelper.GetNameByPid(intComPlusId);
listbox1.Items.AddRange(ComPlusHelper.GetApplicationList().ToArray());