Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DCOM D-Mystified: A DCOM Tutorial, Step 4

0.00/5 (No votes)
11 Aug 2000 1  
Here we add a method to our DCOM-remoted object, and start on implementing its functionality.

Introduction

Welcome to Step 4 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:

Remember, our steps in developing the software in this tutorial are as follows:

  • Step 1: Create the server, HelloServ, using the ATL COM AppWizard.
  • Step 2: Modify the starter files provided by AppWizard.
  • Step 3: Use the New ATL Object Wizard to add a simple COM object, the HelloWorld object, to the server.
  • Step 4: Modify the IHelloWorld interface to include a SayHello() method.
  • Step 5: Add an event method, OnSayHello(), to the connection point source interface, DHelloWorldEvents.
  • Step 6: Build the server, and install it on the server computer.
  • Step 7: Create a MFC client, HelloCli, which calls the server and handles the connection point event sink.

We're currently on Step 4 of this tutorial, where we finally add working code to our DCOM server. We'll add a method to the IHelloWorld interface, and we'll call this method SayHello(). This method will get the network name of the host that it's executing on, plus it will call a as-yet-unimplemented function Fire_OnSayHello(), which in Step 5 we'll add as an event to our DHelloWorldEvents event interface. This function will take a single [in] BSTR parameter, the name of the host. Anyway, enough of my jabber; let's plunge in:

Step 4: Modify the IHelloWorld Interface to Add the SayHello() Method

This step of the tutorial is really short. All we will do is add one method to our ISayHello interface, and implement it using the CHelloWorld ATL class. Then we'll be ready to move on to Step 5! Since the user of our client would like to have some indication as to what computer on their network this code ran on, we'll add some code to get the network name of that computer. The following listing, Listing 1, shows a piece of code which you can cut-and-paste into any application you wish. This code calls the Windows GetComputerName() function:

    TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;

    if (!GetComputerName(szComputerName, &dwSize))
    {
        // Display the cause of the error to the user with the _com_error class
        // To use the _com_error class, you need to #include <comdef.h> in
        // your STDAFX.H file
 
        AfxMessageBox(_com_error(GetLastError()).ErrorMessage(), MB_ICONSTOP);
        return /*whatever error code: -1 or E_FAIL or whatnot here*/;
    }

    // Now szComputerName holds this computer's name
Listing 1. Calling the GetComputerName() function.

Let's now add the IHelloWorld::SayHello() method, and then add its code. To do this, right-click the IHelloWorld interface icon in ClassView, and click Add Method. The Add Method to Interface dialog box appears. Type SayHello in the Method Name box, and leave the Return Type set to HRESULT.

TIP: When doing DCOM programming and adding methods to interfaces, *always* set the Return Type of the method to HRESULT.
This allows DCOM to report network errors and other status to the client.

Anyway, getting back to what we're doing, when you're done filling in the Add Method to Interface dialog box, it should look like that shown in Figure 1, below:

Adding the SayHello method to the IHelloWorld interface.
Figure 1. Adding the SayHello() method to the IHelloWorld interface.

Click OK. When you do, the Add Method to Interface dialog will add code in all the right places to make sure that when a call to the IHelloWorld::SayHello() method comes in over the wire, the CHelloWorld::SayHello() member function will get called and executed. After the method has been added, ClassView should resemble that shown in Figure 2 below:

ClassView after the SayHello method has been added.
Figure 2. ClassView after the SayHello() method has been added.

Look at Figure 2. See the highlighted item? Double-click that item in your ClassView to open up the CHelloWorld::SayHello() member function. This function implements the IHelloWorld::SayHello() method. Let's add some code, shown here in bold, to implement the method:

STDMETHODIMP CHelloWorld::SayHello()
{
    // 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

    // TODO: Add more code here

    return S_OK;
}
Listing 2. Adding code to implement the SayHello() method.

Notes From the Rear

Notice the line which says // TODO: Add more code here? This tells us that we're not done implementing yet; according to the design, this method should fire off some sort of event back to the client. To see how to do this, click Next to advance to Step 5, where we finish the implementation of the server and get ready to move on to the client. To go to the previous step, Step 3, click Back below. If you have questions, try clicking Questions and Answers to go to a page which might help you.

<< Back | Next >>

Questions and Answers

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here