예제 #1
0
        /*
         * Method:  GetTypeLibNameForITypeLib
         *
         * Gets the name of given type library.
         */
        internal static bool GetTypeLibNameForITypeLib(TaskLoggingHelper log, bool silent, ITypeLib typeLib, string typeLibId, out string typeLibName)
        {
            typeLibName = "";

            // see if the type library supports ITypeLib2
            ITypeLib2 typeLib2 = typeLib as ITypeLib2;

            if (typeLib2 == null)
            {
                // Looks like the type lib doesn't support it. Let's use the Marshal method.
                typeLibName = Marshal.GetTypeLibName(typeLib);
                return(true);
            }

            // Get the custom attribute.  If anything fails then just return the
            // type library name.
            try
            {
                object data = null;

                typeLib2.GetCustData(ref NativeMethods.GUID_TYPELIB_NAMESPACE, out data);

                // if returned namespace is null or its type is not System.String, fall back to the default
                // way of getting the type lib name (just to be safe)
                if (data == null || string.Compare(data.GetType().ToString(), "system.string", StringComparison.OrdinalIgnoreCase) != 0)
                {
                    typeLibName = Marshal.GetTypeLibName(typeLib);
                    return(true);
                }

                // Strip off the DLL extension if it's there
                typeLibName = (string)data;

                if (typeLibName.Length >= 4)
                {
                    if (string.Compare(typeLibName.Substring(typeLibName.Length - 4), ".dll", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        typeLibName = typeLibName.Substring(0, typeLibName.Length - 4);
                    }
                }
            }
            catch (COMException ex)
            {
                // If anything fails log a warning and just return the type library name.
                if (!silent)
                {
                    log.LogWarningWithCodeFromResources("ResolveComReference.CannotAccessTypeLibName", typeLibId, ex.Message);
                }
                typeLibName = Marshal.GetTypeLibName(typeLib);
                return(true);
            }

            return(true);
        }