static void ExtractNames(string typelib) { UCOMITypeLib tl = null; OleAut.LoadTypeLibEx(typelib, OleAut.REGKIND_NONE, out tl); // Get the names from ITypeLib string name; string docstring; Int32 ctxt; string helpfile; tl.GetDocumentation(-1, out name, out docstring, out ctxt, out helpfile); IntPtr pa; tl.GetLibAttr(out pa); TYPELIBATTR ta = (TYPELIBATTR)Marshal.PtrToStructure(pa, typeof(TYPELIBATTR)); DealWithName(name, ta.guid); // Enumerate TypeInfos int nTypeInfos = tl.GetTypeInfoCount(); for (int i = 0; i < nTypeInfos; i++) { UCOMITypeInfo it = null; tl.GetTypeInfo(i, out it); DealWithTypeInfo(it); } }
//************************************************************************** // Helper to get a PIA from a typelib. //************************************************************************** internal static bool GetPrimaryInteropAssembly(Object TypeLib, out String asmName, out String asmCodeBase) { IntPtr pAttr = (IntPtr)0; TYPELIBATTR Attr; UCOMITypeLib pTLB = (UCOMITypeLib)TypeLib; int Major = 0; int Minor = 0; Guid TlbId; int lcid = 0; // Retrieve the major and minor version from the typelib. try { pTLB.GetLibAttr(out pAttr); Attr = (TYPELIBATTR)Marshal.PtrToStructure(pAttr, typeof(TYPELIBATTR)); Major = Attr.wMajorVerNum; Minor = Attr.wMinorVerNum; TlbId = Attr.guid; lcid = Attr.lcid; } finally { // Release the typelib attributes. if (pAttr != (IntPtr)0) { pTLB.ReleaseTLibAttr(pAttr); } } // Ask the converter for a PIA for this typelib. return(s_TypeLibConverter.GetPrimaryInteropAssembly(TlbId, Major, Minor, lcid, out asmName, out asmCodeBase)); }
internal Assembly FindRCW(UCOMITypeLib typeLib) { if (rcwCache == null) { return(null); } IntPtr typeLibAttr = NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out typeLibAttr); try { if (typeLibAttr != NativeMethods.InvalidIntPtr) { // Marshal the returned int as a TLibAttr structure // TYPELIBATTR tlbAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttr, typeof(TYPELIBATTR)); return((Assembly)rcwCache[tlbAttr.guid]); } } finally { typeLib.ReleaseTLibAttr(typeLibAttr); } return(null); }
string AxImporter.IReferenceResolver.ResolveComReference(UCOMITypeLib typeLib) { string rcwName = Marshal.GetTypeLibName(typeLib); // It's OK if this returns null, meaning no match. return(rcwReferences[rcwName]); }
public Object ResolveRef(Assembly asm) { UCOMITypeLib rslt = null; // If the typelib name is null then create it from the assembly name. String FullyQualifiedTypeLibName = Path.Combine(TlbExpCode.s_Options.m_strOutputDir, asm.GetName().Name) + ".tlb"; if (TlbExpCode.s_Options.m_bVerboseMode) { Console.WriteLine(Resource.FormatString("Msg_AutoExportingAssembly", asm.GetName().Name, FullyQualifiedTypeLibName)); } // Make sure the current typelib will not be overriten by the // typelib generated by the assembly being exported. if (String.Compare(FullyQualifiedTypeLibName, TlbExpCode.s_Options.m_strTypeLibName, true, CultureInfo.InvariantCulture) == 0) { String str = Resource.FormatString("Err_RefTlbOverwrittenByOutput", asm.GetName().Name, FullyQualifiedTypeLibName); TlbExpCode.WriteErrorMsg(str); throw new ApplicationException(str); } // Export the typelib for the module. rslt = TlbExpCode.DoExport(asm, FullyQualifiedTypeLibName); return(rslt); }
private static void RegisterMainTypeLib(Assembly asm) { UCOMITypeLib Tlb = null; // If the assembly contains an embedded type library, then register it. if (s_Options.m_strAssemblyName == s_Options.m_strTypeLibName) { try { LoadTypeLibEx(s_Options.m_strTypeLibName, REGKIND.REGKIND_REGISTER, out Tlb); // Display the success message unless silent mode is enabled. if (Tlb != null && !s_Options.m_bSilentMode) { Console.WriteLine(Resource.FormatString("Msg_EmbeddedTypelibReg", s_Options.m_strTypeLibName)); } } catch (Exception) { } } if (Tlb == null) { // Export the assembly to a typelib. Tlb = DoExportAndRegister(asm, s_Options.m_strTypeLibName); // Display the success message unless silent mode is enabled. if (!s_Options.m_bSilentMode) { Console.WriteLine(Resource.FormatString("Msg_AssemblyExportedAndReg", s_Options.m_strTypeLibName)); } } }
internal static UCOMITypeLib DoExportAndRegister(Assembly asm, String strTypeLibName) { // Create the TypeLibConverter. ITypeLibConverter TLBConv = new TypeLibConverter(); // Convert the assembly. ExporterCallback callback = new ExporterCallback(); UCOMITypeLib Tlb = (UCOMITypeLib)TLBConv.ConvertAssemblyToTypeLib(asm, strTypeLibName, s_Options.m_Flags, callback); // Persist the typelib. try { UCOMICreateITypeLib CreateTlb = (UCOMICreateITypeLib)Tlb; CreateTlb.SaveAllChanges(); } catch (Exception e) { ThrowAppException(Resource.FormatString("Err_TypelibSaveFailed"), e); } // Register the typelib. try { RegisterTypeLib(Tlb, strTypeLibName, Path.GetDirectoryName(strTypeLibName)); } catch (Exception e) { ThrowAppException(Resource.FormatString("Err_TypelibRegisterFailed"), e); } return(Tlb); }
static void Main(string[] args) { // Get the path to the assembly. Console.WriteLine("Please enter the path to the .NET binary"); Console.WriteLine(@"Example: C:\MyStuff\Blah\myDotNetServer.dll"); Console.Write("Path: "); string pathToAssembly = Console.ReadLine(); // Generate type lib for this assembly. UCOMITypeLib i = GenerateTLBFromAsm(pathToAssembly); // Ask if user wants to register this server with COM. int regValue; Console.WriteLine("Would you like to register this .NET library with COM?"); Console.Write("1 = yes or 0 = no "); regValue = Console.Read(); if (regValue == 1) { RegistrationServices rs = new RegistrationServices(); Assembly asm = Assembly.LoadFrom(pathToAssembly); rs.RegisterAssembly(asm, AssemblyRegistrationFlags.None); Console.WriteLine(".NET assembly registered with COM!"); } }
internal virtual void Setup(TypeLibrary typeLib, TYPEKIND typeKind, int index, UCOMITypeInfo typeInfo, Guid guid) { _typeLib = typeLib; _iTypeLib = typeLib.ITypeLib; if (typeInfo != null) { _typeInfo = typeInfo; } else { _iTypeLib.GetTypeInfo(index, out _typeInfo); } if (!guid.Equals(Guid.Empty)) { InitGuid(guid); } else { InitGuid(GuidFromTypeInfo(_typeInfo)); } _typeKind = typeKind; _presInfo = PresentationMap.GetInfo(_typeKind); GetDocumentation(index); if (TraceUtil.If(this, TraceLevel.Info)) { Trace.WriteLine(this, "Basic: " + typeKind + " " + this); } }
public static int Run(TlbExpOptions options) { s_Options = options; int RetCode = SuccessReturnCode; try { // Load the assembly. Assembly asm = Assembly.LoadFrom(s_Options.m_strAssemblyName); // If the typelib name is null then create it from the assembly name. if (s_Options.m_strTypeLibName == null) { s_Options.m_strTypeLibName = Path.Combine(s_Options.m_strOutputDir, asm.GetName().Name) + ".tlb"; } // If the input assembly has an embedded type library, give a warning unless we are in silent mode. if (!s_Options.m_bSilentMode && (LoadEmbeddedTlb(s_Options.m_strAssemblyName) != null)) { WriteWarningMsg(Resource.FormatString("Wrn_AssemblyHasEmbeddedTlb", s_Options.m_strAssemblyName)); } // Import the typelib to an assembly. UCOMITypeLib Tlb = DoExport(asm, s_Options.m_strTypeLibName); // Display the success message unless silent mode is enabled. if (!s_Options.m_bSilentMode) { Console.WriteLine(Resource.FormatString("Msg_AssemblyExported", s_Options.m_strTypeLibName)); } } catch (ReflectionTypeLoadException e) { int i; Exception[] exceptions; WriteErrorMsg(Resource.FormatString("Err_TypeLoadExceptions")); exceptions = e.LoaderExceptions; for (i = 0; i < exceptions.Length; i++) { try { Console.Error.WriteLine(Resource.FormatString("Msg_DisplayException", i, exceptions[i])); } catch (Exception ex) { Console.Error.WriteLine(Resource.FormatString("Msg_DisplayNestedException", i, ex)); } } RetCode = ErrorReturnCode; } catch (Exception e) { WriteErrorMsg(null, e); RetCode = ErrorReturnCode; } return(RetCode); }
private string GetComReference(UCOMITypeLib typeLib) { if (options.references == null) { return(null); } return(options.references.ResolveComReference(typeLib)); }
internal Assembly GetPrimaryInteropAssembly(UCOMITypeLib typeLib, TypeLibConverter tlbConverter) { Assembly pia = FindRCW(typeLib); if (pia != null) { return(pia); } IntPtr typeLibAttr = NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out typeLibAttr); if (typeLibAttr != NativeMethods.InvalidIntPtr) { // Marshal the returned int as a TLibAttr structure // TYPELIBATTR tlbAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttr, typeof(TYPELIBATTR)); string asmName = null; string asmCodeBase = null; try { tlbConverter.GetPrimaryInteropAssembly(tlbAttr.guid, tlbAttr.wMajorVerNum, tlbAttr.wMinorVerNum, tlbAttr.lcid, out asmName, out asmCodeBase); if (asmName != null && asmCodeBase == null) { // We found the PIA in the GAC... we need a codebase for this // so we can pass this to the compiler. // try { pia = Assembly.Load(asmName); asmCodeBase = GetLocalPath(pia.EscapedCodeBase); } catch (Exception e) { Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Could load assembly from GAC " + asmName + " Exception " + e.Message); } } else if (asmCodeBase != null) { asmCodeBase = GetLocalPath(asmCodeBase); pia = Assembly.LoadFrom(asmCodeBase); } if (pia != null) { AddRCW(typeLib, pia); AddReferencedAssembly(asmCodeBase); } } finally { typeLib.ReleaseTLibAttr(typeLibAttr); } } return(pia); }
private string SaveAssemblyBuilder(UCOMITypeLib typeLib, AssemblyBuilder asmBldr, string rcwName) { string assembly = null; FileInfo rcwFile = new FileInfo(rcwName); string fullPath = rcwFile.FullName; string assemName = rcwFile.Name; // Check to see if the assembly is already in the referenced set of assemblies. // If so, just use the referenced assembly instead of creating a new one on disk. // // Otherwise, create our assembly by saving to disk. // if (rcwFile.Exists) { if (options.overwriteRCW) { if (typeLibName != null && String.Compare(typeLibName, rcwFile.FullName, true, CultureInfo.InvariantCulture) == 0) { throw new Exception(SR.GetString(SR.AXCannotOverwriteFile, rcwFile.FullName)); } if (((int)rcwFile.Attributes & (int)FileAttributes.ReadOnly) == (int)FileAttributes.ReadOnly) { throw new Exception(SR.GetString(SR.AXReadOnlyFile, rcwFile.FullName)); } try { rcwFile.Delete(); asmBldr.Save(assemName); } catch (Exception e) { Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Exception deleting file " + rcwFile.FullName + " Exception: " + e.ToString()); throw new Exception(SR.GetString(SR.AXCannotOverwriteFile, rcwFile.FullName)); } } } else { asmBldr.Save(assemName); } assembly = rcwFile.FullName; // Add the generated assembly to our list. // AddReferencedAssembly(assembly); AddTypeLibAttr(typeLib); AddGeneratedAssembly(assembly); return(assembly); }
private static UCOMITypeLib LoadEmbeddedTlb(String strFileName) { UCOMITypeLib Tlb = null; try { LoadTypeLibEx(s_Options.m_strAssemblyName, REGKIND.REGKIND_NONE, out Tlb); } catch (Exception) { } return(Tlb); }
private static bool ContainsEmbeddedTlb(String strFileName) { UCOMITypeLib Tlb = null; try { LoadTypeLibEx(strFileName, REGKIND.REGKIND_NONE, out Tlb); } catch (Exception) { } return(Tlb != null ? true : false); }
internal static UCOMITypeLib GenerateTypeLib(Assembly asm, ref String strTlbName) { ITypeLibConverter tlbConv = new TypeLibConverter(); ExporterCallback callback = new ExporterCallback(); UCOMITypeLib tlb = (UCOMITypeLib)tlbConv.ConvertAssemblyToTypeLib(asm, strTlbName, 0, callback); // Persist the typelib. UCOMICreateITypeLib createTlb = (UCOMICreateITypeLib)tlb; createTlb.SaveAllChanges(); return(tlb); }
public Object ResolveRef(Assembly asm) { UCOMITypeLib rslt = null; Module[] aModule = asm.GetLoadedModules(); String asmPath = Path.GetDirectoryName(aModule[0].FullyQualifiedName); // If the typelib name is null then create it from the assembly name. String FullyQualifiedTypeLibName = Path.Combine(asmPath, asm.GetName().Name) + ".tlb.tmp"; // Export the typelib for the module. rslt = GenMan32Code.GenerateTypeLib(asm, ref FullyQualifiedTypeLibName); return(rslt); }
static void Main(string[] args) { // Check for -NOGUI switch. bool usingGUI = true; for (int i = 0; i < args.Length; i++) { if (args[i] == "-NOGUI") { usingGUI = false; } } // Gather user input. string pathToComServer = ""; if (!usingGUI) { // Prompt for path. Console.WriteLine("Please enter path to COM type information."); Console.WriteLine(@"Example: C:\Stuff\Blah\Debug\MyComServer.dll"); Console.Write("Path: "); pathToComServer = Console.ReadLine(); } else { Console.WriteLine("Pick a COM server..."); OpenFileDialog d = new OpenFileDialog(); if (d.ShowDialog() == DialogResult.OK) { pathToComServer = d.FileName; } } // Show path to COM server. Console.WriteLine("Path: {0}\n", pathToComServer); // Generate an .NET assembly (no strong name). UCOMITypeLib theTypeLib = LoadCOMTypeInfo(pathToComServer); if (theTypeLib == null) { return; } GenerateAssemblyFromTypeLib(theTypeLib); Console.WriteLine("All done!"); }
private static void SetVersionInformation(AssemblyBuilder asmBldr, Object typeLib, AssemblyName asmName) { // Extract the name of the typelib. String strTypeLibName = null; String strDocString = null; int dwHelpContext = 0; String strHelpFile = null; UCOMITypeLib pTLB = (UCOMITypeLib)typeLib; pTLB.GetDocumentation(-1, out strTypeLibName, out strDocString, out dwHelpContext, out strHelpFile); // Generate the product name string from the named of the typelib. String strProductName = String.Format(Environment.GetResourceString("TypeLibConverter_ImportedTypeLibProductName"), strTypeLibName); // Set the OS version information. asmBldr.DefineVersionInfoResource(strProductName, asmName.Version.ToString(), null, null, null); }
/// <include file='doc\AxImporter.uex' path='docs/doc[@for="AxImporter.GenerateFromTypeLibrary"]/*' /> /// <devdoc> /// <para>Generates a wrapper for an ActiveX control for use in the design-time /// environment.</para> /// </devdoc> public string GenerateFromTypeLibrary(UCOMITypeLib typeLib) { bool foundAxCtl = false; int ctypes = typeLib.GetTypeInfoCount(); Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Number of TypeInfos found in typelib: " + ctypes); for (int i = 0; i < ctypes; ++i) { IntPtr pAttr; TYPEATTR typeAttr; UCOMITypeInfo pTI; typeLib.GetTypeInfo(i, out pTI); pTI.GetTypeAttr(out pAttr); typeAttr = (TYPEATTR)Marshal.PtrToStructure(pAttr, typeof(TYPEATTR)); if ((int)typeAttr.typekind == (int)TYPEKIND.TKIND_COCLASS) { Guid g = typeAttr.guid; string controlKey = "CLSID\\{" + g.ToString() + "}\\Control"; RegistryKey key = Registry.ClassesRoot.OpenSubKey(controlKey); if (key != null) { Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Found ActiveX control with the following GUID: " + g.ToString()); foundAxCtl = true; } } pTI.ReleaseTypeAttr(pAttr); pAttr = IntPtr.Zero; Marshal.ReleaseComObject(pTI); pTI = null; } if (foundAxCtl) { Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Generating Windows Forms wrappers for: " + typeLibName); return(GenerateFromTypeLibrary(typeLib, Guid.Empty)); } else { throw new Exception(SR.GetString(SR.AXNoActiveXControls, (typeLibName != null) ? typeLibName : Marshal.GetTypeLibName(typeLib))); } }
private static void UnRegisterMainTypeLib() { UCOMITypeLib Tlb = null; IntPtr pAttr = (IntPtr)0; try { // Try and load the typelib. LoadTypeLibEx(s_Options.m_strTypeLibName, REGKIND.REGKIND_NONE, out Tlb); // Retrieve the version information from the typelib. Tlb.GetLibAttr(out pAttr); // Copy the int we got back from GetLibAttr to a TypeLibAttr struct. TYPELIBATTR Attr = (TYPELIBATTR)Marshal.PtrToStructure((IntPtr)pAttr, typeof(TYPELIBATTR)); // Unregister the typelib. UnRegisterTypeLib(ref Attr.guid, Attr.wMajorVerNum, Attr.wMinorVerNum, Attr.lcid, Attr.syskind); } catch (COMException e) { // TYPE_E_REGISTRYACCESS errors simply mean that the typelib has already been unregistered. if (e.ErrorCode != unchecked ((int)0x8002801C)) { ThrowAppException(Resource.FormatString("Err_UnregistrationFailed"), e); } } catch (Exception e) { ThrowAppException(Resource.FormatString("Err_UnregistrationFailed"), e); } finally { // Release the typelib attributes. if (pAttr != (IntPtr)0) { Tlb.ReleaseTLibAttr(pAttr); } } // Display the success message unless silent mode is enabled. if (!s_Options.m_bSilentMode) { Console.WriteLine(Resource.FormatString("Msg_TypelibUnregistered", s_Options.m_strTypeLibName)); } }
public static string GenerateAssemblyFromTypeLib(UCOMITypeLib typLib) { // Need a sink for the TypeLibConverter. ImporterNotiferSink sink = new ImporterNotiferSink(); #region Notes on Strong Name... /* Don't need a *.snk file if you are not building a primary interop asm. * Just send in nulls to the ConvertTypeLibToAssembly() method. * But if you have a valid *.snk file, you can use it as so: * * // Object representation of *.snk file. * FileStream fs = File.Open(@"D:\APress Books\InteropBook\MyTypeLibImporter\bin\Debug\theKey.snk", * FileMode.Open); * System.Reflection.StrongNameKeyPair keyPair = new StrongNameKeyPair(fs); */ #endregion // This class will covert COM type info into // .NET metadata and vice-versa. TypeLibConverter tlc = new TypeLibConverter(); // Generate name of the assembly. string typeLibName = Marshal.GetTypeLibName(typLib); string asmName = "interop." + typeLibName + ".dll"; // Now make the assembly based on COM type information. AssemblyBuilder asmBuilder = tlc.ConvertTypeLibToAssembly((UCOMITypeLib)typLib, asmName, TypeLibImporterFlags.SafeArrayAsSystemArray, sink, null, // If you have a strong name: keyPair.PublicKey, null, // If you have a strong name: keyPair typeLibName, // Namespace name is same as file name. null); // null = (typeLibMajor.typeLibMinor.0.0) // Save the assembly in the app directory! asmBuilder.Save(asmName); // return Assembly ref to call. return(asmName); }
public static string GenerateAssemblyFromTypeLib(UCOMITypeLib typLib) { // Need a sink for the TypeLibConverter. ImporterNotiferSink sink = new ImporterNotiferSink(); #region Notes on Strong Name... /* Don't need a *.snk file if you are not building a primary interop asm. * Just send in nulls to the ConvertTypeLibToAssembly() method. * But if you have a valid *.snk file, you can use it as so: * * // Object representation of *.snk file. * FileStream fs = File.Open(@"D:\APress Books\InteropBook\MyTypeLibImporter\bin\Debug\theKey.snk", * FileMode.Open); * System.Reflection.StrongNameKeyPair keyPair = new StrongNameKeyPair(fs); */ #endregion // This class will covert COM type info into // .NET metadata and vice-versa. TypeLibConverter tlc = new TypeLibConverter(); // Generate name of the assembly. string typeLibName = Marshal.GetTypeLibName(typLib); string asmName = "interop." + typeLibName + ".dll"; // Now make the assembly based on COM type information. AssemblyBuilder asmBuilder = tlc.ConvertTypeLibToAssembly((UCOMITypeLib)typLib, asmName, TypeLibImporterFlags.SafeArrayAsSystemArray, sink, null, // If you have a strong name: keyPair.PublicKey, null, // If you have a strong name: keyPair typeLibName, // Namespace name is same as file name. null); // null = (typeLibMajor.typeLibMinor.0.0) // Save the assembly in the app directory! asmBuilder.Save(asmName); // return Assembly ref to call. return asmName; }
/// <devdoc> /// <para>Gets the file name corresponding to the given TypelibAttribute. </para> /// </devdoc> private static string GetFileOfTypeLib(UCOMITypeLib typeLib) { IntPtr typeLibAttr = NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out typeLibAttr); if (typeLibAttr != NativeMethods.InvalidIntPtr) { // Marshal the returned int as a TLibAttr structure // TYPELIBATTR typeLibraryAttributes = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttr, typeof(TYPELIBATTR)); try { return(GetFileOfTypeLib(ref typeLibraryAttributes)); } finally { typeLib.ReleaseTLibAttr(typeLibAttr); } } return(null); }
public Object ResolveRef(Assembly asm) { UCOMITypeLib rslt = null; // Retrieve the path of the assembly on disk. Module[] aModule = asm.GetLoadedModules(); String asmPath = Path.GetDirectoryName(aModule[0].FullyQualifiedName); // If the typelib name is null then create it from the assembly name. String FullyQualifiedTypeLibName = Path.Combine(asmPath, asm.GetName().Name) + ".tlb"; if (RegCode.s_Options.m_bVerboseMode) { Console.WriteLine(Resource.FormatString("Msg_AutoExpAndRegAssembly", asm.GetName().Name, FullyQualifiedTypeLibName)); } // Export the typelib for the module. rslt = RegCode.DoExportAndRegister(asm, FullyQualifiedTypeLibName); return(rslt); }
private static void SetPIAAttributeOnAssembly(AssemblyBuilder asmBldr, Object typeLib) { IntPtr pAttr = Win32Native.NULL; TYPELIBATTR Attr; UCOMITypeLib pTLB = (UCOMITypeLib)typeLib; int Major = 0; int Minor = 0; // Retrieve the PrimaryInteropAssemblyAttribute constructor. Type [] aConsParams = new Type[2] { typeof(int), typeof(int) }; ConstructorInfo PIAAttrCons = typeof(PrimaryInteropAssemblyAttribute).GetConstructor(aConsParams); // Retrieve the major and minor version from the typelib. try { pTLB.GetLibAttr(out pAttr); Attr = (TYPELIBATTR)Marshal.PtrToStructure(pAttr, typeof(TYPELIBATTR)); Major = Attr.wMajorVerNum; Minor = Attr.wMinorVerNum; } finally { // Release the typelib attributes. if (pAttr != Win32Native.NULL) { pTLB.ReleaseTLibAttr(pAttr); } } // Create an instance of the custom attribute builder. Object[] aArgs = new Object[2] { Major, Minor }; CustomAttributeBuilder PIACABuilder = new CustomAttributeBuilder(PIAAttrCons, aArgs); // Set the PrimaryInteropAssemblyAttribute on the assembly builder. asmBldr.SetCustomAttribute(PIACABuilder); }
private void AddTypeLibAttr(UCOMITypeLib typeLib) { // Add the TYPELIBATTR of the TypeLib to our list. // if (tlbAttrs == null) { tlbAttrs = new ArrayList(); } IntPtr typeLibAttr = NativeMethods.InvalidIntPtr; typeLib.GetLibAttr(out typeLibAttr); if (typeLibAttr != NativeMethods.InvalidIntPtr) { // Marshal the returned int as a TLibAttr structure // TYPELIBATTR typeLibraryAttributes = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttr, typeof(TYPELIBATTR)); tlbAttrs.Add(typeLibraryAttributes); typeLib.ReleaseTLibAttr(typeLibAttr); } }
public static UCOMITypeLib DoExport(Assembly asm, String strTypeLibName) { // Create the TypeLibConverter. ITypeLibConverter TLBConv = new TypeLibConverter(); // Convert the assembly. ExporterCallback callback = new ExporterCallback(); UCOMITypeLib Tlb = (UCOMITypeLib)TLBConv.ConvertAssemblyToTypeLib(asm, strTypeLibName, 0, callback); // Persist the typelib. try { UCOMICreateITypeLib CreateTlb = (UCOMICreateITypeLib)Tlb; CreateTlb.SaveAllChanges(); } catch (Exception e) { ThrowAppException(Resource.FormatString("Err_ErrorSavingTypeLib"), e); } return(Tlb); }
/// <include file='doc\AxImporter.uex' path='docs/doc[@for="AxImporter.GenerateFromFile"]/*' /> /// <devdoc> /// <para>Generates a wrapper for an ActiveX control for use in the design-time /// environment.</para> /// </devdoc> public string GenerateFromFile(FileInfo file) { typeLibName = file.FullName; UCOMITypeLib typeLib = null; Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Loading Typelib from " + typeLibName); typeLib = (UCOMITypeLib)NativeMethods.LoadTypeLib(typeLibName); if (typeLib == null) { throw new Exception(SR.GetString(SR.AXCannotLoadTypeLib, typeLibName)); } try { return(GenerateFromTypeLibrary(typeLib)); } finally { if (typeLib != null) { Marshal.ReleaseComObject(typeLib); } } }
/// <summary> /// This method creates (and saves) a .NET assembly given a COM /// type library. /// The name of the assembly is: "interop.[ComLibName].dll" /// The version of the assembly is: [ComLib.Major], [ComLib.Minor], 0, 0 /// </summary> public static UCOMITypeLib LoadCOMTypeInfo(string pathToComServer) { // Load type information for COM server. UCOMITypeLib typLib = null; try { LoadTypeLibEx(pathToComServer, REGKIND.REGKIND_NONE, out typLib); string strName, strDoc, strHelpFile; int helpCtx; Console.WriteLine("COM Library Description:"); typLib.GetDocumentation(-1, out strName, out strDoc, out helpCtx, out strHelpFile); Console.WriteLine("->Name: {0}", strName); Console.WriteLine("->Doc: {0}", strDoc); Console.WriteLine("->Help Context: {0}", helpCtx.ToString()); Console.WriteLine("->Help File: {0}", strHelpFile); } catch { Console.WriteLine("ugh...can't load COM type info!"); } return(typLib); }
public string GenerateFromTypeLibrary(UCOMITypeLib typeLib) { bool flag = false; int typeInfoCount = ((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib).GetTypeInfoCount(); for (int i = 0; i < typeInfoCount; i++) { IntPtr zero; System.Runtime.InteropServices.ComTypes.ITypeInfo info; ((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib).GetTypeInfo(i, out info); info.GetTypeAttr(out zero); System.Runtime.InteropServices.ComTypes.TYPEATTR typeattr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(zero, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR)); if (typeattr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS) { string name = @"CLSID\{" + typeattr.guid.ToString() + @"}\Control"; if (Registry.ClassesRoot.OpenSubKey(name) != null) { flag = true; } } info.ReleaseTypeAttr(zero); zero = IntPtr.Zero; Marshal.ReleaseComObject(info); info = null; } if (flag) { return(this.GenerateFromTypeLibrary(typeLib, Guid.Empty)); } string message = System.Design.SR.GetString("AXNoActiveXControls", new object[] { (this.typeLibName != null) ? this.typeLibName : Marshal.GetTypeLibName((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib) }); if (this.options.msBuildErrors) { message = "AxImp: error aximp000: " + message; } throw new Exception(message); }
public String ResolveActiveXReference(UCOMITypeLib typeLib) { TraceUtil.WriteLineInfo(this, "TypeLib - ResolveActiveXRef: " + typeLib); return null; }
private static extern void LoadTypeLibEx(string strTypeLibName, REGKIND regKind, out UCOMITypeLib TypeLib);
RegisterTypeLib(UCOMITypeLib typeLib, String fullPath, String helpDir);
public static string GetTypeLibName(UCOMITypeLib pTLB) { return default(string); }
public static int GetTypeLibLcid (UCOMITypeLib pTLB) { throw new NotImplementedException (); }
public string GenerateFromTypeLibrary(UCOMITypeLib typeLib) { bool ignoreRegisteredOcx = this.options.ignoreRegisteredOcx; if (!ignoreRegisteredOcx) { int typeInfoCount = ((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib).GetTypeInfoCount(); for (int i = 0; i < typeInfoCount; i++) { IntPtr zero; System.Runtime.InteropServices.ComTypes.ITypeInfo info; ((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib).GetTypeInfo(i, out info); info.GetTypeAttr(out zero); System.Runtime.InteropServices.ComTypes.TYPEATTR typeattr = (System.Runtime.InteropServices.ComTypes.TYPEATTR) Marshal.PtrToStructure(zero, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR)); if (typeattr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS) { string name = @"CLSID\{" + typeattr.guid.ToString() + @"}\Control"; if (Registry.ClassesRoot.OpenSubKey(name) != null) { ignoreRegisteredOcx = true; } } info.ReleaseTypeAttr(zero); zero = IntPtr.Zero; Marshal.ReleaseComObject(info); info = null; } } if (ignoreRegisteredOcx) { return this.GenerateFromTypeLibrary(typeLib, Guid.Empty); } string message = System.Design.SR.GetString("AXNoActiveXControls", new object[] { (this.typeLibName != null) ? this.typeLibName : Marshal.GetTypeLibName((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib) }); if (this.options.msBuildErrors) { message = "AxImp: error aximp000: " + message; } throw new Exception(message); }
public static string GetTypeLibName(UCOMITypeLib pTLB) {}
public static Guid GetTypeLibGuid(UCOMITypeLib pTLB) { // // Summary: // Retrieves the library identifier (LIBID) that is assigned to a type library // when it was exported from the specified assembly. // // Parameters: // asm: // A managed System.Reflection.Assembly. // // Returns: // The LIBID (that is, the System.Guid) that is assigned to a type library when // it is exported from the asm parameter. return default(Guid); }
internal virtual void Setup(TypeLibrary typeLib, TYPEKIND typeKind, int index, UCOMITypeInfo typeInfo, Guid guid) { _typeLib = typeLib; _iTypeLib = typeLib.ITypeLib; if (typeInfo != null) _typeInfo = typeInfo; else _iTypeLib.GetTypeInfo(index, out _typeInfo); if (!guid.Equals(Guid.Empty)) InitGuid(guid); else InitGuid(GuidFromTypeInfo(_typeInfo)); _typeKind = typeKind; _presInfo = PresentationMap.GetInfo(_typeKind); GetDocumentation(index); if (TraceUtil.If(this, TraceLevel.Info)) Trace.WriteLine(this, "Basic: " + typeKind + " " + this); }
// If the UCOMITypeLib pointer is good, then remember // that, because the type library may not be in the registry // or a file we can open. But we can still convert it and // read its information internal static TypeLibrary GetTypeLib(TypeLibKey key, UCOMITypeLib iTypeLib) { TraceUtil.WriteLineInfo(null, "TypeLib - GetTypeLib: " + key); lock (typeof(TypeLibrary)) { TypeLibrary lib = (TypeLibrary)_openedTypeLibs[key]; if (lib != null && lib._converted) return lib; try { if (lib == null) { // Have to create one lib = new TypeLibrary(key); lib._iTypeLib = iTypeLib; try { lib.PopulateFromRegistry(key); } catch (Exception ex) { // This could be ok, sometimes a typelib is not // in the registry TraceUtil.WriteLineWarning (typeof(TypeLibrary), "GetTypeLib exception (not in registry) " + ex); if (lib._iTypeLib == null) { throw new Exception("Failed to find type library information in registry", ex); } } } lib.TranslateTypeLib(); // Generate the C# source after the translation for // now because the source generation requires the // definitions to be read which can cause a call // to GetTypeLib(), don't want to be re-entered // FIXME //lib.GenerateSource(); return lib; } catch (Exception ex) { TraceUtil.WriteLineWarning(typeof(TypeLibrary), "GetTypeLib exception " + ex); throw new Exception("Error getting type library: ", ex); } } }
internal static TypeLibKey GetTypeLibKey(UCOMITypeLib typeLib) { TypeLibKey typeLibKey; IntPtr typeAttrPtr; typeLib.GetLibAttr(out typeAttrPtr); TYPELIBATTR typeLibAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeAttrPtr, typeof(TYPELIBATTR)); typeLibKey = GetTypeLibKey(typeLibAttr); // Release it only after we are not going to touch // even the TYPELIBATTR structure any more typeLib.ReleaseTLibAttr(typeAttrPtr); return typeLibKey; }
#pragma warning disable 618,612 public string ResolveActiveXReference(UCOMITypeLib typeLib) { return null; }
public string ResolveComReference(UCOMITypeLib typeLib) { string fileName; _existingWrappers.TryGetValue(Marshal.GetTypeLibName(typeLib), out fileName); return fileName; }
public static int GetTypeLibLcid(UCOMITypeLib pTLB) {}
public static System.Guid GetTypeLibGuid(UCOMITypeLib pTLB) {}
public static int GetTypeLibLcid(UCOMITypeLib pTLB) { // // Summary: // Retrieves the name of a type library. // // Parameters: // typelib: // An System.Runtime.InteropServices.ComTypes.ITypeLib object that represents // an ITypeLib pointer. // // Returns: // The name of the type library pointed to by the typelib parameter. // // Exceptions: // System.ArgumentNullException: // The typelib parameter is null. return default(int); }
public string GenerateFromTypeLibrary(UCOMITypeLib typeLib, Guid clsid) { string fileName = null; string axTypeFromAssembly = null; Assembly assem = null; fileName = this.GetAxReference((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib); if ((fileName != null) && (clsid != Guid.Empty)) { axTypeFromAssembly = this.GetAxTypeFromAssembly(fileName, clsid); } if (fileName == null) { string typeLibName = Marshal.GetTypeLibName((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib); string asmFileName = Path.Combine(this.options.outputDirectory, typeLibName + ".dll"); this.AddReferencedAssembly(this.GetManagedReference("System.Windows.Forms")); this.AddReferencedAssembly(this.GetManagedReference("System.Drawing")); this.AddReferencedAssembly(this.GetManagedReference("System")); string comReference = this.GetComReference((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib); if (comReference != null) { this.AddReferencedAssembly(comReference); assem = this.GetCopiedAssembly(comReference, false, false); this.AddDependentAssemblies(assem, comReference); } else { TypeLibConverter tlbConverter = new TypeLibConverter(); assem = this.GetPrimaryInteropAssembly((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib, tlbConverter); if (assem != null) { comReference = this.GetLocalPath(assem.EscapedCodeBase); this.AddDependentAssemblies(assem, comReference); } else { AssemblyBuilder asmBldr = tlbConverter.ConvertTypeLibToAssembly((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib, asmFileName, TypeLibImporterFlags.None, new ImporterCallback(this), this.options.publicKey, this.options.keyPair, null, null); if (comReference == null) { comReference = this.SaveAssemblyBuilder((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib, asmBldr, asmFileName); assem = asmBldr; } } } int num = 0; string[] refAssemblies = new string[this.refAssems.Count]; foreach (string str6 in this.refAssems) { string str7 = str6; str7 = str7.Replace("%20", " "); refAssemblies[num++] = str7; } if (axTypeFromAssembly == null) { string path = null; if (this.options.ignoreRegisteredOcx) { path = this.typeLibName; } else { path = GetFileOfTypeLib((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib); } DateTime tlbTimeStamp = (path == null) ? DateTime.Now : File.GetLastWriteTime(path); ResolveEventHandler handler = new ResolveEventHandler(this.OnAssemblyResolve); AppDomain.CurrentDomain.AssemblyResolve += handler; AppDomain.CurrentDomain.TypeResolve += new ResolveEventHandler(this.OnTypeResolve); try { if (this.options.genSources) { AxWrapperGen.GeneratedSources = new ArrayList(); } if (this.options.outputName == null) { this.options.outputName = "Ax" + typeLibName + ".dll"; } axTypeFromAssembly = AxWrapperGen.GenerateWrappers(this, clsid, assem, refAssemblies, tlbTimeStamp, this.options.ignoreRegisteredOcx, out fileName); if (this.options.genSources) { this.generatedSources = AxWrapperGen.GeneratedSources; } } finally { AppDomain.CurrentDomain.AssemblyResolve -= handler; AppDomain.CurrentDomain.TypeResolve -= new ResolveEventHandler(this.OnTypeResolve); } if (axTypeFromAssembly == null) { string message = System.Design.SR.GetString("AXNoActiveXControls", new object[] { (this.typeLibName != null) ? this.typeLibName : typeLibName }); if (this.options.msBuildErrors) { message = "AxImp: error aximp000: " + message; } throw new Exception(message); } } if (axTypeFromAssembly != null) { this.AddReferencedAssembly(fileName); this.AddTypeLibAttr((System.Runtime.InteropServices.ComTypes.ITypeLib) typeLib); this.AddGeneratedAssembly(fileName); } } return axTypeFromAssembly; }
public static string GetTypeLibName(UCOMITypeLib pTLB) { // // Summary: // Retrieves the version number of a type library that will be exported from // the specified assembly. // // Parameters: // majorVersion: // The major version number. // // minorVersion: // The minor version number. // // inputAssembly: // A managed System.Reflection.Assembly object. return default(string); }
public static Guid GetTypeLibGuid (UCOMITypeLib pTLB) { throw new NotImplementedException (); }
public string GenerateFromTypeLibrary (UCOMITypeLib typeLib, Guid clsid) { throw new NotImplementedException (); }
public static string GetTypeLibName (UCOMITypeLib pTLB) { throw new NotImplementedException (); }
// Returns a translated type library given the specified COM typelib internal static TypeLibrary GetTypeLib(UCOMITypeLib iTypeLib) { return GetTypeLib(GetTypeLibKey(iTypeLib), iTypeLib); }
LoadTypeLibEx(String strTypeLibName, RegKind regKind, out UCOMITypeLib typeLib);
protected TypeLibrary ResolveRef(UCOMITypeLib iTypeLib) { TraceUtil.WriteLineIf(null, TraceLevel.Info, "TypeLib - resolving: " + iTypeLib); TypeLibrary lib = GetTypeLib(iTypeLib); TraceUtil.WriteLineInfo(null, "TypeLib - resolved: " + lib + " assy: " + lib._assy); return lib; }
public String ResolveComReference(UCOMITypeLib typeLib) { String refStr = ResolveRef(typeLib)._assyInfo._fileName; TraceUtil.WriteLineInfo(this, "TypeLib - ResolveCOMRef (typeLib): " + typeLib + " ret: " + refStr); return refStr; }
public static int GetTypeLibLcid(UCOMITypeLib pTLB) { return default(int); }
private void FillListBoxes(UCOMITypeLib itfTypeLib) { // Clear out current contents. lstBoxCoclasses.Items.Clear(); lstBoxInterfaces.Items.Clear(); lstBoxEnums.Items.Clear(); // Get # of COM types in the library. int typeCount = itfTypeLib.GetTypeInfoCount(); lblNumbOfTypes.Text = "Number of COM Types in file: " + typeCount.ToString(); // Switch between COM type. // Dump out the types. for(int typeIndex = 0; typeIndex < typeCount; typeIndex++) { string typeInfoString; UCOMITypeInfo pInfo; // Get TYPEATTR structure set up. TYPEATTR typeAtt = new TYPEATTR(); Type TYPEATTRType = typeAtt.GetType(); int structSize = Marshal.SizeOf(TYPEATTRType); IntPtr ptr = IntPtr.Zero; ptr = Marshal.AllocHGlobal(structSize); // Get next type info. itfTypeLib.GetTypeInfo(typeIndex, out pInfo); pInfo.GetTypeAttr(out ptr); typeAtt = (TYPEATTR) Marshal.PtrToStructure(ptr, TYPEATTRType); // Based on the kind of COM type, print out some information. string typeName, helpFile, docString; int helpID; switch(typeAtt.typekind) { case TYPEKIND.TKIND_COCLASS: // type is a coclass. pInfo.GetDocumentation(-1, out typeName, out docString, out helpID, out helpFile); typeInfoString = "Name: " + typeName + "\tCLSID: {" + typeAtt.guid.ToString() + "}"; lstBoxCoclasses.Items.Add(typeInfoString); break; case TYPEKIND.TKIND_INTERFACE: // type is a interface. case TYPEKIND.TKIND_DISPATCH: pInfo.GetDocumentation(-1, out typeName, out docString, out helpID, out helpFile); typeInfoString = "Name: " + typeName + "\tIID: {" + typeAtt.guid.ToString() + "}"; lstBoxInterfaces.Items.Add(typeInfoString); break; case TYPEKIND.TKIND_ENUM: // type is a enum. pInfo.GetDocumentation(-1, out typeName, out docString, out helpID, out helpFile); typeInfoString = "Name: " + typeName; lstBoxEnums.Items.Add(typeInfoString); break; } Marshal.DestroyStructure(ptr, typeAtt.GetType()); } }
public static Guid GetTypeLibGuid(UCOMITypeLib pTLB) { return default(Guid); }