コード例 #1
0
        /// <summary>
        /// Returns all running com proxies + add. information from the running object table there matched with the input parameters
        /// WARNING: the method returns always the first com proxy from the running object table if multiple (match) proxies exists.
        /// </summary>
        /// <returns>IDisposableEnumeration with proxy information</returns>
        public static IDisposableSequence <ProxyInformation> GetActiveProxyInformations()
        {
            IEnumMoniker        monikerList             = null;
            IRunningObjectTable runningObjectTable      = null;
            RunningObjectTableItemCollection resultList = new RunningObjectTableItemCollection();

            try
            {
                // query table and returns null if no objects running
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null)
                {
                    return(null);
                }

                // query moniker & reset
                runningObjectTable.EnumRunning(out monikerList);
                monikerList.Reset();

                IMoniker[] monikerContainer       = new IMoniker[1];
                IntPtr     pointerFetchedMonikers = IntPtr.Zero;

                // fetch all moniker
                while (monikerList.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
                {
                    // query com proxy info
                    object comInstance = null;
                    runningObjectTable.GetObject(monikerContainer[0], out comInstance);
                    if (null == comInstance)
                    {
                        continue;
                    }

                    string name      = TypeDescriptor.GetClassName(comInstance);
                    string component = TypeDescriptor.GetComponentName(comInstance, false);

                    IBindCtx bindInfo    = null;
                    string   displayName = String.Empty;
                    Guid     classID     = Guid.Empty;
                    if (CreateBindCtx(0, out bindInfo) == 0)
                    {
                        monikerContainer[0].GetDisplayName(bindInfo, null, out displayName);
                        monikerContainer[0].GetClassID(out classID);
                        TryMarshalReleaseComObject(bindInfo);
                    }

                    string itemClassName     = TypeDescriptor.GetClassName(comInstance);
                    string itemComponentName = TypeDescriptor.GetComponentName(comInstance);

                    COMTypes.ITypeInfo typeInfo    = null;
                    string             itemLibrary = String.Empty;
                    if (classID != Guid.Empty)
                    {
                        typeInfo    = TryCreateTypeInfo(comInstance);
                        itemLibrary = null != typeInfo?GetParentLibraryGuid(typeInfo).ToString() : String.Empty;
                    }

                    string itemID = classID != Guid.Empty ? classID.ToString() : String.Empty;

                    ProxyInformation entry =
                        new ProxyInformation(comInstance, displayName, itemID, itemClassName,
                                             itemComponentName, itemLibrary, IntPtr.Zero, ProxyInformation.ProcessElevation.Unknown);

                    resultList.Add(entry);
                    if (classID != Guid.Empty && typeInfo != null)
                    {
                        ReleaseTypeInfo(typeInfo);
                    }
                }

                return(resultList);
            }
            catch (Exception exception)
            {
                DebugConsole.Default.WriteException(exception);
                throw;
            }
            finally
            {
                // release proxies
                if (runningObjectTable != null)
                {
                    TryMarshalReleaseComObject(runningObjectTable);
                }
                if (monikerList != null)
                {
                    TryMarshalReleaseComObject(monikerList);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns all running com proxies + add. informations from the running object table there matched with the input parameters
        /// WARNING: the method returns always the first com proxy from the running object table if multiple (match) proxies exists.
        /// </summary>
        /// <param name="componentName">name of the target component</param>
        /// <param name="className">name of the target proxy class name</param>
        /// <returns>IDisposableEnumeration with proxy informations</returns>
        public static IDisposableEnumeration <ProxyInformation> GetActiveProxyInformations(string componentName, string className)
        {
            IEnumMoniker        monikerList             = null;
            IRunningObjectTable runningObjectTable      = null;
            RunningObjectTableItemCollection resultList = new RunningObjectTableItemCollection();

            try
            {
                // query table and returns null if no objects running
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null)
                {
                    return(null);
                }

                // query moniker & reset
                runningObjectTable.EnumRunning(out monikerList);
                monikerList.Reset();

                IMoniker[] monikerContainer       = new IMoniker[1];
                IntPtr     pointerFetchedMonikers = IntPtr.Zero;

                // fetch all moniker
                while (monikerList.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
                {
                    // query com proxy info
                    object comInstance = null;
                    runningObjectTable.GetObject(monikerContainer[0], out comInstance);
                    if (null == comInstance)
                    {
                        continue;
                    }

                    // match for equal and add to list
                    string name               = TypeDescriptor.GetClassName(comInstance);
                    string component          = TypeDescriptor.GetComponentName(comInstance, false);
                    bool   componentNameEqual = String.IsNullOrWhiteSpace(componentName) ? true :
                                                (componentName.Equals(component, StringComparison.InvariantCultureIgnoreCase));
                    bool classNameEqual = String.IsNullOrWhiteSpace(className) ? true :
                                          (className.Equals(name, StringComparison.InvariantCultureIgnoreCase));
                    bool match = false;

                    if (componentNameEqual && classNameEqual)
                    {
                        match = true;
                    }
                    else
                    {
                        componentNameEqual = ((_ballmersPlace + componentName).Equals(component, StringComparison.InvariantCultureIgnoreCase));
                        if (componentNameEqual && classNameEqual)
                        {
                            match = true;
                        }
                        else
                        {
                            if (comInstance.GetType().IsCOMObject)
                            {
                                Marshal.ReleaseComObject(comInstance);
                            }
                        }
                    }

                    if (match)
                    {
                        IBindCtx bindInfo    = null;
                        string   displayName = String.Empty;
                        Guid     classID     = Guid.Empty;
                        if (CreateBindCtx(0, out bindInfo) == 0)
                        {
                            monikerContainer[0].GetDisplayName(bindInfo, null, out displayName);
                            monikerContainer[0].GetClassID(out classID);
                            Marshal.ReleaseComObject(bindInfo);
                        }

                        string itemClassName     = TypeDescriptor.GetClassName(comInstance);
                        string itemComponentName = TypeDescriptor.GetComponentName(comInstance);

                        COMTypes.ITypeInfo typeInfo    = null;
                        string             itemLibrary = String.Empty;
                        if (classID != Guid.Empty)
                        {
                            typeInfo    = TryCreateTypeInfo(comInstance);
                            itemLibrary = null != typeInfo?GetParentLibraryGuid(typeInfo).ToString() : String.Empty;
                        }

                        string itemID = classID != Guid.Empty ? classID.ToString() : String.Empty;

                        ProxyInformation entry =
                            new ProxyInformation(comInstance, displayName, itemID, itemClassName,
                                                 itemComponentName, itemLibrary, IntPtr.Zero, ProxyInformation.ProcessElevation.Unknown);

                        resultList.Add(entry);
                        if (classID != Guid.Empty && typeInfo != null)
                        {
                            ReleaseTypeInfo(typeInfo);
                        }
                    }
                }

                return(resultList);
            }
            catch (Exception exception)
            {
                DebugConsole.Default.WriteException(exception);
                throw;
            }
            finally
            {
                // release proxies
                if (runningObjectTable != null)
                {
                    Marshal.ReleaseComObject(runningObjectTable);
                }
                if (monikerList != null)
                {
                    Marshal.ReleaseComObject(monikerList);
                }
            }
        }