INFO: Win32 Replacement for the hmemcpy Function

This article was previously published under Q129947
This article has been archived. It is offered "as is" and will no longer be updated.
The hmemcpy function is no longer available in Windows 32-bit developmentsystems. This article describes the Win32 Replacement for HMEMCPY function.
Excerpted from "Hardcore Visual Basic" by Bruce McKinney, published byMicrosoft Press:

CopyMemory: A Strange and Terrible Saga

Experienced Basic API programmers have come to know, if not love, thehmemcpy function. When C's weak data typing and Basic's strong data typingmeet in the Windows API, hmemcpy is frequently called on to mediate. The hin the name indicates that hmemcpy can handle huge memory (greater than 64Kbytes), but Basic programmers rarely need it for such large chunks.Unfortunately, if you look for hmemcpy in the Win32 documentation, you'llcome up with nothing - not even a note saying that the function isobsolete.

But you might happen to run across the Win32 CopyMemory function, which hasexactly the same arguments and in fact looks like the same procedure. The hhas disappeared because all memory in 32-bit mode is huge. If you write aDeclare statement for CopyMemory, however, giving KERNEL32.DLL as the mostlikely library, you'll get nothing but an error indicating that no suchfunction exists. In fact, you can search all the 32-bit DLLs with theDumpBin utility, but you won't find any containing CopyMemory.

But a careful search of Win32 C include files turns up the following inWINBASE.H:
   #define CopyMemory RtlCopyMemory   #define MoveMemory RtlMoveMemory   #define ZeroMemory RtlZeroMemory				

This C equivalent of an alias indicates that CopyMemory is another name fora function called RtlCopyMemory. Don't ask why; just check forRtlCopyMemory in KERNEL32.DLL. Again, nothing. A little more sleuthing inWin32 include files reveals the reason. WINNT.H contains something likethis:
   #define RtlCopyMemory(dst, src, len) memcpy(dst, src, len)				

In other words, RtlCopyMemory is an alias for the C memcpy function, butyou can't use memcpy or any other C library function from Basic. If it'snot exported from a DLL, you can't call it.

But KERNEL32.DLL does contain an entry for RtlMoveMemory. If you check theWin32 documentation, you'll see that MoveMemory does the same thing asCopyMemory except that it handles overlapped memory differently. I can'timagine a situation in which a Basic programmer would be copying overlappedmemory. No reason not to use MoveMemory instead. Because CopyMemory is moreintelligible than hmemcpy, I alias this name for both 16-bit and 32-bitversions:
   #If Win32 Then      Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( _         hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)   #Else      Declare Sub CopyMemory Lib "KERNEL" Alias "hmemcpy" ( _         hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)   #End If				

You must still be careful about what you pass to these functions. There area lot of issues with ByVal versus ByRef depending on whether you're passingstrings, UDTs, pointers, and so on.
4.00 vb4win vb432

Article ID: 129947 - Last Review: 12/04/2015 11:21:03 - Revision: 2.0

Microsoft Visual Basic 4.0 Standard Edition, Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

  • kbnosurvey kbarchive kbinfo kbcode KB129947