add better err msg

This commit is contained in:
Mylloon 2024-04-03 15:27:38 +02:00
parent 83bfcdbd54
commit f3615c5adf
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 22 additions and 12 deletions

View file

@ -17,11 +17,12 @@
*************************************************************************** */
int get_battery_level(const char *device_path, bool *is_charging) {
HANDLE hDevice = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
fprintf_s(stderr, "Failed to open device.\n");
HANDLE hDevice;
if ((hDevice = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL)) ==
INVALID_HANDLE_VALUE) {
fprintf_s(stderr, "[%lu] Failed to open device.\n", GetLastError());
return -1;
}
@ -33,7 +34,8 @@ int get_battery_level(const char *device_path, bool *is_charging) {
// Send the report
if (!HidD_SetFeature(hDevice, report, RAZER_REPORT_LEN)) {
fprintf_s(stderr, "Failed to send report to device.\n");
fprintf_s(stderr, "[%lu] Failed to send report to device.\n",
GetLastError());
CloseHandle(hDevice);
return -1;
@ -41,7 +43,8 @@ int get_battery_level(const char *device_path, bool *is_charging) {
// Read the response
if (!HidD_GetFeature(hDevice, report, RAZER_REPORT_LEN)) {
fprintf_s(stderr, "Failed to read report from device.\n");
fprintf_s(stderr, "[%lu] Failed to read report from device.\n",
GetLastError());
CloseHandle(hDevice);
return -1;

View file

@ -6,7 +6,7 @@
#include <setupapi.h>
#include <stdio.h>
#define VID 0x1532
#define VID_RAZER 0x1532
void find_device_path(int pid, char *device_path) {
GUID hidGuid;
@ -15,7 +15,9 @@ void find_device_path(int pid, char *device_path) {
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
fprintf_s(stderr, "Failed to get device information.\n");
fprintf_s(stderr, "[%lu] Failed to get any device information\n",
GetLastError());
return;
}
@ -24,7 +26,7 @@ void find_device_path(int pid, char *device_path) {
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &hidGuid, i,
&deviceInterfaceData);
i++) {
++i) {
DWORD requiredSize;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0,
&requiredSize, NULL);
@ -35,6 +37,7 @@ void find_device_path(int pid, char *device_path) {
fprintf_s(
stderr,
"Failed to allocated memory for PSP_DEVICE_INTERFACE_DETAIL_DATA.\n");
return;
}
detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
@ -44,21 +47,25 @@ void find_device_path(int pid, char *device_path) {
HANDLE hDevice = CreateFile(
detailData->DevicePath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice != INVALID_HANDLE_VALUE) {
// Get device attributes
HIDD_ATTRIBUTES attributes;
attributes.Size = sizeof(HIDD_ATTRIBUTES);
if (HidD_GetAttributes(hDevice, &attributes)) {
// Check VID and PID
if (attributes.VendorID == VID && attributes.ProductID == pid) {
if (attributes.VendorID == VID_RAZER && attributes.ProductID == pid) {
strcpy(device_path, detailData->DevicePath);
CloseHandle(hDevice);
break;
}
}
CloseHandle(hDevice);
}
}
free(detailData);
}

View file

@ -6,7 +6,7 @@
int main(void) {
char device_path[2048];
int min_len = 10;
unsigned long long min_len = 10;
// Try wired
find_device_path(USB_DEVICE_ID_RAZER_VIPER_ULTIMATE_WIRED, device_path);