Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

Regiser Com Server without Regasm.exe

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric Carlson - 04 Dec 2004 23:39 GMT
How can you register a c#.net assembly for com interop without regasm.exe?

I successfully use this:  "regasm /tlb"

...but would like to do the same through code.  The
IRegistrationServices.RegisterAssemby will not generate and register the type
library , since it only gives a flag for the Codebase option.

How can I do this through code?
Signature

-----------------
Eric Carlson

Phil Wilson - 06 Dec 2004 19:41 GMT
I believe this is what the TypeLibConverter class is for.
Signature

Phil Wilson [MVP Windows Installer]
----

> How can you register a c#.net assembly for com interop without regasm.exe?
>
[quoted text clipped - 5 lines]
>
> How can I do this through code?
Eric Carlson - 08 Dec 2004 15:17 GMT
I looked into the TypeLibConverter class, but it does not provide any
registration functions, only ways to generate the type library.  I still need
to generate this type library.

> 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?
Eric Carlson - 13 Dec 2004 18:07 GMT
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;
        }    
    }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.