RtlCompareDeviceMemory - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
// wdm.h

SIZE_T RtlCompareDeviceMemory(
  [in] const VOID *Source1,
  [in] const VOID *Source2,
  [in] SIZE_T     Length
);
View the official Windows Driver Kit DDI reference

NtDoc

No description available.

Windows Driver Kit DDI reference (nf-wdm-rtlcomparedevicememory)

Description

The RtlCompareDeviceMemory function compares two blocks of device memory and returns the number of bytes that match until the first difference. This function is safe for use on device memory because it uses byte-level accesses that are guaranteed to be performed as specified.

Parameters

Source1 [in]

A pointer to the first block of memory to compare.

Source2 [in]

A pointer to the second block of memory to compare.

Length [in]

The number of bytes to compare.

Return value

RtlCompareDeviceMemory returns the number of bytes in the two blocks that match. If all bytes match up to the specified Length value, the Length value is returned.

Remarks

This function works on all versions of Windows, not just the latest. You need to consume the latest WDK to get the function declaration from the wdm.h header. You also need the library (volatileaccessk.lib) from the latest WDK. However, the resulting driver will run fine on older versions of Windows.

Example

UCHAR DeviceBuffer1[256];
UCHAR DeviceBuffer2[256];

// Read data from device memory into buffers
ReadFromDevice(DeviceBuffer1, sizeof(DeviceBuffer1));
ReadFromDevice(DeviceBuffer2, sizeof(DeviceBuffer2));

// Compare the device memory buffers
size_t matchingBytes = RtlCompareDeviceMemory(DeviceBuffer1, DeviceBuffer2, sizeof(DeviceBuffer1));

if (matchingBytes == sizeof(DeviceBuffer1)) {
    // All bytes matched
    DbgPrint("Device buffers are identical\n");
} else {
    // Difference found at byte offset 'matchingBytes'
    DbgPrint("Device buffers differ starting at byte %zu\n", matchingBytes);
}

See also

RtlCompareMemory

RtlFillVolatileMemory