请教一个数值变量,如何获取变量的地址?

解决方案 »

  1.   

    dim a as integer
    a=10
    yourfunction( addressof a)
      

  2.   

    2、VatPtr/StrPtr/ObjPtr 
        它们是VB提供给我们的好宝贝,它们是VBA函数库中的隐藏函数。为什么要隐藏?因为VB开发小组,不鼓励我们用指针嘛。 
        实际上这三个函数在VB运行时库MSVBVM60.DLL(或MSVBVM50.DLL)中是同一个函数VarPtr(可参见我在本系列第一篇文章里介绍的方法)。 
    其库型库定义如下: 
            [entry("VarPtr"), hidden] 
            long _stdcall VarPtr([in] void* Ptr); 
            [entry("VarPtr"), hidden] 
            long _stdcall StrPtr([in] BSTR Ptr); 
            [entry("VarPtr"), hidden] 
            long _stdcall ObjPtr([in] IUnknown* Ptr);    
        即然它们是VB运行时库中的同一个函数,我们也可以在VB里用API方式重新声明这几个函数,如下: 
    Private Declare Function ObjPtr Lib "MSVBVM60" Alias "VarPtr" _ 
      (var As Object) As Long 
    Private Declare Function VarPtr Lib "MSVBVM60"  _ 
      (var As Any) As Long 
    (没有StrPtr,是因为VB对字符串处理方式有点不同,这方面的问题太多,在本系列中另用一篇《VB字符串全攻略》来详谈。 
        顺便提一下,听说VB.NET里没有这几个函数,但只要还能调用API,我们就可以试试上面的几个声明,这样在VB.NET里我们一样可以进行指针操作。 
        但是请注意,如果通过API调用来使用VarPtr,整个程序二SwapPtr将比原来使用内置VarPtr函数时慢6倍。) 
        如果你喜欢刨根问底,那么下面就是VarPtr函数在C和汇编语言里的样子: 
        在C里样子是这样的: 
        long VarPtr(void* pv){ 
            return (long)pv; 
        } 
        所对就的汇编代码就两行: 
        mov         eax,dword ptr [esp+4] 
        ret         4           注释:弹出栈里参数的值并返回。 
        之所以让大家了解VarPtr的具体实现,是想告诉大家它的开销并不大,因为它们不过两条指令,即使加上参数赋值、压栈和调用指令,整个获取指针的过程也就六条指令。当然,同样的功能在C语言里,由于语言的直接支持,仅需要一条指令即可。但在VB里,它已经算是最快的函数了,所以我们完全不用担心使用VarPtr会让我们失去效率!速度是使用指针技术的根本要求。 
        一句话,VarPtr返回的是变量所在处的内存地址,也可以说返回了指向变量内存位置的指针,它是我们在VB里处理指针最重要的武器之一。
      

  3.   

    addressof只能取函数地址Public Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Declare Sub ZeroMemory Lib "KERNEL32" Alias "RtlZeroMemory" (dest As Any, ByVal numBytes As Long)To save and restore a Byte Array
    ' To Save the array b():
    Public Function SaveToMemory(ByRef b() as Byte) As Long
       Dim lSize As Long
       Dim lPtr As Long   On Error Resume Next ' Error checking: UBound/LBound can fail.
       lSize = UBound(b) - LBound(b)
       If Err.Number=0 Then
          lSize = lSize + 1
       Else
          ' No Data:
          lSize = 0
       End If   On Error Goto 0
       lPtr = isMalloc.Alloc(lSize + 4)
       If Not (lPtr=0) Then
          ' Store the size of the array:
          CopyMemory ByVal lPtr, lSize, 4
          If lSize > 0 Then
             ' Store the array:
             CopyMemory ByVal lPtr + 4, b(0), lSize 
          End If
       End If   ' Return the Value of lPtr
       SaveToMemory = lPtr
    End Function' To Restore the array from the Pointer lPtr:
    Public Sub RestoreFromMemory(ByVal lPtr As Long, ByRef b() as Byte)
       Dim lSize As Long   Erase b
       If Not (lPtr = 0) Then
          ' Get the size of the array:
          CopyMemory lSize, ByVal lPtr, 4
          If lSize > 0 Then
             ReDim b(0 To lSize-1) As Byte
             CopyMemory b(0), ByVal lPtr, lSize
          End If
       End If End Sub To save and restore a User-Defined Type
    Provided there are no variable length strings in the type, then you can save and restore a type in exactly the same way as for a ByteArray. Just replace the last argument with Len(udtThis) where udtThis is your Type.To save and restore a String
    ' To Save the String sThis
    Public Function SaveToMemory(ByRef sThis as String) As Long
       Dim lSize As Long
       Dim lPtr As Long   ' Size of sThis in Bytes:
       lSize = LenB(sThis)   lPtr = isMalloc.Alloc(lSize + 4)
       If Not (lPtr=0) Then
          ' Store the size of the string:
          CopyMemory ByVal lPtr, lSize, 4
          If lSize > 0 Then
             ' Store the Unicode String:
             CopyMemory ByVal lPtr + 4, ByVal StrPtr(sThis), lSize 
          End If
       End If   ' Return the Value of lPtr
       SaveToMemory = lPtr
    End Function' To Restore the array from the Pointer lPtr:
    Public Function RestoreFromMemory(ByVal lPtr As Long) As String
       Dim lSize As Long
       Dim sThis As String   If Not (lPtr = 0) Then
          ' Get the size of the array:
          CopyMemory lSize, ByVal lPtr, 4
          If lSize > 0 Then
             sThis = String$(lSize\2,0)
             CopyMemory ByVal StrPtr(sThis), ByVal lPtr + 4, lSize
          End If
       End If End Sub 详细看http://www.vbaccelerator.com/home/VB/Code/Techniques/Malloc_in_VB/article.asp