主题:[讨论]关于CopyMemory的奇怪的现象
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim s1 As String
Dim s2 As String
Dim pStr As Long
s1 = "English中文"
'Test1 :OK
s2 = String$(9, 0)
CopyMemory s2, s1, Len(s1) 'lenB(s1)
'是否在Text1里显示字符,居然会影响到s1、s2的取值???!!!
Text1.Text = Text1.Text & "s2=" & s2 & ",len(s2)=" & Len(s2) & ",lenB(s2)=" & LenB(s2) & vbCrLf
Text1.Text = Text1.Text & "s1=" & s1 & vbCrLf
Debug.Print "s2="; s2, "len(s2)="; Len(s2), "lenB(s2)="; LenB(s2) 'English中文,9,18
Debug.Print "s1="; s1
'Test2 : OK
s2 = String$(9, 0)
CopyMemory pStr, ByVal VarPtr(s1), 4
'CopyMemory ByVal s2, ByVal pStr, LenB(s1) 'Engl,4,9
'CopyMemory ByVal s2, pStr, LenB(s1) '??,3,7
'CopyMemory s2, ByVal pStr, LenB(s1) ' ,0,0
'CopyMemory ByVal StrPtr(s2), ByVal pStr, LenB(s1) 'without StrConv() ,English中文,9,18
CopyMemory s2, pStr, LenB(s1) 'English中文,9,18
s2 = StrConv(s2, vbFromUnicode)
Text1.Text = Text1.Text & "s2=" & s2 & ",len(s2)=" & Len(s2) & ",lenB(s2)=" & LenB(s2) & vbCrLf
Text1.Text = Text1.Text & "s1=" & s1 & vbCrLf
Debug.Print "s2="; s2, "len(s2)="; Len(s2), "lenB(s2)="; LenB(s2)
Debug.Print "s1="; s1
'Test3
s2 = String$(9, 0) '此处对s2的赋值会影响到s1 ?
CopyMemory ByVal StrPtr(s2), ByVal StrPtr(s1), LenB(s1)
Text1.Text = Text1.Text & "s2=" & s2 & ",len(s2)=" & Len(s2) & ",lenB(s2)=" & LenB(s2) & vbCrLf
Text1.Text = Text1.Text & "s1=" & s1 & vbCrLf
Debug.Print "s2="; s2, "len(s2)="; Len(s2), "lenB(s2)="; LenB(s2)
Debug.Print "s1="; s1 's2是否重新赋值,以及是否在Text1里显示,似乎会影响到s1的值!!!莫名其妙
End Sub
copymemory的正确使用方法是怎样的?
字符串拷贝时如果用copyMemory我一直都是下面这样用的。
CopyMemory s2, s1, Len(s1) 'lenB(s1)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim s1 As String
Dim s2 As String
Dim pStr As Long
s1 = "English中文"
'Test1 :OK
s2 = String$(9, 0)
CopyMemory s2, s1, Len(s1) 'lenB(s1)
'是否在Text1里显示字符,居然会影响到s1、s2的取值???!!!
Text1.Text = Text1.Text & "s2=" & s2 & ",len(s2)=" & Len(s2) & ",lenB(s2)=" & LenB(s2) & vbCrLf
Text1.Text = Text1.Text & "s1=" & s1 & vbCrLf
Debug.Print "s2="; s2, "len(s2)="; Len(s2), "lenB(s2)="; LenB(s2) 'English中文,9,18
Debug.Print "s1="; s1
'Test2 : OK
s2 = String$(9, 0)
CopyMemory pStr, ByVal VarPtr(s1), 4
'CopyMemory ByVal s2, ByVal pStr, LenB(s1) 'Engl,4,9
'CopyMemory ByVal s2, pStr, LenB(s1) '??,3,7
'CopyMemory s2, ByVal pStr, LenB(s1) ' ,0,0
'CopyMemory ByVal StrPtr(s2), ByVal pStr, LenB(s1) 'without StrConv() ,English中文,9,18
CopyMemory s2, pStr, LenB(s1) 'English中文,9,18
s2 = StrConv(s2, vbFromUnicode)
Text1.Text = Text1.Text & "s2=" & s2 & ",len(s2)=" & Len(s2) & ",lenB(s2)=" & LenB(s2) & vbCrLf
Text1.Text = Text1.Text & "s1=" & s1 & vbCrLf
Debug.Print "s2="; s2, "len(s2)="; Len(s2), "lenB(s2)="; LenB(s2)
Debug.Print "s1="; s1
'Test3
s2 = String$(9, 0) '此处对s2的赋值会影响到s1 ?
CopyMemory ByVal StrPtr(s2), ByVal StrPtr(s1), LenB(s1)
Text1.Text = Text1.Text & "s2=" & s2 & ",len(s2)=" & Len(s2) & ",lenB(s2)=" & LenB(s2) & vbCrLf
Text1.Text = Text1.Text & "s1=" & s1 & vbCrLf
Debug.Print "s2="; s2, "len(s2)="; Len(s2), "lenB(s2)="; LenB(s2)
Debug.Print "s1="; s1 's2是否重新赋值,以及是否在Text1里显示,似乎会影响到s1的值!!!莫名其妙
End Sub
copymemory的正确使用方法是怎样的?
字符串拷贝时如果用copyMemory我一直都是下面这样用的。
CopyMemory s2, s1, Len(s1) 'lenB(s1)