Skip to content

Commit 7751736

Browse files
authored
Update nf-winuser-getrawinputdevicelist.md (#1019)
* Update nf-winuser-getrawinputdevicelist.md Update example code. Call `GetRawInputDeviceList` in a loop to properly handle device list change between calls to `GetRawInputDeviceList`. * Update nf-winuser-getrawinputdevicelist.md fixup
1 parent 1ad8a3f commit 7751736

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

sdk-api-src/content/winuser/nf-winuser-getrawinputdevicelist.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,25 @@ To get more detailed information about the attached devices, call <a href="/wind
101101
The following sample code shows a typical call to <b>GetRawInputDeviceList</b>:
102102

103103

104-
```
105-
UINT nDevices;
104+
```cpp
105+
UINT nDevices, nStored, i;
106106
PRAWINPUTDEVICELIST pRawInputDeviceList;
107-
if (GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST)) != 0) { Error();}
108-
if ((pRawInputDeviceList = malloc(sizeof(RAWINPUTDEVICELIST) * nDevices)) == NULL) {Error();}
109-
if (GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST)) == (<dtype rid="UINT"/>)-1) {Error();}
110-
// do the job...
107+
if (GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST)) != 0) { Error(); }
108+
109+
// The list of devices can change between calls to GetRawInputDeviceList,
110+
// so call it again if the function returns ERROR_INSUFFICIENT_BUFFER
111+
do
112+
{
113+
if ((pRawInputDeviceList = realloc(sizeof(RAWINPUTDEVICELIST) * nDevices)) == NULL) { Error(); }
114+
nStored = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST));
115+
} while (nStored == (UINT)-1) && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
116+
117+
if (nStored == (UINT)-1) { Error(); }
118+
119+
for (i = 0; i < nStored; ++i)
120+
{
121+
// do the job with each pRawInputDeviceList[i] element...
122+
}
111123

112124
// after the job, free the RAWINPUTDEVICELIST
113125
free(pRawInputDeviceList);

0 commit comments

Comments
 (0)