java - Why does `ReadFile()` return error 87? -


below java code.

handle h = kernel32.createfile("\\\\.\\d:",                                kernel32.generic_read                                 | kernel32.generic_write,                                 0, null, kernel32.open_existing,                                0, null);  intbyreference nbread = new intbyreference();     bytebuffer b = bytebuffer.allocate(48);  boolean result = kernel32.readfile(h, b, b.capacity(), nbread, null);  system.out.println(kernel32.getlasterror()); 

it gives error 87 (invalid parameter)

what might problem ?

you aren't checking errors. must put right. documentation explains how. there's no substitute reading documentation.

check return values. compare h against invalid_handle_value. value indicates failure, , if so, , so, call getlasterror.

the documentation says opening volumes direct access:

the following requirements must met such call succeed:

  • the caller must have administrative privileges. more information, see running special privileges.
  • the dwcreationdisposition parameter must have open_existing flag.
  • when opening volume or floppy disk, dwsharemode parameter must have file_share_write flag.

you aren't meeting final requirement. , possibly not first.

as call readfile aren't checking return value. documentation says:

return value

if function succeeds, return value nonzero (true).

if function fails, or completing asynchronously, return value 0 (false). extended error information, call getlasterror function.

you need make sure perform sector aligned reads. not doing so.

your calls getlasterror wrong too. need fixed. see question: how make getlasterror reliably work jna?


Comments