IP Helper Functions
The IP Helper API provides a set of functions that enable applications to retrieve and modify network configuration settings. Below is a list of the most commonly used functions.
| Function | Header | Description | Sample |
|---|---|---|---|
GetAdaptersInfo |
iphlpapi.h |
Retrieves adapter information for the local computer. | View |
GetIfTable |
iphlpapi.h |
Retrieves a table of interface entries. | View |
GetIpForwardTable |
iphlpapi.h |
Retrieves the IP routing table. | View |
FlushIpNetTable |
iphlpapi.h |
Deletes all entries from the ARP table. | View |
SetDHCPOptionInfo |
iphlpapi.h |
Configures DHCP options for a specific scope. | View |
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
int main() {
DWORD dwSize = 0;
GetAdaptersInfo(NULL, &dwSize);
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)malloc(dwSize);
if (GetAdaptersInfo(pAdapterInfo, &dwSize) == NO_ERROR) {
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter) {
printf("Adapter: %s\n", pAdapter->Description);
printf("IP Address: %s\n", pAdapter->IpAddressList.IpAddress.String);
pAdapter = pAdapter->Next;
}
}
free(pAdapterInfo);
return 0;
}
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
int main() {
MIB_IFTABLE *pIfTable = NULL;
DWORD dwSize = 0;
GetIfTable(NULL, &dwSize, FALSE);
pIfTable = (MIB_IFTABLE *)malloc(dwSize);
if (GetIfTable(pIfTable, &dwSize, FALSE) == NO_ERROR) {
for (DWORD i = 0; i < pIfTable->dwNumEntries; i++) {
MIB_IFROW *row = &pIfTable->table[i];
wprintf(L"Interface %d: %s (%u)\n",
i, row->wszName, row->dwOperStatus);
}
}
free(pIfTable);
return 0;
}
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
int main() {
PMIB_IPFORWARDTABLE pTable = NULL;
DWORD dwSize = 0;
GetIpForwardTable(NULL, &dwSize, FALSE);
pTable = (PMIB_IPFORWARDTABLE)malloc(dwSize);
if (GetIpForwardTable(pTable, &dwSize, FALSE) == NO_ERROR) {
for (DWORD i = 0; i < pTable->dwNumEntries; i++) {
MIB_IPFORWARDROW *row = &pTable->table[i];
printf("Dest: %s Mask: %s NextHop: %s\n",
inet_ntoa(*(struct in_addr *)&row->dwForwardDest),
inet_ntoa(*(struct in_addr *)&row->dwForwardMask),
inet_ntoa(*(struct in_addr *)&row->dwForwardNextHop));
}
}
free(pTable);
return 0;
}
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
int main() {
DWORD dwResult = FlushIpNetTable2(NULL);
if (dwResult == NO_ERROR) {
printf("ARP table flushed successfully.\n");
} else {
printf("Failed to flush ARP table. Error: %lu\n", dwResult);
}
return 0;
}
#include <windows.h>
#include <dhcpsapi.h>
#include <stdio.h>
#pragma comment(lib, "dhcpsapi.lib")
int main() {
DHCP_OPTION_DATA optionData = {0};
DHCP_OPTION_VALUE optionValue = {0};
optionValue.OptionID = OPTION_ROUTER_ADDRESS;
optionValue.ValueType = DHCP_BINARY_DATA;
optionValue.Value.BinaryData.Data = (BYTE *)malloc(4);
optionValue.Value.BinaryData.Len = 4;
// Set router IP to 192.168.1.1
optionValue.Value.BinaryData.Data[0] = 192;
optionValue.Value.BinaryData.Data[1] = 168;
optionValue.Value.BinaryData.Data[2] = 1;
optionValue.Value.BinaryData.Data[3] = 1;
optionData.OptionValue = &optionValue;
optionData.NumElements = 1;
DWORD dwRet = DhcpSetOptionInfoV5(
NULL, // Server IP (NULL = local)
L"Scope1", // Scope name
&optionData,
DHCP_OPTION_ROUTER_ADDRESS
);
if (dwRet == ERROR_SUCCESS) {
wprintf(L"Router option set successfully.\n");
} else {
wprintf(L"Failed to set router option. Error: %lu\n", dwRet);
}
free(optionValue.Value.BinaryData.Data);
return 0;
}