ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check if DLL is registered (https://www.excelbanter.com/excel-programming/313557-check-if-dll-registered.html)

R Avery

Check if DLL is registered
 
Is there any way to check whether or not a particular DLL is registered?
The problem I have is that some of my .xla's require certain network
DLL's to be registered, but I do not want to have to re-register these
DLL's every time i load the .xla, since registering a DLL takes about
0.1 second each on my 3 Ghz comp.

I thought perhaps instead I could much more quickly check to see if a
particular DLL is registered, and then if not, register it. Is this
possible?




The code I use to register DLL's is below:



Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As
Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA"
(ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As
Long, ByVal lpProcName As String) As Long
Private Declare Function CallWindowProc Lib "user32" Alias
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long,
ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Const ERROR_SUCCESS = &H0
Private Const ERROR_AHHHHHH = &HF

Public Function RegisterServer(hwnd As Long, DllServerPath As String,
bRegister As Boolean) As Boolean
On Error Resume Next

Dim lb As Long, pa As Long

lb = LoadLibrary(DllServerPath)
If bRegister Then
pa = GetProcAddress(lb, "DllRegisterServer")
Else
pa = GetProcAddress(lb, "DllUnregisterServer")
End If

If CallWindowProc(pa, hwnd, ByVal 0&, ByVal 0&, ByVal 0&) =
ERROR_SUCCESS Then
RegisterServer = True
Else
RegisterServer = False
End If
FreeLibrary lb
End Function

Juan Pablo González

Check if DLL is registered
 
I just use a simple 'On Error Resume Next' routine for the dlls that I write
in conjunction with the vba code:

On Error Resume Next
Set Obj = CreateObject("MyDll.MyClass")
On Error Goto 0

If Obj Is Nothing Then
MsgBox "MyDll is not correctly installed"
End If

--
Regards

Juan Pablo González

"R Avery" wrote in message
...
Is there any way to check whether or not a particular DLL is registered?
The problem I have is that some of my .xla's require certain network
DLL's to be registered, but I do not want to have to re-register these
DLL's every time i load the .xla, since registering a DLL takes about
0.1 second each on my 3 Ghz comp.

I thought perhaps instead I could much more quickly check to see if a
particular DLL is registered, and then if not, register it. Is this
possible?




The code I use to register DLL's is below:



Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As
Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA"
(ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As
Long, ByVal lpProcName As String) As Long
Private Declare Function CallWindowProc Lib "user32" Alias
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long,
ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Const ERROR_SUCCESS = &H0
Private Const ERROR_AHHHHHH = &HF

Public Function RegisterServer(hwnd As Long, DllServerPath As String,
bRegister As Boolean) As Boolean
On Error Resume Next

Dim lb As Long, pa As Long

lb = LoadLibrary(DllServerPath)
If bRegister Then
pa = GetProcAddress(lb, "DllRegisterServer")
Else
pa = GetProcAddress(lb, "DllUnregisterServer")
End If

If CallWindowProc(pa, hwnd, ByVal 0&, ByVal 0&, ByVal 0&) =
ERROR_SUCCESS Then
RegisterServer = True
Else
RegisterServer = False
End If
FreeLibrary lb
End Function




R Avery

Check if DLL is registered
 
I have tested this for speed and it is about 33-100 times faster than
registering the DLL every time. So is the best idea to always include
some lightweight test class in every DLL object library I create whose
sole purpose is testing whether or not the DLL is registered?

This approach is definitely far superior to my old approach, but I still
feel as though there should be some (elegant) way to test for the
registration directly. For example, using the TLI.TLIApplication object
to test this? I have scanned through the objects functions and
properties, but I do not see an obvious way how to do this.


Juan Pablo González wrote:
I just use a simple 'On Error Resume Next' routine for the dlls that I write
in conjunction with the vba code:

On Error Resume Next
Set Obj = CreateObject("MyDll.MyClass")
On Error Goto 0

If Obj Is Nothing Then
MsgBox "MyDll is not correctly installed"
End If



All times are GMT +1. The time now is 01:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com