65

I know how to register dlls but I've never really been sure why I'm doing it or under what conditions a dll must be registered. Could somebody explain or point me to some documentation?

gbjbaanb
  • 49,287
  • 10
  • 99
  • 143
stimms
  • 39,499
  • 27
  • 88
  • 144

3 Answers3

47

When a DLL is registered, the DllRegisterServer method entry point in your DLL is invoked. Similarly, DllUnregisterServer is invoked when a DLL is unregistered.

As described in this MSDN article:

Instructs an in-process server to create its registry entries for all classes supported in this server module. If this function fails, the state of the registry for all its classes is indeterminate.

For COM DLLs, you will need to implement your own DllRegisterServer and DllUnregisterServer entry point methods which do the registering/unregistering as appropriate. Example code for DllRegisterServer can be found here.

The end result of registering a DLL is that all of the CLSIDs for the components in the DLL are registered under HKEY_CLASSES_ROOT\CLSID. This allows CoCreateInstance to find the correct server when instantiating COM objects from another DLL or application.

DllUnregisterServer will do the reverse, and remove all of the CLSIDs from the registry that were put in there by DllRegisterServer.

More general information about DllRegisterServer can be found here.

CJ7
  • 20,640
  • 59
  • 173
  • 305
LeopardSkinPillBoxHat
  • 26,928
  • 14
  • 70
  • 108
  • When I create an ActiveX DLL in VB6 I don't have to implement DllRegisterServer. – CJ7 Dec 20 '11 at 03:15
8

What is most commonly referred to as DLL registering is when it implements a COM object. regsvr32 ensures that an instance of the object can be created. When e.g. VBScript uses New or CreateObject(), the registration ensures that COM knows what DLL to load in order to make a new instance, whether by name or CLSID.

See "the layman's explanation" for a (very) brief summary.

Pontus Gagge
  • 16,731
  • 1
  • 36
  • 50
-1

Simply see the source code of regsvr32.exe

  • 6
    I was surprised to find out that this source code is actually available with Visual Studio: http://msdn.microsoft.com/en-us/library/ms177531.aspx – M. Dudley Jul 29 '10 at 20:39
  • 2
    _"Simply"_ ... (at least is subjective, if not a bit loaded) – Zac Oct 15 '20 at 07:35