WebDevelopersJournal.comTips on Web Page Design, HTML and Graphics
SITE SEARCH
Newsletters
Java/Open Source Daily



Jobs at webdeveloper.com

Resources By Subject
Technical
Graphical
Authoring
Business
WDJ resources
Archive

internet.com

internet.commerce
  • Partner With Us
















Developer Channel


Find a web host with:
CGI Access DB Support Telnet Access
NT Servers UNIX Servers



Semi-automatic?

JavaScript
JavaScript Helper:
Meet Paige Turner, the least geeky geek we've ever come across.

Variables and Operators Explained:
First of a three part guide to JavaScript basics.

Controlling Forms:
Enhance your HTML forms with a touch of JS.

DHTML:
Forget how it works, let's see some in action!


Create Your Own ActiveX Components For ASP

by David M. Williams

Server Side Enhancements

There's been a lot of noise about ActiveX controls - visual controls - that run within the browser on the client side. But ActiveX components on the server-side have even more to offer. ASP is great, and the ability to effectively roll-your-own ActiveX components is a highly flexible and extensible benefit. It really permits unlimited functionality in an ASP page. Suddenly, full control and reporting of your system - even your network - is at your hands.
February 14, 2000

Recently I wrote some code for a local ISP. They use SQL Server for administration and billing and a Unix machine for user accounts and subscriber Web serving. To free the system administrators from a lot of manual and repetitive work, they wanted a Web page that would let the help-desk staff create accounts. This would be on their internal administrative Web pages, on a Windows NT server running Internet Information Server.

Well - what to do? In this case, it was all resolved nicely and easily. I designed a simple client-server system spread over the NT and Unix machines. I made an ActiveX DLL, which sat on the NT server - it provided methods to call a custom server running on the Unix machine, passing a username and password. Winsock was used on the NT side, and Berkeley sockets on the Unix side. The DLL was designed to be invoked from within an ASP page when staff elected to create accounts.

I won't go into either socket or Unix code here, because NT and Unix collaboration is a subject in itself. Instead, let's look at the fundamentals of making an ActiveX DLL using Visual Basic 5 or 6.

Creating Your ActiveX DLL

Start Visual Basic. From the File menu, select New Project. Choose ActiveX DLL from the dialog box. You will get a blank window with a new class module created for you. An ActiveX DLL has no GUI - nor does any server-side component - and the class module is where you implement its functionality.

As usual, Visual Basic names your project 'Project1', and your class module is called 'Class1'. You can change these, of course, and you will use these names when instantiating your object. For example, once the DLL is created, you can make an instance of it in an ASP page with code like this -

set myObject = Server.CreateObject ("Project1.Class1")

and voila, you then have an object that you can use to invoke the functions you are going to define, as follows:

First, use the Add-Ins menu to add the VB Class Builder utility to your IDE. Once you have marked this as 'load on startup' and 'loaded' it will then always be available from the Add-Ins menu.

Call the Class Builder utility by selecting it from the Add-Ins menu. The first time you run it, VB will tell you your class module was not created by the utility. We know that, because we just made it from the New Project dialog. Ignore the message and click OK.

The Class Builder is a nice little tool to quickly add Properties, Methods and Events to your class. These are the things you will need if anyone is ever to interact with your DLL. Properties are essentially the values and variables that your class exposes to the outside world. And remember - as everyone who has taken Visual Basic exam 70-176 had drummed into them - if you want a property to be read only, just define a 'Get' method for it!

Methods are the functional bits of code in your DLL that can be directly invoked from an external environment. These are the parts of the DLL that 'do things'. Actually, setting a property can cause things to happen too, because as hinted at above, a property is really a combination of a 'Get' and 'Let' combination of methods. Yes - a property need not be just a simple variable, but could well be a calculated field. Finally, Events are methods too, but they execute in response to a trigger - perhaps the setting of a value, the execution of a timer - some non-asynchronous thing.

Starting Point

Once you've finished with the Class Builder (you can return to use it many times throughout the development of your DLL), use CTRL+S to save your modifications and update your project. Automatically, VB adds 'real' code to your class module - though you will need to flesh out the implementations. What you have are 'stubs' to work around.

Here's a sample class module for a very simple class -

Option Explicit

' Local variables to hold property values
Private mvarValue As Integer

Public Function AddOne As Integer
AddOne = mvarValue + 1
End Function

Public Property Let Value(ByVal vData As Integer)
mvarValue = vData
End Property

Public Property Get Value() As Integer
Value = mvarValue
End Property

Private Sub Class_Initialize()
mvarValue = 0
End Sub

This class is trivial, and has one property - Value - and a method - AddOne - which simply returns the value of Value plus one.

The next thing you really should do is use the Project menu, then Properties, to adjust some default settings for your DLL. On the Make tab page, elect to increment the version number automatically. On the Component tab page, change to Binary Compatibility. Incrementing the version number will help track copies of your DLL after you begin release them. Changing the compatibility will save registry space. Each time you re-compile, VB will determine if the newly compiled DLL is backwards compatible with the previous version and if so, it will be registered with the same class ID. Otherwise, or when using version compatibility, a new registry entry would be made.

Finally, use the File menu to make your DLL. This DLL will be a little dull, but the processes described are the same no matter how complex your next DLL might be!

You can now make an ASP page like this -

<html><head><title>DLL test</title></head>
<body>
<%
dim myObject
dim intValue

set myObject = Server.CreateObject ("Project1.Class1")
intValue = 5
%>
<h1>Our number is <%=intValue%></h1>
<%
myObject.Value = intValue
%>
<p><h1>Adding one gives <%=myObject.AddOne%></h1>
<p><h1>Note the original value is still <%=myObject.Value%></h1>
<%
set myObject = Nothing ' Remember to tidy up
%>
</body></html>

One absolutely final comment must be made - as mentioned, VB adds registry entries for your DLL when you compile it. What happens if you want to use the component on a different PC? Simply copy the DLL across, and then run regsvr32 on it (use the DLL name as a command-line parameter). Regsvr32 should be found in the Windows (or WinNT) system directory. If you use a deployment/packaging program to install your component, it should handle the registry itself.

And that's it! We've made an ActiveX DLL, and used it from within an ASP page. Suddenly all kinds of worlds of possibilities become open to you. There is no limit to what you can do - the full power of any modern Windows programming language can be encapsulated into an ActiveX component like this, and used in your server-side code.

More techie stuff

David Williams is a computer consultant in Newcastle, Australia. Recent projects include an ASP-based task logging system for a University help desk environment, and 'hack-proofing' an evaluation version of a package to manage mobile phone shops.
Suits PonytailsPropheadsContact WDJDiscussWeb AudioSearch


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers