Get local network connection adapter information specific Code _c language

Source: Internet
Author: User

The effect is as follows:

The specific code is as follows:

Copy Code code as follows:

#include <Windows.h>
#include <IPHlpApi.h>
#include <stdio.h>

#pragma comment (lib, "Iphlpapi")
#pragma comment (lib, "Ws2_32")

int main (int argc, char **argv)
{
Pip_adapter_info padapterinfo = NULL;
ULONG Ullen = sizeof (Ip_adapter_info);
struct TM NewTime;
Char szbuffer[32];
errno_t error;

   //Request memory for Adapter structure
   //padapterinfo = (pip_adapter_info) GlobalAlloc (gptr, Ullen );
    padapterinfo = (pip_adapter_info) HeapAlloc (GetProcessHeap (), 0, sizeof (ip_adapter_info));
    if (NULL = padapterinfo)
    {
         printf ("Error allocating memory needed to call getadaptersinfo.\n");
        return 1;
   }

    if (Error_buffer_overflow = GetAdaptersInfo (Padapterinfo, &ullen))
    {
        HeapFree (GetProcessHeap (), 0, Padapterinfo);
        padapterinfo = (pip_adapter_info) HeapAlloc (GetProcessHeap (), 0, Ullen);
        if (NULL = padapterinfo)
         {
            printf ("Error allocating memory needed to call getadaptersinfo.\n ");
            return 1;
       }
   }

Get Local Adapter structure information
if (error_success!= getadaptersinfo (Padapterinfo, &ullen))
{
printf ("GetAdaptersInfo error!\n");
return 0;
}
if (NULL = = Padapterinfo)
{
printf ("There is no adapters!\n");
return 0;
}

Setconsoletitle (TEXT ("Local Network Connection adapter information");

Todo
{
printf ("comboindex:%d\n", Padapterinfo->comboindex);
printf ("Adapter name:%s\n", padapterinfo->adaptername);
printf ("Adapter desc:%s\n", padapterinfo->description);
printf ("Adapter Addr:");
for (size_t i = 0; i < padapterinfo->addresslength; i++)
{
if (i = = (padapterinfo->addresslength-1))
{
printf ("%02x", (int) padapterinfo->address[i]);
}
Else
{
printf ("%02x-", (int) padapterinfo->address[i]);
}
}
printf ("\ n");
printf ("index:%d\n", Padapterinfo->index);
printf ("Type:");
Switch (padapterinfo->type)
{
Case mib_if_type_other:printf ("other\n"); Break
Case mib_if_type_ethernet:printf ("ethernet\n"); Break
Case mib_if_type_tokenring:printf ("Token ring\n"); Break
Case mib_if_type_fddi:printf ("fddi\n"); Break
Case mib_if_type_ppp:printf ("ppp\n"); Break
Case mib_if_type_loopback:printf ("lookback\n"); Break
Case mib_if_type_slip:printf ("slip\n"); Break
default:printf ("Unknow type%ld\n", Padapterinfo->type); Break
}
printf ("IP address:%s\n", padapterinfo->ipaddresslist.ipaddress.string);
printf ("IP mask:%s\n", padapterinfo->ipaddresslist.ipmask.string);
printf ("gateway:%s\n", padapterinfo->gatewaylist.ipaddress.string);

if (padapterinfo->dhcpenabled)
{
printf ("DHCP enabled:yes\n");
printf ("DHCP server:%s\n", padapterinfo->dhcpserver.ipaddress.string);
printf ("Lease obtained:");
Error = _localtime32_s (&newtime, (__time32_t*) &padapterinfo->leaseobtained);
if (Error)
{
printf ("Invalid Argument to _localtime32_s.\n");
}
Else
{
Error = asctime_s (Szbuffer, &newtime);
if (Error)
{
printf ("Invalid Argument to asctime_s.\n");
}
Else
{
printf ("%s", szbuffer);
}
}

printf ("Lease Expires:");
Error = _localtime32_s (&newtime, (__time32_t*) &padapterinfo->leaseexpires);
if (Error)
{
printf ("Invalid Argument to _localtime32_s.\n");
}
Else
{
Error = asctime_s (Szbuffer, &newtime);
if (Error)
{
printf ("Invalid Argument to asctime_s.\n");
}
Else
{
printf ("%s", szbuffer);
}
}
}
Else
{
printf ("DHCP enabled:no\n");
}

if (padapterinfo->havewins)
{
printf ("Have wins:yes\n");
printf ("Primary Wins server:%s\n", padapterinfo->primarywinsserver.ipaddress.string);
printf ("Secondary Wins server:%s\n", padapterinfo->secondarywinsserver.ipaddress.string);
}
Else
{
printf ("Have wins:no\n");
}

printf ("=================================================================\n");

Padapterinfo = padapterinfo->next;
while (Padapterinfo);

if (padapterinfo)
{
HeapFree (GetProcessHeap (), 0, Padapterinfo);
}

return 0;
}

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.