GetAdaptersInfoでネットワークアダプタ情報を取得します。
#include <stdio.h> #include <tchar.h> #include <iostream> #include <fstream> #include <winsock2.h> #include <iphlpapi.h> #include <windows.h> // ライブラリ #pragma comment( lib, "Iphlpapi.lib" ) /* ファイルサイズの取得 */ int _tmain ( int argc , _TCHAR* argv[] ) { // 標準出力にユニコードを表示できるようにする setlocale( LC_ALL, "Japanese" ); ULONG qOutBufLen = sizeof( IP_ADAPTER_INFO ); // メモリの確保 IP_ADAPTER_INFO* pIpAdapterInfo = (IP_ADAPTER_INFO*)malloc( qOutBufLen ); if ( NULL == pIpAdapterInfo ) { // メモリ不足 return( -1 ); } // アダプター情報の取得 DWORD dwResult = ::GetAdaptersInfo( pIpAdapterInfo, &qOutBufLen ); if ( NO_ERROR != dwResult ) { // バッファ不足 if ( ERROR_BUFFER_OVERFLOW == dwResult ) { // メモリ解放 free( pIpAdapterInfo ); // メモリ確保 pIpAdapterInfo = (IP_ADAPTER_INFO*)malloc( qOutBufLen ); if ( NULL == pIpAdapterInfo ) { // メモリ不足 return( -1 ); } else { // アダプター情報の取得 dwResult = ::GetAdaptersInfo( pIpAdapterInfo, &qOutBufLen ); } } } if ( NO_ERROR == dwResult ) { IP_ADAPTER_INFO* pAdapter = pIpAdapterInfo; while( pAdapter ) { std::wcout << L"ComboIndex = " << pAdapter->ComboIndex << std::endl; std::wcout << L"AdapterName = " << pAdapter->AdapterName << std::endl; std::wcout << L"Description = " << pAdapter->Description << std::endl; std::wcout << L"Address = "; for ( int nI = 0; nI < (int)pAdapter->AddressLength; nI++ ) { if ( nI == ( pAdapter->AddressLength - 1 ) ) { wprintf( L"%.2X¥n", (int)pAdapter->Address[ nI ] ); } else { wprintf( L"%.2X-", (int)pAdapter->Address[ nI ] ); } } std::wcout << L"Type = "; switch( pAdapter->Type ) { case MIB_IF_TYPE_OTHER: std::wcout << L"MIB_IF_TYPE_OTHER" << std::endl; break; case MIB_IF_TYPE_ETHERNET: std::wcout << L"MIB_IF_TYPE_ETHERNET" << std::endl; break; case MIB_IF_TYPE_TOKENRING: std::wcout << L"MIB_IF_TYPE_TOKENRING" << std::endl; break; case MIB_IF_TYPE_FDDI: std::wcout << L"MIB_IF_TYPE_FDDI" << std::endl; break; case MIB_IF_TYPE_PPP: std::wcout << L"MIB_IF_TYPE_PPP" << std::endl; break; case MIB_IF_TYPE_LOOPBACK: std::wcout << L"MIB_IF_TYPE_LOOPBACK" << std::endl; break; case MIB_IF_TYPE_SLIP: std::wcout << L"MIB_IF_TYPE_SLIP" << std::endl; break; default: std::wcout << L"Unknown type( " << pAdapter->Type << L" )" << std::endl; break; break; } std::wcout << L"IpAddress = " << pAdapter->IpAddressList.IpAddress.String << std::endl; std::wcout << L"GatewayList = " << pAdapter->GatewayList.IpAddress.String << std::endl; std::wcout << L"DhcpEnabled = " << pAdapter->DhcpEnabled << std::endl; if ( FALSE != pAdapter->DhcpEnabled ) { CHAR caBuffer[ 32 ]; struct tm newtime; errno_t error; std::wcout << L"DhcpServer = " << pAdapter->DhcpServer.IpAddress.String << std::endl; std::wcout << L"LeaseObtained = "; error = _localtime32_s( &newtime, (__time32_t*) &pAdapter->LeaseObtained ); if ( FALSE == error ) { error = asctime_s( caBuffer, 32, &newtime ); if ( FALSE == error ) { std::wcout << caBuffer; } } std::wcout << L"LeaseExpires = "; error = _localtime32_s( &newtime, (__time32_t*) &pAdapter->LeaseExpires ); if ( FALSE == error ) { error = asctime_s( caBuffer, 32, &newtime ); if ( FALSE == error ) { std::wcout << caBuffer; } } } else { } std::wcout << L"HaveWins = " << pAdapter->HaveWins << std::endl; if ( FALSE != pAdapter->HaveWins ) { std::wcout << L"PrimaryWinsServer = " << pAdapter->PrimaryWinsServer.IpAddress.String << std::endl; std::wcout << L"SecondaryWinsServer = " << pAdapter->SecondaryWinsServer.IpAddress.String << std::endl; } std::wcout << std::endl; pAdapter = pAdapter->Next; } } // メモリ解放 free( pIpAdapterInfo ); // 正常終了 return( 0 ); }
ComboIndex = 2 AdapterName = {E40FE84B-D7F3-4E0E-B122-724F561A5CD8} Description = Atheros AR8121/AR8113/AR8114 PCI-E Ethernet Controller Address = 00-11-22-33-44-55 Type = MIB_IF_TYPE_ETHERNET IpAddress = 192.168.0.13 GatewayList = 192.168.0.11 DhcpEnabled = 1 DhcpServer = 192.168.0.11 LeaseObtained = Tue Aug 26 20:14:13 2014 LeaseExpires = Wed May 13 15:39:44 1970 HaveWins = 0 ComboIndex = 8 AdapterName = {828D80B6-2D16-44D1-99FD-A3CA65FF9272} Description = VMware Virtual Ethernet Adapter for VMnet1 Address = 00-11-22-33-44-66 Type = MIB_IF_TYPE_ETHERNET IpAddress = 192.168.32.11 GatewayList = 0.0.0.0 DhcpEnabled = 0 HaveWins = 0 ComboIndex = 9 AdapterName = {927D2471-2673-414B-9D9B-62E3EBF7782D} Description = VMware Virtual Ethernet Adapter for VMnet8 Address = 00-11-22-33-44-77 Type = MIB_IF_TYPE_ETHERNET IpAddress = 192.168.17.11 GatewayList = 0.0.0.0 DhcpEnabled = 0 HaveWins = 0