The main API getnetworkparams () and getadaptersinfo () functions enumerate all network adapters in windows, print the IP, gateway, and Mac, and convert the MAC address from hex to string.
/*
Add to project
Iphlpapi. Lib
File
*/
# Include <windows. h>
# Include <iphlpapi. h>
# Include <stdio. h>
# Include <time. h>
Void _ fastcall enumadapterinfo ()
{
// Obtain the address of the local network device
DWORD err;
Pfixed_info pfixedinfo;
DWORD fixedinfosize = 0;
Pip_adapter_info padapterinfo, padapt;
DWORD adapterinfosize;
Pip_addr_string paddrstr;
String pstrip = "";
String pstrgateway = "";
String pstrmac = "";
//
// 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)
{
// Printf ("getnetworkparams sizing failed with error % d/N", err );
Return;
}
}
// Allocate memory from sizing information
If (pfixedinfo = (pfixed_info) globalalloc (gptr, fixedinfosize) = NULL)
{
// Printf ("Memory Allocation Error/N ");
Return;
}
Adapterinfosize = 0;
If (ERR = getadaptersinfo (null, & adapterinfosize ))! = 0)
{
If (Err! = Error_buffer_overflow)
{
// Printf ("getadaptersinfo sizing failed with error % d/N", err );
Return;
}
}
If (padapterinfo = (pip_adapter_info) globalalloc (gptr, adapterinfosize) = NULL)
{
// Printf ("Memory Allocation Error/N ");
Return;
}
// Get actual adapter Information
If (ERR = getadaptersinfo (padapterinfo, & adapterinfosize ))! = 0)
{
// Printf ("getadaptersinfo failed with error % d/N", err );
Return;
}
While (padapterinfo! = NULL)
{
Try
{
// Obtain the IP address and gateway
String IP = padapterinfo-> ipaddresslist. IPaddress. String;
String gateway = padapterinfo-> gatewaylist. IPaddress. String;
//-----------------------------------------
// Convert mac from hex
// Convert hex Mac to string Mac
String Strmac = "";
Char hexmac [mac_length];
Memset (hexmac, 0, mac_length );
// Obtain Mac
Memcpy (hexmac, padapterinfo-> address, mac_length );
// Get the length
Int length = strlen (hexmac );
// Temporary storage Mac
Char Buf [16];
// Cyclically Convert hex to Char
For (INT I = 0; I <length; I ++)
{
Sprintf (BUF, "% 2.2x", hexmac [I]);
If (Strmac. Length ()> 0)
{
Strmac = Strmac + "-";
}
// Add the corresponding Char to the Mac string
Strmac = Strmac + String (BUF [6]);
Strmac = Strmac + String (BUF [7]);
}
// Convert to uppercase
Pstrmac = Strmac. uppercase ();
//-----------------------------------------
// Obtain the IP address
Pstrip = IP;
//-----------------------------------------
// Obtain the Gateway
Pstrgateway = gateway;
Printf ("IP: % s \ n", IP. t_str ());
Printf ("Mac: % s \ n", pstrmac. t_str ());
Printf ("Gateway: % s \ n", pstrgateway. t_str ());
Printf ("------------------ \ n ");
}
Catch (...)
{
}
// No corresponding device found to traverse the next Device
Padapterinfo = padapterinfo-> next;
}
}
Q group 236201801 Discussion
.