![](/books/4/460/1/html/2/gfx/ab.gif) | | | | | Dim iSource As Integer Dim iDest As Integer iSource = 5
CopyMemory iDest, iSource, 2 | | | | | | After running this code, iDest will contain 5. | | | | | | Alternatively, we could replace the call to CopyMemory with: | | | | ![](/books/4/460/1/html/2/gfx/ab.gif) | | | | | CopyMemory ByVal VarPtr(iDest), iSource, 2 | | | | | | Admittedly, while there is no reason to do this here, there will be cases when we want to pass by value instead of by reference but don't want to create a separate VB declaration for this purpose. | | | | | | | | | CopyMemory A VB Hacker's Dream | | | | | | One of the API functions we will use most often is CopyMemory. The purpose of CopyMemory is simply to copy a block of memory byte-by-byte from one memory address to another. This opens up a whole new set of possibilities, since VB does not have this sort of capability, except in the rather restricted form of LSet, and even then, the documentation recommends against using LSet for this purpose. | | | | | | The CopyMemory function has an interesting history. Actually, it is an alias for the RtlMoveMemory API function. Apparently, the name CopyMemory was first coined by Bruce McKinney, the author of Hardcore Visual Basic. Nevertheless, CopyMemory is now in the official Microsoft documentation as: | | | | ![](/books/4/460/1/html/2/gfx/ab.gif) | | | | | VOID CopyMemory( PVOID Destination, // pointer to address of copy destination CONST VOID *Source, // pointer to address of block to copy DWORD Length // size, in bytes, of block to copy ); | | | | | | The keyword CONST simply means that the function CopyMemory guarantees not to make any changes to the argument Source. Since PVOID is a synonym for VOID *, the two parameters Source and Destination have the same data type a pointer to a target variable of unknown type. | | | | | | The simplest VB declaration for CopyMemory is: | | | | ![](/books/4/460/1/html/2/gfx/ab.gif) | | | | | Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _ lpDest As Any, _ lpSource As Any, _ ByVal cbCopy As Long) | | | | | | In this case, lpDest is the address of the first byte of the destination memory, lpSource is the address of the first byte of the source memory and cbCopy is the number of bytes to copy (the cb in cbCopy stands for count of bytes). | | | | |