Use a global interface table to transfer com interfaces in different threads

Source: Internet
Author: User

In MultithreadingProgramUsing COM objects in is really a headache. When you can access an interface pointer, it does not mean that you can call methods on the interface. I have never really understood several thread modes of COM. The problem is that when I tried to call a COM interface method in a thread and this COM interface was created in another thread, it never succeeded. Even worse, there are no error messages.
Passing interfaces in multiple threads requires additional work. The so-called Marshal interface method is introduced in various books, but I have never used it. A simpler method is to use a global interface table (globalinterfacetable ).
Globalinterfacetable allows you to access the COM interface created in any thread anywhere. Globalinterfacetable is a COM Object and implements the iglobalinterfacetable interface. This interface has three methods for registering an interface, obtaining an interface, and canceling an interface. The following example describes how to use globalinterfacetable.
First, when you create a COM interface that needs to be used by other threads, register it with globalinterfacetable.

Ccomptr < Imyinterface > Spmyinterface;
Spmyinterface. cocreateinstance ();
// Register interface in global interface table
Ccomptr < Iglobalinterfacetable > Spgit;
Spgit. cocreateinstance (clsid_stdglobalinterfacetable );
If (Spgit)
{
Spgit->Registerinterfaceinglobal (spmyinterface, iid_imyinterface,&M_dwcookie );
}

A cookie is returned during registration. Remember this cookie and obtain the interface through this cookie when any thread needs to use the previous interface.

Ccomptr < Imyinterface > Spmyinterface;

If (M_dwcookie ! = 0 )
{
Ccomptr < Iglobalinterfacetable > Spgit;
Spgit. cocreateinstance (clsid_stdglobalinterfacetable );
If (Spgit)
{
Spgit->Getinterfacefromglobal (m_dwcookie, iid_imyinterface ,(Void**)&Spmyinterface. P );
}
}

If (Spmyinterface)
{
//Call my interface
}

Finally, as a responsible programmer, close the previously registered interface.

If (M_dwcookie ! = 0 )
{
Ccomptr < Iglobalinterfacetable > Spgit;
Spgit. cocreateinstance (clsid_stdglobalinterfacetable );
If (Spgit)
{
Spgit->Revokeinterfacefromglobal (m_dwcookie );
M_dwcookie= 0;
}
}

Note that every time we create a new globalinterfacetable instance, because it is a COM object and its pointer cannot be passed between different threads.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.