There are two kinds of programming. Programming with reusable code and non reusable code. To achieve reusable code you can write functions and procedures. In ASP scripts you can write functions and procedures and call them as often you want. But the main drawback with ASP script is you cannot hide your code from the buyer. This is applicable when you are writing some commercial applications and sell it. Your buyer can tweak the code and even resell it as it is his own. When you want to avoid this, here comes the concept of VB COM.
ActiveX DLLs are components where you can hide your code, call it in your ASP and distribute with your application. In this article we shall discuss how to write functions in ActiveX Dlls and then call those same functions from ASP Scripts. We can write Dlls with C++, VC++, Delphi and Visual Basic. Here we can discuss How to with VB?
DLL’s with VB
We shall begin creating the DLL in Visual Basic. As an example we will create a simple calculator that can do addition, division, subtraction and multiplication with two numbers together. When we create the project we are asked to choose the type of program we are creating.
Here we should select ActiveX DLL.
1. Let us change the project name first. Go to the menu, choose Project and then Project1 Properties. Under the Project Name box enter the name you want to call this object we are creating. I have changed as Simple Calculator as the Project name.
2. Leave the Threading model as “Apartment Threaded”
3. Adding the reference Library.
Now let us add the reference Library. Open the Project -> Preferences, then select Active server Pages Object Library from the List of References appearing.
4. Change the names of the Class1 module. In the properties box, normally on the right hand side of the screen, change the (name) to something pertinent. We shall name this “AddNumbers”. We have now set up the foundation for the DLL.
Likewise let us create the other classes with the names “MulNumbers, SubNumbers and DivNumbers”.
Now let us to code the SimpleCalculator DLL. The first thing that we need to do is declaring the variables. We will place the following lines into our AddNumbers class:
Public Number1 As Variant Public Number2 As Variant
We will now create the function that adds these two variables. We will call this function Add. The function Add will get the two variables which will be added and then passed back as the addition result.
Function Add(Number1, Number2) Add = Number1 + Number2 End Function
Likewise let us create the functions Subtract, Multiple and Divide and the corresponding code lines. You know how to do that.
Compiling the code
Compiling the code will produce the DLL. From the File menu choose “Make SimpleCalculator.dll”, this will produce the SimpleCalculator.dll. Save the DLL in a directory, which you have chosen. If you have already saved the project or compiled the DLL it will save it by default in that directory.
Now we have created the created our first ActiveX DLL. Now we need to set up our web page to send and receive the information to the DLL and display the results.
Registering the DLL
We will need to be running this on an IIS Server version 4.0 or later version. Means we need the platforms Windows NT 4.0, 2000, or XP First, copy the SimpleCalculator.dll to the server where the website will reside. Next, open a MS-DOS command line window on the server. When the command line window appears change the directory where you saved the dll. In my server I have saved the file under D:/dllDemo. Move to that directory now, then type the following
This will register the DLL for use of the server. What happens is, the dll will be registered in your system Registry and your ASP script, which is running on the same server, can call that. If you are courageous enough to open and check your registry you will be able to find it.
Creating the webpage
Open your HTML editor and type the following lines.
<HTML <TITLE>Simple Calculator Test</TITLE> <BODY> <% Dim AddResult,SubResult,MulResult,DivResult Dim AddNum,SubNum,MulNum,DivNum
This line of code will create the object, which will allow us to interact with our dll “SimpleCalculator”. It references the class object “AddNumbers”.
Our variable, final answer will be equal to the results of the function “additionfunction” in the class “AddNumbers” in the dll “SimpleCalcuator”. We are passing the numbers 140774 and 51174 to be added.
Like wise we can add the other lines of code,
Set SubNum = Server.CreateObject("SimpleCalculator.SubNumbers") Set MulNum = Server.CreateObject("SimpleCalculator.MulNumbers") Set DivNum = Server.CreateObject("SimpleCalculator.DivNumbers")
SubResult = SubNum.Add(140774,51174) MulResult = DivNum.Add(140774,51174) DivResult = MulNum.Add(140774,51174) Response.write AddResult & “<br>” Response.write SubResult & “<br>” Response.write MulResult & “<br>” Response.write DivResult %> <BODY> <HTML>
Now you can under stand the syntax of calling a dlls as
Set objName = Server.CreateObject("ProjectName.ClassModuleName" )
Call the function name by passing the necessary variables.
OK. You have done it, Congrats. Now you may think why you need a dll and hence the object in your coding life? Let us see as a conclusion.
- You can reuse your code, as you have created your code as an Object.
- Its fast in execution, as its already compiled.
- You can hide your code, when you create code as a commercial product.
- You can always use it in your other programs and applications in later developments.
- Finally, It’s challenging and interesting. Huh?