#include <windows.h>
#include <stdio.h>
#include <Iphlpapi.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "WS2_32.lib")
u_char g_ucLocalMac[6];
DWORD g_dwGatewayIP;
DWORD g_dwLocalIP;
DWORD g_dwMask;
BOOL GetGloballData()
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG ulLen = 0;
::GetAdaptersInfo(pAdapterInfo, &ulLen);
pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulLen);
if (::GetAdaptersInfo(pAdapterInfo, &ulLen) == ERROR_SUCCESS)
{
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);
}
}
cout << "-------- local workstation information ------------" << endl;
in_addr in;
in.S_un.S_addr = g_dwLocalIP;
cout << "IP address: " << ::inet_ntoa(in) << endl;
in.S_un.S_addr = g_dwMask;
cout << "subnet mask: " << ::inet_ntoa(in) << endl;
in.S_un.S_addr = g_dwGatewayIP;
cout << "default gataway: " << ::inet_ntoa(in) << endl;
u_char* p = g_ucLocalMac;
char str[100] = "/0";
sprintf(str, "%02x-%02x-%02x-%02x-%02x-%02x", p[0], p[1], p[2], p[3], p[4], p[5]);
cout << "mac address: " << str << endl;
return TRUE;
}
int main()
{
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0)
{
cout << "initlization failed!" << endl;
exit(0);
}
GetGloballData();
WSACleanup();
}