diff --git a/windows/hid.c b/windows/hid.c index 5415bc0..7b10d13 100755 --- a/windows/hid.c +++ b/windows/hid.c @@ -36,6 +36,10 @@ typedef LONG NTSTATUS; #define _wcsdup wcsdup #endif +/* The maximum number of characters that can be passed into the + HidD_Get*String() functions without it failing.*/ +#define MAX_STRING_WCHARS 0xFFF + /*#define HIDAPI_USE_DDK*/ #ifdef __cplusplus @@ -62,6 +66,9 @@ extern "C" { #include "hidapi.h" +#undef MIN +#define MIN(x,y) ((x) < (y)? (x): (y)) + #ifdef _MSC_VER /* Thanks Microsoft, but I know how to use strncpy(). */ #pragma warning(disable:4996) @@ -811,7 +818,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev { BOOL res; - res = HidD_GetManufacturerString(dev->device_handle, string, sizeof(wchar_t) * maxlen); + res = HidD_GetManufacturerString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS)); if (!res) { register_error(dev, "HidD_GetManufacturerString"); return -1; @@ -824,7 +831,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wch { BOOL res; - res = HidD_GetProductString(dev->device_handle, string, sizeof(wchar_t) * maxlen); + res = HidD_GetProductString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS)); if (!res) { register_error(dev, "HidD_GetProductString"); return -1; @@ -837,7 +844,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *de { BOOL res; - res = HidD_GetSerialNumberString(dev->device_handle, string, sizeof(wchar_t) * maxlen); + res = HidD_GetSerialNumberString(dev->device_handle, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS)); if (!res) { register_error(dev, "HidD_GetSerialNumberString"); return -1; @@ -850,7 +857,7 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int { BOOL res; - res = HidD_GetIndexedString(dev->device_handle, string_index, string, sizeof(wchar_t) * maxlen); + res = HidD_GetIndexedString(dev->device_handle, string_index, string, sizeof(wchar_t) * MIN(maxlen, MAX_STRING_WCHARS)); if (!res) { register_error(dev, "HidD_GetIndexedString"); return -1;