Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #124
Online Version at http://www.codeoftheweek.com/membersonly/bi/0124.html (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Registering/Unregistering controls programmatically

IMPORTANT! - Publication Change

Starting with this issue the full Code of the Week issues will only be published every other week. All current subscribers which have purchased an annual subscription will still receive all 52 issues they paid for, it will just take two years to get them instead of one year. In the weeks between publication of Code of the Week we will usually send out shorter issues with more a tip type content (similar to the Ziff Davis VBTips ezine).

If you have any question about this publication change, please notify us at pubchg@codeoftheweek.com

Requirements

In this Issue

In this issue we discuss how to register any ActiveX DLL or OCX using only API calls and code.

If you have any questions about using this module, let us know at questions@codeoftheweek.com

basRegistration

This module makes it easy to register any 32-bit ActiveX component on-the-fly from your Visual Basic program. This is useful for writing setup utilities and more reliable applications that do not crash if a single file is not registered correctly. See the sample for a good use of these routines. All the fun work is done with several API calls.

Properties

Public Enum eRegStatus

This is one of four possible values: statSuccess, statCouldNotLoadInMemorySpace, statInvalidActiveXComponent, statRegistrationFailed. If the RegisterComponent or UnregisterComponent returns statSuccess then everything worked fine. statCouldNotLoadInMemorySpace usually means the ActiveX file you are trying to register can not be found.

Functions

Public Function RegisterComponent(sFilename As String) As eRegStatus

Call this with sFilename pointing to the fully-qualified path and filename of an ActiveX control to register. The return value will be one of the values discussed in eRegStatus.

Public Function UnregisterComponent(sFilename As String) As eRegStatus

Call this with sFilename pointing to the fully-qualified path and filename of an ActiveX control to unregister. The return value will be one of the values discussed in eRegStatus.

Sample Usage

This sample shows how to use the RegisterComponent routine in order to more intelligently handle problems loading OCX and DLL files. The sample tries to load a form and if an error occurs it first assumes there was a problem loading the control(s) that it depends on. You can add additional registrations and you might need to be smarter about how you find the files to register. This is beyond the scope of this issue.

Public Sub Main()
    Dim frm As Object

    On Error Resume Next
    Set frm = New Form1
    frm.Show vbModal
    If Err Then
        ' Let's assume that we had a problem loading an OCX or DLL.
        'MsgBox "try registering tdbg6.ocx " & Err.Number & Err.Description
        Err.Clear
        ' we assume the control is located in c:\tdbg6. You would most
        ' likely need to code this more intelligently.
        If RegisterComponent("c:\tdbg6\tdbg6.ocx") = statSuccess Then
            Set frm = New Form1
            frm.Show vbModal
            If Err Then
                MsgBox "Sorry, we can not figure out why the form will not load."
            End If
        End If
    End If
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0124.html


This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com