主机名、域名、IP、MAC地址的获取
时间:2019-07-04 13:00:01
[导读]GetPCInfo类头文件: 1 #pragma once
2 #include3 #include4 #include5 #include6 #include7 #pragma comment(
GetPCInfo类头文件:
1 #pragma once
2 #include3 #include4 #include5 #include6 #include7 #pragma comment(lib, "IPHLPAPI.lib")
8 #pragma comment(lib, "ws2_32.lib")
9 //电脑相关信息的结构体
10 typedef struct MAC_INFO
11 {
12 char ipAddr[16];//IP地址
13 char macAddr[32];//MAC地址
14 char hostName[MAX_PATH];//主机名
15 char domain[MAX_PATH];//域名
16 char time[20];//时间
17
18 }MacInfo;
19 class GetPCInfos
20 {
21 public:
22 GetPCInfos();
23 ~GetPCInfos(void);
24 MacInfo GetPcInfo();
25 void GetIpAddr();//获取IP地址
26 void GetHostName();//获取主机名
27 void GetDomain();//获取域名
28 void GetTime();//获取当前时间
29 void GetMacAddr();//获取Mac地址
30 bool SaveFile();//保存文件
31 std::string GetFilePath();//获取保存文件路径
32 bool GetMacByGetAdaptersInfo(char* macOut);//获取Mac地址
33 private:
34 MacInfo m_MacInfo;//电脑信息
35 std::string m_FileName;//文件名
36 WSADATA wsa;
37 WORD wVersionRequested;
38 };
CPP文件
1 #include "StdAfx.h"
2 #include "GetPCInfos.h"
3
4 GetPCInfos::GetPCInfos()
5 {
6 memset(m_MacInfo.ipAddr, ' ', 16);
7 memset(m_MacInfo.macAddr, ' ', 32);
8 memset(m_MacInfo.hostName, ' ', MAX_PATH);
9 memset(m_MacInfo.domain, ' ' ,20);
10 memset(m_MacInfo.time, ' ', 20);
11 if(WSAStartup(wVersionRequested , &wsa) !=0 )
12 AfxMessageBox("加载套接字库失败");
13
14 }
15
16 GetPCInfos::~GetPCInfos(void)
17 {
18 }
19
20 /*****************************************
21 函 数 名:GetPcInfo
22 功能描述:获取电脑信息
23 输 入:无
24 输 出:无
25 返 回 值:MacInfo结构体。保存有电脑相关信息
26 *****************************************/
27 MacInfo GetPCInfos::GetPcInfo()
28 {
29 GetIpAddr();
30 GetMacAddr();
31 GetHostName();
32 GetDomain();
33 GetTime();
34 wVersionRequested = MAKEWORD( 2, 0 );
35 return m_MacInfo;
36 }
37
38 /*****************************************
39 函 数 名:GetHostName
40 功能描述:获取主机名
41 输 入:无
42 输 出:无
43 返 回 值:void
44 *****************************************/
45 void GetPCInfos::GetHostName()
46 {
47 struct hostent* pHost;
48 pHost = gethostbyname("");
49
50 std::string host;
51 host += pHost->h_name;
52 std::string domain=host.substr(0, host.find_first_of('.'));
53
54 memcpy(m_MacInfo.hostName, domain.c_str(), MAX_PATH);
55
56 }
57
58 /*****************************************
59 函 数 名:GetDomain
60 功能描述:获取域名
61 输 入:无
62 输 出:无
63 返 回 值:void
64 void GetPCInfos::GetDomain()
65 {
66 struct hostent * pHost;
67 pHost = gethostbyname("");
68 //pHost->h_name存有完整的域名信息xx.soft.com
69
70 std::string host;
71 host += pHost->h_name;
72 std::string domain=host.substr(host.find_first_of('.')+1, host.size()-host.find_first_of('.')-1);//获取第一个·后的域名
73
74 memcpy(m_MacInfo.domain, domain.c_str(), MAX_PATH);
75 }
76
77 /*****************************************
78 函 数 名:GetIpAddr
79 功能描述:获取电脑IP地址
80 输 入:无
81 输 出:无
82 返 回 值:void
83 void GetPCInfos::GetIpAddr()
84 {
85
86 struct hostent *hp;
87 struct in_addr sa;
88 char *buf ;
89 hp = gethostbyname("");//获取主机名
90 if (hp != NULL)
91 {
92 memcpy (&sa, hp->h_addr_list[0],hp->h_length);//hp->addr_list[0]为hostent结构保存的多IP主机的第一个完整域名信息
93 buf = inet_ntoa(sa);//转换成字符串形式的IP地址
94 memcpy(m_MacInfo.ipAddr, buf, sizeof(m_MacInfo.ipAddr));
95 }
96 }
97
98 /*****************************************
99 函 数 名:GetMacAddr
100 功能描述:获取MAC地址
101 输 入:无
102 输 出:无
103 返 回 值:void
104 *****************************************/
105 void GetPCInfos::GetMacAddr()
106 {
107 GetMacByGetAdaptersInfo(m_MacInfo.macAddr);
108 }
109
110 /*****************************************
111 函 数 名:GetTime
112 功能描述:获取当前时间
113 输 入:无
114 输 出:无
115 返 回 值:void
116 *****************************************/
117 void GetPCInfos::GetTime()
118 {
119 CTime currentTime=CTime::GetCurrentTime();
120 CString currentTimeStr;
121
122 currentTimeStr.Format(_T("%04d/%02d/%02d %02d:%02d:%02d"),
123 currentTime.GetYear(),
124 currentTime.GetMonth(),
125 currentTime.GetDay(),
126 currentTime.GetHour(),
127 currentTime.GetMinute(),
128 currentTime.GetSecond());
129
130 memcpy(m_MacInfo.time, currentTimeStr.GetBuffer(0), 20);
131 currentTimeStr.ReleaseBuffer();
132 }
133
134 /*****************************************
135 函 数 名:SaveFile
136 功能描述:将获取的电脑信息保存到文件
137 输 入:无
138 输 出:无
139 返 回 值:bool
140 bool GetPCInfos::SaveFile()
141 {
142 char chTempPath[MAX_PATH] = {0};
143 BOOL b= SHGetSpecialFolderPath(NULL,chTempPath, CSIDL_INTERNET_CACHE,0);//获取特殊路径,此处获取的是ie临时文件目录。没什么用
144
145 CString strTempPath(chTempPath);
146 std::string tempFile;
147 strTempPath += TEXT("\");
148 tempFile=strTempPath;
149 std::string strNull;
150 m_FileName += "MAC_" + strNull + m_MacInfo.macAddr + strNull + ".txt";
151 std::ofstream fWrite(m_FileName.c_str(),ios::binary);//,std::ios_base::app
152
153 if (!fWrite)
154 {
155 return false;
156 }
157
158 std::string strContent;
159 strContent += "ip:"+strNull + m_MacInfo.ipAddr + strNull + "n";
160 strContent += "mac:"+strNull + m_MacInfo.macAddr + strNull + "n";
161 strContent += "hostname:" + strNull + m_MacInfo.hostName + strNull + "n";
162 strContent += "domain:" + strNull + m_MacInfo.domain + strNull + "n";
163 strContent += "time:" + strNull + m_MacInfo.time + strNull + "n";
164
165
166
167 result=new char[strContent.size()+1];
168 memset(result,0,strContent.size()+1);
169 memcpy(result,strContent.c_str(),strContent.size());
170 result[strContent.size()] = ' ';
171 encode.SetValue(result);
172 tmpBuf = encode.ProcessData(TRUE);
173 delete []result;
174 result=NULL;
175 strContent=tmpBuf;
176 fWrite<Next)
220 {
221 // 确保是以太网
222 if(pAdapter->Type != MIB_IF_TYPE_ETHERNET)
223 continue;
224 // 确保MAC地址的长度为 00-00-00-00-00-00
225 if(pAdapter->AddressLength != 6)
226 continue;
227 sprintf(macOut, "%02X-%02X-%02X-%02X-%02X-%02X",
228 int (pAdapter->Address[0]),
229 int (pAdapter->Address[1]),
230 int (pAdapter->Address[2]),
231 int (pAdapter->Address[3]),
232 int (pAdapter->Address[4]),
233 int (pAdapter->Address[5]));
234 ret = true;
235 break;
236 }
237 }
238
239 free(pAdapterInfo);
240 return ret;
241 }





