vbÖÐcopymemoryº¯ÊýµÄʹÓÃͦÄÍÈËѰζµÄ¡£copymemoryµÄʹÓÃ˵Ã÷×ÊÁÏÊéÉϾÍÒ»¾ä¡°¸Ãº¯ÊýÓÃÓÚ½«Ò»¿éÄÚ´æµÄÊý¾Ý´ÓÒ»¸öλÖø´ÖƵ½ÁíÒ»¸öλÖᱡ£Æä²ÎÊýÊý¾ÝÀàÐÍdestination as any,source as any¡£¾¡¹ÜÊÇanyÐÍ¿ÉÀí½â³ÉÈÎÒ»ÀàÐ͵«ÊÇÎÒ¿´ºÜ¶àµØ·½¶¼Ëµ²ÎÊýÊÇÖ¸ÕëÀàÐ͵ġ£Òò´ËÆð³õÎҺܲ»½â£¬¼ÈÈ»ÊÇÖ¸ÕëÐ͵IJÎÊýÎÒÃÇÍùÍùÖ±½Ó½«±äÁ¿´«µÝ¹ýÈ¥¶ø²»ÊDZäÁ¿µÄµØÖ·´«µÝ¹ýÈ¥²»ÊÇ·Ç·¨µÄÂð£¿ÆäʵcopymemoryʹÓõÄÁé»î¶ÈÊǺܴóµÄ£¬»¹ÊÇ¿ªÊ¼¸ø³öµÄ¶¨Òå×îÓÐ˵Ã÷ÐÔ£¬½«Ò»¿éÄÚ´æµÄÊý¾Ý´ÓÒ»¸öλÖø´ÖƵ½ÁíÒ»¸öλÖþÍÊÇ×îºÃµÄ½âÊÍ¡£¿´ÏÂÃ漸¶Î³ÌÐòÓÐÖúÓÚÎÒÃǸüºÃÀí½â¡£
³ÌÐòÒ»£ºPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String
str2 = "hello world"
CopyMemory str1, str2, 4
Text1.Text = str1
End Sub
ÕâÊǺòµ÷ÊÔ½á¹ûtext1ÏÔʾµÄÄÚÈÝÍêÈ«¾ÍÊÇstr2¸´ÖƹýÈ¥µÄÄÚÈÝ£¬¼´hello world¡£
Dim str1 As String
Dim str2 As String
str2 = "hello world"
CopyMemory str1, str2, 4
Text1.Text = str1
End Sub
ÕâÊǺòµ÷ÊÔ½á¹ûtext1ÏÔʾµÄÄÚÈÝÍêÈ«¾ÍÊÇstr2¸´ÖƹýÈ¥µÄÄÚÈÝ£¬¼´hello world¡£
Èç¹û¸ÄÒ»¾ä¡°copymemory str1,strptr(str2),4"£¬Õâʱºò´«µÝµÄÊÇstr2µÄÖ¸Õ룬ÄÇôtext1ÏÔʾµÄ½á¹û¾ÍÊÇh¡£
ÔõôÑùÈ¥Àí½â£¿strptr(str2)µÃµ½µÄÊÇÒ»¸öÖ¸ÕëÎãÓ¹ÖÃÒÉ£¬¼´Ê¹º¯Êý²ÎÊýÊÇanyÐ͵ÄÒò´Ë¸Ã²ÎÊýÊǺϷ¨µÄûÎÊÌ⣬ÄÇô´«µÝ¸østr1£¬str1¿ªÊ¼ÉêÃ÷µÄÊÇÒ»¸östringÐ͵ıäÁ¿£¬¶ø²»ÊÇÒ»¸östringÐ͵ÄÖ¸Õë±äÁ¿£¬Òò´Ë´«µÝ²ÎÊýµÄÀàÐͲ»Ò»ÖÂΪʲôҲÊǺϷ¨µÄ£¬¶øÇÒ×îÖյõ½Ò»¸ö½á¹ûh¡£ÓÚÊÇÎÒÉÏÍøÕÒÁËvbÖйØÓÚÖ¸Õë±äÁ¿µÄ¶¨Ò壬´ó¼Ò¶¼ËµvbûÓÐÖ±½Ó¶¨ÒåÖ¸ÕëµÄ¡£Òò´ËÎÒÏëÊDz»ÊÇstr1·ÅÔÚcopymemoryÖÐ×ö²ÎÊý½ÓÊÜÖ¸Õë±äÁ¿µÄʱºòÒѾ±»Ç¿ÖÆת±ä³ÉÖ¸ÕëÐͱäÁ¿ÁË¡£
³ÌÐò¶þ£ºPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
Dim i As Long
Dim k As Long
k = 5
i = VarPtr(k)
Debug.Print i
Dim i As Long
Dim k As Long
k = 5
i = VarPtr(k)
Debug.Print i
Debug.Print VarPtr(i)
CopyMemory i, 40000, 4
Debug.Print VarPtr(i)
Debug.Print i
Debug.Print k
Debug.Print "*******"
End Sub
CopyMemory i, 40000, 4
Debug.Print VarPtr(i)
Debug.Print i
Debug.Print k
Debug.Print "*******"
End Sub
µ÷ÊÔ½á¹û£º
1243232
1243232
1243236
1243236
40000
5
*******
1243236
40000
5
*******
˵Ã÷±äÁ¿i£¬kµÄµØÖ·ÊÇ1243232,1243236¡£ÕâÒ»¾ä£ºCopyMemory i, 40000, 4¾ÍÊÇÖ±½Ó¸ø±äÁ¿i¸³Öµ40000¶ø²»ÊǰѵØÖ·Öµ40000¸³¹ýÈ¥½«±äÁ¿iµÄµØÖ·¸Ä±ä³É40000¡£Èç¹ûÏëͨ¹ýÖ¸ÕëµÄ·½Ê½ÐÞ¸ÄkµÄ³õʼֵµÄ»°¡£
¿ÉÒÔÓÃÏÂÃæÕ⼸¾ä£º
Dim i As Long
Dim k As Long
k = 5
i = VarPtr(k)
CopyMemory ByVal i, 40000, 4
Debug.Print k
Debug.Print "*******"
Dim k As Long
k = 5
i = VarPtr(k)
CopyMemory ByVal i, 40000, 4
Debug.Print k
Debug.Print "*******"
Õâʱºò¾Í»á·¢ÏÖkµÄÖµÒѾ±»Ð޸ijÉ40000ÁË¡£ÔõôÑùÈ¥Àí½â£¿i µÄÖµÓÉi=varptr(k)Ò»¾ä±ä³ÉkµÄµØÖ·ÖµÁË£¬ÕâʱºòÓÃbyval i±íʾµÄÊǸõØÖ·Ö¸ÏòµÄÄÚÈÝ£¬Ð޸ijÉ40000ºóÒ²¾ÍÊÇkÖµ±»ÐÞ¸ÄÁË¡£
VBÖÐÓÉÓÚûÓÐÃ÷È·µÄÖ¸Õ붨Ò壬Éæ¼°µ½Ö¸Õë²Ù×÷ÊÇͨ¹ýAPIº¯ÊýcopymemoryÀ´½øÐУ¬Ïà¶ÔÓÚCµÈÓïÑÔÀ´ËµÐÎʽÉÏûÓÐÄÇôÑϸñÒªÇó£¬Òò´ËÍâÐÎÉÏ¿´ºÜ²»Ã÷Îú¡£
ÈçºÎÔÚVBÖпìËÙ½ØÈ¡Êý×éÖеÄÒ»²¿·Ö£¿
Option
Explicit
Private
Declare
Sub
CopyMemory
Lib
"kernel32.dll"
Alias
"RtlMoveMemory"
(
ByRef
Destination
As
Any,
ByRef
Source
As
Any,
ByVal
Length
As
Long
)
Private
Sub
Form_Load()
Dim
a(1
To
1000)
As
Double
Dim
b(1
To
100)
As
Double
Dim
i
As
Long
'Ïȸ³Öµ£¬ÓÚÌâÄ¿ÎÞ¹Ø~~~
For
i = LBound(a)
To
UBound(a)
a(i) = i
Next
i
'ÕâÀï²ÅÊǹؼü
Call
CopyMemory(b(LBound(b)), a(200 + 1), UBound(b) * Len(a(200 + 1)))
'ÏÖÔÚÊä³ö¿´¿´½á¹û
For
i = LBound(b)
To
UBound(b)
Debug.Print b(i)
Next
i
End
Sub