Generic DLL entry point

In-proc COM DCOM module are implemented as dynamic link libraries. DllMain() is the generic DLL entry point. OLE/COM framework loads the DLL using CoLoadLibrary(). CoFreeLibrary/CoFreeAllLibraries is the function to unload the same library. Client application never call these functions directly. DllMain() will be called with DLL_PROCESS_ATTACH when a COM client process is attached. Again DllMain() will be called with DLL_PROCESS_DETACH once COM process exits.

// DLL Entry Point

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  if (dwReason == DLL_PROCESS_ATTACH) {
    /* Client process attached */
  } else if (dwReason == DLL_PROCESS_DETACH)
    /* Client process detached */
  return TRUE;

COM/DCOM entry and exit

There are two additional functions exported from COM DLL which are COM specific entry and exit point function. DllGetClassObject() is the function entry point for all the clients for getting a instance of the class object. DllCanUnloadNow() is the exit point function of an in-process server.

file: COMEntryExitExample.def
LIBRARY      "COMEntryExitExample"


	DllGetClassObject       @1	PRIVATE
	DllCanUnloadNow         @2	PRIVATE


COM client retrieves the class object from a DLL object handler or object application. DllGetClassObject is not called directly by client application. However it called from COM/OLE framework through CoGetClassObject/CoCreateInstance functions. DllGetClassObject is a mandatory function to be exported from a in-proc DLL server.

STDAPI DllGetClassObject(
  REFCLSID rclsid,  //CLSID for the class object
  REFIID riid,      //Reference to the identifier of the interface
                    // that communicates with the class object
  LPVOID * ppv      //Address of output variable that receives the
                    // interface pointer requested in riid


  • rclsid - [in] CLSID that will associate the correct data and code.
  • riid - [in] Reference to the identifier of the interface that the caller is to use to communicate with the class object. Usually, this is IID_IClassFactory (defined in the OLE headers as the interface identifier for IClassFactory).
  • ppv - [out] Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppv contains the requested interface pointer. If an error occurs, the interface pointer is NULL.

Return Values

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

  • S_OK - The object was retrieved successfully.
  • CLASS_E_CLASSNOTAVAILABLE - The DLL does not support the class (object definition).


Determines whether the DLL that implements this function is in use. If not, the caller can safely unload the DLL from memory. This function is not called directly by client application. COM/OLE framework calls this function while CoFreeUnusedLibraries/ CoFreeUnusedLibrariesEx are executed inside the framework. Once S_OK is returned from the function, CoFreeLibrary() will be called to unload the DLL from memory.

This is an optional function to be exported from COM DLL. If this function is not implemented or not exported then there is no way to check if the DLL can be unloaded. DLL will not be unloaded even if no objects are alive. This will be unloaded at the last when CoUninitialize() is called by client.

STDAPI DllCanUnloadNow();



Return Values

  • S_OK - The DLL can be unloaded.
  • S_FALSE - The DLL cannot be unloaded now.

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.