Solve the multi-thread access problem of COM interface with window message

Source: Internet
Author: User
In the previous article, I talked about the multi-thread access problem of the COM interface and solved it using the global interface table method. But sometimes we cannot directly access the interface pointer, but indirectly access through an encapsulation class. For example: Class Someclass
{
Private :
Imyinterface * M_pint;
Public :
Void Method1 ()
{
// Init m_pint
}
Void Method2 ()
{
// Call method of m_pint
}
}

We can only access the public methods of someclass, but cannot directly access the interface pointer. In this case, we cannot use the global interface table method. If you need to call the someclass method in different threads, the only way is to put all the calls in one thread. How can we achieve this? Using a message window for synchronization is a simple method.
First, define a window class and define all operations on someclass as window messages, as shown below:

# Define Wm_method1 wm_user + 100
# Define Wm_method2 wm_user + 101

ClassCthreadwnd:PublicCsf-wimpl<Cthreadwnd>
{
Private:
Someclass m_someclass;
Public:
Begin_msg_map (cthreadwnd)
Message_handler (wm_method1, onmethod1)
Message_handler (wm_method2, onmethod2)
End_msg_map ()

Lresult onmethod1 (uint /* Umsg */ , Wparam /* Wparam */ , Lparam /* Lparam */ , Bool &   /* Bhandled */ );
Lresult onmethod2 (uint /* Umsg */ , Wparam /* Wparam */ , Lparam /* Lparam */ , Bool &   /* Bhandled */ );
}

The m_someclass method is called in onmethod1 and onmethod2. If the method has parameters, it is passed through wparam and lparam.

Then, we need to create this window in a thread. When you need to call the someclass method, messages are indirectly sent to the cthreadwnd window. For example:

Cthreadwnd wndthread;

//In thread
Wndthread. sendmessage (wm_method1 );

//In thread B
Wndthread. sendmessage (wm_method2 );

As for wndthread, it can be stored anywhere. The thread function used to create a thread windowCodeBasically it should be like this:

Uint winapi threadproc (lpvoid lpparam)
{
: Coinitialize ( 0 );

Cthreadwnd*Pwnd=(Cthreadwnd*) Lpparam;

Cmessageloop theloop;
_ Module. addmessageloop (&Theloop );

Pwnd->Create (null, crect (0,0,0,0), Null, ws_popup );

Theloop. Run ();

Pwnd->Destroywindow ();

_ Module. removemessageloop ();

: Couninitialize ();

Return 0;
}

The code of the thread to be created will not be pasted. To destroy the thread, you only need to send the wm_quit message to the thread window. This method is very effective in practical applications.
One problem is that when there are too many method parameters, it is difficult to pass them through wparam and lparam. In this case, you need to define another structure, store various parameters, and then pass the structure address through wparam. In short, people are active and flexible to use: P

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.