At times, if you’re a Windows user, chances are you’ve probably had to run the regsvr32.exe
utility to “register a DLL”. If so, you probably weren’t aware of exactly what was happening under the covers. This cryptic utility is used to manage Object Linking and Embedding (OLE) controls that many Windows applications use.
But what exactly is an OLE control and why would you want to register an OLE control? In this tutorial, you’ll learn the many ways to install, uninstall, register, or unregister an OLE control with the regsvr32.exe
utility.
Prerequisites
Ensure you have the requirements below to follow along in running the regsvr32.exe
utility:
- As the regsvr32.exe utility is built-in to most versions of Windows, you only need a Windows computer with an administrator account.
- You’ll need PowerShell 7 to take advantage of the PowerShell script included in this tutorial.
What are OLE Controls?
Before you learn how to use the regsvr32.exe
utility, you must first have a rudimentary understanding of what this tool manages, OLE controls.
OLE controls are a Windows-specific technology developed to facilitate embedding and linking to documents and other objects. This technology has evolved over the years, and different components were built on top of OLE technology.
You may have heard of Component Object Model (COM), Distributed Component Object Model (DCOM), or ActiveX controls. Each of these technologies is either built on or is an extension of the OLE technology that defines a standard interface to functions written in many different languages.
ActiveX is deprecated but still available via Internet Explorer on Windows 10, but not on Microsoft Edge.
Registering OLE Controls
Now that you understand what the regsvr32.exe
utility does, it’s time to register an OLE control, and there are several command variants to do that. Feel free to pick one command variant below or just try each one instead.
You can register OLE controls via PowerShell or a Windows command prompt. Either way, be sure you’re running your command line as an administrator to ensure the control is properly registered.
Displaying a GUI Result
Registering a DLL with regsvr32
, at the simplest use case, involves passing the DLL path to the tool. By default, regsvr32
will display a pop up indicating success (or failure). To register a DLL, provide the name or the full path of the DLL as shown below.
Running the command below will create the necessary registry classes for the target OLE control in the HKEY_CLASSES_ROOT\CLSID key for x64 instances or HKEY_CLASSES_ROOT\WOW6432Node\CLSID for x86 instances.
regsvr32 msxml3.dll
Upon success, you’ll see a GUI result noting that DllRegisterServer
was successful.
![Registering a DLL](https://adamtheautomator.com/wp-content/uploads/2021/07/reg.png)
When registering an OLE control,
regsvr32.exe
will apply the system search path. Therefore, if you need to register a specific file outside of that path, it is best to specify the absolute path to the file.
Registering an OLE Control Silently
When you’re running commands, it’s not always desirable to have a GUI prompt display. So let’s go over how you’ll suppress any GUI prompt when registering an OLE control.
To suppress GUI prompts, run regsvr32
followed by the DLL name (msxml3.dll
) and the /s
switch to register the OLE control silently. The only downside to this command is that you’ll also be suppressing any error messages.
An undocumented switch,
/e
, exists, that suppresses only the GUI success message but shows the GUI error message—clever!
regsvr32 msxml3.dll /s
![Registering a DLL silently](https://adamtheautomator.com/wp-content/uploads/2021/07/reg_s.png)
Registering an OLE Control and Specifying an Install Action
When you specify a DLL for an OLE registration, you’re only calling the DllRegisterServer
method, but there are times when you need to take action after registration. To do that, you can specify the /i
parameter.
For example, you could pass an input string (U
) to the /i
parameter which will invoke the DllInstall
method and the DllRegisterServer
method simultaneously.
The
DllInstall
method allows theregsvr32.exe
utility to install a DLL in more than one way and various actions invoked for one DLL.
regsvr32 /i:U shell32.dll
![Registering a DLL and running an install command](https://adamtheautomator.com/wp-content/uploads/2021/07/reg_U.png)
Specifying an Install Action Without Calling DllRegisterServer
Method
Perhaps you only want to install an OLE control without re-registering it. If so, run the following command.
regsvr32 /n /i:U shell32.dll
The command above uses the/n
parameter to prevent calling the DllRegisterServer
method for the shell32.dll DLL to the installation method, DllInstall.
![Only running an install command but not registering a DLL](https://adamtheautomator.com/wp-content/uploads/2021/07/reg_n.png)
Unregistering an OLE Control with regsvr32.exe
If you’ve gone through registering an OLE control without errors, then that’s a relief. But there may be a time when you need to uninstall an OLE control when it’s conflicting with other ones. Thankfully, regsvr32.exe
offers the ability to unregister and uninstall an OLE control.
To unregister and uninstall a control, run the regsvr32.exe
and pass the /u
parameter to unregister the OLE control. You can see in the below GUI result that the msxml3.dll DLL was successfully uninstalled.
To run an action along with unregistration, pass the action string (
U
) to the/i
parameter. Then theregsvr32
command will call theDllInstall
method with that action as an uninstallation, like:regsvr32 /i:action /U msxml3.dll
.
regsvr32 /U msxml3.dll
![Unregistering a DLL](https://adamtheautomator.com/wp-content/uploads/2021/07/unreg.png)
Handling Errors for regsvr32.exe
You’ve experienced successfully registering OLE controls, but what if the registration command comes across an error? There are a handful of errors that may appear when you attempt to manage controls. These messages may be cryptic at times but are broken down below to let you know what causes them.
FAIL_ARGS | ⦿ “Unrecognized flag” ⦿ “Extra argument on command line” ⦿ “This command is only valid when an OLE Custom Control project is open” ⦿ “No DLL name specified” | Implies that a command-line argument was incorrect. |
FAIL_OLE | “OleInitialize failed” | Implies that there was an error in properly initializing the OLE subsystem. |
FAIL_LOAD | “LoadLibary(<dllname) failed>“ | This can be a host of reasons such as a dependent library missing, the DLL is inaccessible due to permissions, or the file is missing. |
FAIL_ENTRY | ⦿ ” was loaded, but the entry point was not found. does not appear to be an .DLL or .OCX file” ⦿ ” was loaded, but the entry point was not found. may not be exported or a corrupt version may be in memory. Consider using PView to detect and remove it.” ⦿ ” was loaded, but the entry point was not found. may not be exported, or a corrupt version may be in memory. Consider using WPS to detect and remove it.” | Implies that the entry point in the DLL, or function name, is missing. |
FAIL_REG | “<DLLEntryPoint> in <DLLName> failed“ | Implies that despite all the prior steps completing the system still failed to fully register the DLL. |
Extending the regsvr32.exe
Utility with PowerShell
As regsvr32.exe
prefers the GUI output, automation may be a challenge. To get rid of that challenge, you can wrap the regsvr32.exe
utility in a PowerShell function. By creating a PowerShell function, you can run regsvr32.exe
just as if it were a native PowerShell cmdlet.
As an example, check out the Invoke-RegSvr32
PowerShell function below. This function invokes the regsvr32
utility and uses PowerShell to capture the exit code and process it accordingly. And with applying an argument validation ahead of time, you can even avoid errors altogether!
To experiment with the below function, open a new PowerShell console and copy and paste the code into it. Then, you’ll be able to invoke the function as shown below.
Function Invoke-RegSvr32 {
<#
.SYNOPSIS
Wrap the regsvr32.exe Windows utility for registering OLE controls in a PowerShell function to aid in automation.
.PARAMETER FilePath
Specifies the DLL or control name to pass to regsvr32.exe, must be a valid file path.
.PARAMETER InstallString
Specify a string value to be passed as the pszCmdLine value in the DllInstall function when registering a control.
.PARAMETER Unregister
Unregister a previously registered control.
.PARAMETER InstallOnly
Do not register a control, only run the DllInstall function, which must also pass in an InstallString.
.EXAMPLE
PS> Invoke-RegSvr32 "C:\\Windows\\System32\\msxml3.dll"
#>
[CmdletBinding()]
Param (
[ValidateScript({ Test-Path -Path $_ -PathType 'Leaf' })]
[String]$FilePath,
[ValidateScript({-Not [String]::IsNullOrWhiteSpace($_)})]
$InstallString,
[Switch]$Unregister,
[Switch]$InstallOnly
)
Begin {
# Error codes are documented in this Microsoft article
# <https://devblogs.microsoft.com/oldnewthing/20180920-00/?p=99785>
$ExitCodes = @{
0 = "SUCCESS";
1 = "FAIL_ARGS - Invalid Argument";
2 = "FAIL_OLE - OleInitialize Failed";
3 = "FAIL_LOAD - LoadLibrary Failed";
4 = "FAIL_ENTRY - GetProcAddress failed";
5 = "FAIL_REG - DllRegisterServer or DllUnregisterServer failed.";
}
}
Process {
If ($InstallOnly -And -Not $InstallString) {
Write-Error "If you are running DllInstall by itself, an install string must be included."
Return
}
$Arguments = "/s{0}{1}{2} {3}" -f
(($Unregister) ? ' /U': ''),
(($InstallString) ? " /i:$InstallString": ''),
(($InstallOnly) ? ' /n': ''),
$FilePath
Write-Verbose $Arguments
Try {
$Result = Start-Process -FilePath 'regsvr32.exe' -Args $Arguments -Wait -NoNewWindow -PassThru
If ($Result.ExitCode -NE 0) {
Write-Error $ExitCodes[$Result.ExitCode]
}
} Catch {
Write-Error $_.Exception.Message
}
}
}
Below you’ll find a few examples of how to use the Invoke-RegSvr32
function.
# Attempt to only run the DllInstall against msxml3.dll with Verbose output
Invoke-RegSvr32 "C:\Windows\System32\msxml3.dll" -Verbose -InstallOnly
# Attempt to register msxml3.dll silently with Verbose output
Invoke-RegSvr32 "C:\Windows\System32\msxml3.dll" -Verbose
# Attempt to register msxml3.dll silently but no output
Invoke-RegSvr32 "C:\Windows\System32\msxml3.dll"
![Running Invoke-RegSvr32 PowerShell Command](https://adamtheautomator.com/wp-content/uploads/2021/07/regsvr32.png)
Invoke-RegSvr32
PowerShell CommandConclusion
By now, you’ve learned your way around registering and unregistering DLL and handling errors. Although regsrv32.exe
may not be used as much with day-to-day tasks, there are certain times you would need to register or unregister a DLL.
To extend this even further, you could expand PowerShell to account for even more checks and edge cases. You could even create a PowerShell function to list the registered controls in the CLSID
registry location!