Registration and deregistration

Registration of COM DLL/EXE is the process of creating all necessary Windows registry keys and values. De-registration is the reverse process where all Windows registry keys and values should be deleted. COM EXE can take command line arguments and perform registration and deregistration. COM DLL are not active executables thus direct execution is not possible.

Regsvr32 tool

COM DLL indirectly exports two functions named DllRegisterServer() and DllUnRegisterServer() for performing registration and de-registration. These exported functions can be called by an executable. Regsvr32 is such standard tool used for this purpose. This command-line tool registers .dll files as command components in the registry.

Regsvr32 Syntax

regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname

Regsvr32 Parameters

  • /u : Unregisters server.
  • /s : Specifies regsvr32 to run silently and to not display any message boxes.
  • /n : Specifies not to call DllRegisterServer. You must use this option with /i.
  • /i :cmdline : Calls DllInstall passing it an optional [cmdline]. When used with /u, it calls dll uninstall.
  • dllname : Specifies the name of the dll file that will be registered.
  • /? : Displays help at the command prompt.

COM DLL Windows Registry structure

HKEY_CLASSES_ROOT
{
  MathLib.Calculate.= s 'Calculate Class'
  {
    CLSID = s '{F082638A-748A-44AC-ABB3-DC346B7F128A}'
  }
  MathLib.Calculate = s 'Calculate Class'
  {
    CLSID = s '{F082638A-748A-44AC-ABB3-DC346B7F128A}'
    CurVer = s 'MathLib.Calculate.1'
  }
  CLSID
  {
    {F082638A-748A-44AC-ABB3-DC346B7F128A} = s 'Calculate Class'
    {
      ProgID = s 'MathLib.Calculate.1'
      VersionIndependentProgID = s 'MathLib.Calculate'
      'Programmable'
      InprocServer32 = s '%MODULE%'
      {
        val ThreadingModel = s 'Apartment'
      }
      'TypeLib' = s '{B9BBFE19-AC06-48A4-85BB-F9F8380A511B}'
    }
  }
}

COM DLL Windows Registry entries

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\MathLib.Calculate]
@="Calculate Class"

[HKEY_CLASSES_ROOT\MathLib.Calculate\CLSID]
@="{F082638A-748A-44AC-ABB3-DC346B7F128A}"

[HKEY_CLASSES_ROOT\MathLib.Calculate\CurVer]
@="MathLib.Calculate.1"

[HKEY_CLASSES_ROOT\MathLib.Calculate.1]
@="Calculate Class"

[HKEY_CLASSES_ROOT\MathLib.Calculate.1\CLSID]
@="{F082638A-748A-44AC-ABB3-DC346B7F128A}"

[HKEY_CLASSES_ROOT\CLSID\{F082638A-748A-44AC-ABB3-DC346B7F128A}]
@="Calculate Class"

[HKEY_CLASSES_ROOT\CLSID\{F082638A-748A-44AC-ABB3-DC346B7F128A}\InprocServer32]
@="C:\.....\MathLib.dll"
"ThreadingModel"="Apartment"

[HKEY_CLASSES_ROOT\CLSID\{F082638A-748A-44AC-ABB3-DC346B7F128A}\ProgID]
@="MathLib.Calculate.1"

[HKEY_CLASSES_ROOT\CLSID\{F082638A-748A-44AC-ABB3-DC346B7F128A}\Programmable]

[HKEY_CLASSES_ROOT\CLSID\{F082638A-748A-44AC-ABB3-DC346B7F128A}\TypeLib]
@="{F082638A-748A-44AC-ABB3-DC346B7F128A}"

[HKEY_CLASSES_ROOT\CLSID\{F082638A-748A-44AC-ABB3-DC346B7F128A}\VersionIndependentProgID]
@="MathLib.Calculate"

DllRegisterServer

DllRegisterServer() is the exported function which should be called for self registration of the com server. It should create all necessary registry keys and values to supports If this function fails, the state of the registry for all its classes is indeterminate.

STDAPI DllRegisterServer(void);

Return Values

This function supports the standard return values E_OUTOFMEMORY and E_UNEXPECTED, as well as the following:

  • S_OK
    The registry entries were created successfully.
  • SELFREG_E_TYPELIB
    The server was unable to complete the registration of all the type libraries used by its classes.
  • SELFREG_E_CLASS
    The server was unable to complete the registration of all the object classes.

DllRegisterServer example

