Created
August 9, 2018 17:57
-
-
Save azyobuzin/31ea255b274d4e1001c8332ab2f9bd39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <tchar.h> | |
#include <Windows.h> | |
#include <locale.h> | |
void PrintError(DWORD error) { | |
LPTSTR str; | |
DWORD charCount = FormatMessage( | |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | |
NULL, error, | |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
(LPTSTR)&str, | |
0, NULL); | |
if (charCount > 0) { | |
_ftprintf_s(stderr, _T("%s\n"), str); | |
} | |
else { | |
_ftprintf_s(stderr, _T("Error 0x%x\n"), error); | |
} | |
} | |
void PrintLastError(void) { | |
PrintError(GetLastError()); | |
} | |
int main() | |
{ | |
setlocale(LC_ALL, ""); | |
UINT numDevices; | |
RAWINPUTDEVICELIST *rawInputDeviceList = nullptr; | |
while (1) { | |
if (GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST)) != 0) { | |
PrintLastError(); | |
return 1; | |
} | |
if (rawInputDeviceList != nullptr) | |
delete[] rawInputDeviceList; | |
rawInputDeviceList = new RAWINPUTDEVICELIST[numDevices]; | |
numDevices = GetRawInputDeviceList(rawInputDeviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)); | |
if (numDevices == (UINT)-1) { | |
DWORD error = GetLastError(); | |
// ERROR_INSUFFICIENT_BUFFER の場合はやり直し | |
// それ以外は続行不可能 | |
if (error != ERROR_INSUFFICIENT_BUFFER) { | |
PrintError(error); | |
return 1; | |
} | |
} | |
else { | |
break; | |
} | |
} | |
for (UINT i = 0; i < numDevices; i++) { | |
RAWINPUTDEVICELIST *device = &rawInputDeviceList[i]; | |
const TCHAR *deviceTypeStr; | |
switch (device->dwType) { | |
case RIM_TYPEMOUSE: | |
deviceTypeStr = _T("Mouse"); | |
break; | |
case RIM_TYPEKEYBOARD: | |
deviceTypeStr = _T("Keyboard"); | |
break; | |
case RIM_TYPEHID: | |
deviceTypeStr = _T("HID"); | |
break; | |
default: | |
deviceTypeStr = _T("Unknown"); | |
break; | |
} | |
// 名前を取得 | |
TCHAR *deviceName = nullptr; | |
UINT deviceNameLength = 0; | |
while (1) { | |
// UINT を返すくせに、戻り値が負かどうかをチェックしろだと? | |
INT result = (INT)GetRawInputDeviceInfo(device->hDevice, RIDI_DEVICENAME, deviceName, &deviceNameLength); | |
if ((result >= 0 && deviceName == nullptr) || result == -1) { | |
// バッファー不足 | |
if (deviceName != nullptr) | |
delete[] deviceName; | |
deviceName = new TCHAR[deviceNameLength]; | |
} | |
else if (result < 0) { | |
// 負値 → エラー | |
PrintLastError(); | |
deviceName = nullptr; | |
break; | |
} | |
else { | |
// 成功 | |
break; | |
} | |
} | |
if (deviceName == nullptr) { | |
_tprintf_s(_T("%s 名前取得失敗\n"), deviceTypeStr); | |
} | |
else { | |
_tprintf_s(_T("%s %s\n"), deviceTypeStr, deviceName); | |
delete[] deviceName; | |
} | |
// preparsed を取得 | |
UINT preparsedDataLength = 0; | |
INT result = (INT)GetRawInputDeviceInfo(device->hDevice, RIDI_PREPARSEDDATA, NULL, &preparsedDataLength); | |
if (result >= 0) { | |
_tprintf_s(_T("Preparsed Data Length: %d\n\n"), preparsedDataLength); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment