使用vb建立DCOM服务器端及客户端应用的详细过程(2)
本节介绍如何建立DCOM客户端应用,及其注意事项。(本人使用的是西文版vb6)
1.打开vb,在"file"菜单单击"New Project",选择"Standard EXE"并单击"OK".
2.在"Project"菜单, 单击"References".
3.单击"browse",选择"C:\DCOMDemo\Server\proDCOMDemoServer.exe",单击"ok",
并选中该引用.
4.在工程里插入一个模块,放置两个CommandButton, 两个textbox,以及三个label到form1上
. 设置如下属性:
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
注意:现在还不要给工程更改名称,即名称仍为"PROJECT1"
5.拷贝如下代码到模块里,(不是Form1上):
Option Explicit
Public oServer As New _ proDCOMDemoServer.clsDCOMDemoServer
6.拷贝如下代码到frmDCOMDemoClient代码窗口:
Option Explicit
Private Sub Form_Load()
'设置文本框的初始化值.
txtX.Text = "1"
txtY.Text = "2"
End Sub
Private Sub txtX_GotFocus()
txtX.SelStart = 0
txtX.SelLength = Len(txtX.Text)
End Sub
Private Sub txtY_GotFocus()
txtY.SelStart = 0
txtY.SelLength = Len(txtY.Text)
End Sub
Private Sub cmdServerTime_Click()
'获取服务器端的程序,并将返回值显示在lblServerTime标签里.
lblServerTime.Caption = oServer.ServerTime
End Sub
Private Sub cmdComputeNumbers_Click()
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)
Set oServer = Nothing
End Sub
7.在"Project"菜单, 单击"Project1 Properties."
8.单击"General",设置如下属性:
Project Type: Standard EXE
Startup Object: frmDCOMDemoClient
Project Description: DCOM Demo Project - Client
9.单击"Make",设置如下属性:
Application Title: proDCOMDemoClient
10.单击"Compile",设置如下属性:
Compile to P-Code: <Selected>
11.单击"OK"
12.在"File"菜单, 单击"Save Project As",保存的文件名称如下所述:
Directory File Filename Extension
----------------------------------------------------------------
C:\DCOMDemo\Client Module modDCOMDemoClient .bas
Form1 frmDCOMDemoClient .frm
Project proDCOMDemoClient .vbp
13.按"F5"按钮,测试客户端应用
14.测试完成,在"File"菜单, 单击"Make proDCOMDemoClient.exe".
15.将生成的exe文件保存到C:\DCOMDemo\Client
16.关闭客户端和服务器端应用.
<待续>