Watt-32 tcp/ip  2.2 dev-rel.10
ip_info.c
1 #define WIN32_LEAN_AND_MEAN
2 #define _WIN32_WINNT 0x0501
3 
4 /*
5  * MingW:
6  * gcc -Wall ip_info.c -o ip_info.exe -lws2_32 -liphlpapi
7  *
8  * MSVC:
9  * cl -nologo -W3 ip_info.c -Feip_info.exe ws2_32.lib iphlpapi.lib
10  *
11  * OpenWatcom:
12  * wcl386 -q -w3 -bt=nt ip_info.c ws2_32.lib iphlpapi.lib
13  */
14 
15 #include <stdio.h>
16 #include <malloc.h>
17 #include <windows.h>
18 #include <winsock2.h>
19 #include <iptypes.h>
20 #include <iphlpapi.h>
21 
22 #ifndef GAA_FLAG_INCLUDE_ALL_INTERFACES
23 #define GAA_FLAG_INCLUDE_ALL_INTERFACES 0x0100L
24 #endif
25 
26 /*
27  * Convert a wide-char string to UTF-8.
28  */
29 static const char *utf8_str (const wchar_t *str)
30 {
31  static char buf[100];
32  DWORD len = WideCharToMultiByte (CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
33 
34  if (len == 0)
35  _snprintf (buf, sizeof(buf), "WideCharToMultiByte() failed: %ld", GetLastError());
36  return (buf);
37 }
38 
39 
40 void print_GetAdaptersAddresses (void)
41 {
42  DWORD rc = 0;
43  ULONG family = AF_UNSPEC;
44  ULONG flags = GAA_FLAG_INCLUDE_ALL_INTERFACES;
45  ULONG outBufLen = 0;
46  IP_ADAPTER_ADDRESSES *addr;
47 
48  printf ("Information from GetAdaptersAddresses():\n");
49 
50  rc = GetAdaptersAddresses (family, flags, NULL, NULL, &outBufLen);
51  if (rc != ERROR_BUFFER_OVERFLOW)
52  return;
53 
54  addr = alloca (outBufLen);
55  rc = GetAdaptersAddresses (family, flags, NULL, addr, &outBufLen);
56  if (rc != NO_ERROR)
57  {
58  printf ("\t\tError: %lu\n",rc);
59  return;
60  }
61 
62  while (addr)
63  {
64  printf ("\tAdapter name: %s\n", addr->AdapterName);
65  printf ("\t Description: %s\n", utf8_str(addr->Description));
66  printf ("\t Friendly name: %s\n", utf8_str(addr->FriendlyName));
67  addr = addr->Next;
68  }
69 }
70 
71 int main(void)
72 {
73  print_GetAdaptersAddresses();
74  return (0);
75 }
int main(int argc, char **argv)
Definition: echo.c:223