Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / ATL

DCOM D-Mystified: A DCOM Tutorial, Step 5

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
11 Aug 2000CPOL 285.1K   2.1K   52   41
We look at connection points and set up the server's end of one. We'll also finish implementing our SayHello() method.

Introduction

Welcome to Step 5 of our DCOM tutorial. In this series, I will strip the mystique, the headache, and confusion from DCOM by giving you a comprehensive tutorial with a straightforward example. OK, no promises -- but I will give it a good try.

If you want to follow along with this tutorial and add code and use the Visual C++ Wizards as we go along, that's great. In fact, I very very highly recommend that, because otherwise this tutorial is a big waste of electronic ink (?). However, I follow along exactly with the tutorial myself, as I write it, and develop the code and use the Visual C++ wizards just as I say you should. The screenshots, in fact, are from my development of the files for each step! To download this already-developed code to compare with your own, simply click the 'Download the Step n Files - n KB" links at the top of each step. There's also an archive of the files for all the steps at the Questions and Answers page for this tutorial. I still recommend that you follow along with us as we go; this way, you can learn while you code. If you ever have problems along the way with this tutorial, feel free to:

Connection Points Demystified

Before we plunge in with Step 5 of our tutorial, let's just take a moment for me to rip the shrouds of mystery off of Connection Points. Figure 1 below shows a generic scenario which is true for COM, DCOM, and even function call backs, for goodness' sake.

A source and a sink.
Figure 1. A source and a sink.

This involves two objects, a "source" and a "sink". Think of the "source" like the water faucet of the kitchen sink at home. You turn a handle, and stuff comes out of it (hopefully water). Where does it go? If nothing's backed up, this water flows down into the bottom and goes into the drain (which can be thought of as the "sink"). OK, so things flow from the source, to the sink. In the kitchen sink analogy above, this is water. However, I've never seen a computer network system run with water flowing across the wires, so obviously something else is at work in DCOM.

In DCOM, there is a "client," somewhere on the network, and there is a "server," also somewhere on the network. Without the use of connection points, things flow only one way: method calls replace our water, the client replaces our faucet, and the server replaces the drain. This is way oversimplifying things, but the user "turns a handle" (that is, clicks a button, for example), and "stuff" (that is, method calls) "comes out of" the client. This "stuff that comes out" then "flows" over the network using DCOM. These calls "flow" to the server, which then collects them and acts like the "drain", or our sink. Here's Figure 2, which is almost exactly like Figure 1, but puts the client in place of the "source" and the server in place of the "sink," with the network in between:

Our client and server as the source and the sink.
Figure 2. Our client and server as the source and the sink.

OK, so now we have method calls flowing like water; wonderful. However, when the client calls methods, the server does all kinds of things that might be interesting to clients. So the server fires events all over the place. If our client doesn't care if the server fires events, it will just ignore them. However, if it cares, it will Advise() the server. Then the source-sink relationship of Figure 2 can be thought of in reverse:

The reverse of Figure 2.
Figure 3. The reverse of Figure 2.

Connection points come in when you have the following happening:

  1. The client is the source of a method call,
  2. The server sinks (that is, acts as the sink for) the method call.
  3. An "event call" comes out of the now-server-as-source.
  4. The client sinks the event call and does something.

As you can see, this is a round-trip. A method call goes from the client to the server, and then an event call goes from the server, to the client, as seen in Figure 4.

A round-trip.
Figure 4. A round-trip.

The Advise() step is done before item number 1 above, and the Unadvise() step (where the client goes back to being aloof) happens after item number 4. The points of contact on both the client and server and the Advise()ing and Unadvise()ing that happens all together form...

A CONNECTION POINT!!

Whew... what a revelation... Let's start Step 5 before I get too carried away...

Step 5: Add the OnSayHello Event to the Event Source Interface DHelloWorldEvents

