> I believe this is what the TypeLibConverter class is for.
> > How can you register a c#.net assembly for com interop without regasm.exe?
[quoted text clipped - 7 lines]
> >
> > How can I do this through code?
I figured this out, here is class i now use to generate and register a type
library from a .net assembly:
-------------------------------------------------------------
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace ValidatorApp
{
[ComVisible(false)]
public class TypeLibrary
{
[DllImport("oleaut32.dll")]
private static extern int LoadTypeLib([MarshalAs(UnmanagedType.LPWStr)]
string libPath, ref IntPtr ITypLib);
[DllImport("oleaut32.dll")]
private static extern int RegisterTypeLib(IntPtr ITypLib,
[MarshalAs(UnmanagedType.LPWStr)] string libPath,
[MarshalAs(UnmanagedType.LPWStr)] string helpDir);
[DllImport("oleaut32.dll")]
private static extern int UnRegisterTypeLib(ref Guid libID, Int16
wVerMajor, Int16 wVerMinor, int LCID, SYSKIND syskind);
[ComImport,
GuidAttribute( "00020406-0000-0000-C000-000000000046" ),
InterfaceTypeAttribute( ComInterfaceType.InterfaceIsIUnknown ),
ComVisible( false )]
public interface UCOMICreateITypeLib
{
void CreateTypeInfo();
void SetName();
void SetVersion();
void SetGuid();
void SetDocString();
void SetHelpFileName();
void SetHelpContext();
void SetLcid();
void SetLibFlags();
void SaveAllChanges();
}
public static void RegisterTypeLibrary(string AssemblyPath, string tlbPath)
{
//Convert assembly to type library file
TypeLibConverter TLC = new TypeLibConverter();
Assembly asm = Assembly.LoadFile(AssemblyPath);
ConversionEventHandler eventHandler = new ConversionEventHandler();
UCOMICreateITypeLib typeLib =
(UCOMICreateITypeLib)TLC.ConvertAssemblyToTypeLib(asm, tlbPath, 0,
eventHandler);
typeLib.SaveAllChanges();
//Now register the type library
IntPtr obj = IntPtr.Zero;
LoadTypeLib(tlbPath, ref obj);
RegisterTypeLib(obj, tlbPath, "");
}
public static void UnRegisterTypeLibrary(string tlbPath)
{
//1. Call LoadTypeLib function to get ITypeLib interface pointer
//2. Call GetLibAttr method of ITypeLib interface to get pointer to Type
Library attributes
//3. Call UnRegisterTypeLib to unregister type library
IntPtr obj = IntPtr.Zero;
LoadTypeLib(tlbPath, ref obj);
System.Runtime.InteropServices.TYPELIBATTR TA = new TYPELIBATTR();
System.Runtime.InteropServices.UCOMITypeLib myLib =
(System.Runtime.InteropServices.UCOMITypeLib)Marshal.GetTypedObjectForIUnknown(obj, typeof(System.Runtime.InteropServices.UCOMITypeLib));
myLib.GetLibAttr(out obj);
TA = (TYPELIBATTR)Marshal.PtrToStructure(obj, typeof(TYPELIBATTR));
UnRegisterTypeLib(ref TA.guid, TA.wMajorVerNum, TA.wMinorVerNum, TA.lcid,
SYSKIND.SYS_WIN32);
}
}
[ComVisible(false)]
public class ConversionEventHandler : ITypeLibExporterNotifySink
{
public void ReportEvent( ExporterEventKind eventKind, int eventCode,
string eventMsg )
{
// Handle the warning event here.
}
public Object ResolveRef( Assembly asm )
{
// Resolve the reference here and return a correct type library.
return null;
}
}
}