Recording file causes access violation

So basically I want to write an array of bytes to a file, however the program will crash. Unhandled exception in 0x7766DEE1 (KernelBase.dll) in append.exe: 0xC0000005: location of access violation record 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); 

This print says: We are writing a file 00000038 with a data location of 02451590 and a file size of 161169

+7
source share
1 answer

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

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

All Articles