Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Dec 25, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Pass the hidden Global object to an ActiveX DLL

An ActiveX DLL doesn't have direct access to the environment of the calling EXE. For example, the App and Printer objects in the DLL don't correspond to the objects with the same name in the main application, therefore if you want to print something from the DLL using the same settings as the main app you must explicitly pass the Printer object from the main application. A similar reasoning applies to other global VB objects, such as the Forms collection.

Instead of explicitly passing the Printer, Forms, and App objects from the main app to the DLL, you can just pass it the hidden Global VB object. This object is actually the parent of the above-mentioned objects (and others as well, such as the Clipboard and the Screen objects). This code invokes a method in the DLL that receives the reference to the global object of the main app:

' in the main (client) application
Dim obj As New MyDll.Functions
' note that we need a pair of square brackets
obj.SetGlobal [Global]
Here's is the code in the Functions class module of the MyDLL component that receives the reference to the Global object. Note that the argument can't be declared using a specific object type, because this object is private to the VB library and Visual Basic won't let you pass it across project boundaries. Interestingly, the global object is a vtable-only object, and therefore doesn't support IDispatch, so the receiving parameter must be declared using IUnknown:

' in the Functions module of the MyDLL ActiveX DLL project
Dim glb As VB.Global

Sub SetGlobal(glbIU As IUnknown)
    ' get a reference to the client's global object
    Set glb = glbIU
End Sub
Let's see now how you can enumerate all the forms in the main app from within the DLL:

' inside the MyDLL ActiveX DLL project
Dim f As Form
For Each f In glb.Forms
    List1.AddItem f.Name

Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date