Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:18:06+00:00 2026-06-12T00:18:06+00:00

Two problems with the below code. To begin, I have been scouring this and

  • 0

Two problems with the below code. To begin, I have been scouring this and various other forums for answers to my 1784 error code and everything I’ve tried has failed. Two of the threads I’ve checked on stackoverflow are WriteFile returning error 1784 and BlockWrite I/O Error 1784. I’ve checked some others on this forum but I’m not remembering exactly what the are right now.

I’m trying to save an array of structs to an empty binary file. The first problem is that I get an access violation if my size variable (nNumberOfBytesToWrite parameter) is anything less about 99000 bytes. That number jumps around. For awhile when I was testing it would have the access violation if it was 99,999 bytes but not 100,000 bytes. Of course, what I eventually want to do is set the size to the size of the entire array. The original code to handle that is now commented out so I can test with various sizes.

The second thing that happens (if I don’t get an access violation) is I get error code 1784 and WriteFile fails every time. As other threads on this topic have stated, this is defined on MSDN as ERROR_INVALID_USER_BUFFER and the description is “The supplied user buffer is not valid for the requested operation.” I’ve looked at MSDN’s own example for opening files like this (http://msdn.microsoft.com/en-us/library/windows/desktop/bb540534%28v=vs.85%29.aspx) and have tried some variations based on their code, but nothing seems to work.

This problem is probably massively noob and I’m sure I’m overlooking something ridiculously simple, but if anyone has suggestions they’d be greatly appreciated.

case IDM_SAVE1:
{
    HANDLE hFile = CreateFile("MineSave.mss", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    int test_buffer[] = {1,2,3,4,5,6,7,8,9,10};

    if(hFile != INVALID_HANDLE_VALUE)
    {
        BOOL bSuccess;
        DWORD size = 100000; //DWORD size = (((sizeof(tile)) * tiles_total));
        LPDWORD bytes_written = 0;
        bSuccess = WriteFile(hFile, test_buffer, size, bytes_written, NULL);
        if(bSuccess)
        {
            MessageBox(hwnd, "File saved successfully.", "Great Job!", MB_OK);
        }
        else
        {
            DWORD error = GetLastError();
            MessageBox(hwnd, "Could not write to file.", "Error", MB_OK);
        }

        CloseHandle(hFile);
    }
    else
    {
        MessageBox(hwnd, "Could not create file.", "Error", MB_OK);
    }
}
break;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:18:07+00:00Added an answer on June 12, 2026 at 12:18 am

    Your buffer is the size of 10 ints, which is 40 bytes on Windows. You are trying to write 100,000 bytes from that buffer. That is undefined behaviour, a buffer overrun. Hence the access violation.

    You must not pass a value greater than sizeof(test_buffer), i.e. 40, to the nNumberOfBytesToWrite parameter of WriteFile.

    You’ll need to write this file in a loop, writing 40 bytes at a time, until you have written as much as you need. Perhaps something like this:

    BOOL bSuccess = TRUE;
    DWORD bytesRemaining = 100000;
    while (bSuccess && bytesRemaining>0)
    {
        DWORD bytesToWrite = std::min(sizeof(test_buffer), bytesRemaining);
        DWORD bytesWritten;
        bSuccess = WriteFile(hFile, test_buffer, bytesToWrite, &bytesWritten, NULL);
        bytesRemaining -= bytesToWrite;
    }
    if (!bSuccess)
    {
        //handle error;
    }
    

    Writing 40 bytes at a time is pretty slow. You’ll find it more efficient to write a few KB with each call to WriteFile.

    Note that you aren’t allowed to pass NULL to the lpNumberOfBytesWritten parameter if you also pass NULL to lpOverlapped, as you do here. From the documentation:

    lpNumberOfBytesWritten [out, optional]

    ……

    This parameter can be NULL only when the lpOverlapped parameter is not NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having two problems with my program. The code below generates two panels when
The code below has (at least) two problems: the Copy button doesn't update the
I'm using devexpress and i have two problems. My insert stored procedure work fine.
I have some localization problems in my webpage. There are basically two problems (that
I have my program that can draw rectangles. I have two problems I can't
I have two cmake-related problems: first, I can't make it to find the includes
The sampled code below is for the GUI I am currently making. I have
I just started using gApps scripts and I have run into two problems with
I have below probably a bunch of problems but let's simplify them with easier
This first piece has been solved by Eric's comments below but has led onto

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.