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) applicationDim obj As New MyDll.Functions' note that we need a pair of square bracketsobj.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 projectDim glb As VB.GlobalSub SetGlobal(glbIU As IUnknown) ' get a reference to the client's global object Set glb = glbIUEnd 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 projectDim f As FormFor Each f In glb.Forms List1.AddItem f.NameNext