vb.net 在dll里如何实现回调

用vb.net语言写个dll,在dll里如何执行主程序里的一个自定义函数

可以在dll中提供委托函数,让主程序将其自定义函数注册到dll的委托函数中。然后在dll中调用委托函数实现对主程序的自定义函数调用。
声明一个module用于传递声明委托

Module myModule
    '声明委托
    Public myaction As Func(Of String, String)

    '创建委托
    'Public Delegate Function myact(str As String) As Integer
    'Public Delegate Sub myact2(str As String)
    '声明委托
    'Public myaction As myact
    'Public myaction2 As myact2

    Public Mainf As New MainForm
    Sub Main()

        Application.EnableVisualStyles()  '启用xp视觉样式
        Application.Run(Mainf) '加不加()都是调用默认构造函数,最好加
    End Sub
End Module

主程序

Public Class MainForm
    Dim childF As New ChildForm

    Private Function Mycall(str As String) As String
        TextBox1.Text = str
        Return 123
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        childF.Show()
        childF.RSMsg = TextBox1.Text
        myaction = AddressOf Mycall
        'myaction2 = New myact2(AddressOf Mycall)
    End Sub
End Class

dll子程序:

Public Class ChildForm
    Public Property RSMsg() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal Value As String)
            TextBox1.Text = Value
        End Set
    End Property

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim num As Integer
        num = myaction.Invoke(RSMsg)

    End Sub
End Class


这个是有点东西,因为主程序是在,但是主窗体还没有创建.当登录成功后,登录窗体关闭,卸载显示登录窗体的DLL,然后再创建DLL.


Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 104)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(124, 64)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Integer) As Integer
Public Delegate Function EnumWindowsProc(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean

Private str As New String("")
Private i As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim em As New EnumWindowsProc(AddressOf EnumWndProc)
EnumWindows(em, 0)
MessageBox.Show(str)
End Sub

Public Function EnumWndProc(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean
str += "窗体句柄:" + hWnd.ToString + " "
If i Mod 3 = 0 Then str += vbCrLf : i = 0
i += 1
Return True
End Function
End Class