GetPCInfos

//GetPCInfos.h

#pragma once
#include <iphlpapi.h>
#include <string>
#include <ctime>
#include <fstream>
#include "DisposeData.h"
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "ws2_32.lib")
#include <Winsock2.h>
//電腦相關信息的結構體
typedef struct MAC_INFO
{
 char ipAddr[16];//IP地址
 char macAddr[32];//MAC地址
 char hostName[MAX_PATH];//主機名
 char domain[MAX_PATH];//域名
 char time[20];//時間

}MacInfo;
class GetPCInfos
{
public:
 GetPCInfos();
 ~GetPCInfos(void);
 MacInfo GetPcInfo();
 void GetIpAddr();//獲取IP地址
 void GetHostName();//獲取主機名
 void GetDomain();//獲取域名
 void GetTime();//獲取當前時間
 void GetMacAddr();//獲取Mac地址
 bool SaveFile();//保存文件
 std::string GetFilePath();//獲取保存文件路徑
 bool GetMacByGetAdaptersInfo(char* macOut);//獲取Mac地址
private:
 MacInfo m_MacInfo;//電腦信息
 std::string m_FileName;//文件名
 WSADATA wsa;
 WORD wVersionRequested;
};

//getpcinfos.cpp

#include "StdAfx.h"
#include "GetPCInfos.h"

GetPCInfos::GetPCInfos()
{  
 memset(m_MacInfo.ipAddr, '\0', 16);
 memset(m_MacInfo.macAddr, '\0', 32);
 memset(m_MacInfo.hostName, '\0', MAX_PATH);
 memset(m_MacInfo.domain, '\0' ,20);
 memset(m_MacInfo.time, '\0', 20);
}

GetPCInfos::~GetPCInfos(void)
{
}


MacInfo GetPCInfos::GetPcInfo()
{
 GetIpAddr();
 GetMacAddr();
 GetHostName();
 GetDomain();
 GetTime();
 wVersionRequested = MAKEWORD( 2, 0 ); 
 if(WSAStartup(wVersionRequested , &wsa) !=0 )
  AfxMessageBox("初始化失敗");
 return m_MacInfo;
}


void GetPCInfos::GetHostName()
{
 struct hostent* pHost;
 pHost = gethostbyname("");

 std::string host;
 host += pHost->h_name;
 std::string domain=host.substr(0, host.find_first_of('.'));

 memcpy(m_MacInfo.hostName, domain.c_str(), MAX_PATH);


void GetPCInfos::GetDomain()
{
 struct hostent * pHost;
 pHost = gethostbyname("");
 //pHost->h_name存有完整的域名信息pc-liutaolt.chinasoft.com

 std::string host;
 host += pHost->h_name;
 std::string domain=host.substr(host.find_first_of('.')+1, host.size()-host.find_first_of('.')-1);
 
 memcpy(m_MacInfo.domain, domain.c_str(), MAX_PATH);
}


void GetPCInfos::GetIpAddr()
{
 
 struct hostent  *hp;
 struct in_addr  sa;
 char  *buf ;
 hp = gethostbyname("");
 if (hp != NULL)
 {
  memcpy (&sa, hp->h_addr_list[0],hp->h_length);
  buf = inet_ntoa(sa);
  memcpy(m_MacInfo.ipAddr, buf, sizeof(m_MacInfo.ipAddr));
 } 
}


void GetPCInfos::GetMacAddr()
{
 GetMacByGetAdaptersInfo(m_MacInfo.macAddr);
}


void GetPCInfos::GetTime()
{
 CTime currentTime=CTime::GetCurrentTime();
 CString currentTimeStr;

 currentTimeStr.Format(_T("%04d/%02d/%02d %02d:%02d:%02d"),
  currentTime.GetYear(),
  currentTime.GetMonth(),
  currentTime.GetDay(),
  currentTime.GetHour(),
  currentTime.GetMinute(),
  currentTime.GetSecond());

 memcpy(m_MacInfo.time, currentTimeStr.GetBuffer(0), 20);
 currentTimeStr.ReleaseBuffer();
}


bool GetPCInfos::SaveFile()
{
 char chTempPath[MAX_PATH] = {0};
 BOOL b= SHGetSpecialFolderPath(NULL,chTempPath, CSIDL_INTERNET_CACHE,0);//獲取特殊路徑

 CString strTempPath(chTempPath);
 std::string tempFile;
 strTempPath += TEXT("\\");
 tempFile=strTempPath;
 std::string strNull;
 std::string fileName="MAC_" + strNull + m_MacInfo.macAddr;

 DisposeData encode;
 char *result;
 result=new char[fileName.size()+1];
 memset(result,0,fileName.size()+1);
 memcpy(result,fileName.c_str(),fileName.size());
 result[fileName.size()] = '\0';
 encode.SetValue(result);
 delete []result;
 result=NULL;
 CString tmpBuf;
 tmpBuf = encode.EncryptFileName();
 tempFile += tmpBuf;
 tempFile += strNull+".dat";
 m_FileName=tempFile;
 //m_FileName += "MAC_" + strNull + m_MacInfo.macAddr + strNull + ".txt";
 std::ofstream fWrite(m_FileName.c_str(),ios::binary);//,std::ios_base::app

 if (!fWrite)
 {
  return false;
 }

 std::string strContent;
 strContent += "ip:"+strNull + m_MacInfo.ipAddr + strNull + "\n";
 strContent += "mac:"+strNull + m_MacInfo.macAddr + strNull + "\n";
 strContent += "hostname:" + strNull + m_MacInfo.hostName + strNull + "\n";
 strContent += "domain:" + strNull + m_MacInfo.domain + strNull + "\n";
 strContent += "time:" + strNull + m_MacInfo.time + strNull + "\n";

 

 result=new char[strContent.size()+1];
 memset(result,0,strContent.size()+1);
 memcpy(result,strContent.c_str(),strContent.size());
 result[strContent.size()] = '\0';
 encode.SetValue(result);
 tmpBuf = encode.ProcessData(TRUE); 
 delete []result;
 result=NULL;
 strContent=tmpBuf;
 fWrite<<strContent;
 fWrite.close();

 return true;
}


std::string GetPCInfos::GetFilePath()
{
 return m_FileName;
}

bool GetPCInfos::GetMacByGetAdaptersInfo(char * macOut)
{
 bool ret = false;
 ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
 PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
 if(pAdapterInfo == NULL)
  return false;
 if(GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
 {
  free(pAdapterInfo);
  pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
  if (pAdapterInfo == NULL)
   return false;
 }


 if(GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR)
 {
  for(PIP_ADAPTER_INFO pAdapter = pAdapterInfo; pAdapter != NULL; pAdapter = pAdapter->Next)
  {
   // 確保是以太網
   if(pAdapter->Type != MIB_IF_TYPE_ETHERNET)
    continue;
   // 確保MAC地址的長度爲 00-00-00-00-00-00
   if(pAdapter->AddressLength != 6)
    continue;
   sprintf(macOut, "%02X-%02X-%02X-%02X-%02X-%02X",
    int (pAdapter->Address[0]),
    int (pAdapter->Address[1]),
    int (pAdapter->Address[2]),
    int (pAdapter->Address[3]),
    int (pAdapter->Address[4]),
    int (pAdapter->Address[5]));
   ret = true;
   break;
  }
 }

 free(pAdapterInfo);
 return ret;
}

 

發佈了56 篇原創文章 · 獲贊 5 · 訪問量 81萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章