从 'BYTE *' 到 'ULONG' 的指针截断

Pointer truncation from 'BYTE *' to 'ULONG'

本文关键字:指针 BYTE ULONG      更新时间:2023-10-16

我试图为Windows编写一个简单的过滤器驱动程序,当我想构建项目时,Visual Studio会给我以下警告:

警告C4311'类型Cast':从"字节 *"到'ulong''

的指针截断

和错误: C2220警告被视为错误 - 无"对象"文件生成

这是我的代码:

BOOLEAN GetAllBufferFromChunkedFormat(STREAM_EDIT_PARAMETERS* params, BYTE** 
dataBuffer)
{
if (!CheckPointer(params) || !CheckPointer(dataBuffer))
{
    return FALSE;
}
BYTE* iterator = params->dataStart + params->contentStart;
params->currentContentLength = 0;
UINT currentChunkLength = 0;
BOOLEAN isAllData = FALSE;
while ((ULONG)iterator - (ULONG)params->dataStart < params->streamEditor->dataLength) //calculate total length
{
    currentChunkLength = strtol(iterator, &iterator, 16);
    iterator += s_chunksSeparatorLength + currentChunkLength + s_chunksSeparatorLength;
    ..
    ..
    ..
}

警告在线显示:

while ((ULONG)iterator - (ULONG)params->dataStart < params->streamEditor->dataLength)

有什么问题?为什么?

a long 类型是4个字节宽,而诸如 byte*之类的指针类型具有体系结构的宽度,即。x86平台上的32位或4个字节,x64体系结构上的64位(8byte)。因此,这将在X86平台上予以罚款,但在64位平台上发出警告(被视为错误)。

对于X64架构,正确的转换将是数值类型" "或 uint64 和类似类型,或者如果您真的不在乎,您可以 static_cast(x)值。