tags:

views:

57

answers:

2

A friend of mine is working on a legacy VB6 project. I haven't touched that language in ten years, so I'm pretty rusty. Anyway, is there any kind of reflection API for VB6? Specifically, he needs a way to iterate the properties (and types) of a user-created Class. (In other words, not an external COM object, but an internal "Class Module" as it's called.)

How can this be done?

+1  A: 

Is this the sort of thing you're looking for:

Visual Basic: Inspect COM Components Using the TypeLib Information Object Library

It's discussed on this thread here on SO: Self Inspection of VB6 UDTs

I've never tried this stuff myself.

Jay Riggs
The TypeLib Information Object Library works well for what it does. We use it to dynamically find and load plug-in classes that implement a common `IPlugin` interface when our application starts up. However, since the OP mentions private, internal classes, he's out of luck unless he makes them public and exposes them as COM objects (by using an ActiveX type project). The TypeLib library knows nothing about VB6 per se: it's designed for inspecting COM type libraries.
Mike Spross
I'm not so sure you can't use the TLI for this. Those private Classes and such are just part of the parent program itself, and show as such in the IDE's Object Browser (which uses the TLI itself). The question is whether the information is stored in the compiled program and available at runtime from the EXE.
Bob Riemersma
Alas - it appears a standard EXE does not save any type info. Darn.
Bob Riemersma
@Bob: The Object Browser can be misleading. It uses TLI most of the time, but if have Project ABC open in the IDE and view ABC's contents in the Object Browser, it will display all the private classes and modules, in addition to the public stuff. Also, a standard EXE won't include any type library information at all. Type libraries are a COM thing, so they only apply to the ActiveX type projects in VB6. A more precise way to see exactly what's accessible via TLI for a given VB6 component is to use OLE View rather than the Object Browser, but then you have to know how to read MIDL.
Mike Spross
The reason being that OLE View will show you exactly what's being exposed to COM, because it reads the component's type library directly.
Mike Spross
+2  A: 

Jay's answer is the way to go if your project is an ActiveX (ActiveX EXE, DLL, or OCX, as opposed to Standard EXE), and if the classes are public.

However, you mentioned that your friend wants to do this with "internal class modules". I'm not sure if you are referring to private .cls files (classes), or .bas files (modules), but either way, you can only use the TypeLib Information Object Library to reflect on public classes, user-defined types, constants, or enumerations.

You cannot use the library to reflect modules, private classes, or anything else that is declared private.

As a general rule of thumb, you can only use reflection on the things that you can see in the Object Browser when viewing your project's contents from another project. That is, if you compile your project, create a new project, add the first project as a reference, and then view the first project's contents in the Object Browser, anything you can see in the Object Browser can be accessed via the TypeLib Information Object Library. If something is not listed in the Object Browser, then you won't be able to use the TypeLib Information Object Library to reflect it.

VB6 doesn't have any built-in support for run-time reflection or introspection. Using the TypeLib Information Object Library for reflection works for ActiveX VB6 projects because ActiveX projects are compiled in COM components with embedded type libraries, but as mentioned you can only access the data types that are publicly exposed in the compiled component's type library. Using the Object Browser is a quick way to determine what is in the type library because the Object Browser actually inspects the component's type library to populate what you see in the Object Browser, as long as you are viewing the component's contents from a separate project (if you view the component from its own VB6 project, it will display public and private data structures, i.e. everything that is visible in the IDE).

Mike Spross
This is exactly what I suspected, being an old COM guy myself, and quite familiar with type libraries, IDispatch, and the like. Unfortunately, my friend does not have the luxury of separating his classes into another project, due to the astounding way this ancient legacy app was written. (The state of the UI and the "objects" are tightly intertwined.)
Gregory Higley
I feel bad for your friend. That system sounds painful.
Mike Spross