English 中文(简体)
Why does ATL COM map scanning code expect the first entry to be of _ATL_SIMPLEMAPENTRY type?
原标题:

ATL provides a bunch of macros for creating so-called COM maps - chains of rules of how the QueryInterface() call behaves on a given object. The map begins with BEGIN_COM_MAP and ends with END_COM_MAP. In between the the following can be used (among others):

  • COM_INTERFACE_ENTRY, COM_INTERFACE_ENTRY2 - to ask C++ to simply cast this class to the corresponding COM interface
  • COM_INTERFACE_ENTRY_FUNC - to ask C++ to call a function that will retrieve the interface

Now the problem is I want to use COM_INTERFACE_ENTRY_FUNC for every interface I expose so that I can log all the calls - I believe it will help me debugging my component when it is deployed in the field. The implementation of CComObjectRootBase::InternalQueryInterface contains an ATLASSERT:

ATLASSERT(pEntries->pFunc == _ATL_SIMPLEMAPENTRY);

which implies that the following is allright:

BEGIN_COM_MAP
    COM_INTERFACE_ENTRY( IMyInterface1 )
    COM_INTERFACE_ENTRY_FUNC( __uuidof(IMyInterface2), 0, OnQueryMyInterface2 )
END_COM_MAP

since here the first entry results in _ATL_SIMPLEMAPENTRY type entry but the following is not:

BEGIN_COM_MAP
    COM_INTERFACE_ENTRY_FUNC( __uuidof(IMyInterface1), 0, OnQueryMyInterface1 )
    COM_INTERFACE_ENTRY_FUNC( __uuidof(IMyInterface2), 0, OnQueryMyInterface2 )
END_COM_MAP

since here the entry type will not be _ATL_SIMPLEMAPENTRY.

This makes no sense at all. Why am I enforced into having a "please, C++, do the static_cast" entry as the first entry of the COM map?

Upd: Resolved after many more hour of debugging, answer added.

最佳回答

Inside ATL there s AtlInternalQueryInterface() that actually does scan the COM map. Inside it there s this code:

if (InlineIsEqualUnknown(iid)) // use first interface
{
    IUnknown* pUnk = (IUnknown*)((INT_PTR)pThis+pEntries->dw);
    // call AddRef on pUnk, copy it to ppvObject, return S_OK
}

this code actually relies on the first entry of the table being of _ATL_SIMPLEMAPENTRY type since it expects that _ATL_INTMAP_ENTRY::dw stores an offset from the current object this pointer to the necessary interface. In the use cited in the question if the first entry is this one:

COM_INTERFACE_ENTRY_FUNC( __uuidof(IMyInterface1), 0, OnQueryMyInterface1 )

the entry will be of wrong type, but the _ATL_INTMAP_ENTRY::dw will be zero (second parameter to the macro) and the code will happily work each time returning this pointer as IUnknown*. But if the second macro parameter which corresponds to a pass this value into the function specified as the third parameter variable is not zero the program will use that value as the offset and could run into undefined behaviour.

问题回答

暂无回答




相关问题
How can i add a button to all windows explorer instances?

I am trying to add a button to one of the existing tool bars in any windows explorer instance. After much research i figured out that BHO (browser helper objects) are the best way to hook to ...

Hunting memory leaks

I m finding leaked heap blocks by using the following command in WinDbg !heap –l With each leaked heap block I get, I m running to following to get the stack trace. !heap -p -a leakedheapblock The ...

Why use CComBSTR instead of just passing a WCHAR*?

I m new to COM. What exactly is the advantage of replacing: L"String" with CComBSTR(L"String") I can see a changelist in the COM part of my .NET application where all strings are replaced in this ...

IThumbnailProvider and IInitializeWithItem

I am trying to develop an IThumbnailProvider for use in Windows 7. Since this particular thumbnail would also be dependant on some other files in the same directory, I need to use something other than ...

Getting a byte array from out of process C++ COM to C#

What s the best way to get a chunk of memory (i.e. void*) from a COM server to C#? We have been using an IStream (using CreateStreamOnHGlobal) and passing that back, which worked. However when we ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

热门标签