Microsoft KB Archive/250309

= INFO: Do Not Use ObjectContext in Class_Initialize and Class_Terminate Events =

Article ID: 250309

Article Last Modified on 8/28/2002

-

APPLIES TO


 * Microsoft Transaction Services 2.0
 * Microsoft COM+ 1.0
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



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

 Open a new ActiveX DLL project in Visual Basic. 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.  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  Compile the component and recompile with Version Compatibility to Binary Compatible. Register the component with MTS or add it to Component Services.

Simple Client

 Open a Standard EXE Visual Basic application to test the component.</li> Add a button to Form1.</li>  Add the following code to Command1_Click. Private Sub Command1_Click Dim o As Object Set o = CreateObject("project1.class1") o.Test End Sub </li>  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

<ul> You can resolve this issue by changing the Simple Server code to the following:</li></ul>

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 </li></ol>

<div class="references_section">