Programmatically Register and Unregister ActiveX Dlls and OCXs

ActiveX components such as DLLs and OCXs need to be registered with Windows prior to use. Typically you use Regsvr32.exe after you copy a component to a new PC to do this. To unregister a component you call Regsvr32 with the "/u" switch. This sample shows how to register and unregister components from VB.

The sample program lets you navigate to a folder then lists the DLLs and OCXs in that folder. Once a file is selected, you can register and unregister it by clicking corresponding File menu option. Since only ActiveX components can be registered, a check is performed to verify the selected file is indeed an ActiveX component.

Register and Unregister Activex DLLs and OCXs from VB.
Download Source Code


To check if a file is an ActiveX component the LoadLibrary API is used to load the executable module into memory. LoadLibrary returns a handle to the module. This handle is passed to GetProcAddress to get the address of the DllRegisterServer function within the modiule. If the function is found, the file is an ActiveX component.

If the file is an ActiveX component, the address of the DllRegisterServer or DllUnRegisterServer function is retrieved from the file using LoadLibrary and GetProcAddress APIs as just mentioned. The address is then passed to the CreateThread API to execute the registration function. The program waits a short period of time for the function to complete via a call to WaitForSingleObject. The newly created thread is closed and the module is unloaded from memory.


Run the code and navigate to a DLL or OCX. Select the desired option fom the File menu.

About TheScarms
About TheScarms

Sample code
version info

If you use this code, please mention ""

Email this page

© Copyright 2016 TheScarms
Goto top of page