How to Create and Use ActiveX DLLs in Visual Studio 2010
ActiveX DLLs are dynamic-link libraries that expose their functionality through COM interfaces. They can be used by other applications or components that support COM, such as Visual Basic, C#, or ASP.NET. In this article, we will show you how to create and use ActiveX DLLs in Visual Studio 2010.
Creating an ActiveX DLL
To create an ActiveX DLL in Visual Studio 2010, follow these steps:
Open Visual Studio 2010 and create a new project of type Class Library.
Name the project and solution as you like, for example, MyActiveXDLL.
In the Solution Explorer, right-click on the project name and select Properties.
In the Application tab, change the Assembly name and Default namespace to match the project name, for example, MyActiveXDLL.
In the Compile tab, click on the Advanced Compile Options button.
In the Advanced Compiler Settings dialog box, check the Register for COM interop option and click OK.
In the Solution Explorer, right-click on the Class1.vb file and rename it to something meaningful, for example, MyActiveXClass.vb.
Double-click on the MyActiveXClass.vb file to open it in the code editor.
In the code editor, add the following attributes above the class declaration:
The ComClass attribute indicates that this class is a COM class and specifies its class ID, interface ID, and events ID. The Guid attribute assigns a unique identifier to this class. The ProgId attribute defines a user-friendly name for this class that can be used by other applications to create an instance of this class.
In the code editor, add some methods or properties to your class that you want to expose to other applications. For example:
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
Public ReadOnly Property Version As String
The methods or properties that you want to expose must be public and have simple data types as parameters or return values. You can also use arrays or structures as long as they are defined in your project.
Build your project by pressing F6 or selecting Build Solution from the Build menu. This will compile your code and register your ActiveX DLL in the system registry.
Using an ActiveX DLL
To use an ActiveX DLL in another application or component that supports COM, follow these steps:
Open or create a new project of your choice in Visual Studio 2010.
In the Solution Explorer, right-click on the References node and select Add Reference.
In the Add Reference dialog box, click on the COM tab and find your ActiveX DLL in the list. For example, MyActiveXDLL.MyActiveXClass. Select it and click OK.
In the code editor, add a statement to import your ActiveX DLL namespace at the top of your code file. For example:
In the code editor, declare a variable of your ActiveX class type and create an instance of it using the New keyword. For example:
Dim obj As New MyActiveXClass
In the code editor, call the methods or properties of your ActiveX class using the dot notation. For example: