Microsoft KB Archive/161837

= How To Create a DCOM Client/Server Application =

Article ID: 161837

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition

-



This article was previously published under Q161837



SUMMARY
In the Enterprise Edition of Visual Basic for Windows version 5.0, you can use the Distributed Component Object Model (DCOM) to perform client/server operations. This article shows you how to create a client/server application that uses DCOM to retrieve the name of the server.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.



MORE INFORMATION
This sample was not intended to address multiple user scenarios. If you are attempting to create a DCOM server that will handle multiple users, process callbacks, raise events, or use security then please see the following article in the Microsoft Knowledge Base:

175510 : FILE: VB5DCOM.EXE: Using Callbacks and WithEvents with DCOM

This is an advanced DCOM demonstration using Visual Basic 5.0. If you are using Visual Basic 6, please see the following articles in the Microsoft Knowledge Base:

266717 How To Create a DCOM Client/Server Application by Using VB

267836 How To Create a DCOM Client/Server with Events by Using VisualBasic

For additional information on how to set up security or for help with troubleshooting DCOM issues, click the article numbers below to view the articles in the Microsoft Knowledge Base:

268550 How To Use Dcomcnfg for a Visual Basic DCOM Client/Server Application

269330 How To Troubleshoot DCOM for Visual Basic Client/Server Applications

System Requirements
The following operating systems may be used for DCOM applications:

  Operating System          Server           Client --          --   Windows 2000 Server        YES              YES Windows 2000 Professional YES              YES WinNT 4.0 Server          YES              YES WinNT 4.0 Workstation     YES              YES Windows 98                YES*             YES Windows Me                YES*             YES Windows 95                YES*             YES:

with DCOM for Win95 files

* For more information about using Windows 95/98/Me for a DCOM Server. Please see the following article in the Microsoft Knowledge Base:

165101 : How To Use Windows 95 or Windows 98 as a DCOM Server

Prepare the Computer
(While this step is not required, it keeps your project in order.)

Create the following directories on your computer:

  C:\DCOMDemo C:\DCOMDemo\Server C:\DCOMDemo\Server\Setup C:\DCOMDemo\Client C:\DCOMDemo\Client\Setup

Create the Server
 Start a new Visual Basic Project. In the New Project dialog box, select ActiveX EXE and click Open.  Set the following properties:   Control             Name        Property         Value --  Class Module        Class1      (Name)           clsDCOMDemoServer Instancing      5 - MultiUse   Copy the following code to the Class Module clsDCOMDemoServer: Option Explicit

Public Function ServerTime 'Retrieves the time from the server. ServerTime = Time End Function

Public Function AddNumbers(x As Integer, y As Integer) As Integer 'Receives two integers and adds them together. AddNumbers = x + y     End Function

Public Function SubtractNumbers(x As Integer, y As Integer) _ As Integer 'Receives two integers and subtracts the second from the first. SubtractNumbers = x - y     End Function  On the Project menu, click Project1 Properties.

NOTE: If you saved the project previously or have another project open, the menu item may not be called "Project1." Select the menu item for Properties.  Click the General tab and set the following properties: <pre class="fixed_text">  Project Type:             ActiveX EXE       (Create out-of-process                                                ActiveX component)

Startup Object:          (None)            (None required)

Project Name:            proDCOMDemoServer (Will be used in client                                                code to define the project                                                to be used) Help File Name:          <Blank> Project Help Context ID: 0 Project Description:     DCOM Demo Project - Server Unattended Execution:    <Unchecked> Upgrade ActiveX Controls: <Checked>         (Enables upgrading of                                                 ActiveX controls) </li>  Click the Make tab and set the following properties: <pre class="fixed_text">  Auto Increment:                     <Checked> (Useful for version                                                  comparison.)

Application Title:                 proDCOMDemoServer (Name of the EXE                                                          that will be                                                          compiled.) Version Information:               <As desired> Command Line Arguments:            <Blank> Conditional Compilation Arguments: <Blank> </li>  Click the Compile tab and set the following properties:

<pre class="fixed_text">  Compile to P-Code:       <Selected> Compile to Native Code: <Not Selected> The rest of the options should be unavailable. </li>  Click the Component tab and set the following options:

