네트워크 정보 얻어오는 방법
2001/10/26 조경민 bro@shinbiro.com
====================================================================
1. MAC Address 그냥 보는 방법
C:\>arp -a

Interface: 211.174.252.118 on Interface 0x1000003
  Internet Address      Physical Address      Type
  211.174.252.1         00-90-27-2b-ec-80     dynamic
  211.174.252.54        00-c0-26-a1-2a-9a     dynamic


2. GetNetworkParams를 쓰면 네트워크 정보를 얻어올 수 있다.
게이트웨이 Wins , proxy사용여부 등등 설정 값 얻어올수있음.
(98 이상 플랫폼SDK필요)

GetAdaptersInfo를 쓰면 MAC어드레드 등을 얻어올수있음

DWORD GetAdaptersInfo(
  PIP_ADAPTER_INFO pAdapterInfo,  // buffer to receive data
  PULONG pOutBufLen               // size of data returned
);

pAdapterInfo안에
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]
Specifies the hardware address for the adapter.
가 MAC 어드레스로  6자리의 바이트의 하나씩 주소다
00.00.00.00.00.00

--------------------------------------------------

IP_INTERFACE_INFO ifTable;

if ( GetInterfaceInfo(&ifTable, sizeof(ifTable)) != NO_ERROR )
{
    OutputDebugString("Fail..\r\n");
    return FALSE;
}

CString strDebug;

strDebug.Format("NumAdapters Number : %d\r\n", ifTable.NumAdapters);
OutputDebugString(strDebug);

for ( INT i=0 ; i<ifTable.NumAdapters ; i++ )
{
    strDebug.Format("[%d] %s\r\n", Adapter[i].Index, Adapter[i].Name);
}

