Click here to Skip to main content
14,576,731 members

Global Interface Table: An Easy Way to Marshal an Interface Pointer

Rate this:
4.65 (14 votes)
Please Sign up or sign in to vote.
4.65 (14 votes)
1 Mar 2007CPOL
An easy way to marshal your interface pointer between threads!
This is an old version of the currently published article.


Component Object Modeling (COM) and ActiveX Template Library (ATL) are the toughest areas in Windows programming, but as a Windows programmer, you daily encounter these and seldom face difficulty accessing a COM pointer created in one thread from another thread in a multithreaded environment. COM/ATL has the concept of marshalling and the usage of Global Interface table to cope up with this problem.

Here, I am demonstrating how to marshal an interface pointer from one thread to another worker thread with some quick code. ATL provides an interfaces called IGlobalInterfaceTable and a COM template class called CComGITPtr to help the programmer understand this concept easily..

In my example, I have a COM component with an interface IIGitTest, which I am going to marshal in the GitClient application. If you don't know how to create the COM component, please refer to my Step-by-Step Guide for Creating a COM component.

How to marshal the interface pointer from one apartment to another?

  1. First of all, we have to create a component exposing the IIGitTest with get/set property name and then create a MFC dialog based client application GitClient, which will access this interface from one apartment to another?
  2. Our component name is GitInterface. I will import the DLL using a #import directive since I don't want the namespace in the imported tlh and tli files, so I will use the no_namepace suffix with #import. I.e., in our example:
  3. #import "..\\output\\GitInterface.dll" no_namespace
  4. Now we can access our IIGitTest. We now add the member variable in our class like:
  5. IIGitTestPtr m_pToInterface;
  6. Allocate memory to our interface and don't forget to initialize the COM model in the application.
  7. CoInitialize(NULL);
    // initialize the com CLSID pclsid;
  8. Now, give some values to our interface pointer. This will help us check if we are able to marshal our interface properly.
  9. TCHAR szValues[256]; 
    CComBSTR bstrValues(szValues); 
  10. Now unleash our GIT Code in the client application.
  11. CComGITPtr<IIGITTEST> pToGITTest(m_pToInterface);
    DWORD dwGitCookie = pToGITTest.Detach();

    You must be wondering what this dwGitCookie will do and how the template class is working? CComGITPtr is a template wrapper class, which encapsulates the IGlobalInterfaceTable interface. We are passing our interface pointer to the template class CComGITPtr, and in return it is returning the DWORD cookie, which will help us find our interface in the global interface table.

  12. Now create a thread which will get the IIGitTest pointer from the Global Interface table.
  13. /// Create thread to try marshalling 
  14. Now, initialize COM inside the new thread, and retrieve the cookie identification which we have sent through the lpParam parameter.
  15. CoInitialize(NULL);
    DWORD dwCookie = reinterpret_cast<DWORD>(lpParam);
  16. Now create the CComGITPtr object and pass dwCookie as the parameter, which will instruct the CComGITPtr object to search GIT and retrieve the interface pointer of our interface.
  17. CComGITPtr<IIGITTEST>pToGITTest(dwCookie);
  18. Now, just copy the interface pointer to our interface pointer using the CComGITPtr::CopyTO function.
  19. IIGitTest *pToGitPtr;
  20. Now, retrieve the value. Remeber step 5.
  21. CComBSTR bstValue; 
    MessageBoxW(NULL,bstValue,L"From Git Interface",MB_OK);

    The End :)

In this way, you can marshal an interface pointer from one thread to an other without thinking about the technicality of marshalling and other related stuff. Let us revise the process again.

Quick Revision

  1. Create an interface pointer and attach it to the GIT template class
  2. Retrieve the cookie from the GIT object
  3. In another thread, using a DWORD cookie, find the interface in the GIT table
  4. Copy the object into an interface pointer
  5. Voila, now you can use your interface without any exception :)


  1. When to use the Global Interface Table
  2. CComGITPtr Class


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)

He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!

He started with Visual C++ then moved to C# then he become language agnostic, you give him task,tell him the language or platform, he we start immediately, if he knows the language otherwise he quickly learn it and start contributing productively

Last but not the least, For good 8 years he was Visual CPP MSMVP!

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.

Posted 1 Mar 2007


29 bookmarked