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
%>

You might also like...

Comments

ElementK Journals

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor