Article ID: 171652
Article Last Modified on 2/12/2007
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal numbytes As Long)
A long integer consists of four bytes. These four bytes are recognized by
Visual Basic to be one number. Use the following subroutine to move the
individual bytes into a byte array:
Public Sub LongToBytes(ByRef TheArray() As Byte, ByRef TheLong As Long)
Call CopyMemory(TheArray(LBound(TheArray)), TheLong, 4)
End Sub
The first parameter passed to CopyMemory is the first element of the array
into which the bytes will be stored. (NOTE: The entire array is passed to
longToBytes.) The second parameter is the source variable. The third
parameter is how many bytes will be moved. Because a long integer consists
of four bytes, this parameter has been set to 4.
Public Function BytesToLong(TheArray() As Byte) As Long
Dim TempLong As Long
Call CopyMemory(TempLong, TheArray(LBound(TheArray)), 4)
BytesToLong = TempLong
End Function
NOTE: As with any API call, passing improper memory addresses can cause
data corruption or could cause your program to terminate abnormally.
129947 Win32 Replacement for the hmemcpy Function
Keywords: kb32bitonly kbhowto kbprogramming KB171652