CreateFile return ERROR_ACCESS_DENIED, why ?

  • Thread starter Thread starter Dilly0
  • Start date Start date
D

Dilly0

Guest
This code:

void OpenHidDevice(string DevicePath, ref HID_DEVICE[] HidDevice, int iHIDD)
{
/*++
RoutineDescription:
Given the HardwareDeviceInfo, representing a handle to the plug and
play information, and deviceInfoData, representing a specific hid device,
open that device and fill in all the relivant information in the given
HID_DEVICE structure.
--*/

HidDevice[iHIDD].DevicePath = DevicePath;

//
// The hid.dll api's do not pass the overlapped structure into deviceiocontrol
// so to use them we must have a non overlapped device. If the request is for
// an overlapped device we will close the device below and get a handle to an
// overlapped device
//
CloseHandle(HidDevice[iHIDD].HidDevice);
HidDevice[iHIDD].HidDevice = CreateFile(HidDevice[iHIDD].DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, IntPtr.Zero);
HidDevice[iHIDD].Caps = new HIDP_CAPS();
HidDevice[iHIDD].Attributes = new HIDD_ATTRIBUTES();

MessageBox.Show(GetLastError().ToString());

//
// If the device was not opened as overlapped, then fill in the rest of the
// HidDevice structure. However, if opened as overlapped, this handle cannot
// be used in the calls to the HidD_ exported functions since each of these
// functions does synchronous I/O.
//
HidD_FreePreparsedData(ref HidDevice[iHIDD].Ppd);
HidDevice[iHIDD].Ppd = IntPtr.Zero;
HidD_GetPreparsedData(HidDevice[iHIDD].HidDevice, ref HidDevice[iHIDD].Ppd);
HidD_GetAttributes(HidDevice[iHIDD].HidDevice, ref HidDevice[iHIDD].Attributes);
HidP_GetCaps(HidDevice[iHIDD].Ppd, ref HidDevice[iHIDD].Caps);

//
// At this point the client has a choice. It may chose to look at the
// Usage and Page of the top level collection found in the HIDP_CAPS
// structure. In this way --------*it could just use the usages it knows about.
// If either HidP_GetUsages or HidP_GetUsageValue return an error then
// that particular usage does not exist in the report.
// This is most likely the preferred method as the application can only
// use usages of which it already knows.
// In this case the app need not even call GetButtonCaps or GetValueCaps.
//
// In this example, however, we will call FillDeviceInfo to look for all
// of the usages in the device.
//
//FillDeviceInfo(ref HidDevice);
}

Continue reading...
 
Back
Top