2009年10月7日 星期三

DDk 記憶體複製、填充、比較

DDK提供以下的記憶體操作的function,也可以使用memcpy,memset等function
但為了保證程式碼可攜,要儘量使用標準的function

//記憶體比較
RtlEqualMemory(Destination,Source,Length);

//記憶體複製,可重疊
RtlMoveMemory(Destination,Source,Length);

//記憶體複製,不可重疊
RtlCopyMemory(Destination,Source,Length);

//記憶體填充
RtlFillMemory(Destination,Length,Fill);

//將記憶體清成0
RtlZeroMemory(Destination,Length);


範例
VOID RtlTest()
{
PUCHAR pBuffer = (PUCHAR)ExAllocatePool(PagedPool,BUFFER_SIZE);
//用零填充記憶體
RtlZeroMemory(pBuffer,BUFFER_SIZE);

PUCHAR pBuffer2 = (PUCHAR)ExAllocatePool(PagedPool,BUFFER_SIZE);
//用固定位元組填充記憶體
RtlFillMemory(pBuffer2,BUFFER_SIZE,0xAA);

//記憶體拷貝
RtlCopyMemory(pBuffer,pBuffer2,BUFFER_SIZE);

//判斷記憶體是否一致
ULONG ulRet = RtlCompareMemory(pBuffer,pBuffer2,BUFFER_SIZE);
if (ulRet==BUFFER_SIZE)
{
KdPrint(("The two blocks are same.\n"));
}
}

沒有留言 :