INFO: Do Not Use ObjectContext in Class_Initialize and Class_Terminate Events (250309)



The information in this article applies to:

  • Microsoft Transaction Server 2.0
  • Microsoft COM+ 1.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q250309

SUMMARY

Every Microsoft Transaction Server (MTS) object in Microsoft Windows NT 4.0, or COM+ object in Microsoft Windows 2000, has an associated context object. The context object provides context for the execution of an instance, including transaction, activity, and security properties. When an MTS or COM+ object is created, MTS or COM+ automatically creates a context object for it. When the MTS object is released, MTS automatically releases the context object.

You can access an MTS or COM+ object's context by calling the GetObjectContext function. The GetObjectContext function returns a reference to the IObjectContext interface. The context object is not available during calls to the component's Class_Initialize and Class_Terminate event. If you call one of its methods, the following error message appears in Windows NT 4.0:
Run-time error '91':
Object variable or With block variable not set
Under Windows 2000, the following error message appears:
Run-time error '438':
Object variable or With block variable not set
NOTE: This problem has been corrected in Windows 2000 Service Pack 1.

MORE INFORMATION

Simple Server
  1. Open a new ActiveX DLL project in Visual Basic.
  2. From the Project menu, select References and Set "Reference to Microsoft Transaction Server Type Library" when developing on Windows NT 4.0 or "COM+ Services Type library" under Windows 2000.
  3. In class properties set MTSTransactionMode to anything other than NotAnMTSObject and add the following code.
    Private mobjContext As ObjectContext
    Private Sub Class_Initialize()
        Set mobjContext = GetObjectContext()
    End Sub
    
    Private Sub Class_Terminate()
        Set mobjContext = Nothing
    End Sub
    
    Public Sub Test()
        mobjContext.SetComplete
    End Sub
    					
  4. Compile the component and recompile with Version Compatibility to Binary Compatible.
  5. Register the component with MTS or add it to Component Services.
Simple Client
  1. Open a Standard EXE Visual Basic application to test the component.
  2. Add a button to Form1.
  3. Add the following code to Command1_Click().
    Private Sub Command1_Click()
        Dim o As Object
        Set o = CreateObject("project1.class1")
        o.Test
    End Sub
    					
  4. Run the client application and click the button. The following error message appears under Windows NT 4.0:
    Run-time error '91':
    Object variable or With block variable not set
    or under Windows 2000:
    Run-time error '438':
    Object variable or With block variable not set
    • You can resolve this issue by changing the Simple Server code to the following:
    Implements ObjectControl
    Private mobjContext As ObjectContext
    
    Public Sub Test()
        mobjContext.SetComplete
    End Sub
    
    Private Sub ObjectControl_Activate()
        Set mobjContext = GetObjectContext()
    End Sub
    
    Private Function ObjectControl_CanBePooled() As Boolean
    
    End Function
    
    Private Sub ObjectControl_Deactivate()
        Set mobjContext = Nothing
    End Sub
    					

REFERENCES

For additional information, see the following article or articles in the Microsoft Knowledge Base:

170156 INFO: VB 6.0 Readme Part 12: Transaction Server (MTS) Issues


Modification Type:MajorLast Reviewed:8/28/2002
Keywords:kbDSupport kberrmsg kbinfo KB250309