Pass the hidden Global object to an ActiveX DLL

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) 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

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as