Let's plunge in, shall we? To add an event to the source, it's really, really easy. Just use the Visual C++ Wizards! Open up ClassView, and right-click the DHelloWorldEvents icon, and then click Add Method. The Add Method to Interface dialog box appears. Type OnSayHello in the Method Name box, and type [in] BSTR bstrHost in the Parameters box, as shown in Figure 5, below.

Adding the OnSayHello event to the DHelloWorldEvents event interface.
Figure 5. Adding the OnSayHello() event to the DHelloWorldEvents event interface.

Once you're done, click OK. ClassView should resemble Figure 6 below. Now click FileView, and find the HelloServ.idl file, under the Source Files folder. Right-click that baby, and then choose Compile. Watch the compiler work away in the Output window, and wait until the build is complete.

ClassView after adding the OnSayHello event.
Figure 6. ClassView after adding the OnSayHello event.

Once the build has been finished, click on ClassView. Right-click the CHelloWorld class, and then click Implement Connection Point. The Implement Connection Point dialog box appears. If you haven't compiled the IDL file yet like I told you to, Visual C++ will prompt you to do so. Figure 7, below, shows you how to select that you want to make the server able to fire off its OnSayHello event:

Image 7
Figure 7. Specifying that we want to implement Connection Points for the DHelloWorldEvents event interface.

When everything looks like Figure 7, click OK. The Visual C++ IDE will now generate all the server-side code you need for Connection Points. Each time you change an event in the DHelloWorldEvents event interface, you need to do the (1) compile the IDL, (2) right-click CHelloWorld and choose Implement Connection Point, (3) check the box by DHelloWorldEvents, and (4) click OK steps.

Notes From the Rear

The final part of Step 5 which we have to take care of is firing the event. Remember, we declared the OnSayHello() event in the IDL file as:

HRESULT OnSayHello(BSTR bstrHost);
Listing 1. The declaration of the OnSayHello() event.

To fire the event from any CHelloWorld member function, just call Fire_OnSayHello(). It's a member function of a new base class, CProxyDHelloWorldEvents< > that the Implement Connection Points dialog box just added for us. To this end, let's add code to the CHelloWorld::SayHello() function to fire the event to the client:

STDMETHODIMP CHelloWorld::SayHello()
{
    USES_CONVERSION;

    // Get the network name of this computer
    TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;

    if (!GetComputerName(szComputerName, &dwSize))
        return E_FAIL;    // failed to get the name of this computer

    // Say Hello to the client
    Fire_OnSayHello(T2OLE(szComputerName));

    return S_OK;
}
Listing 2. Code to add to finish the CHelloWorld::SayHello() member function.

That's it! We're finished with Step 5. Click Next to go on to Step 6, or click Back to step back to Step 4 if you're browsing through the tutorial. If you have any questions, try clicking on Questions and Answers to go to the page with the good stuff, and then e-mail me at brian@harttechservices.com if you're still stuck.

<< Back | Next >>

Questions and Answers

License

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


Written By
Team Leader
United States United States
Brian C. Hart, Ph.D., is a strategic engagement leader on a mission to leverage space technology to protect U.S. interests and assets against adversaries. Throughout Dr. Hart's career, he has enjoyed: Working closely with business executives to provide strategic direction and leadership, translating customer and competitive intelligence into compelling capture strategies and solutions, and mentoring teams to enhance individual and company capabilities while fostering an engaging and accountable environment, being involved in STEAM initiatives and education to develop greater awareness in the community, and serving the armed forces with the U.S. Navy and U.S. Army National Guard. He is excited to begin developing his career in Jacobs's Critical Mission Systems business unit, supporting NORAD and the U.S. Space Force.

