我目前正在将我们的软件从VB6升级到VB.NET,并且一次只执行一个DLL。 EXE将是最后一个,因为它有很多第三方UI控件。
我发现的一个问题是DLL的VB6版本包含一个IProgressCallBack类,它类似于一个接口类(VB6中没有正式存在),并允许EXE从DLL获取进度报告并显示它在用户的屏幕上。
迁移此DLL时,可以将IProgressCallBack类设置为Interface类,但不能使用它将其“设置”为表单实例。但是,如果我将它创建为普通类,它仍然不会让我将它“设置”为使用它的表单实例,因为它们是两种不同的对象类型。
我需要知道如何让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 EXE
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
谢谢, 克里斯
答案 0 :(得分: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 EXE
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 EXE示例的VB6 DLL相同的结果,并且EXE中的更改很小,以使其工作。
谢谢, 克里斯