Using A VB ActiveX DLL From Visual LISP
Using A VB ActiveX DLL From Visual LISP
ID: TS15992
Issue
How can I create a user-interface module in Visual Basic that can also be easily called from Visual LISP or VBA?
Solution
The simplest and most efficient way is to write an in-process Automation client of AutoCAD, such as a VB
ActiveX DLL. This DLL can then be loaded from Visual LISP, VBA, Java (via Automation) and ObjectARX. The
DLL may be an Automation client of AutoCAD, or any other Automation server, or of multiple servers.
Using Visual Basic 6:
1. Start Visual Basic.
2. Select "ActiveX DLL" in the New Project Wizard.
3. Change the project's "name" property to "MyProject".
4. There is a default class module in the project, change its "name" property
to "MyClass1".
5. Add a function or sub-routine to the class module such as:
9. Select File -> Make MyProject.dll. This creates the DLL and registers it with COM. (You'll need to ensure that
the DLL is registered on any other computers you want to run this on. That's usually handled by creating a setup
package for your DLL with Visual Basic.)
10. If you want to use the DLL from Visual LISP, simply create a function such as
the following, and load it into AutoCAD:
Sub MyVBAProject()
Dim oMyApp As New MyClass1
Dim vReturn As Variant
Set oMyApp = ThisDrawing.Application.GetInterfaceObject("MyProject.MyClass1")
'Call function w/three arguments
vReturn = oMyApp.MyFn(7, "Arbitrary string", 1.5)
'Display values returned by function
MsgBox "Integer: " & CStr(vReturn(0)) & vbCr _
& "String: " & vReturn(1) & vbCr _
& "Double: " & CStr(vReturn(2)), _
vbOKOnly, "Returned values"
End Sub
If you watch the vReturn variable after the call to MyFn, you will see that it contains the same values as seen in
AutoLISP.
Using Visual Studio .Net:
It is also possible to do this with a Visual Basic Class Library, with some minor changes.
1. Start Visual Studio .Net.
2. Select "VisualBasic Projects" and "Class Library" in the New Project Wizard.
3. Change the project's "name" property to "MyProject".
4. There is a default class module in the project, change its "name" property
to "MyClass1".
5. Add a function or sub-routine to the class module such as:
' Note: In VB.NET, the function now must return a type Object instead of
Variant.
Public Function MyFn(ByRef arg1 As Integer, ByRef arg2 As String, ByRef arg3 As
Double) As Object
Dim MyForm1 As New MyForm1()
'Populate textboxes with passed-in values
MyForm1.TextBox1.Text = arg1
MyForm1.TextBox2.Text = arg2
MyForm1.TextBox3.Text = arg3
MyForm1.ShowDialog()
End Class
6. Perform steps 6 through 8 above.
7. In your project's Property Pages, go to Configuration Properties -> Build.
Check the box labeled "Register for COM Interop". Click "OK".
If you want to use this .NET DLL from VBA, add a reference to the .tlb file.
(MyProject.tlb).