How can I check if an OCX or DLL is already registered using API function.
Printable View
How can I check if an OCX or DLL is already registered using API function.
I think that DLLs and OCXs that are registered on a computer have entries in the registry ressembling this one:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{309A35B5-B3D2-11D5-B13C-00C04F9DA8E7}\InprocServer32
The best way to read this is to loop in the registry "CLSID" key for all subkeys and read each corresponding "InprocServer32" value.
You don't need an API call to see if the file is registered, just use the CreateObject method and check if Object Is Nothing or an error was thrown.
I have included API code to register and unregister DLLs & OCXs
Code:'// These are API calls reqired for registering/unregistering ActiveX DLLs & OCXs //
Declare Function LoadLibraryRegister _
Lib "kernel32" Alias "LoadLibraryA" _
(ByVal lpLibFileName As String) As Long
Declare Function CreateThreadForRegister _
Lib "kernel32" Alias "CreateThread" _
(lpThreadAttributes As Any, ByVal dwStackSize _
As Long, ByVal lpStartAddress As Long, _
ByVal lParameter As Long, ByVal dwCreationFlags As Long, _
lpThreadID As Long) As Long
Declare Function WaitForSingleObject _
Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Declare Function GetProcAddressRegister _
Lib "kernel32" Alias "GetProcAddress" _
(ByVal hModule As Long, ByVal lpProcName As String) _
As Long
Declare Function FreeLibraryRegister Lib _
"kernel32" Alias "FreeLibrary" (ByVal hLibModule As Long) _
As Long
Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Declare Function GetExitCodeThread Lib "kernel32" _
(ByVal hThread As Long, lpExitCode As Long) As Long
Declare Sub ExitThread Lib "kernel32" _
(ByVal dwExitCode As Long)
Public Function RegServer(ByVal FileName As String) As Boolean
'*****************************************************************************
'* Purpose: Registers an ActiveX DLL or OCX.
'*
'* Inputs: sFile: ActiveX component to register.
'*
'* Returns: True or false
'*
'*****************************************************************************
RegServer = RegSvr32(FileName, False)
End Function
Public Function UnRegServer(ByVal FileName As String) As Boolean
'*****************************************************************************
'* Purpose: Unregisters an ActiveX DLL or OCX.
'*
'* Inputs: sFile: ActiveX component to unregister.
'*
'* Returns: True or false
'*
'*****************************************************************************
UnRegServer = RegSvr32(FileName, True)
End Function
Private Function RegSvr32(ByVal FileName As String, bUnReg As _
Boolean) As Boolean
Dim lLib As Long
Dim lProcAddress As Long
Dim lThreadID As Long
Dim lSuccess As Long
Dim lExitCode As Long
Dim lThread As Long
Dim bAns As Boolean
Dim sPurpose As String
sPurpose = IIf(bUnReg, "DllUnregisterServer", _
"DllRegisterServer")
If Dir(FileName) = "" Then Exit Function
lLib = LoadLibraryRegister(FileName)
'could load file
If lLib = 0 Then Exit Function
lProcAddress = GetProcAddressRegister(lLib, sPurpose)
If lProcAddress = 0 Then
'Not an ActiveX Component
FreeLibraryRegister lLib
Exit Function
Else
lThread = CreateThreadForRegister(ByVal 0&, 0&, ByVal lProcAddress, ByVal 0&, 0&, lThread)
If lThread Then
lSuccess = (WaitForSingleObject(lThread, 10000) = 0)
If Not lSuccess Then
Call GetExitCodeThread(lThread, lExitCode)
Call ExitThread(lExitCode)
bAns = False
Exit Function
Else
bAns = True
End If
CloseHandle lThread
FreeLibraryRegister lLib
End If
End If
RegSvr32 = bAns
End Function
Public Function IsRegistered(sObjectClass As String) As Boolean
'*****************************************************************************
'* Purpose: Checks if an ActiveX DLL or OCX is registered.
'*
'* Inputs: sObjectClass: ActiveX component Name.Class
'*
'* Returns: True or false
'*
'*****************************************************************************
On Error GoTo errHandler
'Declare a clean object to use
Dim objCheck As Object
Dim strResponse As String
Set objCheck = CreateObject(sObjectClass)
If objCheck Is Nothing Then
IsRegistered = False
Else
IsRegistered = True
End If
Exit Function
errHandler:
If Err.Number = 429 Then IsRegistered = False
End Function
Jerry,
Your IsRegistered function does not seem to work for OCX's.