Welcome to step 2 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 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 (coming soon) of the files for all the steps at the Questions and Answers page (coming soon) 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 have problems along the way with this tutorial, feel free to:
- Post a message to the message board at the bottom of this page.
- Check out this tutorial's Questions and Answers page - coming soon.
A diagram of how our software will eventually work is shown in Figure 1. The client calls a method on the server, which then fires an event back to the client using a connection point. This connection point's event sink is implemented in the client (using MFC and ClassWizard!!!), and the client shows its user a message telling the user that the server said "Hello!":
Figure 1. Diagram of our DCOM client/server set-up.
Remember, our steps in developing the software in this tutorial are as follows:
- Step 1: Create the server, HelloWorldServ.NET, using the ATL project wizard.
- Step 2: Modify the starter files provided by the ATL project wizard.
- Step 3: Add a simple ATL object,
HelloWorld, to the server, to expose our functionality.
- Step 4: Add a method,
SayHello(), to the server, which fires the event which the client handles.
- Step 5: We look at the connection points and set up the server's end of one.
- More steps coming soon!
We're currently on step 2 of the tutorial, where we modify some things in the starter source code provided to us by the ATL project wizard. We do this to:
- Add a user-friendly 'service name' to make it easier to find our service in the Control Panel;
- Take a moment to tour the starter files that have been generated for us.
- Add code to initialize the security properly.
Step 2: Modify the Starter Files Provided by the ATL Project Wizard
To start, we need to provide Windows with a user-friendly name it can use to display this service to the user. This is the service's "service name" and is what's listed when you open the Services icon under the Administrative Tools folder in Control Panel. To do this, we will change the String Table entry loaded by the ATL project wizard-generated starter code.
The next thing to do is to modify the
IDS_SERVICENAME, to hold the service name we want to use. Figure 2, shown below, illustrates adding the String Table entry. To do this, complete these steps:
- In the Solution Explorer, click the Resource View tab.
- Double-click the String Table folder to open it.
- Double-click the String Table icon in the folder to open the String Table.
- Find the blank entry in the list, and double-click it. The Properties window appears, as in Figure 2, below.
- In the ID box, type
- Press Tab to move to the Caption box, which contains what the
IDS_SERVICENAME symbol maps to in your code.
- In the Caption box, type Hello World.NET Service, as shown in Figure 2.
- Press Enter. This saves the new String Table entry to the String Table.
Figure 2. Modifying the service name by changing the caption of the
IDS_SERVICENAME entry to the String Table.
The above steps merely serve to demonstrate where the name of the service is located. The ATL project wizard is not known for coming up with the world's best names, and if you're designing part of a commercial product, then you want to change the service name to something that makes sense for your application.
Finally, for this - rather short - step, we are going to initialize the security properly. This has to be done through a call to the Win32 function,
CoInitializeSecurity(). This function configures security for servers on the system level. See the documentation for more information on the specifics. If you want to be able to run your server without providing elaborate security info - i.e., anonymously - then you need to call
CoInitializeSecurity(), go to Class View in the Solution Explorer, and click the + sign by the
CHelloWorldServNETModule class, and then double-click the
InitializeSecurity(void) function. Delete everything in the function body, and then add the code shown, in bold, below, in Listing 1:
HRESULT InitializeSecurity() throw()
HRESULT hResult = CoInitializeSecurity(NULL, -1,
NULL, NULL, RPC_C_AUTHN_LEVEL_NONE,
Listing 1: Properly initializing the security with a call to
It turns out that there's still something more left. You have to make sure the value of the
_WIN32_WINNT constant, which is
#define'd in STDAFX.H, is set properly. Since we're using Windows XP, we need to set its value to
0x500. Use Solution Explorer to open STDAFX.H, and then add the code shown in Listing 2:
#ifndef _WIN32_WINNT // Allow use of features
#define _WIN32_WINNT 0x0500 // Change this to the
Listing 2: Changing the value of the
WIN32_WINNT constant to support features found in Windows XP or later.
Notes from the Rear
We've now completed step 2 of this tutorial. We saw where the name of this service - which is displayed to the user - is initialized, and we fixed the security-initialization code for the service. Click Next >> to move on to the next step of this tutorial, step 3, click << Back to go back to step 1 of this tutorial, or click the Questions and Answers link (coming soon!) to jump to the Questions and Answers page. Good luck.
<< Back | Next >>Questions and Answers - coming soon
Tip: If you're having trouble or can't understand something, it's often the case that you just went ahead as far as you could in this tutorial without following thoroughly, and downloaded the code for the latest step that was done. Perhaps if you go back to the previous steps, and work through the tutorial in the places where it wasn't clear, this may help. Also, it could be because there are still more steps yet to be written! Stay tuned!
Tip: Also, if you have a question, go ahead and post it to the message board, below, which is at the bottom of this article page. I will get an email when you do so, and then everyone can see your question and my answer. Don't forget to rate this article either! If you gave it anything less than 5, then post to the message board as to the reason why, so that I can make these articles better for everyone.