Skip to content

Commit 1312cc3

Browse files
authored
README: prevent segmentation fault and mixture of printf/wprintf (signal11#470)
And a few other improvements.
1 parent f2e2b5b commit 1312cc3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ device spec. Writing data (`hid_write`) at random to your HID devices can break
9797

9898
```c
9999
#include <stdio.h> // printf
100-
#include <wchar.h> // wprintf
100+
#include <wchar.h> // wchar_t
101101

102102
#include <hidapi.h>
103103

@@ -117,22 +117,28 @@ int main(int argc, char* argv[])
117117
// Open the device using the VID, PID,
118118
// and optionally the Serial number.
119119
handle = hid_open(0x4d8, 0x3f, NULL);
120+
if (!handle) {
121+
printf("Unable to open device\n");
122+
123+
hid_exit()
124+
return 1;
125+
}
120126

121127
// Read the Manufacturer String
122128
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
123-
wprintf(L"Manufacturer String: %s\n", wstr);
129+
printf("Manufacturer String: %ls\n", wstr);
124130

125131
// Read the Product String
126132
res = hid_get_product_string(handle, wstr, MAX_STR);
127-
wprintf(L"Product String: %s\n", wstr);
133+
printf("Product String: %ls\n", wstr);
128134

129135
// Read the Serial Number String
130136
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
131-
wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);
137+
printf("Serial Number String: (%d) %ls\n", wstr[0], wstr);
132138

133139
// Read Indexed String 1
134140
res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
135-
wprintf(L"Indexed String 1: %s\n", wstr);
141+
printf("Indexed String 1: %ls\n", wstr);
136142

137143
// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
138144
buf[0] = 0x0;

0 commit comments

Comments
 (0)