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

ID: Q191766


The information in this article applies to:
  • Microsoft Transaction Server 2.0
  • Microsoft Visual Studio versions 6.0, 97sp3


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

  1. Open a new instance of Visual Basic, create a new Standard EXE project type, and name this project MTSCalcClient.


  2. Place four command buttons on Form1.


  3. Change the Caption Property to Add, Subtract, Multiply, and Divide.


  4. Name the command buttons cmdAdd, cmdSubtract, cmdMultiply, and cmdDivide.


  5. Add a TextBox on Form1, name it txtOperand1, place a label control next to this text box, and set the caption to Operand1.


  6. Add a TextBox on Form1, name it txtOperand2, place a label control next to this text box, and change set the caption to Operand2.


  7. Add a TextBox on Form1, name it txtResult place a label control next to this text box, and set the caption to Result.


  8. 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 *************** 


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

Test Your Progress

  1. Run the FourFunctionDLL project and minimize it.


  2. Run the MTSCalcClient project and check that the calculator runs.


  3. If the Calculator functions, move on to the next step. If it does not, troubleshoot appropriately.


  4. Stop the MTSCalcClient project.


  5. Stop the FourFunctionDLL project.


  6. In the FourFunctionDLL project properties dialog box verify that the following values are set on the General tab:
    
          Threading Model = "Apartment Threaded"
          Unattended Execution = Checked 


  7. Click the Component tab. In the Remote Server frame, set the Remote Server Files box to True.


  8. Click OK.


  9. Save the project.


  10. Create a directory named C:\MyFourFunctionClient and save the MTSCalcClient project to this directory.


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

  1. Start the Visual Basic Setup wizard and locate the following folder:
    
          C:\MyFourFunctionClient\MTSCalcClient.vpj 


  2. In the Options frame, identify Create a Setup Program.


  3. Click Next.


  4. Select floppy disks (for this example, but use whatever is appropriate when you perform this operation in your production environment).


  5. 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.


  6. 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.


  7. Click OK.


  8. 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


  9. 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.


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 :
Version : WINDOWS:6.0,97sp3; winnt:2.0
Platform : WINDOWS winnt
Issue type : kbinfo


Last Reviewed: October 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.