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

DCOM D-Mystified: A DCOM Tutorial, Step 4

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
11 Aug 2000CPOL 157.7K   1.9K   43   9
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)


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:05
professionalBrian C Hart26-Mar-05 9:05 
GeneralPlease Work Through the Tutorial in Order Pin
Brian C Hart9-Jan-05 23:18
professionalBrian C Hart9-Jan-05 23:18 
QuestionWhat about DLL server?? Pin
11-Jan-02 7:35
suss11-Jan-02 7:35 
AnswerRe: What about DLL server?? Pin
Brian C Hart11-Jan-02 11:27
professionalBrian C Hart11-Jan-02 11:27 
QuestionHaving a Question? Pin
Brian C Hart2-Jan-02 16:00
professionalBrian C Hart2-Jan-02 16:00 
GeneralMarshaling problem Pin
Rana Iftikhar2-Oct-00 2:55
sussRana Iftikhar2-Oct-00 2:55 
GeneralRe: Marshaling problem Pin
19-Nov-00 2:02
suss19-Nov-00 2:02 
GeneralRe: Marshaling problem Pin
Brian C Hart19-Nov-00 9:15
professionalBrian C Hart19-Nov-00 9:15 
GeneralRe: Marshaling problem Pin
MickAB20-Nov-00 1:16
MickAB20-Nov-00 1:16 

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.