Library code snippets

Determine programmatically whether a component is installed

Sometimes you may need to check at runtime whether a particular component is installed. For example, you might have written a common code library that sends email in certain circumstances. But rather than allow it to break if you attempt to use it from a new server, you might wish to check for the CDONTS NewMail object programmatically first, and then send the email message if and only if the component is present. You can use the following function to make this determination. Simply pass the ProgId (the string you'd normally pass as the argument of the CreateObject method) to the IsComponentInstalled function and the Boolean return value will indicate whether the component is available for your use.

<% Option Explicit

Function IsComponentInstalled(ProgId)
    Dim tmpObject

    On Error Resume Next

    Set tmpObject = Server.CreateObject(ProgId)
    If Err.Number = 0 Then
        IsComponentInstalled = True
    Else
        IsComponentInstalled = False
    End If
    Set tmpObject = Nothing
End Function

If IsComponentInstalled("CDONTS.NewMail") Then
    Response.Write "We'll send the email message now ..."
    '...
Else
    Response.Write "We'll *not* send the email message ..."
    '...
End If
%>

Comments

  1. 14 Jun 2004 at 02:02
    • Is it really necessary to use the 'On Error Resume Next'?
    • Use 'CDO.Message' instead of CDONTS. CDONTS has been deprecated.
  2. 01 Jan 1999 at 00:00

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • ASP.NET Caching and Performance

    Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...

Related jobs