I have some code I've written that has worked sporadically, and I don't understand the reasons why it succeeds or fails. The code in question is fairly straightforward, a single call to CreateFile() from VB.NET;
Dim Handle As Integer = Win32.CreateFile(KeyboardPath, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, Nothing, _
OPEN_EXISTING, &H80, 0)
The problem is that when I call this, I get the win32 error ERROR_ACCESS_DENIED, but if I remove the GENERIC_READ request, the handle is granted just fine. This has happened before (on a different machine), and I'm completely stumped as to what sequence of events would result in the ability to write to a device and not read from it. The device in question is a Logitech G510 keyboard, and this code has worked fine before on a Vista 32-bit system (I'm now testing it on a Win7 x64 system and fixing all the compatibility issues I can find).
My question is, what can cause CreateFile to sporadically fall back to only allowing write access, what am I doing wrong that's causing it to do so, and how would I go about fixing the issue? I've included the full function below - note that with ONLY the GENERIC_READ flag passed, I also am not allocated a handle (same error)
As reference, the particular device path I've been using is \\?\hid#vid_046d&pid_c22d&mi_00#7&1a08be07&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
(This is for a Logitech G510 gaming keyboard)
Also, the code has performed similarly both with and without administrator privileges, if at all possible I want to be able to run without those privileges as I do not need them (and it doesn't need them on my vista system to work fine).
Private Function OpenInterface(ByVal KeyboardPath As String) As Integer
Dim Handle As Integer = Win32.CreateFile(KeyboardPath, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, Nothing, _
OPEN_EXISTING, &H80, 0)
Dim ee As Integer = GetLastError()
Return Handle
End Function