While trying to access an HID device using javahidapi at the same time as another application has revealed a permissions/sharing/access issue with the javahidapi library, specifically the native Windows code.

Background

Traktor on Windows 7 uses a service called NIHardwareService as a go-between the software and hardware controllers.  This service is installed with the Controller Editor, and without it, the Femulator (or any other hardware) won’t be detected by Traktor.  The Controller Editor can be downloaded from the NI website.

While developing a test app using the Java HIDAPI library to write output reports to the Femulator hardware I encountered some odd behavior.  During initial testing, things worked well, but as soon as the NIHardwareService is started, the test application could no longer read or write reports!   I can run NIHardwareService and still read reports in USBlyzer, so something funny was going on.   A resolution was required for the proposed Femulator solution to be useful on Windows 7 (works fine on Mac).

The Issue

javahidapi uses platform-specific native code, which it then wraps in a standard way using JNI, and obviously there are differences between the Windows and Mac code.

The Windows code uses a call to CreateFileA(), which requires some permissions be set for access and sharing.  When listing HID devices, the device is opened in  read/write share mode, but with no read or write access.  This allows the code to retrieve basic meta information about the file, and doesn’t prevent other applications from using it.

But, when opening an HID device, the code requests read/write access and read share mode.  According to the documentation, the share and access modes cannot conflict with any other open handles, so this could mean that NIHardwareService doesn’t allow sharing, or javahidapi must share with both read and write.  Since we can’t do anything about NIHardwareService, the fix must lie with javahidapi.

The Resolution

Here’s a patch which both summarizes the solution and provides a fix:

diff -r 6acc5c2c72bb windows/hid.c
--- a/windows/hid.c     Mon Dec 03 19:24:22 2012 +0200
+++ b/windows/hid.c     Wed Jan 30 16:58:22 2013 -0800
@@ -211,9 +211,7 @@
 {
        HANDLE handle;
        DWORD desired_access = (enumerate)? 0: (GENERIC_WRITE | GENERIC_READ);
-       DWORD share_mode = (enumerate)?
-                             FILE_SHARE_READ|FILE_SHARE_WRITE:
-                             FILE_SHARE_READ;
+       DWORD share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;

        handle = CreateFileA(path,
                desired_access,

To apply this patch and rebuild, read how to build javahidapi from source.  This patch has been submitted as an issue to the javahidapi project, and you can download an updated Jar file here, or here:

[wpdm_file id=11]

Tagged with →  
Share →

One Response to Notes On javahidapi Conflict with NIHardwareService

  1. […] Notes On javahidapi Conflict with NIHardwareService […]

Leave a Reply

Your email address will not be published.