Why do people use ATL to program COM?

Can you give an example of how the advantage of ATL is shown by comparison?

+6
com atl
source share
2 answers

It sounds like you need specific examples of where ATL makes COM programming easier; what happened to me.

  • CComObjectRootEx : CComObjectRootEx your COM classes from CComObjectRootEx , you get a 100 percent reference counter for free.

  • CComCoClass : This base class implements all the methods for instantiating your class, including the required IClassFactory material 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 IUnknown::QueryInterface in the right way for your requirements, whatever they are.

  • IDispatchImpl : If you want your components to be accessible to the script, you must implement IDispatch . ATL provides the IDispatchImpl class, which eliminates the need to execute it yourself.

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

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

+7
source share

Developers are lazy and prefer things that are easy to handle, and ATL was designed to facilitate COM development.

In most cases, ATL takes care of the many messy details of COM development, such as managing QueryInterface, reference counting, and lifecycle management, as well as all the various stream models supported by COM. It also provides built-in support for things like dual interfaces, connection points, counters, etc.

If you do not use ATL or something like this, you will write a lot more code. That would be unsuccessful;)

Edit:

I am not going to write COM code without ATL for an example because it is so terrible, but check this:

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

Download this and then view the following files in your simplecomserver project:

registry.cpp

simplecomserverImpl.cpp

(about 700 lines of code)

Now imagine that instead of writing all this monster, you can get a class from CCoComClass and simply implement the Name method, and then do this to process and register an instance of the class:

 // 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; } 
+3
source share

All Articles