zoukankan      html  css  js  c++  java
  • 获取本地计算机名和IP地址

    WSADATA wsadata;
    if(0 != WSAStartup(MAKEWORD(2, 2), &wsadata))
    {
      AfxMessageBox("初始化网络环境失败!");
      return TRUE;
    }
    char szHostName[MAX_PATH + 1]; gethostname(szHostName, MAX_PATH); hostent *p = gethostbyname(szHostName); if(p == NULL) {   AfxMessageBox("得到本机网络信息失败!");   return TRUE; } m_strName = szHostName; int nCount = 0; char *pIP = inet_ntoa(*(in_addr *)p->h_addr_list[0]); m_strAddr = pIP;
    #include <winsock2.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    #pragma comment(lib,"Iphlpapi.lib")
    int main()
    {
    // 初始化winsock
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
    DWORD dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
    // 第一次调用GetAdapterInfo获取ulOutBufLen大小
    if (dwRetVal == ERROR_BUFFER_OVERFLOW)
    {
    free(pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
    dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen);
    }
    if (dwRetVal == NO_ERROR)
    {
    pAdapter = pAdapterInfo;
    while (pAdapter)
    {
    printf("Adapter Name: 	%s
    ", pAdapter->AdapterName);
    printf("Adapter Desc: 	%s
    ", pAdapter->Description);
    printf("MAC Addr: 	%02x-%02x-%02x-%02x-%02x-%02x
    ",
    pAdapter->Address[0],
    pAdapter->Address[1],
    pAdapter->Address[2],
    pAdapter->Address[3],
    pAdapter->Address[4],
    pAdapter->Address[5]);
    printf("IP Address: 	%s
    ", pAdapter->IpAddressList.IpAddress.String);
    printf("IP Mask: 	%s
    ", pAdapter->IpAddressList.IpMask.String);
    printf("Gateway: 	%s
    ", pAdapter->GatewayList.IpAddress.String);
    pAdapter = pAdapter->Next;
    }// end while
    }
    else
    {
    printf("Call to GetAdaptersInfo failed.
    ");
    }
    ...卸载winsock ...
    return 0;
    } //end main
  • 相关阅读:
    Oracle 删除重复数据的几种方法
    12.25模拟赛T3
    java实现第五届蓝桥杯圆周率
    java实现第五届蓝桥杯武功秘籍
    Oracle 审计初步使用
    [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
    ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database
    12.25模拟赛T2
    java实现第五届蓝桥杯写日志
    java实现第五届蓝桥杯李白打酒
  • 原文地址:https://www.cnblogs.com/liaocheng/p/4243473.html
Copyright © 2011-2022 走看看