|
#include <Windows.h>
#include <stdio.h>
#include "Iphlpapi.h" // Contains the definition of the IP helper function
#pragma comment (lib, "Iphlpapi.lib")
#pragma comment (lib, "WS2_32.lib")
// Global data
u_char g_ucLocalMac [6]; // Local MAC address
DWORD g_dwGatewayIP; // Gateway IP address
DWORD g_dwLocalIP; // Local IP address
DWORD g_dwMask; // Subnet mask
BOOL GetGlobalData ()
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulLen = 0;
// Apply memory for the adapter structure
:: GetAdaptersInfo (pAdapterInfo,&ulLen);
pAdapterInfo = (PIP_ADAPTER_INFO) :: GlobalAlloc (GPTR, ulLen);
// Get local adapter structure information
if (:: GetAdaptersInfo (pAdapterInfo,&ulLen) == ERROR_SUCCESS)
{
Ranch
if (pAdapterInfo! = NULL)
{
memcpy (g_ucLocalMac, pAdapterInfo-> Address, 6);
g_dwGatewayIP = :: inet_addr (pAdapterInfo-> GatewayList.IpAddress.String);
g_dwLocalIP = :: inet_addr (pAdapterInfo-> IpAddressList.IpAddress.String);
g_dwMask = :: inet_addr (pAdapterInfo-> IpAddressList.IpMask.String);
}
}
printf ("\n ------------------------------- Local host information ------------ ------------\n\n ");
in_addr in;
in.S_un.S_addr = g_dwLocalIP;
printf ("IP Address:% s\n", :: inet_ntoa (in));
in.S_un.S_addr = g_dwMask;
printf ("Subnet Mask:% s\n", inet_ntoa (in));
in.S_un.S_addr = g_dwGatewayIP;
printf ("Default Gateway:% s\n", :: inet_ntoa (in));
u_char * p = g_ucLocalMac;
printf ("MAC Address:% 02x-% 02x-% 02x-% 02x-% 02x-% 02x\n", p [0],
p [1], p [2], p [3], p [4], p [5]);
printf ("\n\n");
return true;
}
int main ()
{
GetGlobalData ();
system ("pause");
return 0;
}
The result is:
------------------------------- Local host information ---------------- ---
IP Address: 0.0.0.0
Subnet Mask: 0.0.0.0
Default Gateway: 0.0.0.0
MAC Address: 00-ff-da-51-62-0a
Please press any key to continue ...
Why are some running results all zeros? This program is an example in the book. Master friends help me see? |
|