VB6 Automation Error on Calls to .NET 3.5 Assemblies After the First Calls

。_饼干妹妹 提交于 2020-01-03 05:04:18

问题


Some of the sources I've checked already: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23359339.html
http://mygreenpaste.blogspot.com/2006/03/net-framework-20-configuration-tool.html
http://support.microsoft.com/kb/186063

I'm busy developing .NET modules that will hook into our existing VB6 code. I've created a test VB6 project from which to launch the new code which comprises of a form with a button, and on the button's click event is

Dim launcher As New VB6InteropLaunchPad.launcher
launcher.FormTypeEnum = FormTypeEnum_MySpecificForm
launcher.launchAppropriateForm

It is successful the first time I click the button. However, if I click the button again, I get the following error:

Run-time error '-2146233079 (80131509)': Automation Error

Subsequent calls to the .NET code fail with the same error message unless I close and restart the IDE. If I compile the VB6 project to an EXE the same thing happens. I have to close the EXE and run it again to be able to access the .NET code.

I've tried the suggestion on http://support.microsoft.com/kb/186063 and did the following: Option Explicit

' http://support.microsoft.com/kb/186063

Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

Private Declare Function FormatMessage Lib "kernel32" Alias _
    "FormatMessageA" (ByVal dwFlags As Long, lpSource As Long, _
    ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _
    ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Any) _
    As Long

Private Function MessageText(lCode As Long) As String
     Dim sRtrnCode As String
     Dim lRet As Long

     sRtrnCode = Space$(256)
     lRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0&, lCode, 0&, _
                  sRtrnCode, 256&, 0&)
     If lRet > 0 Then
         MessageText = Left(sRtrnCode, lRet)
     Else
         MessageText = "Error not found."
     End If
End Function

On Error GoTo errHandler
   Dim launcher As New VB6InteropLaunchPad.launcher
   launcher.FormTypeEnum = FormTypeEnum_MySpecificForm
   launcher.launchAppropriateForm
   Exit Sub

errHandler:
   MsgBox MessageText(Err.Number)
   MsgBox Err

but the error was not found.

Any ideas on how to fix this? I tried to set launcher = Nothing but it doesn't help.

Thanks guys.


回答1:


Given all that is involved here, I don't think there is enough information to make a meaningful guess as to the actual issue. If I were in your situation, the first thing I would probably do is debug the .NET component in Visual Studio in order to see if the problem is an unhandled exception coming from the .NET component:

  1. (Optional, but often helpful) Configure the VB6 project to generate a PDB file during compilation. Open the VB6 project and go to the Project -> MyProject Properties... menu item. Select the "Compile" tab and check "Create Symbolic Debug Info"
  2. Compile the VB6 project (File -> Make MyProject.exe).
  3. Open the .NET component project/solution, go to the Project -> MyDotNetProject Properties... menu item. Select the "Debug" tab. Set the "Start Action" to "Start External Program" and browse to the .exe generated via step 2.
  4. (Optional, but often helpful) Check "Enable unmanaged code debugging".
  5. Debug!

Hopefully you'll try the same scenario you outlined and this time it will cause an exception that, in turn, causes the VS debugger to break on the offending line of .NET code. If you don't see any change in behavior, it may be helpful to go to the Debug -> Exceptions... menu item (in VS) and check the appropriate check box to break immediately when a "Common Language Runtime Exception" is thrown.




回答2:


Google turned up the following suggestion from someone with the same automation error in a similar interop situation.

The only slightly unusual thing was that my exposed class inherits from a base class. Because I don't want the base class exposed to COM it was decorated with [ComVisible(false)]. When I finally removed this decoration from the base class the COM class worked in VB6.

So it seems mscorlib does not want to expose a .NET class to COM if its base class is not visible. My lesson is that classes which are to be exposed should not inherit. They should single classes that delegate to a contained instance of the class that really does the work.




回答3:


Check out this article. It has a step by step troubleshooting steps for these types of situations.

http://www.vbrad.com/article.aspx?id=46




回答4:


Thanks Mr. Pratt! I did what you suggested by stepping through the .NET code and discovered that I had to add a try/catch where my .NET code starts up (the humourously-but-fittingly-named "VB6LaunchPad" project):

Application.EnableVisualStyles();
try
{
   Application.SetCompatibleTextRenderingDefault(false);
}
catch (InvalidOperationException)
{
   // Do Nothing
}
// Some DevExpress init code
SkinManager.EnableFormSkins();
LookAndFeelHelper.ForceDefaultLookAndFeelChanged();

Are Application.EnableVisualStyles(); and Application.SetCompatibleTextRenderingDefault(false); even necessary?

(Sorry about not posting this as a comment, it was more than 600 characters.)



来源:https://stackoverflow.com/questions/1487406/vb6-automation-error-on-calls-to-net-3-5-assemblies-after-the-first-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!