Microsoft KB Archive/191766

= INFO: Example: Simple DCOM VB Client Talking to an MTS Component =

Article ID: 191766

Article Last Modified on 6/18/2001

-

APPLIES TO


 * Microsoft Transaction Services 2.0
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q191766



SUMMARY
This sample provides the source code and complete instructions for a Visual Basic developer to learn the fundamental steps involved in deploying a Visual Basic application that uses DCOM (Distributed Component Object Model) to talk to a Microsoft Transaction Server (MTS) session. The sample includes step by step instructions for compiling a VB client executable as a remote client and a VB ActiveX DLL as an MTS component. This sample exercise is designed to be built on a development computer that has the Windows NT Option Pack installed. By beginning the development and testing in a controlled environment (for example, on a developer's computer), the sample stresses incremental functional testing at each critical juncture. This type of procedure ensures that the base configuration of the network and software is conducive to DCOM to MTS.



MORE INFORMATION
For best results, do the following before creating the sample procedure:


 * Limit the exercise to producing the samples on a single development computer running Windows NT with the Windows NT Options Pack until the instructions direct otherwise.
 * Apply Microsoft Visual Basic 5.0 with Visual Studio Service Pack 3 on the development computer before producing this sample.

ActiveX DLL Project
Create a new ActiveX DLL project type and name the project FourFunction. Change the name of the class module to cls_calculator. In the class module paste the following code: '***** BEGIN PASTE FOR "cls_calculator" class in  '***** the FourFunction project***

Public Function Add(num1 As Double, num2 As Double) As Double Add = num1 + num2 End Function Public Function Subtract(num1 As Double, num2 As Double) As Double Subtract = num1 - num2 End Function Public Function Multiply(num1 As Double, num2 As Double) As Double Multiply = num1 * num2 End Function Public Function Divide(num1 As Double, num2 As Double) As Double If num2 = 0 Then Divide = 0 Else Divide = num1 / num2 End If  End Function

'***** END PASTE FOR "cls_calculator" class in the FourFunction '***** project*** Create the directory C:\MyFourFunctionDLL and save both the project and the class module there.

Standard EXE Project
 Open a new instance of Visual Basic, create a new Standard EXE project type, and name this project MTSCalcClient . Place four command buttons on Form1. Change the Caption Property to Add, Subtract, Multiply, and Divide. Name the command buttons cmdAdd, cmdSubtract, cmdMultiply, and cmdDivide. Add a TextBox on Form1, name it txtOperand1, place a label control next to this text box, and set the caption to Operand1. Add a TextBox on Form1, name it txtOperand2, place a label control next to this text box, and change set the caption to Operand2. Add a TextBox on Form1, name it txtResult place a label control next to this text box, and set the caption to Result.</li>  In the code window of Form1 past the following code: '*************** BEGIN PASTE FOR MTSCalcClient Client ***************

Dim obj_MTSCalc As Object Private Sub cmdAdd_Click CheckInput txtOperand1, txtOperand2 txtResult = CStr(obj_MTSCalc.Add(CDbl(txtOperand1), CDbl(txtOperand2))) End Sub

Private Sub cmdDivide_Click CheckInput txtOperand1, txtOperand2 txtResult = CStr(obj_MTSCalc.Divide(CDbl(txtOperand1), CDbl(txtOperand2))) End Sub

Private Sub cmdMultiply_Click CheckInput txtOperand1, txtOperand2 txtResult = CStr(obj_MTSCalc.Multiply(CDbl(txtOperand1), CDbl(txtOperand2))) End Sub

Private Sub cmdSubtract_Click CheckInput txtOperand1, txtOperand2 txtResult = CStr(obj_MTSCalc.Subtract(CDbl(txtOperand1), CDbl(txtOperand2))) End Sub

Private Sub Form_Load Set obj_MTSCalc = CreateObject("FourFunction.cls_Calculator") End Sub

Sub CheckInput(num1 As TextBox, num2 As TextBox)

If IsNumeric(num1.Text) And IsNumeric(num2.Text) Then

Else num1.Text = 0 num2.Text = 0 End If

End Sub

'*************** END PASTE FOR MTSCalcClient Client *************** </li></ol>

Create the following directory: C:\MyFourFunctionClient and save the MTSCalcClient project to it.

Test Your Progress
<ol> Run the FourFunctionDLL project and minimize it.</li> Run the MTSCalcClient project and check that the calculator runs.</li> If the Calculator functions, move on to the next step. If it does not, troubleshoot appropriately.</li> Stop the MTSCalcClient project.</li> Stop the FourFunctionDLL project.</li>  In the FourFunctionDLL project properties dialog box verify that the following values are set on the General tab: <pre class="fixed_text">     Threading Model = "Apartment Threaded" Unattended Execution = Checked </li> Click the Component tab. In the Remote Server frame, set the Remote Server Files box to True.</li> Click OK.</li> Save the project.</li> Create a directory named C:\MyFourFunctionClient and save the MTSCalcClient project to this directory.</li></ol>

Create FourFunctionDemo Package

 * 1) Create a directory named C:\MyFourFunctionDemo.
 * 2) On the File menu, click Make FourFunction.dll, and specify that it be saved in the C:\MyFourFunctionDemo directory.
 * 3) Start Transaction Server Explorer and expand the folders until you see My Computer. Expand the folders again until you see Packages Installed.
 * 4) Right-click Packages Installed and click New.
 * 5) Name the new package FourFunctionDemo.
 * 6) Leave the Package Identity as Interactive User.
 * 7) Click Finish.
 * 8) Right-click the Components folder and click New.
 * 9) Click the Install New Component(s) button.
 * 10) Click the Add Files button, locate the C:\My FourFunctionDemo folder, and click FourFunction.dll.
 * 11) Make sure that the components folder of the FourFunctionDemo package is open and that the FourFunction.cls_calculator ball with the green "X" mark is visible on the desktop.
 * 12) Start the MTSCalcClient project and observe that the ball is now spinning. This indicates that the object has been started on the server computer. (Which, in this first case, is the same computer where these two components have been developed.)
 * 13) Operate the functions of the calculator. All the logic is now being executed on the "server" (which currently is the development computer running MTS.)

