2009-09-28 133 views
0

一些來源我已經檢查: 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/186063VB6自動化錯誤調用

我忙於開發.NET模塊將鉤到我們現有的VB6代碼中。我創建從中推出新的代碼,包括帶有按鈕的形式的測試VB6項目,並在按鈕的單擊事件是

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

它是成功的,我第一次單擊按鈕。但是,如果我再次點擊該按鈕時,我得到以下錯誤:

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

後續調用.NET代碼失敗,相同的錯誤消息,除非我關閉並重新啓動IDE。如果我將VB6項目編譯爲EXE,則會發生同樣的情況。我必須關閉EXE並再次運行它才能訪問.NET代碼。

我試過的建議上http://support.microsoft.com/kb/186063,也做了以下內容: 顯式的選項

' 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 

,但沒有發現錯誤。

有關如何解決此問題的任何想法?我試圖set launcher = Nothing但它沒有幫助。

謝謝你們。

回答

4

鑑於這一切在這裏參與,我不認爲有足夠的信息來做出有意義的猜測,實際的問題。如果我處於您的情況,我可能會做的第一件事是在Visual Studio中調試.NET組件,以查看問題是否是來自.NET組件的未處理異常:

  1. (可選,但通常很有幫助)配置VB6項目以在編譯期間生成PDB文件。打開VB6項目並轉到Project - > MyProject Properties ...菜單項。選擇「編譯」選項卡並選中「創建符號調試信息」
  2. 編譯VB6項目(文件 - >生成MyProject.exe)。
  3. 打開.NET組件項目/解決方案,進入項目 - > MyDotNetProject屬性...菜單項。選擇「調試」選項卡。將「開始操作」設置爲「啓動外部程序」並瀏覽到通過步驟2生成的.exe。
  4. (可選,但通常有幫助)選中「啓用非託管代碼調試」。
  5. 調試!

希望你會嘗試一下你列出的同樣的場景,這次它會導致一個異常,反過來會導致VS調試器在.NET代碼的違規行中斷裂。如果在行爲中沒有看到任何更改,則可以轉到Debug - > Exceptions ...菜單項(在VS中)並在「Common Language Runtime Exception」爲「Common Language Runtime Exception」時檢查相應的複選框以立即中斷拋出。

+0

感謝普拉特先生!請在頁面底部查看我的完整評論。 – AndrewJacksonZA 2009-09-29 14:26:51

0

谷歌提出了以下建議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.

0

謝謝普拉特先生!我按照.NET代碼的步驟做了你的建議,並且發現我必須在.NET代碼啓動的地方添加一個try/catch(名爲「VB6LaunchPad」的項目):

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

是否需要Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);

(對不起,不發表此評論,因此超過600個字符。)