source: trunk/testapp/network/iphlpapi/GetAdaptersInfo.c

Last change on this file was 21415, checked in by dmik, 15 years ago

Replaced an iphlpapi test case with a bettter one.

File size: 5.3 KB
Line 
1#include <winsock2.h>
2#include <iphlpapi.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6#include <time.h>
7
8#define errno_t int
9
10int __cdecl main()
11{
12 /* Declare and initialize variables */
13
14// It is possible for an adapter to have multiple
15// IPv4 addresses, gateways, and secondary WINS servers
16// assigned to the adapter.
17//
18// Note that this sample code only prints out the
19// first entry for the IP address/mask, and gateway, and
20// the primary and secondary WINS server for each adapter.
21
22 PIP_ADAPTER_INFO pAdapterInfo;
23 PIP_ADAPTER_INFO pAdapter = NULL;
24 DWORD dwRetVal = 0;
25 UINT i;
26
27/* variables used to print DHCP time info */
28 struct tm *newtime;
29 char buffer[32];
30 errno_t error;
31
32 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
33 pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO));
34 if (pAdapterInfo == NULL) {
35 printf("Error allocating memory needed to call GetAdaptersinfo\n");
36 return 1;
37 }
38// Make an initial call to GetAdaptersInfo to get
39// the necessary size into the ulOutBufLen variable
40 if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
41 free(pAdapterInfo);
42 pAdapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen);
43 if (pAdapterInfo == NULL) {
44 printf("Error allocating memory needed to call GetAdaptersinfo\n");
45 return 1;
46 }
47 }
48
49 if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
50 pAdapter = pAdapterInfo;
51 while (pAdapter) {
52 printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex);
53 printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
54 printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
55 printf("\tAdapter Addr: \t");
56 for (i = 0; i < pAdapter->AddressLength; i++) {
57 if (i == (pAdapter->AddressLength - 1))
58 printf("%.2X\n", (int) pAdapter->Address[i]);
59 else
60 printf("%.2X-", (int) pAdapter->Address[i]);
61 }
62 printf("\tIndex: \t%d\n", pAdapter->Index);
63 printf("\tType: \t");
64 switch (pAdapter->Type) {
65 case MIB_IF_TYPE_OTHER:
66 printf("Other\n");
67 break;
68 case MIB_IF_TYPE_ETHERNET:
69 printf("Ethernet\n");
70 break;
71 case MIB_IF_TYPE_TOKENRING:
72 printf("Token Ring\n");
73 break;
74 case MIB_IF_TYPE_FDDI:
75 printf("FDDI\n");
76 break;
77 case MIB_IF_TYPE_PPP:
78 printf("PPP\n");
79 break;
80 case MIB_IF_TYPE_LOOPBACK:
81 printf("Lookback\n");
82 break;
83 case MIB_IF_TYPE_SLIP:
84 printf("Slip\n");
85 break;
86 default:
87 printf("Unknown type %ld\n", pAdapter->Type);
88 break;
89 }
90
91 printf("\tIP Address: \t%s\n",
92 pAdapter->IpAddressList.IpAddress.String);
93 printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
94
95 printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
96 printf("\t***\n");
97
98 if (pAdapter->DhcpEnabled) {
99 printf("\tDHCP Enabled: Yes\n");
100 printf("\t DHCP Server: \t%s\n",
101 pAdapter->DhcpServer.IpAddress.String);
102
103 printf("\t Lease Obtained: ");
104 /* Display local time */
105 newtime = localtime((time_t*) &pAdapter->LeaseObtained);
106 if (!newtime)
107 printf("Invalid Argument to localtime\n");
108 else {
109 // Convert to an ASCII representation
110 char *str = asctime(newtime);
111 if (!str)
112 printf("Invalid Argument to asctime_s\n");
113 else
114 /* asctime_s returns the string terminated by \n\0 */
115 printf("%s", str);
116 }
117
118 printf("\t Lease Expires: ");
119 newtime = localtime((time_t*) &pAdapter->LeaseExpires);
120 if (!newtime)
121 printf("Invalid Argument to localtime\n");
122 else {
123 // Convert to an ASCII representation
124 char *str = asctime(newtime);
125 if (!str)
126 printf("Invalid Argument to asctime_s\n");
127 else
128 /* asctime_s returns the string terminated by \n\0 */
129 printf("%s", str);
130 }
131 } else
132 printf("\tDHCP Enabled: No\n");
133
134 if (pAdapter->HaveWins) {
135 printf("\tHave Wins: Yes\n");
136 printf("\t Primary Wins Server: %s\n",
137 pAdapter->PrimaryWinsServer.IpAddress.String);
138 printf("\t Secondary Wins Server: %s\n",
139 pAdapter->SecondaryWinsServer.IpAddress.String);
140 } else
141 printf("\tHave Wins: No\n");
142 pAdapter = pAdapter->Next;
143 printf("\n");
144 }
145 } else {
146 printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
147
148 }
149 if (pAdapterInfo)
150 free(pAdapterInfo);
151
152 return 0;
153}
Note: See TracBrowser for help on using the repository browser.