Continue with this sample to learn how to move the client code (MTSCalcClient.exe) to another computer on your network and continue to have the logic executing on your development computer through DCOM.

Save the MTSCalcClient project and then close it.

Create a Setup Program
<ol>  Start the Visual Basic Setup wizard and locate the following folder: <pre class="fixed_text">     C:\MyFourFunctionClient\MTSCalcClient.vpj </li> In the Options frame, identify Create a Setup Program.</li> Click Next.</li> Select floppy disks (for this example, but use whatever is appropriate when you perform this operation in your production environment).</li> <li>On the ActiveX Server Components screen, click AddRemote and you will be prompted to look for the a *.vbr file. Locate the C:\MyFourFunctionDemo folder and click the MyFourFunction.vbr file.</li> <li>The next screen prompts you for the name of the computer on which the actual MyFourFunction.dll resides. This should be the name of the computer you are using now, for example, MyDevMachine. Enter the name of this computer in the Network Address box.</li> <li>Click OK.</li> <li>In the list that displays all the files that will be included in the setup package, note that the following files and extensions are unique to the remote setup distribution media for a client that specifies DCOM. For instance in this case:

MyFourFunction.tlb

MyFourFunction.vbr

ClientReg.exe

</li> <li>When your distribution disks are complete, you can then take them to a remote computer and install them in the customary manner.

NOTE: For a Windows 95 computer, you must first install DCOM. It is presently available on http://www.microsoft.com/com/comresource.asp. DCOM 95 version 1.2, the latest version of DCOM for Windows 95, provides enhanced features and bug fixes over DCOM 95 version 1.0 and is now available over the Web.

NOTE: For Windows NT Workstation version 4.0, you should have completely installed Service Pack 3.</li></ol>

For the application to run, you must first be able to ping the MTS computer by name. If you cannot ping the server by name, you must resolve your connectivity issues before you can test your application.

An issue may arise in one specific area. Quite often the refresh of the component after a *.dll file is recompiled is not done. It is a good practice to always refresh the component in MTS to ensure that the progID is consistent with the information used in MTS.

Additional query words: prodsms Building Distributed Applications Apps service pak

Keywords: kbinfo KB191766

-

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

© Microsoft Corporation. All rights reserved.