Tushar Suradkar [ www.geocities.com/cadgurucool ]


Create a COM component using VB 6 and access it in Visual LISP



It is assumed that you have first completed the Visual LISP tutorial.

This and many other tutorials are available for FREE at :

www.geocities.com/cadgurucool ( Click Tutorials 4U )


All VB6 and Visual LISP source code is included with the zip file
that contained this tutorial and is also available at the above site.

What is COM ?

COM is the backbone of ActiveX which in turn is a technology
that enables software code developed in different languages to share data
and work cooperatively in a networked environment.

In this tutorial you will learn,
how functions written in VB can be called in Visual LISP,
which are two distinct languages.

We will do this by creating a COM Component in VB.

What is COM component?

A COM Component is a piece of code code bundled as a exe or dll file.

What is a dll file?

A file with extension dll is called a dll file.

DLL stands for Dynamic Link Library.

It is a collection of programs (functions),
that can be called from another program,
written in another language.

If a function is written in the same program as its calling function,
then it is called statically linked, because both the calling and the called function,
are compiled together. This creates a standalone exe.

Whereas, in a dll, the called functions are in a seperate program
and compiled seperately.

The calling function links to the functions in a library dynamically,
hence the name dll or dynamic link library.

In this tutorial, we write a library of functions in VB and compile it as a dll.

We will then write a program in Visual LISP,
which calles the functions in the VB dll,
using the new ActiveX capabilities in Visual LISP.

Begin creating the dll

Fire up VB 6 and select a ActiveX DLL project as shown in figure.

Click Open.

A class module named Class1 is added to the current project.


Write the functions

Option Explicit

Public Function CalcHypo(ByVal cSideA As Double, _ 
		  	 ByVal cSideB As Double) As Double
    CalcHypo = Sqr((cSideA * cSideA) + (cSideB * cSideB))
End Function

Public Function ChangeCase(ByVal strString As String, _
			   ByVal opMode As Integer) As String
  ChangeCase = StrConv(strString, opMode)
End Function
 
Add two functions to the module:

The first one CalcHypo takes two doubles and also returns a double.

The two numbers are the sides of a right-triangle and the return value is the Hypotenuse.

One could easily write the CalcHypo function right inside AutoLISP.
But, the purpose here is to demostrate the the portability of functions
from one programming paradigm to another.

Actually, more complex tasks such as connecting to a database,
passing SQL queries which are also beneficial to a AutoLISP program,
is more easily handled in VB than in AutoLISP.

So VB could be used to bundle all such functionality into one COM Component,
and accessed by Visual LISP just by passing a SQL string to one of the functions.

This is demonstrated in the second function that we add to the class module,

The ChangeCase function, which, again, could well be written in Visual LISP
using intrinsic functions like substr, strcase and strlen, etc.
can easily be implemented using just one VB function StrConv

The ChangeCase function takes two arguments :
a string and an integer and returns a string.

The function employs the VB StrConv built-in function
to change the case of the given string in three possible ways

Visual LISP 1 => returns VISUAL LISP (ie . upper case)

Visual LISP 2 => returns visual lisp (ie . lower case)

Visual LISP 3 => returns Visual Lisp (ie . proper case)

Save the Project

Change the project name to General as shown in figure.

Also, save the project as General.vbp


Save the class

Change the class name to Utilties as shown in figure.

Also, save the class as Utilities.cls


Register the DLL

Select File > Make General.dll

After compiling, we will register the dll.

To do this, start Windows Explorer and locate General.dll

Click Start > Run as shown in figure.

Type regsvr32 and drag and drop the General.dll file in front of regsvr32.

The complete file name appears in front of regsvr32. (Win2000 only)

Click OK

A message saying "dll registration succeeded" appears.

If you are using Windows 98, you will have to type the complete path and filename
for the General.dll and then click OK.

