-
Notifications
You must be signed in to change notification settings - Fork 1
/
libbindconfig.cpp
282 lines (232 loc) · 6.63 KB
/
libbindconfig.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "bindconfig.h"
#include <stdio.h>
#include <netcfgx.h>
#include <devguid.h>
#include <shlwapi.h>
int doPrint = 0;
static INetCfg *netCfg;
static int configBindings(INetCfgComponent *netCfgComp, LPCWSTR pathStr,
int status)
{
int count = 0;
HRESULT ret;
LPWSTR idStr, nameStr;
INetCfgComponentBindings *bindings;
IEnumNetCfgBindingPath *bindingIter;
INetCfgBindingPath *bindingPath;
netCfgComp->GetBindName(&idStr);
netCfgComp->GetDisplayName(&nameStr);
printf("%ws (%ws)\n", idStr, nameStr);
CoTaskMemFree(idStr);
CoTaskMemFree(nameStr);
ret = netCfgComp->QueryInterface(IID_INetCfgComponentBindings,
(void**)&bindings);
if (ret != S_OK)
{
printf("netCfgComp->QueryInterface() returned %x\n", ret);
goto _error_exit;
}
ret = bindings->EnumBindingPaths(EBP_ABOVE, &bindingIter);
if (ret != S_OK)
{
printf("bindings->EnumBindingPaths() returned %x\n", ret);
goto _error_exit;
}
bindingIter->Reset();
while ((ret = bindingIter->Next(1, &bindingPath, NULL)) == S_OK)
{
LPWSTR str;
bindingPath->GetPathToken(&str);
//printf("@%ws\n@%ws\n", pathStr, str);
if (pathStr == NULL || (StrCmpLogicalW(pathStr, str) == 0))
{
if (status < 0)
{
int ena = bindingPath->IsEnabled() == S_OK ? 1 : 0;
count += ena;
if (doPrint)
printf(" %ws: %d\n", str, ena);
}
else
{
int ena = bindingPath->IsEnabled() == S_OK ? 1 : 0;
if (doPrint)
printf(" %ws: %d->%d\n", str, ena, (status > 0));
if (status > 0)
count += ena;
ret = bindingPath->Enable(status > 0);
if (ret != S_OK)
printf("bindingPath->Enable(%d) returned %x\n", status,ret);
}
CoTaskMemFree(str);
if (pathStr)
break;
}
}
if (status >= 0)
{
if (doPrint)
printf("Applying changes ... ");
ret = netCfg->Apply();
if (doPrint)
{
if (ret == S_OK)
printf("success\n");
else
printf("failed - error code %x\n", ret);
}
}
return count;
_error_exit:
return -1;
}
// bindconfig() returns the count of "enabled" bindings; in case of error,
// it returns a negative number
int bindconfig(CompClass compClass, char *comp, char *path, int status)
{
HRESULT ret;
int count = 0;
const GUID *clsId;
WCHAR compWStr[64];
WCHAR pathWStr[64];
LPCWSTR compStr = NULL;
LPCWSTR pathStr = NULL;
INetCfgLock *lock;
INetCfgClass *netCfgClass;
IEnumNetCfgComponent *iter;
INetCfgComponent *netCfgComp;
switch(compClass)
{
case kCompClassDevice:
clsId = &GUID_DEVCLASS_NET;
break;
case kCompClassClient:
clsId = &GUID_DEVCLASS_NETCLIENT;
break;
case kCompClassProtocol:
clsId = &GUID_DEVCLASS_NETTRANS;
break;
case kCompClassService:
clsId = &GUID_DEVCLASS_NETSERVICE;
break;
default:
printf("invalid compClass %d\n", compClass);
goto _invalid_param;
}
if (comp)
{
mbstowcs(compWStr, comp, strlen(comp)+1);
compStr = (LPWSTR) &compWStr;
}
if (path)
{
mbstowcs(pathWStr, path, strlen(path)+1);
pathStr = (LPWSTR) &pathWStr;
}
ret = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (ret != S_OK)
{
printf("CoInitializeEx() returned %x\n", ret);
goto _co_initialize_ex_failed;
}
ret = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER,
IID_INetCfg, (void**)&netCfg);
if (ret != S_OK)
{
printf("CoCreateInstance(CLSID_CNetCfg) returned %x\n", ret);
goto _co_create_instance_failed;
}
ret = netCfg->QueryInterface(IID_INetCfgLock, (void**) &lock);
if (ret != S_OK)
{
printf("netCfg->QueryInterface(IID_INetCfgLock) returned %x\n", ret);
goto _query_interface_lock_failed;
}
ret = lock->AcquireWriteLock(5000, L"bindconfig", NULL);
if (ret != S_OK)
{
printf("lock->AcquireWriteLock() returned %x\n", ret);
goto _lock_acquire_failed;
}
ret = netCfg->Initialize(NULL);
if (ret != S_OK)
{
printf("netCfg->Initialize() returned %x\n", ret);
goto _initialize_failed;
}
ret = netCfg->QueryNetCfgClass(clsId, IID_INetCfgClass,
(void**)&netCfgClass);
if (ret != S_OK)
{
printf("QueryNetCfgClass() returned %x\n", ret);
goto _query_class_failed;
}
ret = netCfgClass->EnumComponents(&iter);
if (ret != S_OK)
{
printf("EnumComponents() returned %x\n", ret);
goto _enum_comp_failed;
}
iter->Reset();
while ((ret = iter->Next(1, &netCfgComp, NULL)) == S_OK)
{
LPWSTR idStr, nameStr;
GUID guid;
netCfgComp->GetBindName(&idStr);
netCfgComp->GetDisplayName(&nameStr);
if (compStr == NULL || (StrCmpLogicalW(idStr, compStr) == 0))
{
count += configBindings(netCfgComp, pathStr, status);
CoTaskMemFree(idStr);
CoTaskMemFree(nameStr);
}
}
_query_class_failed:
_enum_comp_failed:
ret = netCfg->Uninitialize();
if (ret != S_OK)
{
printf("netCfg->Uninitialize() returned %x\n", ret);
}
_initialize_failed:
lock->ReleaseWriteLock();
_query_interface_lock_failed:
_lock_acquire_failed:
netCfg->Release();
_co_create_instance_failed:
CoUninitialize();
_co_initialize_ex_failed:
return count;
_invalid_param:
return 255;
}
// bindingStatus() returns "enabled" if one or more bindings are enabled,
// else returns "disabled"
BindingStatus bindingStatus(CompClass compClass, char *comp, char *path)
{
if (bindconfig(compClass, comp, path, -1) > 0)
return kBindingEnabled;
else
return kBindingDisabled;
}
// setBindingStatus() returns true if the new 'status' has been successfully
// set otherwise returns false
bool setBindingStatus(CompClass compClass, char *comp, char *path,
BindingStatus status)
{
if (status == kBindingEnabled)
{
if (bindconfig(compClass, comp, path, 1) > 0)
return true;
else
return false;
}
else if (status == kBindingDisabled)
{
if (bindconfig(compClass, comp, path, 0) == 0)
return true;
else
return false;
}
return false;
}