Register/Unregister DLL at Runtime VBA (Visual Basic for Applications)

Register/Unregister DLL at Runtime VBA (Visual Basic for Applications)

DLL

Dynamic Link Library refers to a binary file which is pre-compiled file supported by Windows. A Dll file is a Class object which contains pre-defined functions or methods and properties which can be exposed in a programming language by referencing it. In this article we will dynamically register and unregister a DLL using VBA code.

APIs Declaration

#If VBA7 And Win64 Then
    
    '64 bit Excel.
    Public Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                                    (ByVal hwnd As LongPtr, _
                                    ByVal lpOperation As String, _
                                    ByVal lpFile As String, _
                                    ByVal lpParameters As String, _
                                    ByVal lpDirectory As String, _
                                    ByVal nShowCmd As Long) As LongPtr
                                    
#Else

    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                            (ByVal hwnd As Long, _
                             ByVal lpOperation As String, _
                             ByVal lpFile As String, _
                             ByVal lpParameters As String, _
                             ByVal lpDirectory As String, _
                             ByVal nShowCmd As Long) As Long
#End If

Register

Public Function RegisterDLL(FilePath As String) As Boolean
    RegisterDLL = False
    ShellExecute 0, "RunAs", "cmd", "/c regsvr32 " & """" & FilePath & """", "C:", 0
    RegisterDLL = True
End Function

Unregister

Public Function UnregisterDLL(FilePath As String) As Boolean
    UnregisterDLL = False
    ShellExecute 0, "RunAs", "cmd", "/c regsvr32 /u " & """" & FilePath & """", "C:", 0
    UnregisterDLL = True
End Function

Validation

public Sub DLL_Register_Unegister(oDLLPath As String, oRegister As Boolean)
    
    'Check if the given file path exists.
    If isFileExists(oDLLPath) = False Then
        MsgBox "The DLL file doesn't exist!" & vbNewLine & "The file path you entered is incorrect.", vbCritical, "File Path Error"
        shMain.Range("C4").Select
        Exit Sub
    End If
    
    'Validate valid DLL file.
    If LCase(Right(oDLLPath, 3)) <> "dll" Then
        MsgBox "Not a valid Dynamic Link Library (.dll) file", vbCritical, "VBAOVERALL- File Validation Error"
        Exit Sub
    End If
    
    'Validate oRegister flag
    If oRegister = True Then
        If RegisterDLL(oDLLPath) = True Then
            MsgBox "Library registered successfully"
            Exit Sub
        Else
            MsgBox "Fail to Registre"
            Exit Sub
        End If
    Else
        If UnregisterDLL(oDLLPath) = True Then
            MsgBox "Library Unregistered successfully"
            Exit Sub
        Else
            MsgBox "Fail to Unregistre"
            Exit Sub
        End If
    End If
    
End Sub

FileExists function

Public Function isFileExists(oFilePath As String) As Boolean
    
    isFileExists = False
    
    If oFilePath <> vbNullString And Len(oFilePath) > 0 Then
        If Not Dir(oFilePath, vbDirectory) = vbNullString Then
            isFileExists = True
        End If
    End If
    
End Function

Call Register DLL

Public Sub DLLRegisterExample(oDLLPath As String)

    DLL_Register_Unegister oDLLPath, True

End Sub

Call Unregister DLL

Sub DLLUnregisterExample(oDLLPath As String)

DLL_Register_Unegister oDLLPath, False

End Sub

Please leave your comments or queries under comment section also please do subscribe to our blogs to keep your self upto date.

Leave a Reply

Your email address will not be published.