The information in this article applies to:
- Enterprise Edition of Microsoft Visual Basic, for Windows, version 5.0
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:
ARTICLE-ID: Q175510
TITLE : FILE: VB5DCOM.EXE: Using Callbacks and WithEvents with DCOM
This is an advanced DCOM demonstration using Visual Basic 5.0.
System Requirements
The following operating systems may be used for DCOM applications:
Operating System Server Client
---------------- ------ ------
WinNT 4.0 Server YES YES
WinNT 4.0 Workstation YES YES
Windows 95 YES: YES:
See Q165101 with DCOM for Win95 files
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:
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)
- Click the Make tab and set the following properties:
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>
- Click the Compile tab and set the following properties:
Compile to P-Code: <Selected>
Compile to Native Code: <Not Selected>
The rest of the options should be unavailable.
- Click the Component tab and set the following options:
Start Mode: ActiveX Component
Remote Server Files: <Checked> (Creates TLB and VBR files)
Version Compatibility: Project Compatibility (Keeps GUIDs updated)
Click OK.
- On the File menu, click Save Project As. Save the following files as
described:
Directory File Filename Extension
---------------------------------------------------------------------
C:\DCOMDemo\Server Class Module clsDCOMDemoServer .cls
Project proDCOMDemoServer .vbp
- Select File from the menu, and choose "Make proDCOMDemoServer.exe."
- In the Make Project dialog box, save the executable to the
\DCOMDemo\Server directory.
- Select Project from the menu and choose "proDCOMDemoServer
Properties."
- Click the Component tab and confirm that Version Compatibility is
still set to Project Compatibility.
- 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, go to
step 16.
- Click OK to close the "proDCOMDemoServer" - Project Properties
dialog box.
- Open a new project ("File... New Project") and save the changes in the
server project.
Create the Client
- In the New Project dialog box, select "Standard EXE" and click OK.
- On the Project menu, click References.
- 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.
- Insert a Module. Place two CommandButtons, two text boxes, and three
labels on Form1. Set the following properties:
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.
- Copy the following code to the Module:
Option Explicit
Public oServer As New proDCOMDemoServer.clsDCOMDemoServer
- 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
- 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:
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)
- Click the Make tab and set the following properties:
Auto Increment: <Checked> (Useful for Version
checking)
Application Title: proDCOMDemoClient
Version Information: <As desired>
Command Line Arguments: <Blank>
Conditional Compilation Arguments: <Blank>
- Click the Compile tab and set the following properties:
Compile to P-Code: <Selected>
Compile to Native Code: <Not Selected>
The rest of the options should be unavailable.
- Click the Component tab and ensure that all options are disabled.
Click OK.
- On the File menu, click Save Project As. Save the following files
as described:
Directory File Filename Extension
----------------------------------------------------------------
C:\DCOMDemo\Client Module modDCOMDemoClient .bas
Form1 frmDCOMDemoClient .frm
Project proDCOMDemoClient .vbp
- 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.
- On the File menu, click "Make proDCOMDemoClient.exe".
- In the Make Project dialog box, save the executable to the
C:\DCOMDemo\Client directory.
- Close your project, and save the changes.
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:
- Start the Application Setup Wizard by clicking "Start, Programs,
Visual Basic 5.0, Application Setup Wizard." (If the "Introduction"
dialog box appears, click Next.)
- 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.
- Click Open and set the following options:
Rebuild the Project: <Unchecked>
Options: Create a Setup Program
Click Next.
- In the Distribution Method dialog box, select "Single Directory" and
click Next.
- In the Single Directory dialog box, select the C:\DCOMDemo\Server\Setup
directory. Click Next.
- In the ActiveX Components dialog box, click Next. (Do not select any
components. They are not needed for this project.)
- 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.
- 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.
- 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.
- Save the template if desired, and then click Finish.
- Connect to the distribution directory from your server and run the
Setup.exe file that the Setup Wizard generated.
- After the server setup is complete, start the "Dcom Configuration"
application by clicking "Start, 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.
- Click the General tab. This tab lists the following information about
the application, its name, type, and where it is stored:
Application Name: proDCOMDemoServer.clsDCOMDemoServer
Application Type: Local Server
Local Path: C:\WINNT\system32\proDCOMDemoServer.exe
- Click the Location tab to choose where you want the application to run
from. Set the following properties:
Run application on the computer where the data is located <Unchecked>
Run application on this computer <Checked>
Run application on the following computer <Unchecked>
- Click the Security tab to set application security. You set Access,
Launch, and Configuration permissions from this tab.
- Select "Use custom access permissions." Click "Edit," which just
became available. In the Registry Value Permissions dialog box, click
"Add."
- 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.
- 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.
- On the Security tab, select "Use custom launch permissions." Click
"Edit" which just became available. In the Registry Value Permissions
dialog box, click "Add."
- 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.
- 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.
- Leave Configure Permissions in the Custom selection.
- Click the Identity tab to set the user account that you want to use
to run the application. Set the following properties:
The Interactive User <Unselected>
The Launching User <Selected>
This User <Unselected>
- Click OK. When you get back to "Distributed COM Configuration
Properties," click OK. The following message should appear: "The
operation completed successfully." Click OK.
Distribute the Client
- Start the Application Setup Wizard by clicking "Start, Programs,
Visual Basic 5.0, Application Setup Wizard." (If the Introduction
dialog box appears, click Next.)
- 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.
- Click Open and set the following options:
Rebuild the Project: <Unchecked>
Options: Create a Setup Program
Click Next.
- 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.
- In the Distribution Method dialog box, select Single Directory and then
click Next.
- In the Single Directory dialog box, select the C:\DCOMDemo\Client\Setup
directory. Click Next.
- In the ActiveX Components dialog box, deselect proDCOMDemoServer.exe.
You do not need to distribute it with the client.
- 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.
- Click Open. The Remote Connection Details dialog box appears. Set the
following options:
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.
- 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.
- 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.
- Save the template if desired, and then click Finish.
- Connect to the distribution directory from your client and run the
Setup.exe file that the Setup Wizard generated.
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 : vb5all vb5howto VBKBAutomation VBKBAX VBKBComp kbtool kbhowto
Technology : kbole
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type : kbhowto
|