蚂蚁power的个人主页

http://www.qtcn.org/bbs/u/163171  [收藏] [复制]

蚂蚁power

  • 6

    关注

  • 2

    粉丝

  • 22

    访客

  • 等级:新手上路
  • 总积分:74
  • 男,2011-06-12

最后登录:2018-06-06

更多资料

日志

分别在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;
}

分类:默认分类|回复:1|浏览:1322|全站可见|转载
 
 
删除

蚂蚁power:使用的工具是VS2010.需要设置预处理器定义。在工程上右键-》属性-》配置属性-》C/C++-》预处理器-》预处理器定义。双击该选项 点击编辑选项。加入WINDOWS_ENV 即可在windows系统下正常使用。

2016-06-07 16:01 -

Powered by phpwind v8.7 Certificate Copyright Time now is:04-30 12:49
©2005-2016 QTCN开发网 版权所有 Gzip disabled