StdGlobalInterfaceTable provides a way for COM objects to be used from multiple threads in an application, when the threading model of the object doesn’t allow the cross-thread calls to be made from the pointer returned when the object was created.
The sequence of events is as follows:
- Create the com object on some thread.
- Get a StdGlobalInterfaceTable object.
- Register the interface with the Global Interface Table.
- Using the cookie returned when the object was registered, get a safe pointer from any thread.
- Use that pointer to make calls on the interface.
Create the COM object:
IDspPtr m_pDspX;
HRESULT hr = m_pDspX.CreateInstance(__uuidof(Dsp));
Now create a StdGlobalInterfaceTable object:
CComPtr<IGlobalInterfaceTable> pGIT;
hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&pGIT);
Now register our object with the GIT:
DWORD m_dsp_cookie;
hr = pGIT->RegisterInterfaceInGlobal(m_pDspX, __uuidof(IDsp), &m_dsp_cookie);
// m_dsp_cookie now gives us a way to access the dsp from different threads
Use the GIT to get a pointer to the object (in a function that can be called from any thread)
bool CPecoDsp::ReadProbe(int Mask)
{
IDspPtr pDsp;
HRESULT hr = pGIT->GetInterfaceFromGlobal( m_dsp_cookie, __uuidof(IDsp), (void**)&pDsp );
if(FAILED(hr))
{
pDsp = NULL;
return false;
}
return true;
}