internal static bool GetTypeLibNameForITypeLib(TaskLoggingHelper log, ITypeLib typeLib, string typeLibId, out string typeLibName) { typeLibName = ""; ITypeLib2 lib = typeLib as ITypeLib2; if (lib == null) { typeLibName = Marshal.GetTypeLibName(typeLib); return(true); } try { object pVarVal = null; lib.GetCustData(ref Microsoft.Build.Tasks.NativeMethods.GUID_TYPELIB_NAMESPACE, out pVarVal); if ((pVarVal == null) || (string.Compare(pVarVal.GetType().ToString(), "system.string", StringComparison.OrdinalIgnoreCase) != 0)) { typeLibName = Marshal.GetTypeLibName(typeLib); return(true); } typeLibName = (string)pVarVal; if ((typeLibName.Length >= 4) && (string.Compare(typeLibName.Substring(typeLibName.Length - 4), ".dll", StringComparison.OrdinalIgnoreCase) == 0)) { typeLibName = typeLibName.Substring(0, typeLibName.Length - 4); } } catch (COMException exception) { log.LogWarningWithCodeFromResources("ResolveComReference.CannotAccessTypeLibName", new object[] { typeLibId, exception.Message }); typeLibName = Marshal.GetTypeLibName(typeLib); return(true); } return(true); }
private bool CanSkipType(ITypeInfo typeInfo, ITypeLib typeLib, System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttributes, System.Runtime.InteropServices.ComTypes.TYPELIBATTR typeLibAttributes) { if (((typeAttributes.guid == Microsoft.Build.Tasks.NativeMethods.IID_IUnknown) || (typeAttributes.guid == Microsoft.Build.Tasks.NativeMethods.IID_IDispatch)) || (((typeAttributes.guid == Microsoft.Build.Tasks.NativeMethods.IID_IDispatchEx) || (typeAttributes.guid == Microsoft.Build.Tasks.NativeMethods.IID_IEnumVariant)) || (typeAttributes.guid == Microsoft.Build.Tasks.NativeMethods.IID_ITypeInfo))) { return(true); } if (typeLibAttributes.guid == Microsoft.Build.Tasks.NativeMethods.IID_StdOle) { string str; string str2; string str3; int num; typeInfo.GetDocumentation(-1, out str, out str2, out num, out str3); if (string.CompareOrdinal(str, "GUID") == 0) { return(true); } } ITypeLib2 lib = typeLib as ITypeLib2; if (lib != null) { object obj2; lib.GetCustData(ref Microsoft.Build.Tasks.NativeMethods.GUID_ExportedFromComPlus, out obj2); string str4 = obj2 as string; if (!string.IsNullOrEmpty(str4)) { return(true); } } return(false); }
/* * 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); }
/// <summary> /// Returns true if we don't need to analyze this particular type. /// </summary> /// <param name="typeInfo"></param> /// <param name="typeLib"></param> /// <param name="typeAttributes"></param> /// <param name="typeLibAttributes"></param> /// <returns></returns> private bool CanSkipType(ITypeInfo typeInfo, ITypeLib typeLib, TYPEATTR typeAttributes, TYPELIBATTR typeLibAttributes) { // Well known OLE type? if ((typeAttributes.guid == NativeMethods.IID_IUnknown) || (typeAttributes.guid == NativeMethods.IID_IDispatch) || (typeAttributes.guid == NativeMethods.IID_IDispatchEx) || (typeAttributes.guid == NativeMethods.IID_IEnumVariant) || (typeAttributes.guid == NativeMethods.IID_ITypeInfo)) { return(true); } // Is this the Guid type? If so we should be using the corresponding .NET type. if (typeLibAttributes.guid == NativeMethods.IID_StdOle) { string typeName, ignoredDocString, ignoredHelpFile; int ignoredHelpContext; typeInfo.GetDocumentation(-1, out typeName, out ignoredDocString, out ignoredHelpContext, out ignoredHelpFile); if (string.CompareOrdinal(typeName, "GUID") == 0) { return(true); } } // Skip types exported from .NET assemblies ITypeLib2 typeLib2 = typeLib as ITypeLib2; if (typeLib2 != null) { object exportedFromComPlusObj; typeLib2.GetCustData(ref NativeMethods.GUID_ExportedFromComPlus, out exportedFromComPlusObj); string exportedFromComPlus = exportedFromComPlusObj as string; if (!string.IsNullOrEmpty(exportedFromComPlus)) { return(true); } } return(false); }
private Assembly ResolveAssemblyFromTypeLibID(Guid iid, Guid typeLibraryID, string version, bool noAssemblyGeneration) { Assembly assembly; ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, 0x5000d, "TraceCodeComIntegrationTLBImportStarting", iid, typeLibraryID); bool flag = false; ITypeLib2 typeLibrary = null; try { lock (this.assemblyTableLock) { this.assemblyTable.TryGetValue(typeLibraryID, out assembly); if (assembly == null) { typeLibrary = this.GettypeLibrary(typeLibraryID, version); object pVarVal = null; typeLibrary.GetCustData(ref clrAssemblyCustomID, out pVarVal); if (pVarVal == null) { flag = true; } string str = pVarVal as string; if (string.IsNullOrEmpty(str)) { flag = true; } if (noAssemblyGeneration && flag) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("NativeTypeLibraryNotAllowed", new object[] { typeLibraryID }))); } if (!flag) { ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, 0x5000e, "TraceCodeComIntegrationTLBImportFromAssembly", iid, typeLibraryID, str); assembly = Assembly.Load(str); } else { ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, 0x5000f, "TraceCodeComIntegrationTLBImportFromTypelib", iid, typeLibraryID); assembly = TypeLibraryHelper.GenerateAssemblyFromNativeTypeLibrary(iid, typeLibraryID, typeLibrary); } this.assemblyTable[typeLibraryID] = assembly; } } } catch (Exception exception) { DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId)(-1073610728), new string[] { iid.ToString(), typeLibraryID.ToString(), exception.ToString() }); throw; } finally { if (typeLibrary != null) { Marshal.ReleaseComObject(typeLibrary); } } if (null == assembly) { throw Fx.AssertAndThrow("Assembly should not be null"); } ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, 0x50011, "TraceCodeComIntegrationTLBImportFinished", iid, typeLibraryID); return(assembly); }
private Assembly ResolveAssemblyFromTypeLibID(Guid iid, Guid typeLibraryID, string version, bool parseVersionAsHex, bool noAssemblyGeneration) { ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTLBImportStarting, SR.TraceCodeComIntegrationTLBImportStarting, iid, typeLibraryID); Assembly asm; bool generateNativeAssembly = false; ITypeLib2 typeLibrary = null; try { lock (assemblyTableLock) { assemblyTable.TryGetValue(typeLibraryID, out asm); if (asm == null) { typeLibrary = GettypeLibrary(typeLibraryID, version, parseVersionAsHex); object opaqueData = null; typeLibrary.GetCustData(ref clrAssemblyCustomID, out opaqueData); if (opaqueData == null) { generateNativeAssembly = true; // No custom data for this IID this is not a CLR typeLibrary } String assembly = opaqueData as String; if (String.IsNullOrEmpty(assembly)) { generateNativeAssembly = true; // No custom data for this IID this is not a CLR typeLibrary } if (noAssemblyGeneration && generateNativeAssembly) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.NativeTypeLibraryNotAllowed, typeLibraryID))); } else if (!generateNativeAssembly) { ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTLBImportFromAssembly, SR.TraceCodeComIntegrationTLBImportFromAssembly, iid, typeLibraryID, assembly); asm = Assembly.Load(assembly); // Assembly.Load will get a full assembly name } else { ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTLBImportFromTypelib, SR.TraceCodeComIntegrationTLBImportFromTypelib, iid, typeLibraryID); asm = TypeLibraryHelper.GenerateAssemblyFromNativeTypeLibrary(iid, typeLibraryID, typeLibrary as ITypeLib); } assemblyTable[typeLibraryID] = asm; } } } catch (Exception e) { DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus, (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusTLBImportError, iid.ToString(), typeLibraryID.ToString(), e.ToString()); throw; } finally { // Add Try Finally to cleanup typeLibrary if (typeLibrary != null) { Marshal.ReleaseComObject((object)typeLibrary); } } if (null == asm) { throw Fx.AssertAndThrow("Assembly should not be null"); } ComPlusTLBImportTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationTLBImportFinished, SR.TraceCodeComIntegrationTLBImportFinished, iid, typeLibraryID); return(asm); }