2

I have a com dll written in vb6. I need to write c# code that wil get me a list of all the classes within it.

My Objective is to get the classes and generate the classes in .net with all the properties and create a mapping class.

I just need to get a list of classes from the dll.

Summary: How can I get a list of all the classes in my com dll?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
David
  • 5,403
  • 15
  • 42
  • 72
  • If you for some reason can't use out-of-the-box functionality from VS, this: http://www.codeproject.com/Articles/523417/Reflection-with-IDispatch-based-COM-objects and this: https://msdn.microsoft.com/en-us/magazine/dd347981.aspx will be helpful. – Dennis Apr 15 '15 at 08:13

2 Answers2

2

Using reflection to walk the Interop for your COM DLL will give you classes and interfaces that a vaguely analogous to COM CoClasses and interfaces.

If quick and easy is what you're after, add your COM DLL as a reference in Visual Studio and use the code below to walk through the Interop assembly looking for classes. If it's a VB6 COM DLL everything is going to be IDispatch anyway, but you could optionally filter on objects with a CoClass, Guid or InterfaceType attributes on the types identified below (you didn't specify whether you were after classes or CoClasses).

var classes = Assembly.GetAssembly(typeof(MyComDLL.RandomTypeFromWithin))
    .GetTypes()
    .Where(type => type.IsClass);

If you're trying to really do this properly, the articles Dennis linked to in his comment are better, as is p/Invoke'ing LoadTypeLib and walking ITypeLib (the latter option being non-trivial).

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
ScheuNZ
  • 911
  • 8
  • 19
2

I would suggest using TLBINF32.dll. It is a COM component that ships with most versions of Windows. Make sure it is registered using regsvr32.exe from an elevated command prompt (along with your COM dll). Reference it in your .NET app (compiled to x86) and use the TLIApplication.TypeLibInfoFromFile method. From there you can traverse the type library of the COM component and get the classes, methods, interfaces, and properties exposed.

We use this library for various utilities.

Craig Johnson
  • 744
  • 4
  • 8
  • Seems this library is no longer available in Windows 10. Are there any other ways to implement such functionality; for instance by using the Windows API? – Matze Jul 07 '16 at 13:16
  • The latest version works with Windows 10. We use it as part of a build process. I don't know what licensing restrictions there might be but as far as just raw functionality it works fine. – Craig Johnson Jul 07 '16 at 17:18