<<

Winsock Programmer's FAQ
Examples: Get Interface Information

>>

This example shows how to get the computer's network interface list, along with associated information like the broadcast addresses, the netmasks, and some of the interface's capabilities. This program is actually a semi-useful tool, sort of like an output-only version of the Unix ifconfig program. However, its output is significantly more readable.

This program requires Winsock 2. It works under Windows NT SP3 and its successors. I've tried it under Windows 98, and it sort of works, but many fields are not implemented properly (e.g. the interface netmasks). This program reportedly does not work on Windows 95, even with the Winsock 2 updates.

There are two articles in the MS Knowledge Base describing problems with this mechanism: Q181520 and Q170642.


getifaces.cpp

// Borland C++ 5.0: bcc32.cpp getifaces.cpp ws2_32.lib
// Visual C++ 5.0: cl getifaces.cpp ws2_32.lib 
//
// This sample program is hereby placed in the public domain.

#include <iostream.h>
#include <winsock2.h>
#include <ws2tcpip.h>

int doit()
{
    SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
    if (sd == SOCKET_ERROR) {
        cerr << "Failed to get a socket. Error " << WSAGetLastError() <<
            endl; return 1;
    }

    INTERFACE_INFO InterfaceList[20];
    unsigned long nBytesReturned;
    if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
			sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) {
        cerr << "Failed calling WSAIoctl: error " << WSAGetLastError() <<
				endl;
		return 1;
    }

    int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
    cout << "There are " << nNumInterfaces << " interfaces:" << endl;
    for (int i = 0; i < nNumInterfaces; ++i) {
        cout << endl;

        sockaddr_in *pAddress;
        pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress);
        cout << " " << inet_ntoa(pAddress->sin_addr);

        pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress);
        cout << " has bcast " << inet_ntoa(pAddress->sin_addr);

        pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask);
        cout << " and netmask " << inet_ntoa(pAddress->sin_addr) << endl;

        cout << " Iface is ";
        u_long nFlags = InterfaceList[i].iiFlags;
        if (nFlags & IFF_UP) cout << "up";
        else                 cout << "down";
        if (nFlags & IFF_POINTTOPOINT) cout << ", is point-to-point";
        if (nFlags & IFF_LOOPBACK)     cout << ", is a loopback iface";
        cout << ", and can do: ";
        if (nFlags & IFF_BROADCAST) cout << "bcast ";
        if (nFlags & IFF_MULTICAST) cout << "multicast ";
        cout << endl;
    }

    return 0;
}

int main()
{
    WSADATA WinsockData;
    if (WSAStartup(MAKEWORD(2, 2), &WinsockData) != 0) {
        cerr << "Failed to find Winsock 2.2!" << endl;
        return 2;
    }

    int nRetVal = doit();

    WSACleanup();

    return nRetVal;
}



<< Get the Local IP Address(es)
Get MAC Address, NetBIOS Method >>

Updated Sat Jun 11 2005 00:32 MDT   Go to my home page