Comments and Discussions

 
GeneralMake Sure and Configure DCOM on Windows XP Pro SP2 Pin
Brian C Hart26-Mar-05 9:09
professionalBrian C Hart26-Mar-05 9:09 
GeneralPlease Work Through the Tutorial Pin
Brian C Hart9-Jan-05 23:17
professionalBrian C Hart9-Jan-05 23:17 
GeneralAdd Connection Point wizard fails to implement Fire_OnSayHello Pin
Kza Wah5-Jan-05 0:49
Kza Wah5-Jan-05 0:49 
GeneralRe: Add Connection Point wizard fails to implement Fire_OnSayHello Pin
Brian C Hart9-Jan-05 23:31
professionalBrian C Hart9-Jan-05 23:31 
GeneralRe: Add Connection Point wizard fails to implement Fire_OnSayHello Pin
E. del Ayre5-Feb-07 21:17
E. del Ayre5-Feb-07 21:17 
GeneralXMLDOM.IDL solution Pin
Robert Bielik18-Mar-04 2:41
Robert Bielik18-Mar-04 2:41 
GeneralRe: XMLDOM.IDL solution Pin
Anonymous23-Mar-05 6:19
Anonymous23-Mar-05 6:19 
GeneralT2OLE macro unknown Pin
Stober2-Jan-04 5:08
Stober2-Jan-04 5:08 
GeneralRe: T2OLE macro unknown Pin
shuklao26-Apr-04 4:12
shuklao26-Apr-04 4:12 
GeneralRe: T2OLE macro unknown Pin
lasombra25-Nov-04 22:33
lasombra25-Nov-04 22:33 
GeneralRe: T2OLE macro unknown Pin
sanangel28-Jan-05 23:52
sanangel28-Jan-05 23:52 
GeneralRe: T2OLE macro unknown Pin
Brian C Hart29-Jan-05 8:29
professionalBrian C Hart29-Jan-05 8:29 
sanangel wrote:
How to use USE_CONVERSION ,can you get a sample to me? thanks, I also had to use mbstowcs! So i want know how to use USE_CONVERSION!

Look in Listing 2 above (at http://www.codeproject.com/com/hellotutorial5.asp).

Sincerely Yours,
Brian Hart
Department of Physics and Astronomy
University of California, Irvine
Generalxmldom.idl Missing Pin
elderdo2-Dec-03 11:16
elderdo2-Dec-03 11:16 
GeneralRe: xmldom.idl Missing Pin
Brian C Hart2-Dec-03 11:50
professionalBrian C Hart2-Dec-03 11:50 
GeneralRe: xmldom.idl Missing Pin
elderdo4-Dec-03 4:07
elderdo4-Dec-03 4:07 
GeneralRe: xmldom.idl Missing Pin
Brian C Hart9-Jan-05 23:29
professionalBrian C Hart9-Jan-05 23:29 
GeneralImplementation of Fire_OnsayHello Pin
Anonymous4-Feb-03 11:17
Anonymous4-Feb-03 11:17 
GeneralRe: Implementation of Fire_OnsayHello Pin
Brian C Hart3-Jan-04 11:12
professionalBrian C Hart3-Jan-04 11:12 
GeneralI've a prob with the wizard creating IID_DHalloWeltEvents Pin
3-Jul-02 12:53
suss3-Jul-02 12:53 
GeneralRe: I've a prob with the wizard creating IID_DHalloWeltEvents Pin
Schnemar10-Jul-02 7:40
Schnemar10-Jul-02 7:40 
GeneralRe: I've a prob with the wizard creating IID_DHalloWeltEvents Pin
9-Sep-02 9:16
suss9-Sep-02 9:16 
GeneralRe: I've a prob with the wizard creating IID_DHalloWeltEvents Pin
Brian C Hart9-Jan-05 23:22
professionalBrian C Hart9-Jan-05 23:22 
GeneralYo, Yo! Pin
Philip Patrick2-Jun-02 20:55
professionalPhilip Patrick2-Jun-02 20:55 
GeneralRe: Yo, Yo! Pin
Brian C Hart9-Jan-05 23:24
professionalBrian C Hart9-Jan-05 23:24 
GeneralThread context. Pin
Giles29-Mar-02 11:58
Giles29-Mar-02 11:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.