Sunday, November 30, 2008

The Main C++ Module


When we ran the ATL COM Appwizard, we chose to create a DLL-based server and we chose not to use MFC. The first selection screen of the

wizard determined the overall configuration of the server.

The AppWizard created a standard DLL module. This type of standard DLL does not have a WinMain application loop, but it does have a DllMain

function used for the initialization of the DLL when it gets loaded:

CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)

OBJECT_ENTRY(CLSID_BeepObj, CBeepObj)

END_OBJECT_MAP()

////////////////////////////////////////

// DLL Entry Point

extern "C"

BOOL WINAPI DllMain(HINSTANCE hInstance,

DWORD dwReason, LPVOID /*lpReserved*/)

{

if (dwReason == DLL_PROCESS_ATTACH)

{

http://devcentral.iticentral.com/articles/DCOM/intro_DCOM/part2/3.php (1 of 6) [7/9/2001 2:52:53 PM]

DevCentral - Understanding DCOM - Part II

_Module.Init(ObjectMap, hInstance);

DisableThreadLibraryCalls(hInstance);

}

else if (dwReason == DLL_PROCESS_DETACH)

_Module.Term();

return TRUE; // ok

}

All the DllMain function really does is check to see if a client is attaching to the DLL and then does some initialization. At first glance, there is no

obvious indication that this is a COM application at all.

The COM portion of our new server is encapsulated in the ATL class CComModule. CComModule is the ATL server base class. It contains all the

COM logic for registering and running servers, and for starting and maintaining COM objects. CComModule is defined in the header file

"altbase.h". This code declares a global CComModule object in the following line:

CComModule _Module;

This single object contains much of the COM server functionality for our application. Its creation and initialization at the start of program

execution sets a chain of events in motion.

ATL requires that your server always name its global CComModule object "_Module". It is possible to override CComModule with your own

class, but you are not allowed to change the name.

If we had chosen an executable-based server, or even a DLL with MFC, this code would be significantly different. There would still be a

CComModule based global object, but the entry point of the program would have been WinMain(). Choosing a MFC-based DLL would have

created a CWinApp-based main object.


0 comments: