本文介绍了Writefile 导致崩溃,访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我希望将字节数组写入文件,但是程序崩溃了.append.exe 中 0x7766DEE1 (KernelBase.dll) 处的未处理异常:0xC0000005:访问冲突写入位置 0x00000000.

So basically I wish to write a byte array to a file, however the program crashes.Unhandled exception at 0x7766DEE1 (KernelBase.dll) in append.exe: 0xC0000005: Access violation writing location 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 %d
", file, image, fileSize);
LPDWORD at = 0;
WriteFile(file, image, fileSize, at, NULL);

那个印刷品说:我们正在写入文件 00000038,数据位置为 02451590,文件大小为 161169

That print says:We are writing to the file 00000038, with data location 02451590, and filesize 161169

推荐答案

传递给 WriteFile 用于存储写入的字节数 (at) 可以onlynot 为 null,则为 null.我建议将 at 更改为 DWORD 并传递一个指向它的指针.

The argument passed to WriteFile used to store the number of bytes written (at) can only be null if the argument for the overlapped structure is not null. I suggest changing at to be a DWORD and pass a pointer to it.

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

这篇关于Writefile 导致崩溃,访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 10:14