-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmi.h
125 lines (98 loc) · 3.52 KB
/
wmi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#ifndef MOUSE_CONFIG_WMI_H
#define MOUSE_CONFIG_WMI_H
#include "base.h"
#include <stdio.h>
#include <wbemidl.h>
#define QUERY_BUFFER_SIZE 512
#define MOUSE_DEVICE L"Win32_PointingDevice"
#define KEYBOARD_DEVICE L"Win32_Keyboard"
ULONG CountDeviceById(IWbemServices *services, LPCWSTR device, LPCWSTR deviceId) {
HRESULT hr = 0;
ULONG returnedCount = 0;
IEnumWbemClassObject *results = NULL;
BSTR language = SysAllocString(L"WQL");
wchar_t queryBuffer[QUERY_BUFFER_SIZE] = {0};
swprintf_s(queryBuffer, QUERY_BUFFER_SIZE,
L"SELECT * FROM %ls WHERE DeviceID like '%%%ls%%'", device, deviceId);
BSTR query = SysAllocString(queryBuffer);
// issue a WMI query
COM_CALL(hr, services, ExecQuery, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &results);
// list the query results
if (results == NULL) {
goto Exit;
}
IWbemClassObject *result = NULL;
// enumerate the retrieved objects
COM_CALL(hr, results, Next, WBEM_INFINITE, 1, &result, &returnedCount);
if (result != NULL) {
COM_CALL(hr, result, Release);
}
Exit:
SysFreeString(query);
return returnedCount;
}
ULONG CountMouseById(IWbemServices *services, LPCWSTR deviceId) {
return CountDeviceById(services, MOUSE_DEVICE, deviceId);
}
ULONG CountKeyboardById(IWbemServices *services, LPCWSTR deviceId) {
return CountDeviceById(services, KEYBOARD_DEVICE, deviceId);
}
#define DeviceIdMaxLen 256
#define DEFAULT_CFG_ID L"Default"
typedef struct _MouseConfig {
DWORD mouseSpeed;
DWORD mouseParams[3];
} MouseConfig;
typedef struct _MouseConfigId {
MouseConfig cfg;
wchar_t deviceId[DeviceIdMaxLen];
} MouseConfigId;
typedef struct _KeyboardConfig {
BOOL numLock;
BOOL capsLock;
BOOL scrollLock;
wchar_t deviceId[DeviceIdMaxLen];
} KeyboardConfig;
void ExecMouseConfig(const MouseConfigId *cfg, size_t len, IWbemServices *services);
void ExecKeyboardConfig(const KeyboardConfig *cfg, size_t len, IWbemServices *services);
void ExecConfig(MouseConfigId *mouseCfg, size_t mouseCfgLen,
const KeyboardConfig *kbdCfg, size_t kbdCfgLen) {
if ((mouseCfg == NULL || mouseCfgLen < 1) && (kbdCfg == NULL || kbdCfgLen < 1)) {
return;
}
HRESULT hr = 0;
IWbemLocator *locator = NULL;
IWbemServices *services = NULL;
// BSTR strings we'll use (http://msdn.microsoft.com/en-us/library/ms221069.aspx)
BSTR resource = SysAllocString(L"ROOT\\CIMV2");
BSTR language = SysAllocString(L"WQL");
// initialize COM
CoInitializeEx(0, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
// connect to WMI
hr = CoCreateInstance(&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
&IID_IWbemLocator, (LPVOID *) &locator);
EXIT_ON_ERROR(hr, CoCreateInstance);
COM_CALL(hr, locator, ConnectServer, resource, NULL, NULL, NULL, 0, NULL, NULL, &services);
if (services == NULL) {
goto Exit;
}
if (mouseCfg != NULL && mouseCfgLen > 0) {
ExecMouseConfig(mouseCfg, mouseCfgLen, services);
}
if (kbdCfg != NULL && kbdCfgLen > 0) {
ExecKeyboardConfig(kbdCfg, kbdCfgLen, services);
}
COM_CALL(hr, services, Release);
Exit:
if (locator != NULL) {
locator->lpVtbl->Release(locator);
}
SysFreeString(language);
SysFreeString(resource);
// unwind everything else we've allocated
CoUninitialize();
}
#endif //MOUSE_CONFIG_WMI_H