<pre class="fixed_text">  Start Mode:             ActiveX Component Remote Server Files:   <Checked> (Creates TLB and VBR files) Version Compatibility: Project Compatibility (Keeps GUIDs updated) Click OK. </li>  On the File menu, click Save Project As. Save the following files as described: <pre class="fixed_text">  Directory             File           Filename              Extension -  C:\DCOMDemo\Server    Class Module   clsDCOMDemoServer     .cls Project       proDCOMDemoServer     .vbp </li> Select File from the menu, and choose Make proDCOMDemoServer.exe.</li> In the Make Project dialog box, save the executable to the \DCOMDemo\Server directory.</li> Select Project from the menu and choose proDCOMDemoServer Properties.</li> Click the Component tab and confirm that Version Compatibility is still set to Project Compatibility.</li> The file proDCOMDemo.exe appears in the text box at the bottom of the Version Compatibility group. In the Compatible ActiveX Component dialog box, select the file that you just compiled (C:\DCOMDemo\Server\proDCOMDemoServer.exe).

If it is not there, click on the button with the three ellipses (...) and go to step 15. If the file is already in the text box, proceed to step 16.</li> Click OK to close the proDCOMDemoServer - Project Properties dialog box.</li> Open a new project (select New Project on the File menu) and save the changes in the server project.</li></ol>

Create the Client
<ol> In the New Project dialog box, select Standard EXE and click OK.</li> On the Project menu, click References.</li> Scroll down in the Available References text box until you see DCOM Project - Server. Select the box on the left and click OK. Note that the component automatically appears in the Available References text box. In Remote OLE Automation, you have to double-click the server executable to register it. In DCOM, the server component is automatically registered.</li>  Insert a Module. Place two CommandButtons, two text boxes, and three labels on Form1. Set the following properties:

<pre class="fixed_text">  Control         Name              Property         Value --  Module          Module1           Name             modDCOMDemoClient

Form           Form1             Name             frmDCOMDemoClient Caption         DCOM Demo - Client

CommandButton Command1           Name             cmdServerTime Caption         &Get Server Time

Command2          Name             cmdComputeNumbers Caption         &Compute Numbers

Text Box       Text1             Name             txtX TabStop         True TabIndex        0

Text2            Name             txtY TabStop         True TabIndex        1

Label          Label1            Name             lblAnswerAdd Label2           Name             lblAnswerSubtract Label3           Name             lblServerTime

You may want to add additional labels to the form to serve as captions for these labels. </li>  Copy the following code to the Module: Option Explicit

Public oServer As New proDCOMDemoServer.clsDCOMDemoServer

</li> <li> Copy the following code to the frmDCOMDemoClient code window: Option Explicit

Private Sub Form_Load 'Set initial values for text boxes. txtX.Text = "1" txtY.Text = "2" End Sub

Private Sub txtX_GotFocus 'Select all the text when the text box gets focus. 'This makes it easier to change the values. txtX.SelStart = 0 txtX.SelLength = Len(txtX.Text) End Sub

Private Sub txtY_GotFocus 'Select all the text when the text box gets focus. 'This makes it easier to change the values. txtY.SelStart = 0 txtY.SelLength = Len(txtY.Text) End Sub

Private Sub cmdServerTime_Click 'Get the time from the server and put it in the 'lblServerTime label. lblServerTime.Caption = oServer.ServerTime End Sub

Private Sub cmdComputeNumbers_Click 'Passes the integers to the server and puts the answers in the 'appropriate labels. The "CInt" is to ensure that you are 'passing integers, which is what the server is expecting. lblAnswerAdd.Caption = oServer.AddNumbers _ (CInt(txtX.Text), CInt(txtY.Text)) lblAnswerSubtract.Caption = oServer.SubtractNumbers _ (CInt(txtX.Text), CInt(txtY.Text)) End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As _               Integer) 'Closes the object when the form unloads. Set oServer = Nothing End Sub

</li> <li>On the Project menu, click Project1 Properties.

NOTE: If you saved the project previously or have another project open, the menu item may not be called "Project1." Select the menu item for Properties.</li> <li> Click the General tab and set the following properties: <pre class="fixed_text">  Project Type:             Standard EXE       (Client is a Standalone                                                executable)

Startup Object:          frmDCOMDemoClient  (frmDCOMDemoClient and                                                the code behind                                                the form run upon                                                execution)

Project Name:            proDCOMDemoClient  (Will be the name of the                                                client executable) Help File Name:          <Blank> Project Help Context ID: 0 Project Description:     DCOM Demo Project - Client Unattended Execution:    <Disabled> Upgrade ActiveX Controls: <Checked>         (Enables upgrading of                                                ActiveX controls)

</li> <li> Click the Make tab and set the following properties: <pre class="fixed_text">  Auto Increment:                     <Checked> (Useful for Version                                                 checking)

Application Title:                 proDCOMDemoClient Version Information:               <As desired> Command Line Arguments:            <Blank> Conditional Compilation Arguments: <Blank>

</li> <li> Click the Compile tab and set the following properties:

<pre class="fixed_text">  Compile to P-Code:       <Selected> Compile to Native Code: <Not Selected>

The rest of the options should be unavailable. </li> <li>Click the Component tab and ensure that all options are disabled. Click OK.</li> <li> On the File menu, click Save Project As. Save the following files as described: <pre class="fixed_text">  Directory           File         Filename              Extension C:\DCOMDemo\Client Module       modDCOMDemoClient     .bas Form1       frmDCOMDemoClient     .frm Project     proDCOMDemoClient     .vbp

</li> <li>Test the client by clicking Run or pressing the F5 key. When you click Get Server Time, the current system time appears in the label. When you click Compute Numbers, the sum and the difference between the numbers appear in the appropriate labels. Stop the project.</li> <li>On the File menu, click Make proDCOMDemoClient.exe.</li> <li>In the Make Project dialog box, save the executable to the C:\DCOMDemo\Client directory.</li> <li>Close your project, and save the changes.</li></ol>

Distribute the Server
You can use the same computer that you developed the application on as your server. However, if you want to distribute your server to another computer, follow these instructions:

<ol> <li>Start the Application Setup Wizard by clicking Start, Programs, Visual Basic 5.0, Application Setup Wizard. (If the Introduction dialog box appears, click Next.)</li> <li>In the Select Project and Options dialog box, click Browse. In the Locate VB application's .VBP file dialog box, select C:\DCOMDemo\Server\proDCOMDemoServer.VBP.</li> <li> Click Open and set the following options:

<pre class="fixed_text">  Rebuild the Project:  <Unchecked> Options:             Create a Setup Program

Click Next. </li> <li>In the Distribution Method dialog box, select Single Directory and click Next.</li> <li>In the Single Directory dialog box, select the C:\DCOMDemo\Server\Setup directory. Click Next.</li> <li>In the ActiveX Components dialog box, click Next. (Do not select any components. They are not needed for this project.)</li> <li>In the Confirm Dependencies dialog box, make sure that the Stdole2.tlb file is listed. (If it is not listed, you will have to add it later. It is located in your \Windows\System directory.) Click Next.</li> <li>In the Shared ActiveX Application dialog box, select Install as a shared component and No since we are installing this as a DCOM application. Click Next.</li> <li>In the File Summary dialog box, the Setup Wizard processes your files. When it is finished you will see a list of files that will be included in your project. If the Stdole2.tlb file is not listed, click Add. In the Add Files dialog box, select the file from your system directory. Click Next.</li> <li>Save the template if desired, and then click Finish.</li> <li>Connect to the distribution directory from your server and run the Setup.exe file that the Setup Wizard generated.</li> <li>After the server setup is complete, start the Dcom Configuration application by clicking Start then Run and type Dcomcnfg.exe in the Run dialog box.

If you do not configure the server class that was just installed, the default configuration is used. For this example, select proDCOMDemoServer.clsDCOMDemoServer and click Properties.</li> <li> Click the General tab. This tab lists the following information about the application, its name, type, and where it is stored: <pre class="fixed_text">  Application Name:  proDCOMDemoServer.clsDCOMDemoServer Application Type: Local Server Local Path:       C:\WINNT\system32\proDCOMDemoServer.exe

