| | | | | Dim iSource As IntegerDim iDest As IntegeriSource = 5CopyMemory iDest, iSource, 2 | | | | | | After running this code, iDest will contain 5. | | | | | | Alternatively, we could replace the call to CopyMemory with: | | | | | | | | | 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: | | | | | | | | | 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: | | | | | | | | | 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). | | | | |