日志
分别在windows和Linux系统下获取主机名和Mac地址。纯C++
2016-06-07 15:56
#include <stdio.h>
#include <string.h> #include <string> #ifdef WINDOWS_ENV #include <winsock2.h> #include <Iphlpapi.h> #pragma comment(lib,"Iphlpapi.lib") #else #include <sys/ioctl.h> #include <net/if.h> #include <unistd.h> #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #endif using std::string; #ifdef WINDOWS_ENV void Init_Windows(unsigned char *mac); #else void Init_Linux(char* szMac_Len); #endif void writeFile(unsigned char *address); int main(int argc, char **argv) { (void)argc; (void)argv; unsigned char address[1024]; memset(address,0,sizeof(address)); #ifdef WINDOWS_ENV Init_Windows(address); #else char szMac_Len[1024]; Init_Linux(szMac_Len); memset(address,0,sizeof(address)); memcpy(address,szMac_Len,sizeof(szMac_Len)); #endif //printf("address:%s\n",address); writeFile(address); //Sleep(10000); return 0; } void byte2Hex(unsigned char bData,unsigned char hex[]) { int high=bData/16,low =bData %16; hex[0] = (high <10)?('0'+high):('A'+high-10); hex[1] = (low <10)?('0'+low):('A'+low-10); } #ifdef WINDOWS_ENV void Init_Windows(unsigned char *mac) { ULONG ulSize=0; PIP_ADAPTER_INFO pInfo=NULL; int temp=0; temp = GetAdaptersInfo(pInfo,&ulSize);//第一处调用,获取缓冲区大小 pInfo=(PIP_ADAPTER_INFO)malloc(ulSize); temp = GetAdaptersInfo(pInfo,&ulSize); //遍历得到MAC地址 int iCount=0; string str_mac="Mac="; memcpy(&mac[iCount],str_mac.c_str(),str_mac.length()); iCount = str_mac.length(); while(pInfo)//遍历每一张网卡 { // pInfo->Address MAC址 for(int i=0;i<(int)pInfo->AddressLength;i++) { byte2Hex(pInfo->Address,&mac[iCount]); iCount+=2; if(i<(int)pInfo->AddressLength-1) { mac[iCount++] = ':'; } else { mac[iCount++] = '#'; } } pInfo = pInfo->Next; } //获取计算机名称 char szBuffer[500]; DWORD dwNameLen; dwNameLen = 500; if (!GetComputerName(szBuffer, &dwNameLen)) printf("Error %d\n", GetLastError()); //memset(&unchar,0,sizeof(szBuffer)); string name = szBuffer; name += "\0"; name = "\nhostname="+name; memcpy(&mac[iCount++],name.c_str(),name.length()); } #else void Init_Linux(char* szMac_Len) { struct ifreq ifr; struct ifconf ifc; char buf[2048]; int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sock == -1) { printf("socket error\n"); return ; } ifc.ifc_len = sizeof(buf); ifc.ifc_buf = buf; if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) { printf("ioctl error\n"); return ; } struct ifreq* it = ifc.ifc_req; const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq)); char szMac[64]; memset(szMac_Len,0,sizeof(szMac_Len)); string macStr = "Mac="; for (; it != end; ++it) { strcpy(ifr.ifr_name, it->ifr_name); if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) { if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) { unsigned char * ptr ; ptr = (unsigned char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0]; snprintf(szMac,64,"%02X:%02X:%02X:%02X:%02X:%02X",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5)); macStr += szMac; macStr+="#"; //count ++ ; //printf("%d,Interface name : %s , Mac address : %s \n",count,ifr.ifr_name,macStr.c_str()); } } } else { printf("get mac info error\n"); return; } } //获取计算机名称 char name[65]; gethostname(name, sizeof(name)); //macStr = macStr.substr(0,macStr.length()-1); macStr = macStr+"\n"; macStr = macStr+"hostname="+name; macStr += '\0'; printf("%s\n",macStr.c_str()); memcpy(szMac_Len,macStr.c_str(),macStr.length()); } #endif void writeFile(unsigned char *address) { FILE *fp; fp=fopen("./license.dat","w"); if(fp==NULL) { printf("create file error!\n"); return; } fprintf(fp, "%s", address); fclose(fp); fp = NULL; } |
上一篇: Qt 表格粘贴复制快捷键