Writefile导致崩溃,并导致访问冲突

Writefile causes crash, with access violation

本文关键字:访问冲突 崩溃 Writefile      更新时间:2023-10-16

所以基本上我希望将字节数组写入文件,但是程序崩溃了。

附录.exe中0x7766DEE1 (KernelBase.dll)的未处理异常:0xC0000005:访问冲突写入位置0x00000000.
BYTE *image ;
BYTE *bigMem;
#define REASONABLY_LARGE_BUFFER 16777216
file = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
fileSize = GetFileSize(file, NULL);
bigMem = (BYTE *)HeapCreate(NULL, REASONABLY_LARGE_BUFFER, 0);
image = (BYTE *)HeapAlloc(bigMem, HEAP_ZERO_MEMORY, fileSize);
if (bigMem == NULL || image == NULL){
    printf("Allocation failed");
    return EXIT_FAILURE;
}
printf("We are writing to the file %p, with data location %p, and filesize %dn", file, image, fileSize);
LPDWORD at = 0;
WriteFile(file, image, fileSize, at, NULL);

上面写着:我们写入文件00000038,数据位置为02451590,文件大小为161169

传递给WriteFile用于存储写入字节数(at)的参数只能为null,如果重叠结构的参数不是为null。我建议将at改为DWORD,并传递一个指针给它。

DWORD at;
WriteFile(file, image, fileSize, &at, NULL);