Src/Pscx.Core/Interop/RunningObjectTable/RunningObjectTableHelper.cs
/// <summary>
/// Returns a COM object from the ROT, given a prog or class Id.
/// </summary>
/// <param name="progOrClassId">The prog or class id of the object to return.</param>
/// <returns>The requested object, or null if the object is not found.</returns>
Returns a COM object from the ROT, given a prog or class Id.
Returns a COM object from the ROT given a prog or class Id .
public static object GetActiveObject(string progOrClassId)
        {
            if (String.IsNullOrEmpty(progOrClassId)) {
                throw new ArgumentException("progOrClassId is null or empty.");
            }
            string classId = progOrClassId;
            if ((progOrClassId.IndexOf("{") == -1) && (!progOrClassId.StartsWith("clsid:"))) {
                classId = ConvertProgIdToClassId(progOrClassId);
                if (classId == null) {
                    throw new ArgumentException(
                        String.Format("Unable to translate '{0}' to a class Id.", progOrClassId), "progOrClassId");
                }
            }
            else {
            }
            IRunningObjectTable prot = null;
            IEnumMoniker pMonkEnum = null;
            try
            {
                NativeMethods.GetRunningObjectTable(0, out prot);
                prot.EnumRunning(out pMonkEnum);
                pMonkEnum.Reset();
                IMoniker[] pmon = new IMoniker[1];
                IntPtr fetched = IntPtr.Zero;
                while (pMonkEnum.Next(1, pmon, fetched) == 0)
                {
                    IBindCtx pCtx = null;
                    string displayName;
                    try
                    {
                        NativeMethods.CreateBindCtx(0, out pCtx);
                        pmon[0].GetDisplayName(pCtx, null, out displayName);
                    }
                    finally
                    {
                        if (pCtx != null)
                        {
                            Marshal.ReleaseComObject(pCtx);
                        }
                    }
                    if (displayName != null)
                    {
                        if (displayName.IndexOf(classId) != -1)
                        {
                            object objReturnObject;
                            prot.GetObject(pmon[0], out objReturnObject);
                            return objReturnObject;
                        }
                    }
                }
                return null;
            }
            finally
            {
                if (prot != null)
                {
                    Marshal.ReleaseComObject(prot);
                }
                if (pMonkEnum != null)
                {
                    Marshal.ReleaseComObject(pMonkEnum);
                }
            }
        }
public static object GetActiveObject ( string progOrClassId ) { if ( String . IsNullOrEmpty ( progOrClassId ) ) { throw new ArgumentException ( " " ) ; } string classId = progOrClassId ; if ( ( progOrClassId . IndexOf ( " " ) == - 1 ) && ( ! progOrClassId . StartsWith ( " " ) ) ) { classId = ConvertProgIdToClassId ( progOrClassId ) ; if ( classId == null ) { throw new ArgumentException ( String . Format ( " " , progOrClassId ) , " " ) ; } } else { } IRunningObjectTable prot = null ; IEnumMoniker pMonkEnum = null ; try { NativeMethods . GetRunningObjectTable ( 0 , out prot ) ; prot . EnumRunning ( out pMonkEnum ) ; pMonkEnum . Reset ( ) ; IMoniker [ ] pmon = new IMoniker [ 1 ] ; IntPtr fetched = IntPtr . Zero ; while ( pMonkEnum . Next ( 1 , pmon , fetched ) == 0 ) { IBindCtx pCtx = null ; string displayName ; try { NativeMethods . CreateBindCtx ( 0 , out pCtx ) ; pmon [ 0 ] . GetDisplayName ( pCtx , null , out displayName ) ; } finally { if ( pCtx != null ) { Marshal . ReleaseComObject ( pCtx ) ; } } if ( displayName != null ) { if ( displayName . IndexOf ( classId ) != - 1 ) { object objReturnObject ; prot . GetObject ( pmon [ 0 ] , out objReturnObject ) ; return objReturnObject ; } } } return null ; } finally { if ( prot != null ) { Marshal . ReleaseComObject ( prot ) ; } if ( pMonkEnum != null ) { Marshal . ReleaseComObject ( pMonkEnum ) ; } } }
Returns a COM object from the ROT, given a prog or class Id.
Returns a COM object from the ROT given a prog or class Id .
// Convert the prog id into a class id // Open the running objects table. // Iterate through the results // Return the matching object
The requested object, or null if the object is not found.
The requested object or null if the object is not found .
progOrClassId
string
The prog or class id of the object to return.
The prog or class id of the object to return .