/* DllRegisterServer - Adds entries to the system registry */
STDAPI DllRegisterServer(void)
{
  HKEY hKey,hKey2,hKey3;
  long lReturn;
  WCHAR szCOMPath[MAX_PATH+1] = { 0 };

  lReturn = GetModuleFileName(_Module.m_hInst, szCOMPath, MAX_PATH);
  if(lReturn <= ERROR_SUCCESS){
    return ERROR_SUCCESS;
  }

  /* MathLib.Calculate.1 and sub keys */
  lReturn = RegCreateKey(HKEY_CLASSES_ROOT,L"MathLib.Calculate.1",&hKey);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey,NULL,REG_SZ ,L"Calculate Class",15);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegCreateKey(hKey,L"CLSID",&hKey2);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey2,NULL,REG_SZ ,L"{F082638A-748A-44AC-ABB3-DC346B7F128A}",34);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey);
  RegFlushKey(hKey2);
  RegCloseKey(hKey);
  RegCloseKey(hKey2);

  
  /* MathLib.Calculate and sub keys */
  lReturn = RegCreateKey(HKEY_CLASSES_ROOT,L"MathLib.Calculate",&hKey);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey,NULL,REG_SZ ,L"Calculate Class",30);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegCreateKey(hKey,L"CLSID",&hKey2);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey2,NULL,REG_SZ ,L"{F082638A-748A-44AC-ABB3-DC346B7F128A}",68);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegCreateKey(hKey,L"CurVer",&hKey2);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey2,NULL,REG_SZ ,L"MathLib.Calculate.1",38);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey);
  RegFlushKey(hKey2);
  RegCloseKey(hKey);
  RegCloseKey(hKey2);
  
  /* CLSID\{CLSID} and sub keys */
  lReturn = RegOpenKeyA(HKEY_CLASSES_ROOT,"CLSID",&hKey);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegCreateKey(hKey,L"{F082638A-748A-44AC-ABB3-DC346B7F128A}",&hKey2);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey2,NULL,REG_SZ ,L"Calculate Class",30);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegCreateKey(hKey2,L"ProgID",&hKey3);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey3,NULL,REG_SZ ,L"MathLib.Calculate.1",38);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey3);
  RegCloseKey(hKey3);
  lReturn = RegCreateKey(hKey2,L"VersionIndependentProgID",&hKey3);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey3,NULL,REG_SZ ,L"MathLib.Calculate",34);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey3);
  RegCloseKey(hKey3);
  lReturn = RegCreateKey(hKey2,L"Programmable",&hKey3);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey3);
  RegCloseKey(hKey3);
  lReturn = RegCreateKey(hKey2,L"InprocServer32",&hKey3);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey3,NULL,REG_SZ ,szCOMPath,wcslen(szCOMPath)*2);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValueEx(hKey3,L"ThreadingModel", 0, REG_SZ ,(const unsigned char*)L"Apartment",18);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey3);
  RegCloseKey(hKey3);
  lReturn = RegCreateKey(hKey2,L"TypeLib",&hKey3);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegSetValue(hKey3,NULL,REG_SZ,L"{F082638A-748A-44AC-ABB3-DC346B7F128A}",68);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegFlushKey(hKey3);
  RegCloseKey(hKey3);
  RegFlushKey(hKey2);
  RegCloseKey(hKey2);
  RegFlushKey(hKey);
  RegCloseKey(hKey);
  return S_OK;
}

regsvr32 dllregisterserver

DllUnregisterServer

DllUnregisterServer() in the export function which should be called for deregistering a COM server.
Instructs an in-process server to remove only those entries created through DllRegisterServer.

STDAPI DllUnregisterServer(void);

Return Values

This function supports the standard return values E_OUTOFMEMORY and E_UNEXPECTED, as well as the following:

  • S_OK The registry entries were created successfully.
  • S_FALSE Unregistration of this server's known entries was successful, but other entries still exist for this server's classes.
  • SELFREG_E_TYPELIB The server was unable to remove the entries of all the type libraries used by its classes.
  • SELFREG_E_CLASS The server was unable to remove the entries of all the object classes.

DllUnregisterServer Example

/* DllUnregisterServer - Removes entries from the system registry */

STDAPI DllUnregisterServer(void)
{
  HKEY hKey, hKey2;
  long lReturn;

  /* MathLib.Calculate.1 and sub keys */
  lReturn = RegOpenKeyEx(HKEY_CLASSES_ROOT,L"MathLib.Calculate.1",0,KEY_ALL_ACCESS ,&hKey);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }

  lReturn =  RegDeleteKey(hKey,L"CLSID");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegCloseKey(hKey);
  
  lReturn =  RegDeleteKey(HKEY_CLASSES_ROOT,L"MathLib.Calculate.1");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }

  /* MathLib.Calculate and sub keys */
  lReturn = RegOpenKeyEx(HKEY_CLASSES_ROOT,L"MathLib.Calculate",0,KEY_ALL_ACCESS ,&hKey);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }

  lReturn =  RegDeleteKey(hKey,L"CLSID");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn =  RegDeleteKey(hKey,L"CurVer");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegCloseKey(hKey);
  lReturn =  RegDeleteKey(HKEY_CLASSES_ROOT,L"MathLib.Calculate");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }

  lReturn = RegOpenKeyEx(HKEY_CLASSES_ROOT,L"CLSID",0,KEY_ALL_ACCESS ,&hKey);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn = RegOpenKeyEx(hKey,L"{F082638A-748A-44AC-ABB3-DC346B7F128A}",0,KEY_ALL_ACCESS ,&hKey2);
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }

  lReturn =  RegDeleteKey(hKey2,L"ProgID");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn =  RegDeleteKey(hKey2,L"VersionIndependentProgID");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn =  RegDeleteKey(hKey2,L"Programmable");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn =  RegDeleteKey(hKey2,L"InprocServer32");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  lReturn =  RegDeleteKey(hKey2,L"TypeLib");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }

  RegCloseKey(hKey2);
  lReturn =  RegDeleteKey(hKey,L"{F082638A-748A-44AC-ABB3-DC346B7F128A}");
  if(lReturn != ERROR_SUCCESS){
    return SELFREG_E_CLASS;
  }
  RegCloseKey(hKey);
  return S_OK;
}

regsvr32 dllunregisterserver

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

#