VB and VBA Users Source Code: NEW (improved) Register a COM DLL/OCX without using regsvr32.exe
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
NEW (improved) Register a COM DLL/OCX without using regsvr32.exe
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, August 15, 2003
Hits:
3137
Category:
Windows API
Article:
The following code registers COM DLLs/OCXs without using REGSVR32.exe. You, will first need to download the DLL from http://www.vbusers.com/downloads/regcomp.zip. Then simply extract the DLL to the windows system 32 directory, or anywhere else along the path. Note, you DON'T need to register the DLL included the regcomp.zip file (as it's not a COM dll). 'Note the is an alternative version of this at 'http://www.vbusers.com/code/codeget.asp?ThreadID=46&PostID=1&NumReplies=0 Option Explicit Private Declare Function RegisterComp Lib "RegComp.dll" (ByVal szFilePath As String, ByVal bUnregister As Boolean, ByVal szMsgBuffer As String) As Long 'Purpose : This function registers and Unregisters OLE components 'Inputs : sFilePath The path to the DLL/OCX or ActiveX EXE ' bRegister If True Registers the control, else unregisters control 'Outputs : Returns True if successful 'Author : Andrewb 'Date : 04/09/2000 'Notes : This is the API equivalent of RegSvr32.exe. ' If you DDL exports an interface from another component, then you must register this component first. ' If your DDL exports an interface from a non COM component then you must make ' sure that the exported DDL is copied somewhere along the path, or use CHDIR to change your ' currrent directory to the path where this DDL can be found. Use dependancy walker to view the exported interfaces of your component ("depends.exe" is included with Visual Studio). 'Example : ' If RegisterComponent("C:\MyPath\MyFile.dll") = True Then ' Msgbox "Component Successfully Registered" ' Else ' Msgbox "Failed to Registered Component" ' End If 'Revisions : 1/Jan/2002. Updated to include code for registering ActiveX Exes. Function RegisterComponent(ByVal sFilePath As String, Optional bRegister As Boolean = True) As Boolean Dim sError As String, iFreeFile As Integer On Error GoTo ErrFailed If Len(Dir$(sFilePath)) > 0 And Len(sFilePath) > 0 Then 'File exists If UCase$(Right$(sFilePath, 3)) = "EXE" Then 'Wrap path in double quotes (to allow spaces in the path) If Right$(sFilePath, 1) <> """" Then sFilePath = sFilePath & """" End If If Left$(sFilePath, 1) <> """" Then sFilePath = """" & sFilePath End If 'Register/Unregister ActiveX EXE If bRegister Then 'Register EXE Shell sFilePath & " /REGSERVER", vbHide Else 'Unregister ActiveX EXE Shell sFilePath & " /UNREGSERVER", vbHide End If RegisterComponent = True Else 'First check that the file is not in use. If GetAttr(sFilePath) And vbReadOnly Then SetAttr sFilePath, vbNormal End If iFreeFile = FreeFile Open sFilePath For Binary Access Write Lock Read As #iFreeFile Close #iFreeFile If Err.Number Then 'File is in use, don't register/unregister control Debug.Print Err.Description Debug.Assert False RegisterComponent = False Else 'Register/Unregister DLL sError = String(2500, 0) If RegisterComp(sFilePath, Not bRegister, sError) Then 'Failed Debug.Print Left$(sError, InStr(sError, vbNullChar) - 1) Debug.Assert False RegisterComponent = False Else 'Success RegisterComponent = True End If End If End If Else RegisterComponent = False End If Exit Function ErrFailed: Debug.Print Err.Description Debug.Assert False On Error GoTo 0 RegisterComponent = False End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder