tags:

views:

76

answers:

2

Can you provide an example that through comparation shows the benifit of ATL?

+1  A: 

Developers are lazy and prefer things that are easy over things that are hard, and ATL was designed to make COM development easier.

In most cases ATL takes care of many of the messy details of COM development, like QueryInterface management, reference counting and lifetime management, and all of the various threading models that COM supports. It also provides built-in support for things like dual-interfaces, connection points, enumerators, etc.

If you don't use ATL, or something like it, you'll be writing a lot more code. That would be unfortunate ;)

Edit:

I'm not going to write any COM code without ATL for an example because it is so horrendous to do so, but check out this:

http://www.codeproject.com/KB/COM/simplecomserver.aspx

Download that and then look at the following files under the simplecomserver project:

registry.cpp

simplecomserverImpl.cpp

(around 700 lines of code)

Now imagine that instead of writing all of that monstrosity, you could derive a class from CCoComClass and just implement the Name method, and then do this to handle class instantiation and registration:

// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
    return _AtlModule.DllCanUnloadNow();
}


// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}


// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
    // registers object, typelib and all interfaces in typelib
    HRESULT hr = _AtlModule.DllRegisterServer();
    return hr;
}


// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
    HRESULT hr = _AtlModule.DllUnregisterServer();
    return hr;
}
Gerald
Can you provide an example?
wamp
An example of what?
Gerald
Using ATL to ease the developing?
wamp
@wamp: Let's pretend you need to write a component with 20 COM-exposed classes. With ATL you just inherit all those classes from the same set of templates, implement only methods that were introduced in your interfaces and add them to the so-called COM map. That's something like two hundred lines of relatively clean extra code - and ATL does all the rest. Now try to do this without ATL - you will succeed one day.
sharptooth
Do I still need to call `CoInitialize(NULL)` and `CoUninitialize()` if I switch to **ATL**?
wamp
+2  A: 

It sounds like you want some concrete examples of where ATL simplifies COM programming; the following are what occurred to me.

  • CComObjectRootEx: By deriving your COM classes from CComObjectRootEx, you get thread-safe reference-counting free of charge.

  • CComCoClass: This base class implements all the methods for instantiating your class, including the IClassFactory stuff that is required for clients to use CoGetClassObject with your component.

  • COM_INTERFACE_ENTRY macros: ATL offers a whole bunch of macros to use between BEGIN_COM_MAP and END_COM_MAP, which you can use to implement the guts of IUnknown::QueryInterface in the correct manner for your requirements, whatever they may be.

  • IDispatchImpl: If you want your components to be accessible to script, you must implement IDispatch. ATL provides the IDispatchImpl class, which saves the hassle of implementing it yourself.

  • CComPtr / CComQIPtr: ATL provides these smart pointer classes, which encapsulate calls to IUnknown::AddRef, IUnknown::Release and IUnknown::QueryInterface. Using them will make your code easier to read and less prone to COM reference-counting bugs.

  • CComBSTR / CComVariant: ATL provides these classes, which reduce the complexity of dealing with the BSTR and VARIANT COM types.

Phil Booth
WHich header files/libraries do I need to include to use ATL?
wamp
That sounds like a separate question..... :)
Phil Booth
Do I still need to call `CoInitialize(NULL)` and `CoUninitialize()` if I switch to **ATL**?
wamp
That also sounds like another question. Seriously, I'm not trying to be unhelpful, but this site will work much better for you if you ask every question separately, on its own page. :)
Phil Booth