#include <windows.h>
#include <iostream>
#include <stdio.h>
#include "Iphlpapi.h"
#pragma comment(lib,"Iphlpapi")
#pragma comment(lib,"WS2_32")
using namespace std;
u_char g_ucLocalMac[6];
DWORD g_dwGatewayIP;
DWORD g_dwLocalIP;
DWORD g_dwMask;
BOOL GetGlobalData()
{
ZeroMemory(g_ucLocalMac,6);
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<<"--------------输出本机IP地址----------"<<endl<<endl;
in_addr in;
in.S_un.S_addr=g_dwLocalIP;
cout<<" IP地址: "<<inet_ntoa(in)<<endl;
in.S_un.S_addr=g_dwMask;
cout<<"子网掩码:"<<inet_ntoa(in)<<endl;
in.S_un.S_addr=g_dwGatewayIP;
cout<<" 网关号:"<<inet_ntoa(in)<<endl;
/*
u_char *p = g_ucLocalMac;
printf("MAC地址 : %02X-%02X-%02X-%02X-%02X-%02X \n", p[0], p[1], p[2], p[3], p[4], p[5]);
*/
u_char *p = g_ucLocalMac;
cout<<"MAC地址:"<<hex <<setw(2)<< *(BYTE*)&p[0] <<'-'<<*(BYTE*)&p[1]<<'-'<<*(BYTE*)&p[2]<<'-'<<*(BYTE*)&p[3]<<'-'<<*(BYTE*)&p[4]<<'-'<<*(BYTE*)&p[5]<<endl;
return TRUE;
}
评论