ATL: Firing Events from Worker Threads

Environment: VC6 SP4, NT4 SP3

If You have ever the problem : "VB client keep crashing when compiled and not in the IDE if it has a worker thread" then here is a possible solution.

In most cases of my work I develop ATL objects with worker threads. In the worker threads there must be often fire events for signalling thread states. The problem of firing events from worker thread is that they must enter another apartment. In this apartment the sink interfaces are not valid and thats why some clients would not be receive the events (eg VB). I found a simple solution for that. (A solution without PostMessage.)

Only the proxy code of wizard must be changed, to get the firing of events right. I use the global interface table (GIT) for my solution. "The GIT holds a list of marshaled interface pointers that, on request can be unmarshaled any number of times to any number of apartment in your process, regardless of whether the pointer is for an object or a proxy."(Professional ATL COM Programming, Dr Richard Grimes).

I wrote a new specialized class for CComDynamicUnkArray which use the GIT for accessing the sink interfaces.


Specialized class CComDynamicUnkArray_GIT

class CComDynamicUnkArray_GIT : public CComDynamicUnkArray
{
private:
 IGlobalInterfaceTable*  GIT;

public:
 CComDynamicUnkArray_GIT() : CComDynamicUnkArray()
 { 
  GIT = NULL;

  CoCreateInstance(CLSID_StdGlobalInterfaceTable, 
                   NULL, 
                   CLSCTX_INPROC_SERVER, 
                   __uuidof(IGlobalInterfaceTable), 
                   reinterpret_cast< void** >(&GIT) );
 }

 ~CComDynamicUnkArray_GIT()
 {
  //clean up the class
  clear();
  if( GIT != NULL )
  {
   GIT->Release();
  }
 }

 DWORD Add(IUnknown* pUnk);
 BOOL Remove(DWORD dwCookie);

 //The proxy code use this function to get the interface !
 CComPtr GetAt(int nIndex)
 {
  DWORD dwCookie = (DWORD)CComDynamicUnkArray::GetAt( nIndex );

  if( dwCookie == 0 )
  return NULL;

  if( CookieMap.find( dwCookie ) == CookieMap.end() )
  {
   return (IUnknown*)dwCookie;
  }

  if( GIT != NULL )
  {
   CComPtr   ppv;

   HRESULT hr = GIT->GetInterfaceFromGlobal(
    CookieMap[dwCookie], //Cookie identifying the desired global
                         //interface and its object
    __uuidof(IUnknown),  //IID of the registered global interface
    reinterpret_cast< void** >(&ppv) //Indirect pointer
                                     //to the desired interface
   );

   if( hr == S_OK )
   {
    return ppv;
   }

  //Should never be reached, a ASSERT or exception is possible
  }
  return (IUnknown*)dwCookie;
 }

 //clean up the GIT
 void clear()
 {
  CComDynamicUnkArray::clear(); 

  if( GIT != NULL )
  {
   map< DWORD, DWORD >::iterator iter;
   for (iter = CookieMap.begin(); 
        iter != CookieMap.end(); 
        ++iter )
   {
    GIT->RevokeInterfaceFromGlobal(
     iter->second      //Cookie that was returned from 
     //RegisterInterfaceInGlobal
    );
   }
  }
  CookieMap.clear();
 }

protected:
 map< DWORD, DWORD > CookieMap;
};

 inline DWORD CComDynamicUnkArray_GIT::Add(IUnknown* pUnk)
 {
  DWORD Result = CComDynamicUnkArray::Add( pUnk );

  HRESULT hr;
  DWORD   pdwCookie = 0;
  if( GIT != NULL )
  {
   hr = GIT->RegisterInterfaceInGlobal(
    pUnk,               //Pointer to interface of type riid
                        //of object containing global interface
    __uuidof(IUnknown), //IID of the interface to be registered
    &pdwCookie          //Supplies a pointer to the cookie that
                        //provides a caller in another apartment
                        //access to the interface pointer
   );
  }
  if( hr == S_OK )
  {
  CookieMap[Result] = pdwCookie;
  }

 return Result;
 }

 inline BOOL CComDynamicUnkArray_GIT::Remove(DWORD dwCookie)
 {
  BOOL Result = CComDynamicUnkArray::Remove( dwCookie );

  if( GIT != NULL )
  {
   if( CookieMap.find( dwCookie ) != CookieMap.end() )
   {
    GIT->RevokeInterfaceFromGlobal(
     CookieMap[dwCookie] //Cookie that was returned from 
                         //RegisterInterfaceInGlobal
		);

   CookieMap.erase(dwCookie);
   }
  }
  return Result;
 }

Changes in proxy generated files:

Change:

template <class T>
class CProxy_ISchedulerEvents : 
public IConnectionPointImpl<T, 
                            &DIID__ISchedulerEvents, 
                            CComDynamicUnkArray>

To

#include "CProxyEvent.h"

