GetAdaptersInfo / WSAIoctl の SIO_GET_INTERFACE_LIST / WMI の Win32_NetworkAdapterConfiguration でオフラインのIPアドレスの設定情報が取得できない件

GetAdaptersInfo
  if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR) {
        pAdapter = pAdapterInfo;
        while (pAdapter) {
            printf("IP: %s",pAdapter->IpAddressList.IpAddress.String);
            printf("MASK: %s",pAdapter->IpAddressList.IpMask.String);
            printf("DefGW: %s",pAdapter->GatewayList.IpAddress.String);
            pAdapter = pAdapter->Next;
        }
  }

WSAIoctl + SIO_GET_INTERFACE_LIST
    SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
    INTERFACE_INFO if_list[20];
    unsigned long ret_bytes;
    int n,num_if;
    if (sd != SOCKET_ERROR) {
        if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, NULL, 0, &if_list,
               sizeof(if_list), &ret_bytes, NULL, NULL) != SOCKET_ERROR) {
       num_if = ret_bytes / sizeof(INTERFACE_INFO);

       for (n = 0; n < num_if; ++n) {
        struct sockaddr_in *sa;
        sa = (struct sockaddr_in*)&(if_list[n].iiAddress);
            printf("IP: %s",inet_ntoa(sa->sin_addr));
            printf("MASK: %s",inet_ntoa(((struct sockaddr_in*)&if_list[n].iiNetmask)->sin_addr);
      }
   }

WMI
       if((hRes = m_pIWbemServices->ExecQuery(SysAllocString(L"WQL"),SysAllocString(L"select * from Win32_NetworkAdapterConfiguration"),
                                        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
                                        NULL,&pEnumBIOS)) == S_OK)
    {
                 while(((hRes = pEnum->Next(TIMEOUT, 1, &pB, &uReturned)) == S_OK) &&
                (uReturned == 1))
        {
                     char ip[32],ip2[32],ip3[32],mac[64],ifn[128];
                     int ui;
                     *mac=*ip=*ip2=*ip3=*ifn=0;
                     VariantInit(&pVal);
                     pB->Get(L"Description", 0, &pVal, NULL, NULL);
                     if(pVal.vt==VT_BSTR)wsprintf(ifn,"%ws",pVal.bstrVal);

                     VariantInit(&pVal);
                     pB->Get(L"MACAddress", 0, &pVal, NULL, NULL);
                     if(pVal.vt==VT_BSTR)wsprintf(mac,"%ws",pVal.bstrVal);

                     VariantInit(&pVal);
                     pB->Get(L"InterfaceIndex", 0, &pVal, NULL, NULL);
                     ui=pVal.uintVal;
                     VariantInit(&pVal);
                     pB->Get(L"IPEnabled", 0, &pVal, NULL, NULL);
                     if(pVal.vt==VT_BOOL)if(pVal.boolVal)
                     {
                          VariantInit(&pVal);
                          pB->Get(L"IPAddress", 0, &pVal, NULL, NULL);
                          if(pVal.vt==(VT_ARRAY|VT_BSTR))wsprintf(ip,"%ws",*(int*)pVal.parray->pvData) ;
                          VariantInit(&pVal);
                          pB->Get(L"IPSubnet", 0, &pVal, NULL, NULL);
                          if(pVal.vt==(VT_ARRAY|VT_BSTR))wsprintf(ip2,"%ws",*(int*)pVal.parray->pvData) ;
                          VariantInit(&pVal);
                          pB->Get(L"DefaultIPGateway", 0, &pVal, NULL, NULL);
                          if(pVal.vt==(VT_ARRAY|VT_BSTR))wsprintf(ip3,"%ws",*(int*)pVal.parray->pvData) ;
                          VariantInit(&pVal);
                     }
        }


GetAdaptersInfo については、IPがオフラインの場合取得不能
WSAIoctl の SIO_GET_INTERFACE_LIST / WMI の Win32_NetworkAdapterConfiguration の場合は
最後に取得した IPアドレスが格納される模様

おすすめ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です