</li> <li> Click the Location tab to choose where you want the application to run from. Set the following properties: <pre class="fixed_text"> Run application on the computer where the data is located <Unchecked> Run application on this computer                          <Checked> Run application on the following computer                 <Unchecked>

</li> <li>Click the Security tab to set application security. You set Access, Launch, and Configuration permissions from this tab.</li> <li>Select Use custom access permissions. Click Edit, which just became available. In the Registry Value Permissions dialog box, click Add.</li> <li>In the Add Users and Groups dialog box, select Everyone, and then click Add. The Add Users and Groups dialog box enables you to allow or deny access to the class to certain individuals or groups.</li> <li>In Type of Access box, select Allow Access and click OK. The Registry Value Permissions dialog box now reflects that you are allowing access to this class to Everyone. Click OK.</li> <li>On the Security tab, select Use custom launch permissions. Click Edit which just became available. In the Registry Value Permissions dialog box, click Add.</li> <li>In the Add Users and Groups dialog box, select Everyone and then click Add. The Add Users and Groups dialog box enables you to allow or deny certain individuals or groups to launch the class.</li> <li>In the Type of Access box, select Allow Launch and click OK. The Registry Value Permissions dialog box now reflects that you are allowing Everyone to launch this class. Click OK.</li> <li>Close Configure Permissions in the Custom selection.</li> <li> Click the Identity tab to set the user account that you want to use to run the application. Set the following properties: <pre class="fixed_text">  The Interactive User  <Unselected> The Launching User   <Selected> This User            <Unselected>

</li> <li>Click OK. When you get back to Distributed COM Configuration Properties, click OK. The following message should appear: "The operation completed successfully." Click OK.</li></ol>

Distribute the Client
<ol> <li>Start the Application Setup Wizard by clicking Start, Programs, Visual Basic 5.0, Application Setup Wizard. (If the Introduction dialog box appears, click Next.)</li> <li>In the Select Project and Options dialog box, click Browse. In the Locate VB application's .VBP file dialog box, select C:\DCOMDemo\Client\proDCOMDemoClient.VBP.</li> <li> Click Open and set the following options:

<pre class="fixed_text">  Rebuild the Project:  <Unchecked> Options:             Create a Setup Program

Click Next. </li> <li>In the Missing Dependencies dialog box, click Next. Dependency files are not required for this project. In the Confirm Missing Dependencies dialog box, click Yes.</li> <li>In the Distribution Method dialog box, select Single Directory and then click Next.</li> <li>In the Single Directory dialog box, select the C:\DCOMDemo\Client\Setup directory. Click Next.</li> <li>In the ActiveX Components dialog box, deselect proDCOMDemoServer.exe. You do not need to distribute it with the client.</li> <li>Click Add Remote. In the Add ActiveX Components dialog box, select C:\DCOMDemo\Server\proDCOMDemoServer.VBR. This file was created when you compiled the server executable. It was created because you checked the Remote Server Files option on the Server's Properties Component tab.</li> <li> Click Open. The Remote Connection Details dialog box appears. Set the following options:

<pre class="fixed_text">  Remote Transport:   Distributed COM (DCOM) Network Address:   <The name of the computer the server will be                       installed on. Do not include backslashes>

Click OK. Click Next. </li> <li>In the Confirm Dependencies dialog box, make sure that the Stdole2.tlb file is listed. (If it is not listed, you will have to add it later. It is located in your \Windows\System directory.) Click Next.</li> <li>The File Summary dialog box appears and the Setup Wizard processes your files. When it is finished you will see a list of files that will be included in your project. If the Stdole2.tlb file is not listed, click Add. In the Add Files dialog box, select the file from your system directory. Click Next.</li> <li>Save the template if desired, and then click Finish.</li> <li>Connect to the distribution directory from your client and run the Setup.exe file that the Setup Wizard generated.</li></ol>

When you run the application from the client, it returns the time of the computer that the server component is on. When you click Compute Numbers, the result of the addition and subtraction of the numbers appears in the text boxes. Try changing the numbers to confirm that everything works, but don't use a very large number because you told that class that you are passing integers.

Keywords: kbhowto kbtophit kbautomation KB161837

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.