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