CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2007
    Posts
    20

    Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    Hi,

    I must confess I don't do this in c++, but since there is no forum for vb.net, I'll try to make it simple (If you disagree on this thread being here please feel free to delete it).

    I call function like this:

    DeviceHandle = CreateFile _
    (DeviceName, _
    GENERIC_READ Or GENERIC_WRITE, _
    FILE_SHARE_READ Or FILE_SHARE_WRITE, _
    Security, _
    OPEN_EXISTING, _
    0, _
    0)

    And error I get is 5 ERROR_ACCESS_DENIED. I tried to open file and write to it, and I succeeded but with device it just won't work.

    DeviceName: "\\.\HID#Vid_XXXX&Pid_0001#6&2b3c76e7&0&0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"

    Security: bInheritHandle -1 Integer
    lpSecurityDescriptor 0 Integer
    nLength 12 Integer


    Even more troubling is that when I try to open device only with GENERIC_WRITE I get the handle (no error).

    Opening only with GENERIC_READ also returns error 5.

    Anyone had similar experience, or has solution to my problem? Please do post, any help is appreciated.

    PS. Maybe Windows opened device for reading and locked it so I can't access it?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    What type of device are you trying to access?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2007
    Posts
    20

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    A DT-Box which purpose is to convert SY-4 to USB (eg. I have a device that returns some values and this box is seen by Windows as Human Interface Device).

    Box has one button which sends data taken from device to keyboard.

    Device [Data] ----SY-4---->DT-Box-----USB----->Computer

    If you need more information please let me know. Hoping for a solution.
    Last edited by Vila; April 9th, 2010 at 02:18 PM. Reason: bold

  4. #4
    Join Date
    Jul 2007
    Posts
    20

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    I managed to put it down to this:

    I opened device for writing (maybe not good?) like this:

    DeviceHandle = CreateFile _
    (DeviceName, _
    GENERIC_WRITE, _
    FILE_SHARE_READ Or FILE_SHARE_WRITE, _
    Security, _
    OPEN_EXISTING, _
    0, _
    0)

    And all I need to do is send it some information (2 bytes) like this:

    Success = WriteFile _
    (DeviceHandle, _
    OutFeatureReportBuffer(0), _
    2, _
    NumberOfBytesWritten, _
    0)

    Success = False (and NumberOfBytesWritten = 0) so I try:

    Marshal.GetLastWin32Error()

    and get error 1 = ERROR_INVALID_FUNCTION (Incorrect function.)

    Definition of WriteFile:

    <DllImport("kernel32.dll", setlasterror:=True)> Private Shared Function WriteFile _
    (ByVal hFile As Integer, _
    ByRef lpBuffer As Byte, _
    ByVal nNumberOfBytesToWrite As Integer, _
    ByRef lpNumberOfBytesWritten As Integer, _
    ByVal lpOverlapped As Integer) _
    As Boolean
    End Function

    Any ideas this is giving me a headache.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    Well, if you only need to write (to send 2 bytes) to this device then why did you try to pass GENERIC_READ flag?

    Besides, try to use DeviceIoControl rather than WriteFile.
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    >> OutFeatureReportBuffer(0), _
    Shouldn't a feature report be set via HidD_SetFeature?

    >> try to use DeviceIoControl
    Or IOCTL_HID_SET_FEATURE

    gg

  7. #7
    Join Date
    Jul 2007
    Posts
    20

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    Quote Originally Posted by VictorN View Post
    Well, if you only need to write (to send 2 bytes) to this device then why did you try to pass GENERIC_READ flag?

    Besides, try to use DeviceIoControl rather than WriteFile.
    DeviceIoControl also returns error code 5 ACCESS_DENIED.

    Quote Originally Posted by Codeplug View Post
    >> OutFeatureReportBuffer(0), _
    Shouldn't a feature report be set via HidD_SetFeature?

    >> try to use DeviceIoControl
    Or IOCTL_HID_SET_FEATURE

    gg

    Yeah it is, i was trying that function and then tried WriteFile and decided I need WriteFile and forgot to change the name of buffer, but that doesn't make any difference.

    From the beginning, I try to simulate button press on HID. How to do that? WriteFile and DeviceIoControl are good way? If so I can't use them because of error access denied. Any other function or easier way to do that? Can it be done with DirectX? Thanks for help anyway.

  8. #8
    Join Date
    Jul 2007
    Posts
    20

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    I understand this is jumping from subject to subject (WinAPI -> DirectX), but I am trying to solve above mentioned problem:

    I try to simulate button press on HID. How to do that?
    Any solution is acceptable

    So I tried DirectX function to detect devices:

    Microsoft.DirectX.DirectInput.Manager.GetDevices(DeviceClass.All, EnumDevicesFlags.AllDevices)

    I only got my keyboard and mouse

    So, i tried using guid (since when I discover my device and get it's "name" it contains guid):

    deviceGuid = "{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"
    Dim devguid As System.Guid = New System.Guid(deviceGuid)
    Dim gamepadDevice As Device = New Device(devguid)

    Now when second line of code is executed i get:

    - devguid {System.Guid} System.Guid
    Empty Nothing System.Guid

    This means devguid isn't initialized properly??

    And, as I assumed, third line returns exception:

    Microsoft.DirectX.DirectInput.DeviceNotRegisteredException (Message: {"Error in the application."})

    Any ideas on Simulating a key press on non-standard (eg. not joystick) HID?

  9. #9
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    >> Any ideas on Simulating a key press on non-standard (eg. not joystick) HID?
    So you want the OS (and some application) to think a HID report is coming from the actual device? I don't think you can do that by simply writing to the device [driver] (unless the device [driver] explicitly echos back what's written to it).

    You could create your own HID driver that sends up your own HID reports, but if the software that is reading those reports is looking for a specific device (product/vendor ID) then that may not work either. http://www.microsoft.com/whdc/devtools/DSF.mspx

    gg

  10. #10
    Join Date
    Jul 2007
    Posts
    20

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    Quote Originally Posted by Codeplug View Post
    >> Any ideas on Simulating a key press on non-standard (eg. not joystick) HID?
    So you want the OS (and some application) to think a HID report is coming from the actual device? I don't think you can do that by simply writing to the device [driver] (unless the device [driver] explicitly echos back what's written to it).

    You could create your own HID driver that sends up your own HID reports, but if the software that is reading those reports is looking for a specific device (product/vendor ID) then that may not work either. http://www.microsoft.com/whdc/devtools/DSF.mspx

    gg
    Yes it has specific vid and pid (and i know them). But, device puts data on "keyboard" eg. it does keypresses of data, so I detect them with GetAsyncKeyState (and that works and it isn't problem), so only thing i need now is to press that button

  11. #11
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    Well, you can use SenInput() to simulate keystrokes - http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg

  12. #12
    Join Date
    Jul 2007
    Posts
    20

    Re: Opening device with CreateFile fails with error code 5 ERROR_ACCESS_DENIED

    Quote Originally Posted by Codeplug View Post
    Well, you can use SenInput() to simulate keystrokes - http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg
    But, it is non-standard device eg. not recognized as keyboard or mouse. Confusing, but true.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured