Skip to content

Instantly share code, notes, and snippets.

@Grynn
Created December 14, 2011 08:47
Show Gist options
  • Save Grynn/1475780 to your computer and use it in GitHub Desktop.
Save Grynn/1475780 to your computer and use it in GitHub Desktop.
COM running object table utility class
/// <summary>
/// COM running object table utility class
/// </summary>
public static class ROTHelper
{
public static IDictionary<string, object> GetActiveObjects(string MonikerFilter)
{
var ret = new Dictionary<string, object>();
IRunningObjectTable pprot;
IEnumMoniker ppenumMoniker;
IMoniker[] monikers = new IMoniker[1];
Win32.GetRunningObjectTable(0, out pprot);
pprot.EnumRunning(out ppenumMoniker);
ppenumMoniker.Reset();
while (ppenumMoniker.Next(1, monikers, IntPtr.Zero) == 0)
{
IBindCtx ctx;
Win32.CreateBindCtx(0, out ctx);
string name;
monikers[0].GetDisplayName(ctx, null, out name);
if (string.IsNullOrWhiteSpace(MonikerFilter) || (name.IndexOf( MonikerFilter ) != -1))
{
object val;
pprot.GetObject(monikers[0], out val);
ret.Add(name, val);
}
}
return ret;
}
}
public static class Win32
{
/// <summary>
/// Returns a pointer to an implementation of IBindCtx (a bind context object).
/// This object stores information about a particular moniker-binding operation.
/// </summary>
/// <param name="reserved">This parameter is reserved and must be 0.</param>
/// <param name="ppbc">Address of an IBindCtx* pointer variable that receives
/// the interface pointer to the new bind context object. When the function is
/// successful, the caller is responsible for calling Release on the bind context.
/// A NULL value for the bind context indicates that an error occurred.</param>
/// <returns>This function can return the standard return values E_OUTOFMEMORY and S_OK.</returns>
[DllImport("ole32.dll")]
public static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);
/// <summary>
/// Returns a pointer to the IRunningObjectTable
/// interface on the local running object table (ROT).
/// </summary>
/// <param name="reserved">This parameter is reserved and must be 0.</param>
/// <param name="prot">The address of an IRunningObjectTable* pointer variable
/// that receives the interface pointer to the local ROT. When the function is
/// successful, the caller is responsible for calling Release on the interface
/// pointer. If an error occurs, *pprot is undefined.</param>
/// <returns>This function can return the standard return values E_UNEXPECTED and S_OK.</returns>
[DllImport("ole32.dll")]
public static extern int GetRunningObjectTable(uint reserved,
out System.Runtime.InteropServices.ComTypes.IRunningObjectTable pprot);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment