0

I'm using DeviceIoControl with FSCTL_LOCK_VOLUME to lock a USB pen drive prior to direct disk read/write. The program works - sometimes. I am having an issue with the lock call itself. When I step the command in Visual Studio 2008 the result is correct and the lock succeeds - every time!, when running the code (debug or not) the call fails sporadically with invalid handle. The only noticable difference is when stepping there is a half second pause - which I am happy with, but when running/debugging the call fails immediately.

Please can you give me a hint as to where this is falling down.

I think this is one for the true techies!

Matt
  • 31
  • 1
  • Even though CreateFile returned you a valid handle, that handle may not have the correct specifications for your FSCTL_LOCK_VOLUME operation to work. For example `FILE_SHARE_READ` and `FILE_SHARE_WRITE` must be specified. My experiences with DeviceIoControl is that the error messages are accurate but basic in detail. An invalid handle means that your handle is not valid for that operation. –  Jan 24 '13 at 17:54

1 Answers1

0

Sounds like a timing bug. Are there other threads with access to the handle? If so, one of them may be closing it before you call DeviceIoControl.

Marc Sherman
  • 2,303
  • 14
  • 22