Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add better windows error handling #388

Merged
merged 5 commits into from
May 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add better windows error handling
  • Loading branch information
Phyllostachys committed Feb 28, 2022
commit 2e3da37d4df8e894e373853aa9f70d639d23b6e6
53 changes: 38 additions & 15 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ static void free_hid_device(hid_device *dev)
CloseHandle(dev->ol.hEvent);
CloseHandle(dev->write_ol.hEvent);
CloseHandle(dev->device_handle);
LocalFree(dev->last_error_str);
free(dev->last_error_str);
dev->last_error_str = NULL;
free(dev->write_buf);
free(dev->feature_buf);
free(dev->read_buf);
Expand All @@ -225,9 +226,15 @@ static void free_hid_device(hid_device *dev)

static void register_error(hid_device *dev, const char *op)
{
WCHAR *ptr, *msg;
(void)op;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
/* Clear out error messages if NULL is passed into op */
if (dev && !op) {
free(dev->last_error_str);
dev->last_error_str = NULL;
return;
}

WCHAR *msg;
DWORD msg_size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
Expand All @@ -238,19 +245,31 @@ static void register_error(hid_device *dev, const char *op)

/* Get rid of the CR and LF that FormatMessage() sticks at the
end of the message. Thanks Microsoft! */
ptr = msg;
while (*ptr) {
if (*ptr == L'\r') {
*ptr = L'\0';
break;
}
ptr++;
msg[msg_size - 2] = L'\0';
msg_size -= 2;
Youw marked this conversation as resolved.
Show resolved Hide resolved

/* If an error string was passed to register_error then use it
in a bigger string. */
WCHAR *msg_out = NULL;
if (op) {
size_t length = 64 + msg_size + (strlen(op) * sizeof (WCHAR));
msg_out = (WCHAR *)calloc(length, sizeof (WCHAR));
if (msg_out)
swprintf(msg_out, length, L"hidapi err: %hs, windows err: %s", op, msg);
} else {
size_t length = wcslen(msg) + 1;
msg_out = (WCHAR *)calloc(length, sizeof (WCHAR));
if (msg_out)
wcsncpy(msg_out, msg, length);
}
LocalFree(msg);

/* Store the message off in the Device entry so that
the hid_error() function can pick it up. */
LocalFree(dev->last_error_str);
dev->last_error_str = msg;
if (dev) {
/* Store the message off in the Device entry so that
the hid_error() function can pick it up. */
free(dev->last_error_str);
dev->last_error_str = msg_out;
}
}

static HANDLE open_device(const wchar_t *path, BOOL open_rw)
Expand Down Expand Up @@ -656,6 +675,10 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsi
hid_device *handle = NULL;

devs = hid_enumerate(vendor_id, product_id);
if (!devs) {
return NULL;
}

cur_dev = devs;
while (cur_dev) {
if (cur_dev->vendor_id == vendor_id &&
Expand Down