regsvr32 stands for register server.
The server here is our General.dll file which, as the name implies
serves the two functions CalcHypo and ChangeCase from its library
to any calling function in any language with ActiveX capabilities, for eg. Visual LISP.

An even easier method for registering a server for both Win 2000 and Win 98 is as detailed in next step.


Register with right-click

Copy and paste the follwing lines of code into notepad :

REGEDIT4
[HKEY_CLASSES_ROOT\dllfile\Shell\Register\command]
@="Regsvr32 /s \"%1\""
[HKEY_CLASSES_ROOT\dllfile\Shell\Unregister\command]
@="Regsvr32 /s /u \"%1\""
[HKEY_CLASSES_ROOT\.ocx]
@="ocxfile"
[HKEY_CLASSES_ROOT\ocxfile]
@="OCX"
[HKEY_CLASSES_ROOT\ocxfile\Shell\Register\command]
@="Regsvr32 /s \"%1\""
[HKEY_CLASSES_ROOT\ocxfile\Shell\Unregister\command]
@="Regsvr32 /s /u \"%1\""

Save the file as RegUnreg.reg ie. filename is RegUnreg and the extension is reg

Files with the reg extension contain information
that could go into the registry when written with the proper syntax.

After saving, exit notepad and double click the regunreg.reg file in Windows Explorer.

You are asked if you surely want to add the information in the file to the registry.

Click Yes. This whole thing is a one time procedure.

From here onwards, whenever, you right click on a dll or ocx file,
two options, register and unregister are available in the menu as shown in figure.

ClassID and ProgID

The registration process creates two things for the dll ClassID and ProgID of which,

the ProgID is required by Visual LISP.

The progID takes the form Project.Class and in our case, it will be General.Utilities

Click the Start button in and seelct Run.

Type regedit and click .

This starts the registry editor, which closely resembles the Windows Explorer.

Select Edit > Find or Ctrl + F and search for General.Utitlities

The registry took quite some time on my PII - 350 Mhz with 64 MB RAM machine
and finally landed me to General.Utilities as shown in figure below.



On expanding, you will see the Clsid subkey.

Click the subkey to read the class id in the right panel as shown in figure above.

The class id is a unique number assigned to a registered class and never repeats on a given computer.

A small Visual LISP function will also do the trick.



Using the function also yeilds the same results :



Just before using this function, type (vl-load-com) at the command prompt.
This will load the Visual LISP COM environment and then the vl-registry-read function will work properly.

The meaty part

Fire up AutoCAD and type vlide at the Command: prompt or press Alt + F11

The Visual LISP Integrated Development Environment will appear.

Click the New Icon to start a new file.

Save the File as a LISP Source file.



Define a function c:gu() where the name gu has no meaning.

We are interested in knowing how to call the functions in General.dll file from Visual LISP.

Next, the vlax-create-object function "creates a new instance of an application object"
is what the Visual LISP online help has to say about it.

So the variable genu now is an object variable.

Creating an object which is an instance of a class are the building blocks of OOPS.
Don't confuse OOPS with the AutoCAD OOPS command.
This is a completely different animal.

Further, sString is a string variable that holds "Visual LISP" whose case we want to change.

vlax-invoke-method calls the specified ActiveX method - says the online help again.

Supply to it the object variable genu and
then the method (the two functions we defined using VB).
followed by the required arguments and you are done.

Try changing the last argument to 2 or 3
Finally alert the answer.



Similarly, use the calcHypo function.
where, the variable Answer is used again to display a real number.
This is called variable recycling.

Visual LISP has an upper hand when it comes to variable decalration.
where it is not at all required.

This wouldn't have been possible with even the prodigious VB.

To finish it off, use the vlax-release-object to free up memory.

The VB equivalent of this is set objSomeObject = Nothing

AutoCAD online help gives a very narrow deifnition of
vlax-release-object - Releases a drawing object.

Read another tutorial on creating a dll file using Visual C++
and accessing it from Visual LISP at :

www.geocities.com/cadgurucool