OSDN Git Service

Send Attachment successfully
[tortoisegit/TortoiseGitJp.git] / src / Utils / NetAdapterInfo.cpp
1 // OneNetAdapterInfo.cpp: implementation of the COneNetAdapterInfo class.\r
2 //\r
3 //////////////////////////////////////////////////////////////////////\r
4 \r
5 #include "stdafx.h"\r
6 #include "SpeedPostEmail.h"\r
7 #include "NetAdapterInfo.h"\r
8 #include "Iphlpapi.h"\r
9 #include "IpTypes.h"\r
10 \r
11 #ifdef _DEBUG\r
12 #undef THIS_FILE\r
13 static char THIS_FILE[]=__FILE__;\r
14 #define new DEBUG_NEW\r
15 #endif\r
16 \r
17 #define MALLOC( bytes )                 ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, (bytes) )\r
18 #define FREE( ptr )                             if( ptr ) ::HeapFree( ::GetProcessHeap(), 0, ptr )\r
19 #define REMALLOC( ptr, bytes )  ::HeapReAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, bytes )\r
20 \r
21 #pragma comment ( lib, "iphlpapi.lib" )\r
22 \r
23 //////////////////////////////////////////////////////////////////////\r
24 // Construction/Destruction\r
25 //////////////////////////////////////////////////////////////////////\r
26 COneNetAdapterInfo::COneNetAdapterInfo ( IP_ADAPTER_INFO *pAdptInfo )\r
27 {\r
28         m_bInitOk = FALSE;\r
29         memset ( &m_PhysicalAddress, 0, sizeof(m_PhysicalAddress) );\r
30         m_nPhysicalAddressLength = 0;\r
31         ASSERT ( pAdptInfo );\r
32         memcpy ( &m_AdptInfo, pAdptInfo, sizeof(IP_ADAPTER_INFO) );\r
33         if ( !Init() )\r
34         {\r
35                 TRACE ( _T("[%s - %s] initialize failed."), Get_Name(), Get_Desc() );\r
36         }\r
37 }\r
38 \r
39 COneNetAdapterInfo::~COneNetAdapterInfo()\r
40 {\r
41         \r
42 }\r
43 \r
44 //\r
45 // ¸ù¾Ý´«ÈëµÄ pAdptInfo ÐÅÏ¢À´»ñÈ¡Ö¸¶¨Íø¿¨µÄ»ù±¾ÐÅÏ¢\r
46 //\r
47 BOOL COneNetAdapterInfo::Init ()\r
48 {\r
49         IP_ADDR_STRING* pNext                   = NULL;\r
50         IP_PER_ADAPTER_INFO* pPerAdapt  = NULL;\r
51         ULONG ulLen                                             = 0;\r
52         DWORD dwErr = ERROR_SUCCESS;\r
53         ASSERT ( m_AdptInfo.AddressLength > 0 );\r
54         t_IPINFO iphold;\r
55 \r
56         // ½«±äÁ¿Çå¿Õ\r
57         m_bInitOk = FALSE;\r
58         m_csName.Empty ();\r
59         m_csDesc.Empty ();\r
60         m_CurIPInfo.csIP.Empty ();\r
61         m_CurIPInfo.csSubnet.Empty ();\r
62         m_Ary_IP.RemoveAll ();\r
63         m_Ary_DNS.RemoveAll ();\r
64         m_Ary_Gateway.RemoveAll ();\r
65         \r
66 #ifndef _UNICODE\r
67         m_csName                        = m_AdptInfo.AdapterName;\r
68         m_csDesc                        = m_AdptInfo.Description;\r
69 #else\r
70         USES_CONVERSION;\r
71         m_csName                        = A2W ( m_AdptInfo.AdapterName );\r
72         m_csDesc                        = A2W ( m_AdptInfo.Description );\r
73 #endif\r
74         \r
75         // »ñÈ¡µ±Ç°ÕýÔÚʹÓõÄIPµØÖ·\r
76         if ( m_AdptInfo.CurrentIpAddress )\r
77         {\r
78                 m_CurIPInfo.csIP                = m_AdptInfo.CurrentIpAddress->IpAddress.String;\r
79                 m_CurIPInfo.csSubnet    = m_AdptInfo.CurrentIpAddress->IpMask.String;\r
80         }\r
81         else\r
82         {\r
83                 m_CurIPInfo.csIP                = _T("0.0.0.0");\r
84                 m_CurIPInfo.csSubnet    = _T("0.0.0.0");\r
85         }\r
86         \r
87         // »ñÈ¡±¾Íø¿¨ÖÐËùÓеÄIPµØÖ·\r
88         pNext = &( m_AdptInfo.IpAddressList );\r
89         while ( pNext )\r
90         {\r
91                 iphold.csIP             = pNext->IpAddress.String;\r
92                 iphold.csSubnet = pNext->IpMask.String;\r
93                 m_Ary_IP.Add ( iphold );\r
94                 pNext = pNext->Next;\r
95         }\r
96         \r
97         // »ñÈ¡±¾Íø¿¨ÖÐËùÓеÄÍø¹ØÐÅÏ¢\r
98         pNext = &( m_AdptInfo.GatewayList );\r
99         while ( pNext )\r
100         {\r
101                 m_Ary_Gateway.Add ( A2W( pNext->IpAddress.String ));\r
102                 pNext = pNext->Next;\r
103         }\r
104         \r
105         // »ñÈ¡±¾Íø¿¨ÖÐËùÓеĠDNS\r
106         dwErr = ::GetPerAdapterInfo ( m_AdptInfo.Index, pPerAdapt, &ulLen );\r
107         if( dwErr == ERROR_BUFFER_OVERFLOW )\r
108         {\r
109                 pPerAdapt = ( IP_PER_ADAPTER_INFO* ) MALLOC( ulLen );\r
110                 dwErr = ::GetPerAdapterInfo( m_AdptInfo.Index, pPerAdapt, &ulLen );\r
111                 \r
112                 // if we succeed than we need to drop into our loop\r
113                 // and fill the dns array will all available IP\r
114                 // addresses.\r
115                 if( dwErr == ERROR_SUCCESS )\r
116                 {\r
117                         pNext = &( pPerAdapt->DnsServerList );\r
118                         while( pNext )\r
119                         {\r
120                                 m_Ary_DNS.Add( A2W( pNext->IpAddress.String ) );\r
121                                 pNext = pNext->Next;\r
122                         }                               \r
123                         m_bInitOk = TRUE;\r
124                 }\r
125 \r
126                 // this is done outside the dwErr == ERROR_SUCCES just in case. the macro\r
127                 // uses NULL pointer checking so it is ok if pPerAdapt was never allocated.\r
128                 FREE( pPerAdapt );\r
129         }\r
130 \r
131         return m_bInitOk;\r
132 }\r
133 \r
134 //\r
135 // ÊÍ·Å»òˢб¾Íø¿¨µÄIPµØÖ·\r
136 //\r
137 BOOL COneNetAdapterInfo::RenewReleaseIP( Func_OperateIP func )\r
138 {       \r
139         IP_INTERFACE_INFO*      pInfo   = NULL;\r
140         BOOL bDidIt                                     = FALSE;\r
141         ULONG ulLen                                     = 0;\r
142         int nNumInterfaces                      = 0;\r
143         int nCnt                                        = 0;\r
144         DWORD dwErr                                     = ERROR_SUCCESS;\r
145 \r
146         dwErr = ::GetInterfaceInfo ( pInfo, &ulLen );\r
147         if( dwErr == ERROR_INSUFFICIENT_BUFFER )\r
148         {\r
149                 pInfo = ( IP_INTERFACE_INFO* ) MALLOC( ulLen );\r
150                 dwErr = ::GetInterfaceInfo ( pInfo, &ulLen );\r
151                 \r
152                 if( dwErr != ERROR_SUCCESS )\r
153                 {\r
154                         return FALSE;                   \r
155                 }\r
156         }\r
157         \r
158         // we can assume from here out that we have a valid array\r
159         // of IP_INTERFACE_INFO structures due to the error \r
160         // checking one above.\r
161         nNumInterfaces = ulLen / sizeof( IP_INTERFACE_INFO );\r
162         for( nCnt = 0; nCnt < nNumInterfaces; nCnt++ )\r
163         {\r
164                 if( pInfo[ nCnt ].Adapter[ 0 ].Index == m_AdptInfo.Index )\r
165                 {\r
166                         dwErr = func( &pInfo[ nCnt ].Adapter[ 0 ] );\r
167                         \r
168                         // free all used memory since we don't need it any more.\r
169                         FREE( pInfo );  \r
170                         \r
171                         bDidIt = ( dwErr == NO_ERROR );                 \r
172                         if( ! bDidIt ) {                                \r
173                                 return FALSE;\r
174                         }\r
175                         \r
176                         break;\r
177                 }\r
178         }                       \r
179         \r
180         return bDidIt;\r
181 }\r
182 \r
183 ////////////////////////////////////////////////////////////\r
184 //      Desc:\r
185 //              Releases the addresses held by this adapter.\r
186 ////////////////////////////////////////////////////////////\r
187 BOOL COneNetAdapterInfo::ReleaseIP()\r
188 {       \r
189         return RenewReleaseIP ( ::IpReleaseAddress );\r
190 }\r
191 \r
192 ////////////////////////////////////////////////////////////\r
193 //      Desc:\r
194 //              Renews the address being held by this adapter.\r
195 ////////////////////////////////////////////////////////////\r
196 BOOL COneNetAdapterInfo::RenewIP()\r
197 {\r
198         return RenewReleaseIP ( ::IpRenewAddress );\r
199 }\r
200 \r
201 CString COneNetAdapterInfo::GetAdapterTypeString ()\r
202 {\r
203         UINT nType = m_AdptInfo.Type;\r
204         CString csType = _T("");\r
205         switch( nType )\r
206         {\r
207         case MIB_IF_TYPE_OTHER:         csType = _T("Other");                   break;\r
208         case MIB_IF_TYPE_ETHERNET:      csType = _T("Ethernet");                break; \r
209         case MIB_IF_TYPE_TOKENRING:     csType = _T("Token Ring");              break; \r
210         case MIB_IF_TYPE_FDDI:          csType = _T("FDDI");                    break; \r
211         case MIB_IF_TYPE_PPP:           csType = _T("PPP");                             break; \r
212         case MIB_IF_TYPE_LOOPBACK:      csType = _T("Loopback");                break; \r
213         case MIB_IF_TYPE_SLIP:          csType = _T("SLIP");                    break;  \r
214         default: csType = _T("Invalid Adapter Type");                           break;\r
215         };\r
216         \r
217         return csType;\r
218 }\r
219 \r
220 time_t COneNetAdapterInfo::Get_LeaseObtained()                          const { return m_AdptInfo.LeaseObtained; }\r
221 time_t COneNetAdapterInfo::Get_LeaseExpired()                           const { return m_AdptInfo.LeaseExpires; }\r
222 int     COneNetAdapterInfo::Get_IPCount()                                               const { return (int)m_Ary_IP.GetSize(); }\r
223 int     COneNetAdapterInfo::Get_DNSCount()                                              const { return (int)m_Ary_DNS.GetSize(); }\r
224 CString COneNetAdapterInfo::Get_CurrentIP()                                     const { return m_CurIPInfo.csIP; }\r
225 BOOL COneNetAdapterInfo::Is_DHCP_Used()                                         const { return m_AdptInfo.DhcpEnabled; }\r
226 CString COneNetAdapterInfo::Get_DHCPAddr()                                      const { return m_AdptInfo.DhcpServer.IpAddress.String; }\r
227 BOOL COneNetAdapterInfo::Is_Wins_Used()                                         const { return m_AdptInfo.HaveWins; }\r
228 CString COneNetAdapterInfo::Get_PrimaryWinsServer()                     const { return m_AdptInfo.PrimaryWinsServer.IpAddress.String; }\r
229 CString COneNetAdapterInfo::Get_SecondaryWinsServer()           const { return m_AdptInfo.SecondaryWinsServer.IpAddress.String; }\r
230 int     COneNetAdapterInfo::Get_GatewayCount()                                  const { return m_Ary_Gateway.GetSize(); }\r
231 DWORD COneNetAdapterInfo::Get_AdapterIndex()                            const { return m_AdptInfo.Index; }\r
232 UINT COneNetAdapterInfo::Get_AdapterType()                                      const { return m_AdptInfo.Type; }\r
233 \r
234 CString COneNetAdapterInfo::Get_IPAddr ( int nIndex ) const\r
235 {       \r
236         CString csAddr = _T("");\r
237         if ( nIndex >= 0 && nIndex < m_Ary_IP.GetSize() )\r
238         {\r
239         csAddr = m_Ary_IP.GetAt(nIndex).csIP;\r
240         }\r
241         \r
242         return csAddr;\r
243 }\r
244 \r
245 CString COneNetAdapterInfo::Get_Subnet ( int nIndex ) const\r
246\r
247         CString csAddr = _T("");\r
248         if ( nIndex >= 0 && nIndex < m_Ary_IP.GetSize() )\r
249         {\r
250         csAddr = m_Ary_IP.GetAt(nIndex).csSubnet;\r
251         }\r
252         \r
253         return csAddr;\r
254 }\r
255 \r
256 CString COneNetAdapterInfo::Get_DNSAddr ( int nIndex ) const\r
257 {       \r
258         CString csAddr = _T("");\r
259         if ( nIndex >= 0 && nIndex < m_Ary_DNS.GetSize() )\r
260         {\r
261         csAddr = m_Ary_DNS.GetAt(nIndex);\r
262         }\r
263         \r
264         return csAddr;\r
265 }\r
266 \r
267 CString COneNetAdapterInfo::Get_GatewayAddr ( int nIndex ) const\r
268 {       \r
269         CString csAddr = _T("");\r
270         if ( nIndex >= 0 && nIndex < m_Ary_Gateway.GetSize() )\r
271         {\r
272         csAddr = m_Ary_Gateway.GetAt(nIndex);\r
273         }\r
274         \r
275         return csAddr;\r
276 }\r
277 \r
278 void COneNetAdapterInfo::Set_PhysicalAddress ( int nPhysicalAddressLength, BYTE *pPhysicalAddress )\r
279 {\r
280         if ( !pPhysicalAddress ) return;\r
281         m_nPhysicalAddressLength = __min(nPhysicalAddressLength,sizeof(m_PhysicalAddress));\r
282         memcpy ( m_PhysicalAddress, pPhysicalAddress, m_nPhysicalAddressLength );\r
283 }\r
284 \r
285 //////////////////////////////////////////////////////////////////////\r
286 // Construction/Destruction\r
287 //////////////////////////////////////////////////////////////////////\r
288 \r
289 CNetAdapterInfo::CNetAdapterInfo ()\r
290 {\r
291 }\r
292 \r
293 CNetAdapterInfo::~CNetAdapterInfo()\r
294 {\r
295         DeleteAllNetAdapterInfo ();\r
296 }\r
297 \r
298 void CNetAdapterInfo::DeleteAllNetAdapterInfo()\r
299 {\r
300         for ( int i=0; i<m_Ary_NetAdapterInfo.GetSize(); i++ )\r
301         {\r
302                 COneNetAdapterInfo *pOneNetAdapterInfo = (COneNetAdapterInfo*)m_Ary_NetAdapterInfo.GetAt(i);\r
303                 if ( pOneNetAdapterInfo ) delete pOneNetAdapterInfo;\r
304         }\r
305         m_Ary_NetAdapterInfo.RemoveAll ();\r
306 }\r
307 \r
308 //\r
309 // Ã¶¾ÙÍøÂçÊÊÅäÆ÷\r
310 // return : ------------------------------------------------------------\r
311 //      -1      -       Ê§°Ü\r
312 //      >=0     -       ÍøÂçÊÊÅäÆ÷ÊýÁ¿\r
313 //\r
314 int CNetAdapterInfo::EnumNetworkAdapters ()\r
315 {\r
316         DeleteAllNetAdapterInfo ();\r
317 \r
318         IP_ADAPTER_INFO* pAdptInfo      = NULL;\r
319         IP_ADAPTER_INFO* pNextAd        = NULL;\r
320         ULONG ulLen                                     = 0;\r
321         int nCnt                                        = 0;\r
322         \r
323         DWORD dwError = ::GetAdaptersInfo ( pAdptInfo, &ulLen );\r
324         if( dwError != ERROR_BUFFER_OVERFLOW ) return -1;\r
325         pAdptInfo = ( IP_ADAPTER_INFO* )MALLOC ( ulLen );\r
326         dwError = ::GetAdaptersInfo( pAdptInfo, &ulLen );\r
327         if ( dwError != ERROR_SUCCESS ) return -1;\r
328         \r
329         pNextAd = pAdptInfo;\r
330         while( pNextAd )\r
331         {\r
332                 COneNetAdapterInfo *pOneNetAdapterInfo = new COneNetAdapterInfo ( pNextAd );\r
333                 if ( pOneNetAdapterInfo )\r
334                 {\r
335                         m_Ary_NetAdapterInfo.Add ( pOneNetAdapterInfo );\r
336                 }\r
337                 nCnt ++;\r
338                 pNextAd = pNextAd->Next;\r
339         }\r
340         \r
341         // free any memory we allocated from the heap before\r
342         // exit.  we wouldn't wanna leave memory leaks now would we? ;p\r
343         FREE( pAdptInfo );              \r
344         \r
345         return nCnt;\r
346 }\r
347 \r
348 COneNetAdapterInfo* CNetAdapterInfo::Get_OneNetAdapterInfo ( int nIndex )\r
349 {\r
350         if ( nIndex < 0 || nIndex >= m_Ary_NetAdapterInfo.GetSize() )\r
351         {\r
352                 ASSERT ( FALSE );\r
353                 return NULL;\r
354         }\r
355 \r
356         return (COneNetAdapterInfo*)m_Ary_NetAdapterInfo.GetAt(nIndex);\r
357 }\r
358 \r
359 COneNetAdapterInfo* CNetAdapterInfo::Get_OneNetAdapterInfo ( DWORD dwIndex )\r
360 {\r
361         for ( int i=0; i<m_Ary_NetAdapterInfo.GetSize(); i++ )\r
362         {\r
363                 COneNetAdapterInfo *pOneNetAdapterInfo = (COneNetAdapterInfo*)m_Ary_NetAdapterInfo.GetAt(i);\r
364                 if ( pOneNetAdapterInfo && pOneNetAdapterInfo->Get_AdapterIndex() == dwIndex )\r
365                         return pOneNetAdapterInfo;\r
366         }\r
367 \r
368         return NULL;\r
369 }\r
370 \r
371 void CNetAdapterInfo::Refresh ()\r
372 {\r
373         DeleteAllNetAdapterInfo ();\r
374         EnumNetworkAdapters ();\r
375         GetAdapterAddress ();\r
376 }\r
377 \r
378 //\r
379 // »ñÈ¡Íø¿¨µÄµØÖ·ÐÅÏ¢£¬Ö÷ÒªÊÇMACµØÖ·\r
380 //\r
381 BOOL CNetAdapterInfo::GetAdapterAddress ()\r
382 {\r
383         DWORD dwSize = 0;\r
384     DWORD dwRetVal = 0;\r
385         BOOL bRet = FALSE;\r
386 \r
387     int i = 0;\r
388 \r
389     // Set the flags to pass to GetAdaptersAddresses\r
390     ULONG flags = GAA_FLAG_INCLUDE_PREFIX;\r
391 \r
392     // default to unspecified address family (both)\r
393         // AF_INET for IPv4, AF_INET6 for IPv6\r
394     ULONG family = AF_UNSPEC;\r
395 \r
396     LPVOID lpMsgBuf = NULL;\r
397 \r
398     PIP_ADAPTER_ADDRESSES pAddresses = NULL;\r
399     ULONG outBufLen = 0;\r
400 \r
401     PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;\r
402     PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;\r
403     PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;\r
404     PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;\r
405     IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;\r
406     IP_ADAPTER_PREFIX *pPrefix = NULL;\r
407 \r
408     outBufLen = sizeof (IP_ADAPTER_ADDRESSES);\r
409     pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);\r
410     if (pAddresses == NULL) return FALSE;\r
411 \r
412     // Make an initial call to GetAdaptersAddresses to get the \r
413     // size needed into the outBufLen variable\r
414     if ( ::GetAdaptersAddresses ( family, flags, NULL, pAddresses, &outBufLen ) == ERROR_BUFFER_OVERFLOW )\r
415         {\r
416         FREE(pAddresses);\r
417         pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);\r
418     }\r
419     if (pAddresses == NULL) return FALSE;\r
420 \r
421     // Make a second call to GetAdapters Addresses to get the\r
422     // actual data we want\r
423 #ifdef _DEBUG\r
424     TRACE ( _T("Memory allocated for GetAdapterAddresses = %d bytes\n"), outBufLen);\r
425     TRACE ( _T("Calling GetAdaptersAddresses function with family = " ) );\r
426     if (family == AF_INET)\r
427         TRACE(_T("AF_INET\n"));\r
428     if (family == AF_INET6)\r
429         TRACE(_T("AF_INET6\n"));\r
430     if (family == AF_UNSPEC)\r
431         TRACE(_T("AF_UNSPEC\n\n"));\r
432 #endif\r
433     dwRetVal = GetAdaptersAddresses ( family, flags, NULL, pAddresses, &outBufLen );\r
434 \r
435     if ( dwRetVal == NO_ERROR )\r
436         {\r
437         // If successful, output some information from the data we received\r
438         pCurrAddresses = pAddresses;\r
439         while (pCurrAddresses)\r
440                 {\r
441             TRACE(_T("\tLength of the IP_ADAPTER_ADDRESS struct: %ld\n"),\r
442                    pCurrAddresses->Length);\r
443             TRACE(_T("\tIfIndex (IPv4 interface): %u\n"), pCurrAddresses->IfIndex);\r
444             TRACE(_T("\tAdapter name: %s\n"), pCurrAddresses->AdapterName);\r
445 \r
446             pUnicast = pCurrAddresses->FirstUnicastAddress;\r
447             if (pUnicast != NULL)\r
448                         {\r
449                 for (i = 0; pUnicast != NULL; i++)\r
450                     pUnicast = pUnicast->Next;\r
451                 TRACE(_T("\tNumber of Unicast Addresses: %d\n"), i);\r
452             }\r
453                         else\r
454                         {\r
455                 TRACE(_T("\tNo Unicast Addresses\n"));\r
456                         }\r
457 \r
458             pAnycast = pCurrAddresses->FirstAnycastAddress;\r
459             if (pAnycast)\r
460                         {\r
461                 for (i = 0; pAnycast != NULL; i++)\r
462                     pAnycast = pAnycast->Next;\r
463                 TRACE(_T("\tNumber of Anycast Addresses: %d\n"), i);\r
464             }\r
465                         else\r
466                         {\r
467                 TRACE(_T("\tNo Anycast Addresses\n"));\r
468                         }\r
469 \r
470             pMulticast = pCurrAddresses->FirstMulticastAddress;\r
471             if (pMulticast)\r
472                         {\r
473                 for (i = 0; pMulticast != NULL; i++)\r
474                     pMulticast = pMulticast->Next;\r
475                 TRACE(_T("\tNumber of Multicast Addresses: %d\n"), i);\r
476             }\r
477                         else\r
478                         {\r
479                 TRACE(_T("\tNo Multicast Addresses\n"));\r
480                         }\r
481 \r
482             pDnServer = pCurrAddresses->FirstDnsServerAddress;\r
483             if (pDnServer)\r
484                         {\r
485                 for (i = 0; pDnServer != NULL; i++)\r
486                     pDnServer = pDnServer->Next;\r
487                 TRACE(_T("\tNumber of DNS Server Addresses: %d\n"), i);\r
488             }\r
489                         else\r
490                         {\r
491                 TRACE(_T("\tNo DNS Server Addresses\n"));\r
492                         }\r
493 \r
494             TRACE(_T("\tDNS Suffix: %wS\n"), pCurrAddresses->DnsSuffix);\r
495             TRACE(_T("\tDescription: %wS\n"), pCurrAddresses->Description);\r
496             TRACE(_T("\tFriendly name: %wS\n"), pCurrAddresses->FriendlyName);\r
497 \r
498             if (pCurrAddresses->PhysicalAddressLength != 0)\r
499                         {\r
500                 TRACE(_T("\tPhysical address: "));\r
501                 for (i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++)\r
502                                 {\r
503                     if ( i == (int)(pCurrAddresses->PhysicalAddressLength - 1))\r
504                         TRACE(_T("%.2X\n"), (int) pCurrAddresses->PhysicalAddress[i]);\r
505                     else\r
506                         TRACE(_T("%.2X-"), (int) pCurrAddresses->PhysicalAddress[i]);\r
507                 }\r
508 \r
509                                 COneNetAdapterInfo* pNetAdapterInfo = Get_OneNetAdapterInfo ( pCurrAddresses->IfIndex );\r
510                                 if ( pNetAdapterInfo )\r
511                                 {\r
512                                         pNetAdapterInfo->Set_PhysicalAddress ( pCurrAddresses->PhysicalAddressLength, pCurrAddresses->PhysicalAddress );\r
513                                 }\r
514             }\r
515             TRACE(_T("\tFlags: %ld\n"), pCurrAddresses->Flags);\r
516             TRACE(_T("\tMtu: %lu\n"), pCurrAddresses->Mtu);\r
517             TRACE(_T("\tIfType: %ld\n"), pCurrAddresses->IfType);\r
518             TRACE(_T("\tOperStatus: %ld\n"), pCurrAddresses->OperStatus);\r
519             TRACE(_T("\tIpv6IfIndex (IPv6 interface): %u\n"),\r
520                    pCurrAddresses->Ipv6IfIndex);\r
521             TRACE(_T("\tZoneIndices (hex): "));\r
522             for (i = 0; i < 16; i++)\r
523                 TRACE(_T("%lx "), pCurrAddresses->ZoneIndices[i]);\r
524             TRACE(_T("\n"));\r
525 \r
526             pPrefix = pCurrAddresses->FirstPrefix;\r
527             if (pPrefix)\r
528                         {\r
529                 for (i = 0; pPrefix != NULL; i++)\r
530                     pPrefix = pPrefix->Next;\r
531                 TRACE(_T("\tNumber of IP Adapter Prefix entries: %d\n"), i);\r
532             }\r
533                         else\r
534                         {\r
535                 TRACE(_T("\tNumber of IP Adapter Prefix entries: 0\n"));\r
536                         }\r
537 \r
538             TRACE(_T("\n"));\r
539 \r
540             pCurrAddresses = pCurrAddresses->Next;\r
541                         bRet = TRUE;\r
542         }\r
543     }\r
544         else\r
545         {\r
546         TRACE(_T("Call to GetAdaptersAddresses failed with error: %d\n"), dwRetVal );\r
547         if (dwRetVal == ERROR_NO_DATA)\r
548                 {\r
549             TRACE(_T("\tNo addresses were found for the requested parameters\n"));\r
550                 }\r
551         else\r
552                 {\r
553             if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language\r
554                               (LPTSTR) & lpMsgBuf, 0, NULL))\r
555                         {\r
556                 TRACE(_T("\tError: %s"), lpMsgBuf);\r
557                 LocalFree(lpMsgBuf);\r
558                 FREE(pAddresses);\r
559             }\r
560         }\r
561                 bRet = FALSE;\r
562     }\r
563     FREE(pAddresses);\r
564 \r
565         return bRet;\r
566 }\r
567 \r
568 CString MacAddress2String ( int nPhysicalAddressLength, BYTE *pPhysicalAddress )\r
569 {\r
570         ASSERT ( nPhysicalAddressLength >= 6 );\r
571         AfxIsValidAddress(pPhysicalAddress,nPhysicalAddressLength,TRUE);\r
572         CString csMacAddress, csOneCell;\r
573         for ( int i=0; i<nPhysicalAddressLength; i++ )\r
574         {\r
575                 csOneCell.Format ( _T("%.2X"), (int)pPhysicalAddress[i] );\r
576                 if ( !csMacAddress.IsEmpty() ) csMacAddress += _T("-");\r
577                 csMacAddress += csOneCell;\r
578         }\r
579 \r
580         return csMacAddress;\r
581 }