--------------------------------------------------
3. NIC 카드 랜카드 이름 얻기
source code from 진호
void CNetworkDlg::OnIphlpapiButton()
{
    DWORD Err;

    PFIXED_INFO pFixedInfo;
    DWORD FixedInfoSize = 0;

    PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
    DWORD AdapterInfoSize;
    PIP_ADDR_STRING pAddrStr;
        CString strResult,strTemp;

    //
    // Get the main IP configuration information for this machine using a FIXED_INFO structure
    //
    if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {
            AfxMessageBox("GetNetworkParams sizing failed with error\n");
            return;
        }
    }

    // Allocate memory from sizing information
    if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL)
    {
        AfxMessageBox("Memory allocation error\n");
        return;
    }

    if ((Err = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0)
    {
                strTemp.Format("\tHost Name . . . . . . . . . : \t%s\n", pFixedInfo->HostName);
                strResult=strTemp;
        strTemp.Format("\tDNS Servers . . . . . . . . : \t%s\n", pFixedInfo->DnsServerList.IpAddress.String);
                strResult+=strTemp;
        pAddrStr = pFixedInfo->DnsServerList.Next;
        while(pAddrStr)
        {
            strTemp.Format("\t\t\t\t%s\n", pAddrStr->IpAddress.String);
                        strResult+=strTemp;
            pAddrStr = pAddrStr->Next;
        }

        strTemp.Format("\tNode Type . . . . . . . . . : ");
                strResult+=strTemp;
        switch (pFixedInfo->NodeType)
        {
            case 1:
                strTemp.Format("%s\n", "Broadcast");
                break;
            case 2:
                strTemp.Format("%s\n", "Peer to peer");
                break;
            case 4:
                strTemp.Format("%s\n", "Mixed");
                break;
            case 8:
                strTemp.Format("%s\n", "Hybrid");
                break;
            default:
                strTemp.Format("\n");
        }
                strResult+=strTemp;

        strTemp.Format("\tNetBIOS Scope ID. . . . . . : %s\n", pFixedInfo->ScopeId);
                strResult+=strTemp;
        strTemp.Format("\tIP Routing Enabled. . . . . : %s\n", (pFixedInfo->EnableRouting ? "yes" : "no"));
                strResult+=strTemp;
        strTemp.Format("\tWINS Proxy Enabled. . . . . : %s\n", (pFixedInfo->EnableProxy ? "yes" : "no"));
                strResult+=strTemp;
        strTemp.Format("\tNetBIOS Resolution Uses DNS : %s\n", (pFixedInfo->EnableDns ? "yes" : "no"));
                strResult+=strTemp;
    } else
    {
        strTemp.Format("GetNetworkParams failed with error %d\n", Err);
                strResult+=strTemp;
        return;
    }

    //
    // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure.
    // Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.
    //
    AdapterInfoSize = 0;
    if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {
            AfxMessageBox("GetAdaptersInfo sizing failed with error %d\n", Err);
            return;
        }
    }

    // Allocate memory from sizing information
    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
    {
        AfxMessageBox("Memory allocation error\n");
        return;
    }

    // Get actual adapter information
    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
    {
        AfxMessageBox("GetAdaptersInfo failed with error %d\n", Err);
        return;
    }

    pAdapt = pAdapterInfo;

    while (pAdapt)
    {
        switch (pAdapt->Type)
        {
            case MIB_IF_TYPE_ETHERNET:
                strTemp.Format("\nEthernet adapter ");
                break;
            case MIB_IF_TYPE_TOKENRING:
                strTemp.Format("\nToken Ring adapter ");
                break;
            case MIB_IF_TYPE_FDDI:
                strTemp.Format("\nFDDI adapter ");
                break;
            case MIB_IF_TYPE_PPP:
                strTemp.Format("\nPPP adapter ");
                break;
            case MIB_IF_TYPE_LOOPBACK:
                strTemp.Format("\nLoopback adapter ");
                break;
            case MIB_IF_TYPE_SLIP:
                printf("\nSlip adapter ");
                break;
            case MIB_IF_TYPE_OTHER:
            default:
                strTemp.Format("\nOther adapter ");
        }
                strResult+=strTemp;
        strTemp.Format("%s:\n\n", pAdapt->AdapterName);
                strResult+=strTemp;

        strTemp.Format("\tDescription . . . . . . . . : %s\n", pAdapt->Description);
                strResult+=strTemp;

        strTemp.Format("\tPhysical Address. . . . . . : ");
                strResult+=strTemp;
       for (UINT i=0; i<pAdapt->AddressLength; i++)
        {
            if (i == (pAdapt->AddressLength - 1))
                strTemp.Format("%.2X\n",(int)pAdapt->Address[i]);
            else
                strTemp.Format("%.2X-",(int)pAdapt->Address[i]);
                        strResult+=strTemp;
                }        

        strTemp.Format("\tDHCP Enabled. . . . . . . . : %s\n", (pAdapt->DhcpEnabled ? "yes" : "no"));
                   strResult+=strTemp;

        pAddrStr = &(pAdapt->IpAddressList);
        while(pAddrStr)
        {
            strTemp.Format("\tIP Address. . . . . . . . . : %s\n", pAddrStr->IpAddress.String);
                       strResult+=strTemp;
            strTemp.Format("\tSubnet Mask . . . . . . . . : %s\n", pAddrStr->IpMask.String);
                       strResult+=strTemp;
            pAddrStr = pAddrStr->Next;
        }

        strTemp.Format("\tDefault Gateway . . . . . . : %s\n", pAdapt->GatewayList.IpAddress.String);
                   strResult+=strTemp;
        pAddrStr = pAdapt->GatewayList.Next;
        while(pAddrStr)
        {
            strTemp.Format("%52s\n", pAddrStr->IpAddress.String);
                       strResult+=strTemp;
                pAddrStr = pAddrStr->Next;
        }

        strTemp.Format("\tDHCP Server . . . . . . . . : %s\n", pAdapt->DhcpServer.IpAddress.String);
                   strResult+=strTemp;
            strTemp.Format("\tPrimary WINS Server . . . . : %s\n", pAdapt->PrimaryWinsServer.IpAddress.String);
                   strResult+=strTemp;
        strTemp.Format("\tSecondary WINS Server . . . : %s\n", pAdapt->SecondaryWinsServer.IpAddress.String);
                   strResult+=strTemp;

        struct tm *newtime;

        // Display coordinated universal time - GMT
        newtime = gmtime(&pAdapt->LeaseObtained);  
        strTemp.Format( "\tLease Obtained. . . . . . . : %s", asctime( newtime ) );
                   strResult+=strTemp;

        newtime = gmtime(&pAdapt->LeaseExpires);  
        strTemp.Format( "\tLease Expires . . . . . . . : %s", asctime( newtime ) );
                   strResult+=strTemp;

        pAdapt = pAdapt->Next;
    }
        AfxMessageBox(strResult);
}

+ Recent posts