1

我目前正在将我们的软件从 VB6 升级到 VB.NET,并且一次只升级一个 DLL。EXE 将是最后一个,因为它有很多第三方 UI 控件。

我发现的一个问题是 DLL 的 VB6 版本包含一个 IProgressCallBack 类,该类充当接口类(在 VB6 中不正式存在)并允许 EXE 从 DLL 获取进度报告并将其显示在屏幕上为用户。

迁移此 DLL 时,可以将 IProgressCallBack 类设置为接口类,但不能将其“设置”到使用它的表单实例。但是如果我将它创建为一个普通类,它仍然不会让我将它“设置”到使用它的表单实例,因为它们是两种不同的对象类型。

我需要知道如何让 IProgressCallBack 类在 VB6 和 VB.NET 之间工作,或者什么是让 VB6 UI 了解 .NET DLL 进度的替代解决方案?

以下是两者的 VB6 版本及其工作方式的示例:

VB6 DLL: IProgressCallBack

Option Explicit
Public Function NotifyProgress(ByVal Progress As Single, ByRef isCancel As Boolean) As Boolean
End Function

VB6 DLL:MainFunctions 类

Public ProgressNotify As IProgressCallBack
Public Sub DoWork()
    Dim l As Long
    For l = 1 to 100
        Sleep 10 'sleep for 1/100 of a second
        ProgressNotify.NotifyProgress 1, False
    Next
End Sub

VB6 可执行文件

Implements IProgressCallBack
Dim m_oMainFunc As MainFunctions

Private Sub cmdStart_Click()
    Set m_oMainFunc = New MainFunctions
    Set m_oMainFunc.ProgressNotify = Me
    m_oMainFunc.DoWork
    Set m_oMainFunc = Nothing
End Sub

Public Function IProgressCallBack_NotifyProgress(ByVal Progress As Single, isCancel As Boolean) As Boolean
    lblStatus.Caption = CStr(Round(Progress)) & "%"
    pbStatus.Value = Round(Progress)
    Me.Refresh
End Function

谢谢,克里斯

4

1 回答 1

1

我还没有完全找到我上面发布的问题的答案,但我找到了一个很好的解决方法。本质上,我将表单作为对象传递给伪装成接口的类,并直接调用驻留在表单上的接口方法来更新屏幕。这是我如何执行此操作的代码示例:

VB.NET DLL: IProgressCallBack 类

Dim m_oForm As Object

Public Sub New(ByRef oForm As Object)
    m_oForm = oForm
End Sub

Public Function NotifyProgress(ByVal Progress As Single, ByRef isCancel As Boolean) As Boolean
m_oForm.IProgressCallBack_NotifyProgress(Progress, isCancel)
End Function

VB.NET DLL:MainFunctions 类

Public ProgressNotify As IProgressCallBack

Public Sub New()
    'Need Public Sub New() with no parameters for all Com Classes
    MyBase.New()
End Sub

Public Sub Initialize(ByRef oForm As Object)
    ProgressNotify = New IProgressCallBack(oForm)
End Sub

Public Sub DoWork()
    For l As Long = 1 to 100
        Threading.Thread.Sleep(10) 'sleep for 1/100 of a second
        ProgressNotify.NotifyProgress(l,False)
    Next
End Sub

VB6 可执行文件

Dim m_oMainFunc As TestDLL_Net.MainFunctions
Private cmdStart_Click()
    Set m_oMainFunc = New TestDLL_Net.MainFunctions
    m_oMainFunc.Initialize Me
    m_oMainFunc.DoWork
    Set m_oMainFunc = Nothing
End Sub

Public Function IProgressCallBack_NotifyProgress(ByVal Progress As Single, isCancel As Boolean) As Boolean
    DoEvents
    lblStatus.Caption = CStr(Round(Progress)) & "%"
    Me.Refresh
    pbStatus.Value = Round(Progress)
End Function

这提供了与上面发布的 VB6 DLL 到 VB6 EXE 示例相同的结果,并且 EXE 中的更改很少使其工作。

谢谢,克里斯

于 2012-05-01T12:53:18.473 回答