Network Management
The Network Management section of the Win32 API provides developers with a set of functions to configure, monitor, and control network interfaces, routes, and protocols on a Windows system. This article covers the most commonly used functions, structures, and best practices.
Key Functions
GetIfTable– Retrieves a list of network interfaces.GetAdaptersAddresses– Provides detailed information about each adapter.AddIPAddress– Assigns a new IP address to a specific interface.DeleteIPAddress– Removes an IP address from an interface.SetIfEntry– Modifies the operational state of an interface.
Example: Enumerating Network Interfaces
#include <windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
int main() {
DWORD size = 0;
GetIfTable(NULL, &size, FALSE); // get required size
MIB_IFTABLE *iftable = (MIB_IFTABLE*)malloc(size);
if (GetIfTable(iftable, &size, FALSE) == NO_ERROR) {
for (DWORD i = 0; i < iftable->dwNumEntries; ++i) {
MIB_IFROW *row = &iftable->table[i];
wprintf(L"%s (Index: %lu) - %lu bytes sent\\n",
row->wszName, row->dwIndex, row->dwOutOctets);
}
}
free(iftable);
return 0;
}
This snippet demonstrates how to retrieve and display basic information about each network interface using GetIfTable.
Best Practices
- Always check the return value of API calls for
ERROR_INSUFFICIENT_BUFFERand allocate the necessary memory. - Prefer
GetAdaptersAddressesover older functions for IPv6 compatibility. - When modifying interface state, ensure you have administrative privileges.
- Use the
SetIfEntryfunction carefully, as disabling an adapter can affect system connectivity.
Comments (2)
GetAdaptersAddressesas well.