Dim Sptr As Long, Dptr As Long
Dim Sstr As String, Dstr As String
Sstr = "hello"
Sptr = VarPtr(Sstr)
Dptr = VarPtr(Dstr)
Call CopyMemory(Dptr, Sptr, 5)
MsgBox (Dstr)
结果显示: dstr 为空(即无论何值)!在相关资料中,看到vb是可以使用指针的.

解决方案 »

  1.   

    Dim Sptr As Long, Dptr As Long
    Dim Sstr As String, Dstr As String
    Sstr = "hello"
    Sptr = VarPtr(Sstr)
    Dptr = VarPtr(Dstr)
    Call CopyMemory(ByVal Dptr, Byval Sptr, 5)
    MsgBox (Dstr)
      

  2.   

    你的代码有两个问题:
    1.你把地址取出来传递给函数就需要按值传递,前两个参数加 ByVal。
    2.被传递变量的长度应该是按字节计算的 LenB("hello")=10也可以不把变量的地址出来,直接使用,函数参数传递默认按址传递。
    代码如下 :Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click()Dim Sstr As String, Dstr As String
    Sstr = "hello"Call CopyMemory(Dstr, Sstr, LenB(Sstr))
    MsgBox (Dstr)End Sub
      

  3.   

    谢谢两位的回复,本人获益非浅.此问题已决解. 现在遇到一个新的麻烦就是:  Dim Sstr As String, Dstr As String
      Sstr = "hello"
     Sptr = VarPtr(Sstr)
     Dptr = VarPtr(Dstr)
     要使 Dstr得到 字符 "ello", 用指针按值传递.
     Call CopyMemory 该如何写呢?