API view IP address and Subnet

Source: Internet
Author: User
Tags hex to string

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

.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.