-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkAdapter.cpp
231 lines (207 loc) · 5.79 KB
/
NetworkAdapter.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
/*
Copyright(C) Nishant Sivakumar, 2005. All rights reserved.
http://blog.voidnish.com
*/
#include "NetworkAdapter.h"
#include <sstream>
#include <iomanip>
#pragma comment(lib,"comsupp.lib")
#include <comutil.h>
CodeProjectUtils::CNetworkAdapterList::CNetworkAdapterList(void)
{
pAdapterInfo = NULL;
ULONG OutBufLen = 0;
m_count = 0;
//Get the required size of the buffer
if( GetAdaptersInfo(NULL, &OutBufLen) == ERROR_BUFFER_OVERFLOW )
{
int divisor = sizeof IP_ADAPTER_INFO;
if(sizeof time_t == 8)
divisor -= 8;
m_count = OutBufLen/divisor;
pAdapterInfo = new IP_ADAPTER_INFO[m_count];
//Get info for the adapters
if( GetAdaptersInfo(pAdapterInfo, &OutBufLen) != ERROR_SUCCESS )
{
//Call failed
delete[] pAdapterInfo;
pAdapterInfo = NULL;
m_count = 0;
}
}
}
CodeProjectUtils::CNetworkAdapterList::~CNetworkAdapterList(void)
{
delete[] pAdapterInfo;
pAdapterInfo = NULL;
}
//IsValid returns true if the adapter info has been successfully obtained.
//This function should be called before using the class.
bool CodeProjectUtils::CNetworkAdapterList::IsValid(void)
{
return !(pAdapterInfo == NULL);
}
int CodeProjectUtils::CNetworkAdapterList::GetCount(void)
{
if(!IsValid())
return -1;
return m_count;
}
int CodeProjectUtils::CNetworkAdapterList::GetAdapters(ADAPTERINFO* pADAPTERINFO)
{
if(!IsValid())
return -1;
int index = 0;
PIP_ADAPTER_INFO pAdapInfo = pAdapterInfo;
while(pAdapInfo)
{
std::wstringstream sstream;
sstream << pAdapInfo->AdapterName;
pADAPTERINFO[index].InstanceId = sstream.str();
sstream.str(L"");
sstream << pAdapInfo->Description;
pADAPTERINFO[index].Description = sstream.str();
for(int i=0; i < (int)pAdapInfo->AddressLength; i++) {
sstream.str(L"");
sstream << pADAPTERINFO[index].MAC << std::setw(2) << std::setfill(L'0') << std::hex << (int)pAdapInfo->Address[i];
pADAPTERINFO[index].MAC = sstream.str();
}
index++;
pAdapInfo = pAdapInfo->Next;
}
return 0;
}
bool CodeProjectUtils::EnableConnection(GUID guidId, bool bEnable)
{
bool ret = false;
typedef void (__stdcall *LPNCFREENETCONPROPERTIES) (NETCON_PROPERTIES* pProps);
HMODULE hNSModule = LoadLibrary(_T("netshell.dll"));
if (hNSModule)
{
LPNCFREENETCONPROPERTIES NcFreeNetconProperties =
(LPNCFREENETCONPROPERTIES)GetProcAddress(hNSModule, "NcFreeNetconProperties");
if (NcFreeNetconProperties)
{
CoInitialize(0);
INetConnectionManager* pConnMan = 0;
HRESULT hr = CoCreateInstance(CLSID_ConnectionManager, 0,CLSCTX_ALL,
__uuidof(INetConnectionManager), (void**)&pConnMan);
if(SUCCEEDED(hr))
{
IEnumNetConnection* pEnum = 0;
HRESULT hr = pConnMan->EnumConnections(NCME_DEFAULT, &pEnum);
if(SUCCEEDED(hr))
{
INetConnection* pCon = 0;
ULONG count;
bool bDone = false;
while (pEnum->Next(1, &pCon, &count) == S_OK && !bDone)
{
NETCON_PROPERTIES* pProps = 0;
hr = pCon->GetProperties(&pProps);
if(SUCCEEDED(hr))
{
if(IsEqualGUID(guidId, pProps->guidId) == TRUE )
{
int nRetry = 3;
if (bEnable)
{
do
{
ret = ((hr = pCon->Connect()) == S_OK);
}
while(!ret && --nRetry && (ErrorCOMToWin32(hr) == ERROR_RETRY));
}
else
{
do
{
ret = ((hr = pCon->Disconnect()) == S_OK);
}
while(!ret && --nRetry && (ErrorCOMToWin32(hr) == ERROR_RETRY));
}
bDone = true;
}
NcFreeNetconProperties(pProps);
}
pCon->Release();
}
pEnum->Release();
}
pConnMan->Release();
}
CoUninitialize();
}
FreeLibrary(hNSModule);
}
return ret;
}
bool CodeProjectUtils::UpdateRegistry(std::wstring strNetCfgInstanceId, LPCTSTR lpszMacID /*= NULL*/)
{
bool bRet = false;
HKEY hKey = NULL;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"),
0,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
{
DWORD dwIndex = 0;
TCHAR Name[1024];
DWORD cName = 1024;
while( RegEnumKeyEx(hKey, dwIndex, Name, &cName,
NULL, NULL, NULL, NULL) == ERROR_SUCCESS )
{
HKEY hSubKey = NULL;
if(RegOpenKeyEx(hKey,Name,0,KEY_ALL_ACCESS,&hSubKey) == ERROR_SUCCESS)
{
BYTE Data[1204];
DWORD cbData = 1024;
if(RegQueryValueEx(hSubKey,_T("NetCfgInstanceId"),NULL,NULL,Data,&cbData) == ERROR_SUCCESS)
{
if(_tcscmp((TCHAR*)Data,strNetCfgInstanceId.c_str()) == 0)
{
if(lpszMacID == NULL)
{
//Delete the NetCfgInstanceId entry
LONG nErr = RegDeleteValue(hSubKey, _T("NetworkAddress"));
if( (nErr == ERROR_SUCCESS) || (nErr == ERROR_FILE_NOT_FOUND) )
{
bRet = true;
}
}
else
{
//Add the NetCfgInstanceId entry
if(RegSetValueEx(hSubKey,_T("NetworkAddress"),0,REG_SZ,(const BYTE*)lpszMacID,sizeof(TCHAR) * ((DWORD)_tcslen(lpszMacID) + 1)) == ERROR_SUCCESS)
{
bRet = true;
}
}
}
}
RegCloseKey(hSubKey);
}
cName = 1024;
dwIndex++;
}
RegCloseKey(hKey);
}
return bRet;
}
bool CodeProjectUtils::Reset(ADAPTERINFO* pAdInfo)
{
GUID guidId;
std::wstring guidstr = pAdInfo->InstanceId;
guidstr.erase(guidstr.begin(), guidstr.begin() + guidstr.find_first_not_of(L"{}"));
guidstr.erase(guidstr.find_last_not_of(L"{}") + 1);
_bstr_t bstr = guidstr.c_str();
UuidFromStringW((unsigned short*)bstr.GetBSTR(),&guidId);
return(EnableConnection(guidId,false) && EnableConnection(guidId,true));
}
DWORD CodeProjectUtils::ErrorCOMToWin32(HRESULT hr)
{
HRESULT hrfac = HRESULT_FACILITY(hr);
if( (hrfac == FACILITY_WINDOWS) || (hrfac == FACILITY_WIN32) )
return HRESULT_CODE(hr);
else
return hr;
}