Skip to content

Commit

Permalink
Add Windows-specific API to get device container ID
Browse files Browse the repository at this point in the history
  • Loading branch information
triplef committed Feb 10, 2022
1 parent e08ddce commit e7a9e9f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
38 changes: 38 additions & 0 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,44 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int
return 0;
}

int HID_API_EXPORT_CALL hid_winapi_get_container_id(hid_device *dev, GUID *container_id)
{
wchar_t *interface_path = NULL, *device_id = NULL;
CONFIGRET cr = CR_DEFAULT;
DEVINST dev_node;
DEVPROPTYPE property_type;
ULONG len;

if (!container_id)
return -1;

interface_path = hid_internal_UTF8toUTF16(dev->device_info->path);
if (!interface_path)
goto end;

/* Get the device id from interface path */
device_id = hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
if (!device_id)
goto end;

/* Open devnode from device id */
cr = CM_Locate_DevNodeW(&dev_node, (DEVINSTID_W)device_id, CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS)
goto end;

/* Get the container id from devnode */
len = sizeof(*container_id);
cr = CM_Get_DevNode_PropertyW(dev_node, &DEVPKEY_Device_ContainerId, &property_type, (PBYTE)container_id, &len, 0);
if (cr == CR_SUCCESS && property_type != DEVPROP_TYPE_GUID)
cr = CR_WRONG_TYPE;

end:
free(interface_path);
free(device_id);

return cr == CR_SUCCESS ? 0 : -1;
}


HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
{
Expand Down
1 change: 1 addition & 0 deletions windows/hidapi_cfgmgr32.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static DEVPROPKEY DEVPKEY_NAME = { { 0xb725f130, 0x47ef, 0x101a, 0xa5, 0xf1, 0x0
static DEVPROPKEY DEVPKEY_Device_InstanceId = { { 0x78c34fc8, 0x104a, 0x4aca, 0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57 }, 256 }; // DEVPROP_TYPE_STRING
static DEVPROPKEY DEVPKEY_Device_HardwareIds = { { 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0}, 3 }; // DEVPROP_TYPE_STRING_LIST
static DEVPROPKEY DEVPKEY_Device_CompatibleIds = { { 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0}, 4 }; // DEVPROP_TYPE_STRING_LIST
static DEVPROPKEY DEVPKEY_Device_ContainerId = { { 0x8c7ed206, 0x3f8a, 0x4827, 0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c}, 2 }; // DEVPROP_TYPE_GUID

// from propkey.h
static PROPERTYKEY PKEY_DeviceInterface_Bluetooth_DeviceAddress = { { 0x2bd67d8b, 0x8beb, 0x48d5, 0x87, 0xe0, 0x6c, 0xda, 0x34, 0x28, 0x04, 0x0a }, 1 }; // DEVPROP_TYPE_STRING
Expand Down
54 changes: 54 additions & 0 deletions windows/hidapi_winapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************
HIDAPI - Multi-Platform library for
communication with HID devices.
libusb/hidapi Team
Copyright 2022, All Rights Reserved.
At the discretion of the user of this library,
this software may be licensed under the terms of the
GNU General Public License v3, a BSD-Style license, or the
original HIDAPI license as outlined in the LICENSE.txt,
LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
files located at the root of the source distribution.
These files may also be found in the public source
code repository located at:
https://github.com/libusb/hidapi .
********************************************************/

/** @file
* @defgroup API hidapi API
*/

#ifndef HIDAPI_WINAPI_H__
#define HIDAPI_WINAPI_H__

#include <guiddef.h>

#include "hidapi.h"

#ifdef __cplusplus
extern "C" {
#endif

/** @brief Get the container ID for a HID device.
This function returns the DEVPKEY_Device_ContainerId property of
the given device. This can be used to correlate different
interfaces/ports on the same hardware device.
@ingroup API
@param dev A device handle returned from hid_open().
@param guid The device's container ID on return.
@returns
This function returns 0 on success and -1 on error.
*/
int HID_API_EXPORT_CALL hid_winapi_get_container_id(hid_device *dev, GUID *container_id);

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit e7a9e9f

Please sign in to comment.