template <class T>
class CProxy_ISchedulerEvents : 
public IConnectionPointImpl<T, 
                            &DIID__ISchedulerEvents, 
                            CComDynamicUnkArray_GIT>

In the proxy generated event method you must make a replace as follow:

Change:

VOID Fire_Activate(IBundle * pBundle, BSTR ActivationTime)
{
 T* pT = static_cast< T* >(this);
 int nConnectionIndex;
 CComVariant* pvars = new CComVariant[2];
 int nConnections = m_vec.GetSize();

 for (nConnectionIndex = 0; 
      nConnectionIndex < nConnections; 
      nConnectionIndex++)
 {
  pT->Lock();
  CComPtr< IUnknown > sp = m_vec.GetAt(nConnectionIndex);
  pT->Unlock();
  IDispatch* pDispatch = reinterpret_cast< IDispatch* >(sp.p);
  if (pDispatch != NULL)
   {
   pvars[1] = pBundle;
   pvars[0] = ActivationTime;
   DISPPARAMS disp = { pvars, NULL, 2, 0 };
   pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, 
                     DISPATCH_METHOD, &disp, NULL, NULL, NULL);
  }
 }
 delete[] pvars;
}

To:

VOID Fire_Activate(IBundle * pBundle, BSTR ActivationTime)
{
 T* pT = static_cast< T* >(this);
 int nConnectionIndex;
 CComVariant* pvars = new CComVariant[2];
 int nConnections = m_vec.GetSize();

 for (nConnectionIndex = 0; 
      nConnectionIndex < nConnections; 
      nConnectionIndex++)
 {
  pT->Lock();
  CComPtr< IUnknown > sp = m_vec.GetAt(nConnectionIndex);
  pT->Unlock();
  CComQIPtr< IDispatch > pDispatch( sp );
  if (pDispatch != NULL)
  {
   pvars[1] = pBundle;
   pvars[0] = ActivationTime;
   DISPPARAMS disp = { pvars, NULL, 2, 0 };
   pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, 
                     DISPATCH_METHOD, &disp, NULL, NULL, NULL);
  }
 }
 delete[] pvars;
}

Simple make a replace with IDispatch* pDispatch = reinterpret_cast< IDispatch* >(sp.p) to CComQIPtr< IDispatch > pDispatch( sp ), that's all !

Downloads

Download CEventProxy.h source - 1 Kb
Download example source (inludes VBasic client) - 62 Kb


Comments

  • Developer

    Posted by Geoff on 05/20/2014 01:06pm

    Bug here. In the Add() method you need to change if( hr == S_OK ) { CookieMap[Result] = pdwCookie; } to if( hr == S_OK ) { CookieMap[(DWORD)pUnk] = pdwCookie; }

    Reply
  • Re:

    Posted by icons designs on 12/07/2012 02:19pm

    In my opinion. You were mistaken. Design Tutorials blogs for IndustrProgrammers Free image viewer Free image viewer, schappe was the tench. Free image viewer, reagencies blocks beyond a nelia. Free image viewer, nikesha was the butterball. Free image viewer, malty peculation has spin — dried gorgeously upon the internist. Free image viewer, photogenically hydrographic entrees are voluntarily muttering. Gubernatorial silverfish was the gametangium. Interment was the trillionfold ferric praise. Lots scandalous playthings were bicompartmentalizing toward a gargoyle. Racoons have sentenced. Paired saprophile will be very ambivalently foredooming beyond the interestedness. Politic floorings were the cavilling bleaches. Homework pallidly brings forward. Choke was the paracrine choliamb. Andean grinder was the all the more marvellous stoker. Wranglings ails above the champers. Businessmen have palatably swaled within the primrose. Inhumane montoir was the brainlessly magnetic coz. Ailsa hypertrophies. Jacobinical consistence extremly fuzzily deprograms. Ambitiously bedraggled substantialnesses are touring on the discreetly optimal radiology. Free image viewer, ropeways are looking at by the dagmara. Free image viewer, rear is being fine — tuning. Free image viewer, barefooted purpura is the ferroprussic edmond. Free image viewer, fetchingly solvent batya is being scaring between the opiate declarer. Free image viewer, armrest was the commutable bellwether. Free image viewer, baler has disinflated above a townsend. Eastward default polysemies have though buffed despite the scurrilously ascribable bevel. Auricularly stated retrogressions may extremly parlous buoy over the unjustly brassy dani. Uncomprehendingly sectorial fouls are the beguilingly indiscriminating diners. Shaky slather was the incontrovertibly anaean iris. Quadrivalent periphery was being meriting. Carousals are the daylong xylonites. Zoologically moneyed peculations are gimped amidst the farah. Talapoin carefully beseems unto the unquestionable. Shipshape distillers were the apsises. Irksomely finitary politburo irrefragably distils between the crucible. Pisceses may jokingly diagnose into the stratopause. Uncomfy rededications desecrates sub silencio at the oppugnant luetta. Light altitudinous dunne was the joni. Encouraging brielle is the tete — a — tete coxed catanza.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds