コード例 #1
0
ファイル: ComClassInfo.cs プロジェクト: prid77/TickZoomPublic
        // This is used for the case of a class outside of a type
        // libarary
        protected Type GetTypeForClass()
        {
            Type type = null;

            // See if we have type lib information from the registry
            if (_typeLibString != null)
            {
                return(GetTypeFromTypeLib
                           (TypeLibrary.GetTypeLib(new Guid(_typeLibString),
                                                   _typeLibVersion)));
            }


            // Have to try and create the object and get the type lib
            // information from the created object

            if (TraceUtil.If(this, TraceLevel.Info))
            {
                TraceUtil.WriteLineInfo(this,
                                        "Attempting to create obj for: "
                                        + this);
            }


            IntPtr comObj;
            int    result = ActiveX.CoCreateInstance(ref _guid,
                                                     (IntPtr)0,
                                                     ActiveX.CLSCTX_SERVER,
                                                     ref ActiveX.IUnknownIID,
                                                     out comObj);

            if (result == 0)
            {
                ComObjectInfo objInfo = null;
                try
                {
                    // Wrap our object info stuff and get the type
                    objInfo = new ComObjectInfo(comObj);
                    type    = GetTypeFromTypeLib(objInfo.TypeLib);
                }
                catch (Exception ex)
                {
                    _typeFailedException =
                        new Exception("Unable to determine CLR type for "
                                      + GetName(), ex);
                    throw _typeFailedException;
                }
                finally
                {
                    try
                    {
                        // Clean up the object info, if we made it that
                        // far
                        if (objInfo != null)
                        {
                            Object o = objInfo.Obj;
                            ObjectInfo.RemoveObjectInfo(o);
                            while (true)
                            {
                                int count = Marshal.ReleaseComObject(o);
                                TraceUtil.WriteLineInfo
                                    (this,
                                    "final Marshal.ReleaseComObject count: "
                                    + count);
                                if (count <= 0)
                                {
                                    break;
                                }
                            }
                        }
                        while (true)
                        {
                            int count = Marshal.Release(comObj);
                            TraceUtil.WriteLineInfo
                                (this, "final Marshal.Release count: "
                                + count);

                            if (count <= 0)
                            {
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceUtil.WriteLineWarning
                            (this, "error on cleanup: " + ex);
                        // We tried...
                    }
                    ActiveX.CoFreeUnusedLibraries();
                }


                return(type);
            }

            _typeFailedException =
                new Exception("Unable to determine CLR type because "
                              + "I can't create a COM object (0x"
                              + result.ToString("X")
                              + ") from CLSID: " + _guidStr);
            throw _typeFailedException;
        }