GetObject and VB6 ActiveX exe

MarkJ picture MarkJ · May 27, 2009 · Viewed 8.6k times · Source

The VB6 help on GetObject says "You can't use GetObject to obtain a reference to a class created with Visual Basic" (the very last sentence!). My VB6 GUI exposes objects as an ActiveX exe, for other components to manipulate. I want the other components to connect to the GUI that's already running, rather than start a new instance of the exe. I've found using GetObject does work, if you use this syntax:

Set myobj = GetObject("", "ProjectName.ClassName")

It worries me that the help says this shouldn't work, although I have done quite a bit of testing and haven't found any problems so far. Any COM experts out there who can tell me whether I going to run into problems down the line? And would I be OK with CreateObject anyway?

The ActiveX exe settings are: thread pool with only one thread. The class has MultiUse instancing. It's possible these settings are enough to prevent CreateObject starting a new instance of the exe anyway. Is that correct?

Answer

Mike Spross picture Mike Spross · May 27, 2009

The documentation is confusing, but correct. The MSDN page you reference helps to explain why your GetObject call doesn't throw an error:

If pathname [the first argument] is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

It's subtle, but the implication is that

GetObject "", "ProjectName.ClassName

is actually equivalent to

CreateObject "ProjectName.ClassName"

That is to say, passing an empty string to the first parameter of GetObject makes it operate exactly like CreateObject, which means it will create a new instance of the class, rather than returning a reference to an already-running instance.

Going back to the MSDN excerpt, it mentions that omitting the first argument to GetObject altogether will cause GetObject to return a reference to an already-running instance, if one exists. Such a call would look like this:

GetObject , "ProjectName.ClassName" 'Note nothing at all is passed for the first argument'

However, if you try to do this, you will immediately get a run-time error. This is the use-case that the documentation is referring to when it says that GetObject doesn't work with classes created with VB6.

The reason this doesn't work is due to how GetObject performs its magic. When the first parameter is omitted, it tries to return an existing object instance by consulting the Running Object Table (ROT), a machine-wide lookup table that contains running COM objects. The problem is that objects have to be explicitly registered in the Running Object Table by the process that creates them in order to be accessible to other processes - the VB6 runtime doesn't register ActiveX EXE classes in the ROT, so GetObject has no way to retrieve a reference to an already-running instance.