[dba-VB] RE: Web Activex DLL's

Drew Wutka DWUTKA at marlow.com
Mon Mar 3 11:23:22 CST 2003


Hadyn, I am cross posting this to the VB list, because there's going to be
some good info in here.

First of all, to create a .dll in VB, you are using Class modules.  I have
an example here, which shows how to use Classes and Collections within
Access 97 (I actually wrote the code in VB and copied it to Access).

http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=acc&Number=216645
&page=&view=&sb=&o=&vc=1#Post216645

Let me know if you want specific code examples.  I am just going to give
some of the in's and out's with this email/post.

First of all, when you create a .dll in VB, you'll need to 'register' it on
your IIS server.  Open VB, and create a new Advanced Project.  (Should be
the last type of project.).  Leave the form as Form1, and add a commondialog
control to the form.  Then put this code into a module:

Public Declare Function DLLSelfRegister Lib "vb6stkit.dll" (ByVal lpDllName
As String) As Integer
Sub Main()
Form1.CommonDialog1.Filter = "Dynamic Link Libraries | *.dll"
Form1.CommonDialog1.DialogTitle = "What Dynamic Link Library do you want to
register?"
Form1.CommonDialog1.ShowOpen
Dim dwReturn As Long
dwReturn = DLLSelfRegister(Form1.CommonDialog1.FileName)
If dwReturn = 0 Then
    MsgBox ".dll has been registered", vbOKOnly + vbInformation, "Success"
Else
    MsgBox "System Failed to register the .dll for the following reason:" &
_
    vbCrLf & vbCrLf & fGetMessage(dwReturn), vbOKOnly + vbExclamation,
"Failure"
End If
End
End Sub

Save this module, and put this code in another module:

Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA"
(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal
dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long,
Arguments As Long) As Long
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Function fGetMessage(msgNum As Long) As String
Dim strTemp As String
Dim i
strTemp = Space(500)
i = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, "", msgNum, 0, strTemp, 500,
0)
fGetMessage = Left(strTemp, InStr(1, strTemp, Chr(0), vbBinaryCompare) - 1)
End Function

Save your project, and compile the .exe.  You know have a VERY simple way to
register your ActiveX .dll's.  Just run the .exe on your IIS server.

A few things to note about registering your .dll's.  First of all, the
server will need the necessary runtimes.  So you'll need to install some VB
program at some point, for the .dll's to even work at the basic level.
Also, you'll need addition runtimes for whatever your using.  For instance,
you'll need MDAC installed, if you are going to use ADO.  (I have a few
'programs' that have 99% of the drivers I am ever going to use, and I just
install one of them, then I can just use the register program above to
'register' my ActiveX .dlls.

Next, when do you need to 're-register' your .dll's?  If you Add a new
class, you have to re-register the .dll. That is because the 'registration'
process learns what objects are in your .dll to use, so if you add a new
class, recompile, and copy it to your IIS server, your ASP programs will not
pick up the new class (it will just give you an error).  Running the
registering program again fixes that.  Also, this is a quirk I noticed, if
you compile a .dll from one machine, and register it on the IIS server.
Then compile the same .dll on a second machine, you will have to re-register
the compiles from the second machine no matter what you change.  It has to
do with the GUID that is assigned to the .dll, when you compile it.  I have
never bothered to figure out more then that! <grin>

When you actually want to use your .dll in ASP, you must use CreateObject,
however, in ASP you must use the CreateObject method of the Server object.
ie:

Dim cnn
Set cnn=server.createobject("ADODB.Connection")

or for a personal .dll

Dim myClass
Set myClass=server.createobject("MyProject.MyClass")

Finally, my last tip is to take the extra time to make all of your classes
as true owners of what they represent.  For example, in my 'example' project
linked above, I have a 'lone' data class, which holds data on a particular
'object'.  That class can be used to pull up the information by key, or it
can be set to just hold the information, and it can also be used to save
information (for a new object, or for modifying an existing object).  Then I
have a 'collection' class, that builds a collection of existing objects.  By
have the single object class designed to be 'self contained', even if I only
ever use it in the collections class (which fills the data in for each new
instance of the single class), the ability to pull up an item individually
is still there, or to modify that object, etc.  Abilities that make things
easy as a project is expanded (as they tend to do.).

Hope this helps some.

Drew

-----Original Message-----
From: Hadyn Morgan [mailto:hadyn at dataconcepts.co.nz]
Sent: Saturday, March 01, 2003 8:36 PM
To: Drew Wutka
Subject: Web Activex DLL's


Hi Drew

I thought I remembered you saying that you write your own DLL's for your web
projects, so... I am just starting a new web project (online order/quote
system) that has a number of complicated processes.  I thought the best way
to handle this would be through a DLL or two via ASP.  I have not created
DLL's before and wondered if you could give me a few tips/pointers on where
to start :)

I have VB 6 (VS) and IIS 5.1 installed.

Kind regards
Hadyn
___________________________________________
DATA CONCEPTS LTD
Enhancing Your Information Management
PO Box 1154, Hamilton 2001, New Zealand
Tel +64 7 855 9308    Mob +64 21 124 4488
Email: hadyn at dataconcepts.co.nz
Web: http://www.dataconcepts.co.nz




More information about the dba-VB mailing list