Creating the Publisher

team lib

The publisher is the entity that fires an event. It can be an application, as in this example, or it can be a component. COM+ doesnt limit your options in this area. The only thing the publisher must do is invoke the method required to fire the event contained within the event object. The sample application fires an event that broadcasts messages to all subscribers. Figure 10-4 shows the simple interface for this portion of the example.

click to expand
Figure 10-4: The publisher relies on a simple interface to fire an event that broadcasts a message to the subscribers.

As you can see, all you need to do is type a message and click Publish to publish the event. The code for this example looks remarkably similar to the code in Listing 8-6. However, instead of accepting input from the component, this example pushes data to the component in a manner similar to the example in Listing 8-2. You still need to add a reference to the type library for the event object type library, as shown here:

 //Importthetypelibraryandusetheassociatednamespace. #import "SimpleEventObject.TLB" named_guids usingnamespaceSimpleEventObject; 

The publishernot the subscribersmust reference the event object. Some developers get confused on this issue, and the result is that the publish/ subscribe sequence doesnt work as anticipated. Listing 10-2 shows the code to implement the Publish button for this example. You can find the complete listing for this example in the Chapter10\Publisher folder of the books companion content.

Listing 10-2: Simple event publisher
start example
 voidCPublisherDlg::OnBnClickedPublish() { ISendMsg*pSendMsg;//PointertoConstStringobject. _bstr_tOutput;//Temporarydatastring. CStringData;//Datafromthemessagebox. //InitializetheCOMenvironment. CoInitialize(NULL); //Createtheobject. CoCreateInstance(CLSID_SendMsg, NULL, CLSCTX_ALL, IID_ISendMsg, (void**)&pSendMsg); //Sendthemessage. MsgText.GetWindowText(Data); Output=Data; pSendMsg->FireBroadcastMsg(Output); //UninitializetheCOMenvironment. CoUninitialize(); } 
end example
 

As you can see, the code begins by creating an instance of the object. As usual, the CLSID_SendMsg and IID_ISendMsg values come from the TLH and TLI files created from SimpleEventObject.TLB during compilation.

Once the code has an instance of the object to use, it obtains the string typed by the user and places it in a _bstr_t value, Output . The publisher fires the event by calling on the FireBroadcastMsg() method with Output as an argument. As you can see, the publisher can exit at this point without knowing where the event information has gone (if it goes anywhere at all). If you tried the application now, it would succeed because COM+ doesnt require the presence of a subscriber. The publish/subscribe model allows zero, one, or multiple subscribers.

 
team lib


COM Programming with Microsoft .NET
COM Programming with Microsoft .NET
ISBN: 0735618755
EAN: 2147483647
Year: 2006
Pages: 140

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net