#include <Windows.h>
#include <WinSock2.h>
#include <stdlib.h>
#include <IPHlpApi.h>
#pragma comment(lib, "WS2_32.lib")
#pragma comment(lib, "Iphlpapi.lib")
class CInitSock
{
public:
CInitSock(BYTE minorVer = 2, BYTE majorVer =2);
~CInitSock();
};
CInitSock::CInitSock(BYTE minorVer /* = 2 */, BYTE majorVer /* =2 */)
{
WORD sockVersion = MAKEWORD(minorVer, majorVer);
WSADATA wsaData;
if (::WSAStartup(sockVersion, &wsaData) != 0)
{
exit(0);
}
}
CInitSock::~CInitSock()
{
::WSACleanup();
}
CInitSock initSock;
u_char g_ucLocalMac[6];
DWORD g_dwGatewayIP;
DWORD g_dwLocalIp;
DWORD g_dwMask;
int _tmain(int argc, _TCHAR* argv[])
{
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);
}
}
printf("\n ----------------本地主机信息----------------------\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;
}