zoukankan      html  css  js  c++  java
  • C++获取MAC与IP

    #include <Nb30.h>
    #pragma comment(lib,"ws2_32.lib")
    #pragma comment(lib,"netapi32.lib")
    std::string GetMac()
    {
        NCB ncb;
        typedef struct _ASTAT_
        {
            ADAPTER_STATUS   adapt;
            NAME_BUFFER   NameBuff[30];
        }ASTAT, *PASTAT;
    
        ASTAT Adapter;
        typedef struct _LANA_ENUM
        {
            UCHAR length;
            UCHAR lana[MAX_LANA];
        }LANA_ENUM;
        LANA_ENUM lana_enum;
        UCHAR uRetCode;
        memset(&ncb, 0, sizeof(ncb));
        memset(&lana_enum, 0, sizeof(lana_enum));
    
        ncb.ncb_command = NCBENUM;
        ncb.ncb_buffer = (unsigned char *)&lana_enum;
        ncb.ncb_length = sizeof(LANA_ENUM);
        uRetCode = Netbios(&ncb);
        if (uRetCode != NRC_GOODRET)
            return "";
    
        for (int lana = 0; lana<lana_enum.length; lana++)
        {
            ncb.ncb_command = NCBRESET;
            ncb.ncb_lana_num = lana_enum.lana[lana];
            uRetCode = Netbios(&ncb);
            if (uRetCode == NRC_GOODRET)
                break;
        }
        if (uRetCode != NRC_GOODRET)
            return "";
    
        memset(&ncb, 0, sizeof(ncb));
        ncb.ncb_command = NCBASTAT;
        ncb.ncb_lana_num = lana_enum.lana[0];
        strcpy_s((char*)ncb.ncb_callname, 5, "*");
        ncb.ncb_buffer = (unsigned char *)&Adapter;
        ncb.ncb_length = sizeof(Adapter);
        uRetCode = Netbios(&ncb);
        if (uRetCode != NRC_GOODRET)
            return "";
        char mac[128];
        sprintf_s(mac, 18, "%02X-%02X-%02X-%02X-%02X-%02X",
            Adapter.adapt.adapter_address[0],
            Adapter.adapt.adapter_address[1],
            Adapter.adapt.adapter_address[2],
            Adapter.adapt.adapter_address[3],
            Adapter.adapt.adapter_address[4],
            Adapter.adapt.adapter_address[5]
        );
        return mac;
    }
    std::string GetLanIP()
    {
        WSADATA wsaData;
        char name[155];
        char *ip;
        PHOSTENT hostinfo;
        if (WSAStartup(MAKEWORD(2, 0), &wsaData) == 0)
        {
            if (gethostname(name, sizeof(name)) == 0)
            {
                if ((hostinfo = gethostbyname(name)) != NULL)
                {
                    ip = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
                    printf(" IP地址: %s ", ip);
                }
            }
            WSACleanup();
        }
        return ip;
    }
  • 相关阅读:
    react中React.createRef()的使用
    react中this指向问题
    react中对props进行限制
    react中this问题
    react 中this问题
    类中方法的this
    类中方法的this
    react中render函数里面的this指向?
    Android一对一直播系统源码开发,仿朋友圈发布动态的实现
    Android游戏陪玩源码开发中,阴影效果的实现
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/8968583.html
Copyright © 2011-2022 走看看