2

I am creating a program to populate a disk with a dummy file system.

Currently, I am writing files of variable sizes using WriteFile.

        WriteFile(hFile, FileData, i * 1024, &dwWrote, NULL);
        err = GetLastError();

err returns #1784 which translates to

The supplied user buffer is not valid for the requested operation. ERROR_INVALID_USER_BUFFER

So for the first 24 files, the write operation works. For file #25 on, the write operation fails. The files are still created but the WriteFile function does not populate the files.

Any ideas on how to get past ERROR_INVALID_USER_BUFFER?

Every reference I can find to the error is limited to crashing programs and I cannot figure out how it relates to the issue I am experiencing.

EDIT:

FileData = (char *) malloc(sizeof(char) * (size_t)k * 1024);
memset(FileData, 245, sizeof(char) * (size_t)k * 1024);

FileData is set and allocated to the size of the maximum anticipate buffer. i is the loop variable that iterates until it increments to the Maximum Size (k).

jonsca
  • 10,218
  • 26
  • 54
  • 62
jscott
  • 397
  • 2
  • 5
  • 18
  • How are we to know what type those parameters have, and what you've done with them previously? – Lightness Races in Orbit Jun 30 '11 at 15:44
  • Also, note that crashing is usually a result of UB (which, itself, is usually trying to access invalid memory). However, crashing is not _always_ the result of UB. It's unpredictable as to what might occur. So if you expect a crash that's not occurring, don't read too much into it. – Lightness Races in Orbit Jun 30 '11 at 15:45
  • The WriteFile function may fail with ERROR_INVALID_USER_BUFFER or ERROR_NOT_ENOUGH_MEMORY whenever there are too many outstanding asynchronous I/O requests. – n. m. could be an AI Jun 30 '11 at 16:11
  • This is not async I/O, `LPOVERLAPPED` is null. Far more likely a simple buffer misuse imo. That said, without code it's hard to be helpful. – Steve Townsend Jun 30 '11 at 16:21

2 Answers2

4

My guess is that FileData is not large enough for you to write i * 1024 bytes from it. Is i the loop control variable for your list of files? If so, you need the write buffer FileData to grow 1K at a time as you loop through your files.

This is an unusual construct. Are you sure the logic is correct here? Post more code (specifically, all usage of FileData and i) for better accuracy in the answers.

Note that you should not always be checking GetLastError here - you need to check WriteFile's return code before you rely on that being meaningful. Otherwise you could be picking up an error from some unrelated part of your code - whatever failed last.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
2

I got a Error = 1784 and it was because I opened the file without specifying the size of records and then did block reads on the file.

Reset( FileHandle );

Should be

Reset( FileHandle, 1 );
animuson
  • 53,861
  • 28
  • 137
  • 147
Jeff
  • 21
  • 1