-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.cpp
260 lines (224 loc) · 7.16 KB
/
main.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <chrono>
#include <thread>
#include <tchar.h>
#include <strsafe.h>
#include <windows.h>
#include <SetupAPI.h>
#include <string>
#include <iostream>
#pragma comment(lib, "Setupapi.lib")
BOOLEAN GetDevicePath2(
_In_ LPCGUID InterfaceGuid,
_Out_writes_(BufLen) PTCHAR DevicePath,
_In_ size_t BufLen
)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
HDEVINFO hardwareDeviceInfo;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData = { 0 };
BOOLEAN status = FALSE;
HRESULT hr;
hardwareDeviceInfo = SetupDiGetClassDevs(
InterfaceGuid,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.
if (INVALID_HANDLE_VALUE == hardwareDeviceInfo)
{
printf("Idd device: SetupDiGetClassDevs failed, last error 0x%x\n", GetLastError());
return FALSE;
}
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo,
0, // No care about specific PDOs
InterfaceGuid,
0, //
&deviceInterfaceData))
{
printf("Idd device: SetupDiEnumDeviceInterfaces failed, last error 0x%x\n", GetLastError());
goto Clean0;
}
//
// Allocate a function class device data structure to receive the
// information about this particular device.
//
SetupDiGetDeviceInterfaceDetail(
hardwareDeviceInfo,
&deviceInterfaceData,
NULL, // probing so no output buffer yet
0, // probing so output buffer length of zero
&requiredLength,
NULL);//not interested in the specific dev-node
if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
{
printf("Idd device: SetupDiGetDeviceInterfaceDetail failed, last error 0x%x\n", GetLastError());
goto Clean0;
}
predictedLength = requiredLength;
deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)HeapAlloc(
GetProcessHeap(),
HEAP_ZERO_MEMORY,
predictedLength
);
if (deviceInterfaceDetailData)
{
deviceInterfaceDetailData->cbSize =
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
}
else
{
printf("Idd device: HeapAlloc failed, last error 0x%x\n", GetLastError());
goto Clean0;
}
if (!SetupDiGetDeviceInterfaceDetail(
hardwareDeviceInfo,
&deviceInterfaceData,
deviceInterfaceDetailData,
predictedLength,
&requiredLength,
NULL))
{
printf("Idd device: SetupDiGetDeviceInterfaceDetail failed, last error 0x%x\n", GetLastError());
goto Clean1;
}
hr = StringCchCopy(DevicePath, BufLen, deviceInterfaceDetailData->DevicePath);
if (FAILED(hr))
{
printf("Error: StringCchCopy failed with HRESULT 0x%x", hr);
status = FALSE;
goto Clean1;
}
else
{
status = TRUE;
}
Clean1:
(VOID)HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData);
Clean0:
(VOID)SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
return status;
}
HANDLE DeviceOpenHandle(const GUID& devGuid)
{
// const int maxDevPathLen = 256;
TCHAR devicePath[256] = { 0 };
HANDLE hDevice = INVALID_HANDLE_VALUE;
do
{
if (FALSE == GetDevicePath2(
&devGuid,
devicePath,
sizeof(devicePath) / sizeof(devicePath[0])))
{
break;
}
if (_tcslen(devicePath) == 0)
{
printf("GetDevicePath got empty device path\n");
break;
}
_tprintf(_T("Idd device: try open %s\n"), devicePath);
hDevice = CreateFile(
devicePath,
GENERIC_READ | GENERIC_WRITE,
// FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL
);
if (hDevice == INVALID_HANDLE_VALUE || hDevice == NULL)
{
DWORD error = GetLastError();
printf("CreateFile failed 0x%lx\n", error);
}
} while (0);
return hDevice;
}
enum VddCtlCode
{
IOCTL_VDD_CONNECT = 0x22A008,
IOCTL_VDD_ADD = 0x22E004,
IOCTL_VDD_UPDATE = 0x22A00C,
};
void VddIoCtl(HANDLE vdd, VddCtlCode code)
{
BYTE InBuffer[32]{};
int OutBuffer = 0;
OVERLAPPED Overlapped{};
DWORD NumberOfBytesTransferred;
Overlapped.hEvent = CreateEventW(NULL, NULL, NULL, NULL);
DeviceIoControl(vdd, code, InBuffer, _countof(InBuffer), &OutBuffer, sizeof(OutBuffer), NULL, &Overlapped);
GetOverlappedResult(vdd, &Overlapped, &NumberOfBytesTransferred, TRUE);
if (Overlapped.hEvent && Overlapped.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(Overlapped.hEvent);
}
bool quit = false;
void pipe_thread()
{
HANDLE hPipe;
char buffer[1024] = { 0 };
DWORD dwRead;
hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\MyPipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1,
1024 * 16,
1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
if (hPipe != INVALID_HANDLE_VALUE)
{
if (ConnectNamedPipe(hPipe, NULL) != FALSE) // wait for someone to connect to the pipe
{
while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)
{
buffer[dwRead] = '\0';
if (std::string(buffer) == "quit")
{
quit = true;
break;
}
}
}
DisconnectNamedPipe(hPipe);
}
CloseHandle(hPipe);
}
int main()
{
const GUID PARSEC_VDD_DEVINTERFACE = \
{ 0x00b41627, 0x04c4, 0x429e, { 0xa2, 0x6e, 0x02, 0x65, 0xcf, 0x50, 0xc8, 0xfa } };
// try to get device handle with GUID
HANDLE vdd = DeviceOpenHandle(PARSEC_VDD_DEVINTERFACE);
if (!vdd || vdd == INVALID_HANDLE_VALUE)
{
printf("failed to get ParsecVDD device handle.\n");
return 1;
}
// connect & plug in
VddIoCtl(vdd, IOCTL_VDD_CONNECT);
VddIoCtl(vdd, IOCTL_VDD_UPDATE);
VddIoCtl(vdd, IOCTL_VDD_ADD);
VddIoCtl(vdd, IOCTL_VDD_UPDATE);
std::thread pipe_thread_instance(pipe_thread);
while (true)
{
// update each 100ms
std::this_thread::sleep_for(std::chrono::milliseconds(100));
VddIoCtl(vdd, IOCTL_VDD_UPDATE);
if (quit) {
break;
}
}
pipe_thread_instance.join();
// disconnect
VddIoCtl(vdd, IOCTL_VDD_CONNECT);
CloseHandle(vdd);
return 0;
}