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是可以使用指针的.
Dim Sstr As String, Dstr As String
Sstr = "hello"
Sptr = VarPtr(Sstr)
Dptr = VarPtr(Dstr)
Call CopyMemory(Dptr, Sptr, 5)
MsgBox (Dstr)
结果显示: dstr 为空(即无论何值)!在相关资料中,看到vb是可以使用指针的.
Dim Sstr As String, Dstr As String
Sstr = "hello"
Sptr = VarPtr(Sstr)
Dptr = VarPtr(Dstr)
Call CopyMemory(ByVal Dptr, Byval Sptr, 5)
MsgBox (Dstr)
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
Sstr = "hello"
Sptr = VarPtr(Sstr)
Dptr = VarPtr(Dstr)
要使 Dstr得到 字符 "ello", 用指针按值传递.
Call CopyMemory 该如何写呢?