Register & Unregister A DLL File Through VBA

Share this

June 3, 2013

Register & Unregister A DLL File
The problem

Due to the last format on my hard disk, I lost a lot of the settings that I had made throughout the previous years. So, after re-installing windows and various applications, I faced the following issue: the context menu of Adobe Professional X was missing. What is the context menu? Well, it is a menu in the windows explorer that allows you to right click on a supported file and convert it directly to PDF file. It is a very handy feature since it is possible to select multiple files and combine them into a single PDF file or PDF Portfolio.

Adobe Professional Context Menu

I found on Adobe’s site that the problem was caused by an unregistered dynamic-link library (DLL) file. So, in the particular case, the solution was quite simple: I had to register that DLL file (ContextMenu.dll). After fixing the problem I had the idea of creating an Excel workbook, which would make the procedure of registering and unregistering a DLL file much easier. So, below you will find the “manual” solution to register and unregister a DLL file, which involves the command prompt, as well as the easiest solution of using the Excel workbook I developed.

Manual solution

A) Open an elevated command prompt:

Elevated Command Prompt
  1. Choose Start → All Programs.
  2. Click Accessories.
  3. Right-click on the Command Prompt icon and choose Run As Administrator from the context menu.
  4. If the User Account Control dialog box appears, click yes. This step depends on your windows settings. For example, in my computer, I have the UAC setting to “never notify” because all these pop-ups are really annoying, so in my case, the procedure involves only the first 3 steps.

Of course, there are plenty other ways to open an elevated command prompt, but this is probably the easiest way to do it, especially if you are a new windows user.

B) To register a DLL file write on the command prompt:

Register A DLL File

regsvr32 "dll path"

Where “DLL path” corresponds to the full path of your file, for example, ”C:Program FilesAdobeAcrobat 10.0Acrobat ElementsContextMenu.dll”.

C) To unregister a DLL file write on the command prompt:

Unregister A DLL File

regsvr32/u "dll path"

Again, “DLL path” corresponds to the full path of your file, for example, ”C:Program FilesAdobeAcrobat 10.0Acrobat ElementsContextMenu.dll”.

Excel workbook solution

  1. Open the Excel workbook entitled “Register & Unregister A DLL File” and enable macros.
  2. Press the Select DLL button, find the location of the DLL file on your hard disk and select it. 
  3. After selecting the DLL file you have two choices: you can either register or unregister the DLL file by pressing the buttons Register DLL and Unregister DLL correspondingly.

I believe that the workbook solution is much easier than the “manual” one since it doesn’t involve command prompt interaction from the user. However, in reality, the two solutions share the same technique, although in workbook solution the work is done by VBA.

VBA code

Similar to Excel Macro To List All Computer Software, in this code, I have used a small script to simulate the command line procedure described above. It is another case where the PowerShell/WMI reveals its strong potential…

Option Explicit

'------------------------------------------------------------------------------------------------
'This module contains macros that make the registration/unregistration of COM DLL files easier.
'The macros are based on regsvr32.exe utility, which is used with administrator elevated access.

'Written By: Christos Samaras
'Date: 01/06/2013
'Last Update: 15/02/2017
'E-mail: [email protected]
'Site: https://myengineeringworld.net/////
'------------------------------------------------------------------------------------------------

'Declaring the necessary ShellExecute API function for running the regsvr32 utility.
#If VBA7 And Win64 Then

'For 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

'For 32 bit Excel.
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

Sub SelectDLL()

'----------------------------------------------------------------------
'Shows the file picker dialog in order the user to select a DLL file.
'It also checks the selection to ensure that a file was selected.
'----------------------------------------------------------------------

'Clear the output cell.
shMain.Range("C4").Value = ""

'Show the file picker dialog and add the dll filter.
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Please select a DLL file!"
.Filters.Clear
.Filters.Add "DLL file", "*.dll"
.Show
'Check if a file was selected.
If .SelectedItems.Count = 0 Then
shMain.Activate
Range("C4").Select
MsgBox "You did't select a DLL file!", vbExclamation, "Canceled"
Exit Sub
Else
'Put the file path to the output cell.
shMain.Range("C4").Value = .SelectedItems(1)
End If
End With

End Sub

Sub RegisterDLL()

'------------------------------------
'Registers a DLL file on the system.
'------------------------------------

RegisterOrUnegisterDLL shMain.Range("C4").Value, True

End Sub

Sub UnregisterDLL()

'-----------------------------------------
'Unregisters a DLL file from the system.
'-----------------------------------------

RegisterOrUnegisterDLL shMain.Range("C4").Value, False

End Sub

Sub RegisterOrUnegisterDLL(DLLPath As String, Register As Boolean)

'-----------------------------------------------------
'Registers or unregisters a DLL file from the system.
'Helping macro that tests first the given path.
'-----------------------------------------------------

'Check if the given file path exists.
If FileExists(DLLPath) = 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

'Check if the given file path is a DLL file.
If UCase(Right(DLLPath, 3)) <> "DLL" Then
MsgBox "The file you have selected is not a Dynamic-link library (.dll) file!", vbCritical, "File Type Error"
shMain.Range("C4").Select
Exit Sub
End If

'Register or unregister the DLL file based on the Register variable.
If Register = True Then
Call RegisterFile(DLLPath)
Else
Call UnregisterFile(DLLPath)
End If

End Sub

Sub RegisterFile(FilePath As String)

'--------------------------------------------------------------------
'Registers a file on the system using administrator elevated access.
'--------------------------------------------------------------------

ShellExecute 0, "RunAs", "cmd", "/c regsvr32 " & """" & FilePath & """", "C:", 0

End Sub

Sub UnregisterFile(FilePath As String)

'------------------------------------------------------------------------
'Unregisters a file from the system using administrator elevated access.
'------------------------------------------------------------------------

ShellExecute 0, "RunAs", "cmd", "/c regsvr32 /u " & """" & FilePath & """", "C:", 0

End Sub

Sub Clear()

'-----------------------
'Clears the file path.
'-----------------------

shMain.Range("C4").Value = ""

End Sub

Function FileExists(FilePath As String) As Boolean

'-------------------------
'Checks if a file exists.
'-------------------------

If FilePath <> vbNullString Then
If Not Dir(FilePath, vbDirectory) = vbNullString Then FileExists = True
End If

End Function

Download it from here

Download

The zip file contains two workbooks, one for Excel 2003 and another for Excel 2007 or newer. Please remember to enable macros before using them.

Page last modified: January 6, 2019

Christos Samaras

Hi, I am Christos, a Mechanical Engineer by profession (Ph.D.) and a Software Developer by obsession (10+ years of experience)! I founded this site back in 2011 intending to provide solutions to various engineering and programming problems.

Christos E. Samaras

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Add Content Block
>