|
by Derrald Farnsworth-Livingston Technical Setup Stuff In this document we will discuss how to write functions in ActiveX Dlls and then call those same functions from ASP Scripts. Although writing dlls can be accomplished using a variety of languages we will focus on Visual Basic (version 6.0). We shall begin by creating the DLL in Visual Basic. As an example we will create a simple function that adds two numbers together. The setup in Visual Basic is very important. When we first create the project we are asked to choose the type of program we are creating. We want to create an ActiveX DLL. Once we choose this we are given a blank project. You will want to change the reference to this project to something a little more understandable than Project1 or Project2 or Project3, well you get the picture. From the menu choose Project and then Project1 Properties. (Although it may be Project2 properties, or Project3 properties, etc.) Under the ProjectName box enter whatever name you will wish to reference this object by. In our case we shall name it "AdditionHelper". We now want to change the names or references on the class object. Click on the Class1 module. In the properties box, normally on the right hand side of the screen, change the (name) to something pertinant. We shall name this "Addme". We have now set up the foundation for the DLL. Code, Code, Code We will now begin to code the DLL. The first thing that any programmer needs to do is declare the variables. We will place the following lines into our Addme class:
Public FirstNumber As Variant Public SecondNumber As Variant We will now create the function that adds these two variables. We will call this function AdditionFunction. AdditionFunction will receive the two variables which it will add then pass back the result. Here is the code:
Function AdditionFunction(FirstNumber, SecondNumber)
AdditionFunction = FirstNumber + SecondNumber
End Function
We need now to compile the DLL. From the File menu choose "Make AdditionHelper.dll", this will compile the dll into a callable format. Save the DLL in a directory which you have choosen. If you have already saved the project or compiled the DLL it will save it by default in that directory. That's it, we have done all that is necessary to complete our first ActiveX DLL. Now we need to set up our webpage to send and receive the information to the DLL and display the results. Registering to Function, Please have your DLL ready We will need to be running this on an IIS Server version 4.0 or greater. Windows NT 4.0, 2000, or XP will work to accomplish this task. First, copy the AdditionHelper.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. Type the following: regsvr32 additionhelper.dll This will register the DLL for use by the server. If this step is not complete your webpages will not know where to call the functions in the DLL. We must now create the webpage which will use our function. Bring up notepad, InterDev, or your webpage creation tool of choice. (I will be using notepad since it is the most basic).
<HTML
<TITLE>My Cool DLL </TITLE>
<BODY>
<%
Dim finalanswer
Dim addthis
Set addthis=Server.CreateObject("additionhelper.addme")
This piece of code will create the object which will allow us to interact with our dll "AdditionHelper". It references the class object "Addme".
finalanswer=addthis.additionfunction(3,4) Our variable, finalanswer will be equal to the results of the function "additionfunction" in the class "addme" in the dll "additionhelper". We are passing the numbers 3 and 4 to be added.
Response.write finalanswer %> <BODY> <HTML> We are now displaying the result, 7. You have now completed a simple ActiveX DLL which is called from a webpage. Obviously, these example functions could have just as easily been called from within the ASP Script itself. Over time I have been asked the following question: 1. Why don't you just write your functions into the programs you are creating. (Or better yet) Why don't you make include files with these functions? This is a good question. There are many advatages in using and writing ActiveX DLLs and using them, but one must know where to draw the line. This can only be determined on a programmer by programmer basis. However, here are some good guidelines to follow if you are trying to decide the best place for your code: 1. Will you want to use this function on other websites or in other programs? For example, Are you writing a program that will be used on one website, but you are also writing a Visual Basic program which will need the same commands. If so, you can write one DLL and save yourself some time. 2. Will this program be distributed to numerous people? 3. Will this program need to be integrated into various programming languages? 4. Are the functions complex enough that performance is a variable?
If the answer is "yes" to any of these, your best bet is to write an ActiveX component and then call the functions that way. Otherwise you will be ok using include files.
If you found this information useful please contact me at webmaster@tutorial-web.com
|
![]()
| |||||||||||