Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

VB IDE and ObjectContext : Page 2

A myth that is still being spread in various VB and MTS/COM+ related newsgroups, is that an application can not obtain a reference to the MTS or COM+ ObjectContext in the Visual Basic Integrated Development Environment (VB IDE). The truth is, since VB 5 a VB ActiveX DLL project in the IDE can obtain a reference to the MTS ObjectContext object, and this article explains a little bit about how to use this capability, some things to check if it doesn't seem to be working for your project and detailed instructions for creating a VB application that demonstrates the capability. MTS/COM+ ObjectContext not available in VB IDE: Myth Debunked. If you still believe that the MTS or COM+ Object Context doesn't work in the VB IDE, this article is for you!


advertisement

If the call to GetObjectContext returns Nothing in the VB IDE

Here are a few things to check if your code can’t obtain a reference to an ObjectContext object in the VB IDE:

  • Is your VB Project an ActiveX DLL? In the VB IDE click Project, Properties and make sure the Project Type: is ActiveX DLL. Other types of VB Projects can not be MTS components.
  • Is the Class Module’s Instancing Property 5-MultiUse and is its MTSTransactionMode property something other than 0-NotAnMTSObject? An MTS component has to have at least one Multi-Use Class Module with its MTSTransactionMode other than 0. When MTSTransactionMode is set to 0-NotAnMTSObject, the VB IDE will not be connected to the MTS run-time and GetObjectContext will always return Nothing.
  • Is the Distributed Transaction Co-ordinator (DTC) service running? Unless this service is running, your MTS component will not be able to obtain a reference to a context object if the class’s MTSTransactionMode is other than 1 NoTransactions. Usually, the first time you attempt to start an MTS component that may use transactions in the VB IDE, you get a warning if DTC is not running. However, you may not get this warning. Check in the Services dialog to make sure DTC is running (see step 9 for additional information).
  • Perhaps the VB6debug.dll file is missing or damaged. This dll implements the interaction between the VB IDE and MTS. The original VB6 version is in the VB98 folder of CD #1 for Visual Studio 6. An updated version is in SP3 and SP4 for Visual Studio (VS6sp3_3.cab and VS6sp43.cab respectively). This file lives in the VB98 folder of the Visual Studio installation folder (e.g. Program Files\Microsoft Visual Studio\VB98).



Instructions for building and testing a VB MTS component and client

The application built using the instructions below consists of two parts: an MTS component and a client EXE. The function implemented is trivial (return a Boolean variable indicating whether the MTS component is in an MTS Transaction or not), but it demonstrates obtaining a reference to an MTS context object and getting properties from that object in the VB IDE.

  1. Start VB 6 (I'm using SP4, but this also should work with no or earlier SPs).
  2. If you get the New Project dialog box, select ActiveX DLL on the New tab, then click Open. If you don't get that dialog, select File, New Project, ActiveX DLL then click OK.
  3. In the Class Properties box for Class1, set Instancing to 5 - MultiUse and MTSTransactionMode to 2 - Requires Transaction.
  4. From the menu, select Project, References, scroll down, put a check mark beside Microsoft Transaction Server Type Library (if you are running under Windows 2000, choose COM+ Services Type Library instead) and click OK.
  5. In the Project content sub-window, click on Class1 to make sure it is selected.
  6. Paste the following code into the code window. This code returns True if the component is in a transaction and False if not.
  7. Public Function GetTransactionStatus() As Boolean
    Dim CSContext As ObjectContext
    Set CSContext = GetObjectContext()
    GetTransactionStatus = CSContext.IsInTransaction
    Set CSContext = Nothing
    End Function

  8. In the menu, click Project, Project1 Properties, select the Debugging tab and make sure that the Wait for components to be created radio button is selected; click OK.
  9. Click File, Save Project As and save the class module and the project in a suitable folder.
  10. From the Debug Toolbar (or Debug menu), click Step into. You get a warning about using Binary Compatibility Mode, building a DLL etc. Click Yes to continue anyway. You may also get a warning that the Microsoft Distributed Transaction Co-ordinator is not running. Your code can not get a reference to an MTS context object unless DTC is running, so, cancel the warning, open the Services dialog (NT Control Panel, Services; Windows 2000 Start, Programs, Administrative Tools, Services) and start the Distributed Transaction Co-ordinator service, then click Step into again.
    At this point, your component has been compiled, temporarily registered with MTS and is ready to be instantiated by a client.
  11. Open a new instance of VB and create a new Standard EXE project.
  12. In the Project content sub-window, select Project1, then in the Properties window, change the name to Project1Client.
  13. In the Project content sub-window, right click on Form1 and select View Code.
  14. From the menu, select Project, References. Scroll down to Project1 and put a check mark in its box. When you click on Project1, you will see the path to its VBP file in the bottom part of the dialog box. Click OK.
  15. From the drop down list boxes at the top of the code window, in the left side, select Form.
  16. In the Form_Load procedure, paste the following code
  17. Dim TransStatus As Boolean
    Dim obj As Project1.Class1

    Set obj = New Project1.Class1
    TransStatus = obj.GetTransactionStatus()
    Set obj = Nothing

    If TransStatus Then
    MsgBox ("Project1 is in a transaction")
    Else
    MsgBox ("Project1 is not in a transaction")
    End If

  18. From the Debug Toolbar, click Step into.
  19. Step through the code in both the client and Project1 and when you get to the statement:
  20. GetTransactionStatus = CSContext.IsInTransaction

    observe that CSContext has a reference to the MTS context object (it is not Nothing).

  21. Click Step Into again and observe that GetTransactionStatus is assigned True.
  22. Keep stepping through the code and you get the message box that says Project1 is in a transaction.

Web Site References - Documentation:

Microsoft:

Visual Studio/Visual Basic Version 6 Readme: Transaction Server (MTS) Issues

http://support.microsoft.c om/support/kb/articles/Q170/1/56.ASP

MS KB article Q188919: HOWTO: Avoid "Error 91" when Debugging MTS Components with Visual Basic. This article applies to VB Version 5 only (not Version 6).

http://support.microsoft.c om/support/kb/articles/Q188/9/19.ASP

MS KB article Q244807: PRB: Object Required Error for ObjectContext Under Windows 2000 in Visual Basic IDE and Active Server Pages. This article applies to VB Version 6 and Windows 2000.

http://support.microsoft.c om/support/kb/articles/Q244/8/07.ASP

MS KB article Q243437: PRB: Identity Different in MTS and COM+ Library Package by Default. This article describes differences in how security is implemented between MTS under NT 4.0 and COM+ under Windows 2000.

http://support.microsoft.c om/support/kb/articles/Q243/4/37.ASP



Bruce Sanderson was born in England in 1947 and emigrated to Canada with his parents when he was seven. After graduating from the BC Institute of Technology (BCIT) in 1967, Bruce moved to Ottawa, Ontario and worked for Bell Northern Research. While at BNR, he worked initially on support software (e.g. assembler, loader, data compiler, simulator) for the electronic telephone exchange then under development. Later, Bruce provided operating system programming support for IBM’s CP 67/CMS and VM/CMS. Besides diagnosing and fixing problems, Bruce was responsible for performance measurement.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date