Microsoft KB Archive/183164

= FIX: For..Each Loop in ActiveX Component Causes Errors =

Article ID: 183164

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition

-



This article was previously published under Q183164



SYMPTOMS
When using an ActiveX DLL created in Visual Basic, you may receive one of the following errors if the ActiveX component contains a For..Each loop that iterates through a Visual Basic collection:

Run-time error '48':

Error in loading DLL

-or-

Run-time error '-2147319765 (8002802b)':

Automation Error

This problem occurs when a For..Each loop is executed in an ActiveX DLL. For..Each loops elsewhere do not exhibit this problem.



CAUSE
This problem typically occurs after calling the DLL at least twice. The first call into the DLL does not execute code that uses a For..Each loop but the second call does. The error occurs during the second call. See the "Steps to Reproduce Behavior" section below for more information.



RESOLUTION
 Use a For..Next loop in place of the For..Each loop.

-or- Create your own collection class, an example of which can be found in the Visual Basic Books Online. Refer to the topic "Creating Your Own Collection Classes."

-or- If your DLL is running in a multithreaded environment like Internet Information Server (IIS) or Microsoft Transaction Server (MTS), please see the following article here in the Microsoft Knowledge Base:

186273 BUG: AV Running VB-Built Component in Multi-Threaded Environment





STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. This problem has been corrected in Microsoft Visual Basic 6.0.

NOTE: If your DLL is running in a multi-threaded environment, you must still complete the steps provided in the following article in the Microsoft Knowledge Base:

186273 BUG: AV Running VB-Built Component in Multi-Threaded Environment



Steps to Reproduce Behavior
 Create a new ActiveX DLL project in Visual Basic.</li> Click Project1 Properties from the Project menu and set the Project Name to "AutoErr1."</li>  Copy the following code to the Class1 module:

Public Sub Method1 Dim col As New Collection Dim obj As Object For Each obj In col Next obj End Sub

</li> Click "Make AutoErr1.dll" from the File menu to create the DLL file.</li> Quit Visual Basic. You may optionally save the project files.</li> Start Microsoft Excel 97 and create a new workbook. Press the ALT+F11 keys to start the Visual Basic editor.</li>  Click Module from the Insert menu to add a standard module. Add the following code to the new module:

Private Sub Test48 ' First call into dll Dim o as Object Set o = CreateObject("AutoErr1.Class1") Set o = Nothing ' Second call into dll Set o = CreateObject("AutoErr1.Class1") o.Method1 ' calls method that uses For..Each loop set o = Nothing End Sub

</li> Press the F8 key to step through the Sub's code and note that the following error occurs when calling Method1:

Run-time error '48':

Error in loading DLL

</li></ol>

The following steps demonstrate how to reproduce the Automation error using Visual Basic as the client. Note that these steps may not reproduce the error on some machines.

<ol> Follow steps 1 through 5 above to create the AutoErr1.dll.</li> Start a new Standard EXE project in Visual Basic. Form1 is created by default.</li> Place 2 CommandButtons (Command1 and Command2) on Form1.</li>  Copy the following code to the Form1 module:

Private Sub Command1_Click ' First call into dll Dim o As Object Set o = CreateObject("AutoErr1.Class1") Set o = Nothing End Sub

Private Sub Command2_Click ' Second call into dll Dim o As Object Set o = CreateObject("AutoErr1.Class1") o.Method1 Set o = Nothing End Sub

</li> Press the CTRL+F5 keys to run the project.</li> Click Command1.</li> Click Command2. Note that the following error may occur when calling Method1:

Runtime-error '-2147319765 (8002802b)':

Automation Error

</li></ol>

<div class="references_section">