blob: c81e0f84d9440742c530a5a7a8f63e9e8f3c6e69 (
plain)
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
|
// UsbDkHelper.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include "UsbDkHelper.h"
#include "Installer.h"
#include "DriverAccess.h"
#include "RedirectorAccess.h"
//-------------------------------------------------------------------------------------------
typedef struct tag_REDIRECTED_DEVICE_HANDLE
{
USB_DK_DEVICE_ID DeviceID;
unique_ptr<UsbDkRedirectorAccess> RedirectorAccess;
} REDIRECTED_DEVICE_HANDLE, PREDIRECTED_DEVICE_HANDLE;
//-------------------------------------------------------------------------------------------
void printExceptionString(const char *errorStr)
{
auto tString = string2tstring(string(errorStr));
OutputDebugString(tString.c_str());
tcout << tString;
}
//------------------------------------------------------------------------------------------
InstallResult InstallDriver(void)
{
try
{
UsbDkInstaller installer;
return installer.Install() ? InstallSuccess : InstallFailureNeedReboot;
}
catch (const exception &e)
{
printExceptionString(e.what());
UninstallDriver();
return InstallFailure;
}
}
//-------------------------------------------------------------------------------------------
BOOL UninstallDriver(void)
{
try
{
UsbDkInstaller installer;
installer.Uninstall();
return TRUE;
}
catch (const exception &e)
{
printExceptionString(e.what());
return FALSE;
}
}
//-------------------------------------------------------------------------------------------
DLL BOOL GetConfigurationDescriptor(PUSB_DK_CONFIG_DESCRIPTOR_REQUEST Request,
PUSB_CONFIGURATION_DESCRIPTOR *Descriptor,
PULONG Length)
{
try
{
UsbDkDriverAccess driver;
*Descriptor = driver.GetConfigurationDescriptor(*Request, *Length);
return TRUE;
}
catch (const exception &e)
{
printExceptionString(e.what());
return FALSE;
}
}
//-------------------------------------------------------------------------------------------
DLL void ReleaseConfigurationDescriptor(PUSB_CONFIGURATION_DESCRIPTOR Descriptor)
{
try
{
UsbDkDriverAccess::ReleaseConfigurationDescriptor(Descriptor);
}
catch (const exception &e)
{
printExceptionString(e.what());
}
}
//-------------------------------------------------------------------------------------------
BOOL GetDevicesList(PUSB_DK_DEVICE_INFO *DevicesArray, PULONG NumberDevices)
{
try
{
UsbDkDriverAccess driver;
driver.GetDevicesList(*DevicesArray, *NumberDevices);
return TRUE;
}
catch (const exception &e)
{
printExceptionString(e.what());
return FALSE;
}
}
//-------------------------------------------------------------------------------------------
void ReleaseDeviceList(PUSB_DK_DEVICE_INFO DevicesArray)
{
try
{
UsbDkDriverAccess::ReleaseDeviceList(DevicesArray);
}
catch (const exception &e)
{
printExceptionString(e.what());
}
}
//-------------------------------------------------------------------------------------------
static UsbDkRedirectorAccess* ConnectToRedirector(ULONG RedirectorID)
{
// Although we got notification from driver regarding redirection creation
// system requires some (rather short) time to get the device ready for requests processing.
// In case of unfortunate timing we may try to open the redirector device symlink before
// it is ready, in this case we get ERROR_FILE_NOT_FOUND.
// Unfortunately it looks like there is no way to ensure device is ready but spin around
// and poll it for some time.
static const int NumRetries = 100;
static const int TimeoutMS = 50;
int RetryNumber = 0;
for (;;)
{
try
{
return new UsbDkRedirectorAccess(RedirectorID);
}
catch (const UsbDkDriverFileException& e)
{
if ((e.GetErrorCode() != ERROR_FILE_NOT_FOUND) || (++RetryNumber == NumRetries))
{
OutputDebugString(TEXT("Cannot connect to the redirector, gave up."));
throw;
}
}
OutputDebugString(TEXT("Cannot connect to the redirector, will wait and try again..."));
Sleep(TimeoutMS);
}
}
HANDLE StartRedirect(PUSB_DK_DEVICE_ID DeviceID)
{
bool bRedirectAdded = false;
try
{
UsbDkDriverAccess driverAccess;
auto RedirectorID = driverAccess.AddRedirect(*DeviceID);
bRedirectAdded = true;
unique_ptr<REDIRECTED_DEVICE_HANDLE> deviceHandle(new REDIRECTED_DEVICE_HANDLE);
deviceHandle->DeviceID = *DeviceID;
deviceHandle->RedirectorAccess.reset(ConnectToRedirector(RedirectorID));
return reinterpret_cast<HANDLE>(deviceHandle.release());
}
catch (const exception &e)
{
printExceptionString(e.what());
}
if (bRedirectAdded)
{
try
{
UsbDkDriverAccess driverAccess;
driverAccess.RemoveRedirect(*DeviceID);
}
catch (const exception &e)
{
printExceptionString(e.what());
}
}
return nullptr;
}
//-------------------------------------------------------------------------------------------
BOOL StopRedirect(HANDLE DeviceHandle)
{
try
{
UsbDkDriverAccess driverAccess;
unique_ptr<REDIRECTED_DEVICE_HANDLE> deviceHandle(reinterpret_cast<REDIRECTED_DEVICE_HANDLE*>(DeviceHandle));
deviceHandle->RedirectorAccess.reset();
driverAccess.RemoveRedirect(deviceHandle->DeviceID);
return TRUE;
}
catch (const exception &e)
{
printExceptionString(e.what());
return FALSE;
}
}
//-------------------------------------------------------------------------------------------
|