我的memcpy函数有问题。我尝试从char数组中复制部分数据,但未成功。

// Types
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

// My variables
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;

// My Func
void BmpFileHeader::setFileHeader(char* header)
{
    // header -> 42 4D BB E6 00 00 00 00 00 00 36 00 00 00
    char* pointerOfHeader = header;
    std::memcpy(&bfType, header, sizeof(WORD));
    pointerOfHeader+=2;
    std::memcpy(&bfSize, pointerOfHeader, sizeof(DWORD));
    pointerOfHeader+=4;
    std::memcpy(&bfReserved1, pointerOfHeader, sizeof(WORD));
    pointerOfHeader+=2;
    std::memcpy(&bfReserved2, pointerOfHeader, sizeof(WORD));
    pointerOfHeader+=2;
    // This point, pointerOfHeader is 36(catched with debugging)
    std::memcpy(&bfOffBits, pointerOfHeader, sizeof(DWORD));
 }

除了bfOffBits(示例7950260717192478774)之外,我所有的变量都为真值。我尝试更改大小,但没有成功。我想念的是什么?

最佳答案

似乎您的编译器将unsigned long视为64位数量,但是Win32 API将DWORD定义为32位无符号值。尝试:

typedef uint32_t DWORD;

07-24 22:26