AddressOf只是一个运算符,用于取得标准模块中的过程的地址,类似于传递指针

解决方案 »

  1.   

    一个一元运算符,它将其后面的过程的地址传递给一个 API 过程,该 API 过程在参数表对应位置中需要一个函数指针。只是个指针,懂了吗?看个例子:
    'Using the CreateThread function in Visual Basic
    'is very risky! VB5 is 'kinda' stable, but VB6
    'applications will probably crash when you
    'use the CreateThread function.'In a form
    'Add a command button to the form
    Private Sub Command1_Click()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'After you click this button, try to move the window
        'You will see that the AsyncThread-function was executed asynchronously
        hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
        CloseHandle hThread
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'If the thread is still running, close it
        If hThread <> 0 Then TerminateThread hThread, 0
    End Sub
    'In a module
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
    Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public hThread As Long, hThreadID As Long
    Public Sub AsyncThread()
        'Let this thread sleep for 10 seconds
        Sleep 10000
        hThread = 0
    End Sub
    在使用 AddressOf 关键字声明函数时,必须注意下列事项: AddressOf 只能紧接在是参数列表中的参数前;该参数可以是自定义的过程、函数或者属性的名字。
    写在 AddressOf 后面的过程、函数、属性必须与有关的声明和过程在同一个工程中。
    AddressOf 只能用于自定义的过程、函数和属性,不能将其用于 Declare 语句声明的外部函数,也不能将其用于类型库中的函数。
    在声明的 Sub、Function 或自定义的类型定义中,可以将函数指针传递到 As Any 或 As Long 类型的参数。