Click here to Skip to main content
Click here to Skip to main content
Go to top

DCOM D-Mystified: A DCOM Tutorial, Step 4

, 11 Aug 2000
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Brian C Hart
Software Developer (Senior) Corrugated Technologies, Inc.
United States United States
From Fridley, Minnesota and I like computer programming! When I got started, I was working mostly with Windows GUI programming in C/C++. Then later on I worked with COM/DCOM for a school internship. I used COM/DCOM to write an ad hoc cluster server and job-running environment for a cluster of 24 Windows-based high-end visualization workstations. I moved on to C# and have been working in C# and Windows Forms ever since. I have yet to embrace Silverlight Smile | :)
Follow on   Twitter

Comments and Discussions

 
GeneralMake Sure and Configure DCOM on Windows XP Pro SP2 PinmemberBrian Hart26-Mar-05 9:05 
GeneralPlease Work Through the Tutorial in Order PinmemberBrian Hart9-Jan-05 23:18 
QuestionWhat about DLL server?? PinmemberMoh11-Jan-02 7:35 
AnswerRe: What about DLL server?? PinmemberBrian Hart11-Jan-02 11:27 
QuestionHaving a Question? PinmemberBrian Hart2-Jan-02 16:00 
GeneralMarshaling problem PinsussRana Iftikhar2-Oct-00 2:55 
GeneralRe: Marshaling problem PinmemberAndy19-Nov-00 2:02 
GeneralRe: Marshaling problem PinmemberBrian Hart19-Nov-00 9:15 
GeneralRe: Marshaling problem PinmemberMick Bythell20-Nov-00 1:16 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 12 Aug 2000
Article Copyright 2000 by Brian C Hart
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid