Writefile导致崩溃,具有访问冲突 [英] Writefile causes crash, with access violation

查看:500
本文介绍了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\n", 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 )只能为null,如果重叠结构的参数为 not null。我建议将改为为 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导致崩溃,具有访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