Importing COM Components
Importing ActiveX controls turns out to be fairly straightforward. Many of the COM components that companies develop are not ActiveX controls, however; they are standard COM dynamic link library (DLL) files. To see how to use these with .NET, you return to VB6 and create a COM business object that will act exactly as the component from the previous section did.
The first step is to create a new ActiveX DLL project. This is how
VB6 creates standard COM DLLs. Name the class
ComCalc and name the project
ComCalculator. Save the file and project. Copy the
methods from Example 22-4 into the code window.
Example 22-4. Implementing the methods for ComCalc
Public Function _
Add(left As Double, right As Double) _
As Double
Add = left + right
End Function
Public Function _
Subtract(left As Double, right As Double) _
As Double
Subtract = left - right
End Function
Public Function _
Multiply(left As Double, right As Double) _
As Double
Multiply = left * right
End Function
Public Function _
Divide(left As Double, right As Double) _
As Double
Divide = left / right
End FunctionBuild the DLL by using the menu sequence File → Make
ComCalculator.dll. You can test this by returning to your earlier
test program and removing the Calculator control
from the form. Add the new DLL by opening the project reference
window and navigating to the ComCalculator, as
shown in Figure 22-12.
Figure 22-12. Adding a reference to ComCalculator.dll
Coding the COMTestForm program
The code to exercise ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access