-
Notifications
You must be signed in to change notification settings - Fork 2k
libusbK allows claiming the interface from different applications at the same time #807
Description
Some background: I have an application that connects to a USB device. Potentially, there could be multiple instances running, each connected to a unique device. The devices have a serialnumber to differentiate between them in the UI. The program should work like this:
- Create a list of connected devices with libusb_get_device_list
- Open (and immediately close afterwards) every device with the correct VID/PID to get the serialnumber
- Show available serialnumbers in UI
- If the user selects a device, open it again and claim the interfaces (this should fail if the interfaces are already claimed by another instance of the application)
Step 2 is not possible with the WinUSB driver (if the device is already connected to another instance of the program, opening it to get the serialnumber is not possible), so I switched to libusbK. I now am able to retrieve all serialnumbers, however claiming the interface always succeeds, even when it was already claimed by another instance. This leads to serious communication problems because two instances are connected to the same device.
As far as I know, libusb_claim_interface should fail if something else already claimed the interface. This is also what happens if I run my application on Linux.
Here is a minimal example to reproduce the problem:
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <windows.h>
int main()
{
libusb_context *ctx;
int res = libusb_init(&ctx);
if (res != 0)
{
printf("Error initialising libusb.\n");
return 1;
}
libusb_device_handle* handle = libusb_open_device_with_vid_pid(ctx, 0x0483, 0x4121);
if (!handle)
{
printf("Unable to open device.\n");
return 1;
}
int ret = libusb_claim_interface(handle, 1);
if (ret < 0) {
printf("Failed to claim interface: %s\n", libusb_strerror((libusb_error) ret));
} else {
printf("Claimed interface\n");
}
Sleep(10000);
libusb_release_interface(handle, 1);
libusb_close(handle);
libusb_exit(ctx);
}When starting the first instance of this example it prints "Claimed interface" as expected. However, when I start a second instance while the first is still running, it also prints "Claimed interface". I would have expected "Failed to claim interface: LIBUSB_ERROR_BUSY"
I am using these versions:
- libusb v1.0.23
- libusbK v3.0.7.0 (installed with Zadig)
- Windows 10