COM component Design and application (14)--Events and notifications (vc.net)

Source: Internet
Author: User

This article supporting source code

First, the preface

My COM component runs with a window, and when the user double-clicks the window, I need to notify the caller;

My COM component downloads a file on the network in a threaded way, and when I finish the task, I need to notify the caller;

My COM component completes a clock function, and when the scheduled time arrives, I need to notify the caller;

... ... ... ...

This read begins to say COM events, notices, connection points ... These contents are more, I divide two times (altogether four back) to introduce.

Ii. Methods of Notification

When the program party a internal incident occurred, need to inform party B, nothing more than using several methods:

Notification mode Simple description Comments
Direct message PostMessage ()

PostThreadMessage ()

Send a message to a window or thread I don't care when you do it.
SendMessage () Execute the message response function immediately The message handler function does not return without executing
SendMessage (Wm_copydata ...) When you send a message, you can also bring some custom data to the past It's more common, so it's a separate list.
Indirect message InvalidateRect ()

SetTimer ()

......

The called function sends some messages related to the There are too many functions like this.
callback function GetOpenFileName () ... Executes the callback function when the user changes the file selection Hey! Man, this is my phone, I have something to say.

In the era of COM, these methods basically can not play the turn, because ... You think the COM component is running in a distributed environment, the computer running on the other side of the Earth, how can you send a message to your window? Of course not! (But then again, for ActiveX components that can only run locally, you can certainly send window messages.) )

The way the callback function is designed is the basis for designing COM notification methods. The callback function, in essence, tells me the pointer to a function, and when I need it, I call the function directly, and the callback function does what I do, and I don't care. Well, ask you a question: What is the COM interface? Interface is actually a set of related functions (this definition is not rigorous, but you can understand it so well). Therefore, instead of using a "callback function" in COM, you use the "Callback interface" (more clearly, that is, using a bunch of packaged "callback function" sets), the callback interface, we also call the "receiver interface."

Figure I, client delivery sink interface pointer to COM. When an event occurs, the COM call sink interface function completes the notification

The features of this back-end sample program are:

The client initiates the component (simple11.ievent1.1) and obtains the interface pointer IEvent1 *;

The calling interface method Ievent1::advise () passes a receiver (sink) interface pointer (Icallback *) inside the client to the component server;

Call Ievent1::add () to compute the and of two integers;

But the results are not returned through the function, but are returned to the client via Icallback::fire_result ().

When the client no longer needs to accept the event, call the Ievent1::unadvise () disconnect and the component's contact.

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.