public static void CreateTypeLib(String fileName, Guid clsid) { try { ICreateTypeLib typelib = SafeNativeMethods.CreateTypeLib(fileName); typelib.SetGuid(Guid.NewGuid()); typelib.SetName(ListenerWSUName); typelib.SetDocString(ListenerWSUName); ICreateTypeInfo typeInfo = typelib.CreateTypeInfo(ListenerWSUName, System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS); typeInfo.SetGuid(clsid); typeInfo.SetDocString(ListenerWSUName); ICreateTypeInfo2 typeInfo2 = (ICreateTypeInfo2)typeInfo; typeInfo2.SetName(ListenerWSUName + "Component"); typeInfo2.SetTypeFlags(2); typeInfo.LayOut(); typelib.SaveAllChanges(); } catch (Exception ex) { if (ex is NullReferenceException || ex is SEHException) { throw ex; } throw Tool.CreateException(SR.GetString(SR.FailedToCreateTypeLibrary), ex); } }
static bool PerformTypeLibCreationAndRegistration(string strTargetAssemblyFilePath) { try { string strTargetAssemblyDirectory = Path.GetDirectoryName(strTargetAssemblyFilePath); string strTargetAssemblyFileNameWithoutExtension = Path.GetFileNameWithoutExtension(strTargetAssemblyFilePath); string strTargetTypeLibFullPath = strTargetAssemblyDirectory + "\\" + strTargetAssemblyFileNameWithoutExtension + ".tlb"; TypeLibConverter converter = new TypeLibConverter(); Assembly assembly = Assembly.LoadFrom(strTargetAssemblyFilePath); TypeLibExporterFlags flags; if (Is32Bits()) { flags = TypeLibExporterFlags.ExportAs32Bit; } else if (Is64Bits()) { flags = TypeLibExporterFlags.ExportAs64Bit; } else { Console.WriteLine(string.Format("Unknown bit-ness.")); return(false); } ICreateTypeLib create_typeLib = (ICreateTypeLib)(converter.ConvertAssemblyToTypeLib (assembly, strTargetTypeLibFullPath, flags, m_pITypeLibExporterNotifySink)); // SaveAllChanges() will create the TypeLib physical file // based on strTargetTypeLibFullPath. create_typeLib.SaveAllChanges(); ITypeLib typelib = (ITypeLib)create_typeLib; UInt32 uiRetTemp = RegisterTypeLib(typelib, strTargetTypeLibFullPath, null); if (uiRetTemp == 0) { Console.WriteLine(string.Format("TypeLib File [{0:S}] registered.", strTargetTypeLibFullPath)); } else { Console.WriteLine(string.Format("Failed to register TypeLib File [{0:S}]. Error code : [{1:D}]", strTargetTypeLibFullPath, uiRetTemp)); return(false); } return(true); } catch (Exception ex) { Console.WriteLine(string.Format("An exception occurred. Exception description : [{0:S}].", ex.Message)); return(false); } }
internal static extern int CreateTypeLib2(System.Runtime.InteropServices.ComTypes.SYSKIND sysKind, string szFile, out ICreateTypeLib ppctlib);
//static bool PerformAssemblyRegistration(string strTargetAssemblyFilePath, bool bCodeBase) //{ // try // { // RegistrationServices registration_services = new RegistrationServices(); // Assembly assembly = Assembly.LoadFrom(strTargetAssemblyFilePath); // AssemblyRegistrationFlags flags; // bool bRet = false; // if (bCodeBase == true) // { // flags = AssemblyRegistrationFlags.SetCodeBase; // } // else // { // flags = AssemblyRegistrationFlags.None; // } // bRet = registration_services.RegisterAssembly(assembly, flags); // if (bRet) // { // Console.WriteLine(string.Format("Successfully registered assembly [{0:S}].", strTargetAssemblyFilePath)); // if (m_bVerbose) // { // Type[] types = registration_services.GetRegistrableTypesInAssembly(assembly); // Console.WriteLine(string.Format("Types Registered :")); // foreach (Type type in types) // { // Console.WriteLine(string.Format("GUID : [{0:S}] [{1:S}].", type.GUID.ToString(), type.FullName)); // } // } // } // else // { // Console.WriteLine(string.Format("Failed to register assembly [{0:S}].", strTargetAssemblyFilePath)); // } // return bRet; // } // catch (Exception ex) // { // Console.WriteLine(string.Format("An exception occurred. Exception description : [{0:S}].", ex.Message)); // return false; // } //} #endregion public ExecuteResult Run(string strTargetAssemblyFilePath) { try { string strTargetAssemblyDirectory = Path.GetDirectoryName(strTargetAssemblyFilePath); string strTargetAssemblyFileNameWithoutExtension = Path.GetFileNameWithoutExtension(strTargetAssemblyFilePath); string strTargetTypeLibFullPath = strTargetAssemblyDirectory + "\\" + strTargetAssemblyFileNameWithoutExtension + ".tlb"; TypeLibConverter converter = new TypeLibConverter(); Assembly assembly = Assembly.LoadFrom(strTargetAssemblyFilePath); TypeLibExporterFlags flags; if (Platform.Is32Bits()) { flags = TypeLibExporterFlags.ExportAs32Bit; } else if (Platform.Is64Bits()) { flags = TypeLibExporterFlags.ExportAs64Bit; } else { //Console.WriteLine(string.Format("Unknown bit-ness.")); //return false; return(new ExecuteResult() { IsSuccess = false, ErrorMsg = "Unknown bit-ness." }); } ICreateTypeLib create_typeLib = (ICreateTypeLib)(converter.ConvertAssemblyToTypeLib (assembly, strTargetTypeLibFullPath, flags, m_pITypeLibExporterNotifySink)); // SaveAllChanges() will create the TypeLib physical file // based on strTargetTypeLibFullPath. create_typeLib.SaveAllChanges(); ITypeLib typelib = (ITypeLib)create_typeLib; UInt32 uiRetTemp = WindowsAPI.RegisterTypeLib(typelib, strTargetTypeLibFullPath, null); if (uiRetTemp == 0) { //Console.WriteLine(string.Format("TypeLib File [{0:S}] registered.", strTargetTypeLibFullPath)); return(new ExecuteResult() { IsSuccess = true }); } else { //Console.WriteLine(string.Format("Failed to register TypeLib File [{0:S}]. Error code : [{1:D}]", // strTargetTypeLibFullPath, uiRetTemp)); //return false; return(new ExecuteResult() { IsSuccess = false, ErrorMsg = string.Format("Failed to register TypeLib File [{0:S}]. Error code : [{1:D}]", strTargetTypeLibFullPath, uiRetTemp) }); } } catch (Exception ex) { //Console.WriteLine(string.Format("An exception occurred. Exception description : [{0:S}].", ex.Message)); //return false; return(new ExecuteResult() { IsSuccess = false, ErrorMsg = string.Format("An exception occurred. Exception description : [{0:S}].", ex.Message) }); } }
public Object ResolveRef(Assembly asm) { try { // Resolve the reference here and return a correct type library. if (m_bVerbose) { Console.WriteLine("ConversionEventHandler.ResolveRef() [assembly : {0:S}]", asm.FullName); } string strAssemblyDirectory = Path.GetDirectoryName(asm.Location); string strAssemblyFileNameWithoutExtension = Path.GetFileNameWithoutExtension(asm.Location); string strTypeLibFullPath = strAssemblyDirectory + "\\" + strAssemblyFileNameWithoutExtension + ".tlb"; TypeLibConverter converter = new TypeLibConverter(); ConversionEventHandler eventHandler = new ConversionEventHandler(m_bVerbose); TypeLibExporterFlags flags; if (Platform.Is32Bits()) { flags = TypeLibExporterFlags.ExportAs32Bit; } else if (Platform.Is64Bits()) { flags = TypeLibExporterFlags.ExportAs64Bit; } else { Console.WriteLine(string.Format("Unknown bit-ness.")); return(null); } ICreateTypeLib create_typeLib = null; try { create_typeLib = (ICreateTypeLib)(converter.ConvertAssemblyToTypeLib (asm, strTypeLibFullPath, flags, eventHandler)); } catch (Exception ex) { Console.WriteLine(string.Format("Unable to convert assembly [{0:S}] into a Type Lib. Exception description : [{1:S}]", strAssemblyFileNameWithoutExtension, ex.Message)); return(null); } try { // SaveAllChanges() will create the TypeLib physical file // based on strTargetTypeLibFullPath. create_typeLib.SaveAllChanges(); } catch (Exception ex) { Console.WriteLine(string.Format("Unable to save TypeLib File [{0:S}] registered. Exception description : [{1:S}]", strTypeLibFullPath, ex.Message)); return(null); } ITypeLib typelib = (ITypeLib)create_typeLib; UInt32 uiRetTemp = WindowsAPI.RegisterTypeLib(typelib, strTypeLibFullPath, null); if (uiRetTemp == 0) { Console.WriteLine(string.Format("TypeLib File [{0:S}] registered.", strTypeLibFullPath)); } else { Console.WriteLine(string.Format("Failed to register TypeLib File [{0:S}]. Error code : [{1:D}]", strTypeLibFullPath, uiRetTemp)); return(null); } return(typelib); } catch (Exception ex) { Console.WriteLine(string.Format("An exception occurred. Exception description : [{0:S}].", ex.Message)); return(null); } }