使用C#从给定的.dll注册类型库

使用C#从给定的.dll注册类型库,c#,regasm,C#,Regasm,我一直在使用System.EnterpriseServices.Internal.Publish.RegisterAssembly('Path to assembly.dll')来注册我的程序集,但我需要创建一个类型库并注册它 我已尝试使用RegisterTypeLib: [DllImport("oleaut32.dll")] static extern int RegisterTypeLib(ITypeLib ptlib, [MarshalAs(UnmanagedType.

我一直在使用
System.EnterpriseServices.Internal.Publish.RegisterAssembly('Path to assembly.dll')
来注册我的程序集,但我需要创建一个类型库并注册它

我已尝试使用RegisterTypeLib:

[DllImport("oleaut32.dll")]
static extern int RegisterTypeLib(ITypeLib ptlib, [MarshalAs(UnmanagedType.BStr)] string szFullPath, [MarshalAs(UnmanagedType.BStr)] string szHelpDir);
像这样:

Assembly asm = Assembly.LoadFrom(TheAction[2]);
RegistrationServices regAsm = new RegistrationServices();

bool bResult = regAsm.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);

TypeLibConverter converter = new TypeLibConverter();
ConversionEventHandler eventHandler = new ConversionEventHandler();

ITypeLib typeLib = (ITypeLib)converter.ConvertAssemblyToTypeLib(asm, TheAction[2].Replace (".dll",".tlb"), 0, eventHandler);

RegisterTypeLib(typeLib, TheAction[2].Replace(".dll", ".tlb"), string.Empty);
它不会抛出错误,因此如何检查它是否有效


另外,如果有一种更简单的方法从C中实现这一点,我很想听听:)

这是否回答了您的问题?如果返回值小于0,则调用Marshal.ThrowExceptionForHR()