WriteFile() fails and returns strange error

This is a discussion on WriteFile() fails and returns strange error within the Windows Programming forums, part of the Platform Specific Boards category; I am using WriteFile(); with an OVERLAPPED structure but it returns ERROR_INVALID_PARAMETER, which I googled to find out it happens ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    36

    WriteFile() fails and returns strange error

    I am using WriteFile(); with an OVERLAPPED structure but it returns ERROR_INVALID_PARAMETER, which I googled to find out it happens when the OVERLAPPED structure hasn't been initialised to zero. I use ZeroMemory on it before I work with it and I made sure it was zero with a small function to verify it was zero before the write. The variables ARE set to zero, but the fuction still fails with that error code. The page below is what I learned this from, and the OVERLAPPED structure page on MSDN mentions the same thing. I'm not sure what I have to do...

    http://support.microsoft.com/kb/110148

    Code:
    if(!WriteFile(hFile, pOutput, GetFileSize(hFile, NULL), &ulReadWrite, &ovl))

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Could it be that the call
    GetFileSize(hFile, NULL)
    is mucking up the call which then tries to write to the same file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    36
    I don't understand what you mean.

    I took the GetFileSize out of the parameters and had it set a variable, which was then passed in WriteFile. It still returns the error, though.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Following on to Salem's suggestion, is it possible that the GetFileSize returns zero? If you ask to write zero bytes, I guess that could be considered a "strange" thing to do?

    Or perhaps the function is returning INVALID_FILE_SIZE (-1) for some reason?

    Edit: Missed the inbetween post - did you check the value fo GetFileSize? Why are you using GetFileSize of the same file you're writing to, by the way?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    36
    I use GetFileSize to work on stuff before the save and it works fine, but I tried a GetFileSize before the WriteFile and it returns a valid size.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you open the file with "FILE_FLAG_OVERLAPPED" set?

    Could you perhaps provide a tiny bit more context around the writefile?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    But where does GetFileSize() leave the file pointer, the point in the file where you're going to write the data?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As far as I can tell from http://msdn2.microsoft.com/en-us/library/aa364955.aspx, it doesn't change the fileposition at all.

    I still find it rather strange to use the current filesize as the input for a write operation, but that's probably just me being stupid.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 05:02 PM
  2. Syntax Error in resource
    By MrLucky in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2006, 09:05 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 10:20 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21