IIS Development

IisDev

>  Home
>  News

 

Articles

>  Code Samples
>  ASP.NET
>  Security

 

Components & Tools

>  Free Components

 

Service

>  Links
>  Contact us

Checking the Existence of a Component

Most of us are hosting their web sites on servers at an ISP where you don't have full control over the software (components) installed. How do you figure out whether components you need are installed? That's where this neat little piece of code presented here can help you (besides showing some very useful ASP techniques).

The core of our program is the function IsObjInstalled, which does the main work.

Function IsObjInstalled(strClassString)
 On Error Resume Next
 ' initialize default values
 IsObjInstalled = False
 Err = 0
 ' testing code
 Dim xTestObj
 Set xTestObj = Server.CreateObject(strClassString)
 If 0 = Err Then IsObjInstalled = True
 ' cleanup
 Set xTestObj = Nothing
 Err = 0
End Function

This function simply tries to create an instance of an object using the ProgId or ClassId passed by the parameter strClassString:

 Set xTestObj = Server.CreateObject(strClassString)

In case the component is installed on the server, this line of code simply creates a new running object instance. However, if the component is not installed (properly), an error is caused. To catch this error in code, we use the following line of code to disable the standard behavior of ASP to immediately interupt execution after an error has occured:

 On Error Resume Next

To check for an error condition, we can use the Err object, which returns the error number as its default property.

 If 0 = Err Then IsObjInstalled = True

In case that no error occured, the error number is simply 0, and the function IsObjInstalled returns True, otherwise the default of False is returned.

In our example script, a user can input the ProgId or ClassId of a component to be checked for in a basic form,

<FORM action=<%=Request.ServerVariables("SCRIPT_NAME")%> method=post>
<INPUT type=text value="" name="classname" size=40>
<INPUT type=submit value=Submit>
<INPUT type=reset value=Reset>
</FORM>

where the action tag is set to refer to the page itself. <%=Request.ServerVariables("SCRIPT_NAME")%> is just the ASP-Statement for "URL of this page".

When this form is submitted back to the server, then the input is stored in the variable strClass by reading Request.Form("classname"). Furthermore, all leading and trailing spaces are deleted from the string using the Trim function.

<%
Dim strClass
strClass = Trim(Request.Form("classname"))

If there is an input, the script writes back the status of the component, which ProgId or ClassId you did input in the form, using the function IsObjInstalled.

If "" <>  strClass then
  Response.Write strClass & " is "
  If Not IsObjInstalled(strClass) then 
    Response.Write "<strong>not installed</strong>"
  Else
    Response.Write "installed!"
  End If
  Response.Write "<P>" & vbCrLf

Otherwise (even if the page is called the first time) it shows you the status of some standard components which are stored in the array theInstalledObjects. The status of these components is then presented in a table.

Else
  ' default: list all components that should be installed
%>
<TABLE BORDER=0>
<%
Dim i
For i=0 to UBound(theInstalledObjects)
  Response.Write "<TR><TD>" & theInstalledObjects(i) & "</TD><TD>"
  If Not IsObjInstalled(theInstalledObjects(i)) Then 
    Response.Write "<strong>not installed</strong>"
  Else
    Response.Write "installed!"
  End If
  Response.Write "</TD></TR>" & vbCrLf
Next
%>
</TABLE>

The UBound function that is used here automatically returns the index of the last element in an array. This comes in handy here, because we can extend the array of standard components in the source code without having to update a loop variable here.

Now we are at the end of the walk-through of the code. We have put the code online on our site so you can play with the script, as well as you can download it from here.

You can find the Object Tester presented in this article at work on our site - Make a run.!
To download the source code click here!


Written by Christian Koller and Christoph Wille

 

©1998-2018 Christoph Wille. All rights reserved.
All content and images on this site are copyright. Reuse (even parts) needs our written consent.