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?
VB6 ActiveX EXE Components provide reusable code in the form of objects. A VB6 application that uses a component's code, by creating objects and calling their properties and methods, is referred to as a client. And the DLL that exposes those components is referred as the server.
Remarks. Use the GetObject function to access an ActiveX object from a file and assign the object to an object variable. Use the Set statement to assign the object returned by GetObject to the object variable.
The ActiveX EXE/DLL is normally used when you need to build a component that is separate from the main program. The concept is based on COM model. ActiveX DLL/EXE allows multiple applications to share the same code. This allows for scalability of programs, and saves time because you only need to write the code once.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With