大网数据广告活动
网络与域名解析

Retrieving Information Using GetNetworkParams

· 52CE 我爱测

Fills a pointer to a FIXED_INFO structure with data about the current network settings.

The GetNetworkParams function fills a pointer to a FIXED_INFO structure with data about the current network settings.

To use GetNetworkParams

  1. Declare a pointer to a FIXED_INFO object called pFixedInfo, and a ULONG object called ulOutBufLen. These variables are passed as parameters to the GetNetworkParams function. Also create a DWORD variable dwRetVal (used for error checking).

        FIXED_INFO *pFixedInfo;
        IP_ADDR_STRING *pIPAddr;
    
        ULONG ulOutBufLen;
        DWORD dwRetVal;
    
  2. Allocate memory for the structures.

    [!Note]
    The size of ulOutBufLen is not sufficient to hold the information. See the next step.

        pFixedInfo = (FIXED_INFO *) malloc(sizeof (FIXED_INFO));
        ulOutBufLen = sizeof (FIXED_INFO);
    
  3. Make an initial call to GetNetworkParams to get the size required for the ulOutBufLen variable.

    [!Note]
    This function function will fail, and is used to ensure that the ulOutBufLen variable specifies a size sufficient for holding all the data returned to pFixedInfo. This is a common programming model for data structures and functions of this type.

        if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
            free(pFixedInfo);
            pFixedInfo = (FIXED_INFO *) malloc(ulOutBufLen);
            if (pFixedInfo == NULL) {
                printf("Error allocating memory needed to call GetNetworkParams\n");
            }
        }
    
  4. Make a second call to GetNetworkParams using general error checking and returning its value to the DWORD variable dwRetVal; used for more advanced error checking.

        if (dwRetVal = GetNetworkParams(pFixedInfo, &ulOutBufLen) != NO_ERROR) {
            printf("GetNetworkParams failed with error %d\n", dwRetVal);
            if (pFixedInfo) {
                free(pFixedInfo);
            }
        }        
    
  5. If the call was successful, access the data from the pFixedInfo data structure.

            printf("\tHost Name: %s\n", pFixedInfo->HostName);
            printf("\tDomain Name: %s\n", pFixedInfo->DomainName);
            printf("\tDNS Servers:\n");
            printf("\t\t%s\n", pFixedInfo->DnsServerList.IpAddress.String);
    
            pIPAddr = pFixedInfo->DnsServerList.Next;
            while (pIPAddr) {
                printf("\t\t%s\n", pIPAddr->IpAddress.String);
                pIPAddr = pIPAddr->Next;
            }
    
            printf("\tNode Type: ");
            switch (pFixedInfo->NodeType) {
            case 1:
                printf("%s\n", "Broadcast");
                break;
            case 2:
                printf("%s\n", "Peer to peer");
                break;
            case 4:
                printf("%s\n", "Mixed");
                break;
            case 8:
                printf("%s\n", "Hybrid");
                break;
            default:
                printf("\n");
            }
    
            printf("\tNetBIOS Scope ID: %s\n", pFixedInfo->ScopeId);
    
            if (pFixedInfo->EnableRouting)
                printf("\tIP Routing Enabled: Yes\n");
            else
                printf("\tIP Routing Enabled: No\n");
    
            if (pFixedInfo->EnableProxy)
                printf("\tWINS Proxy Enabled: Yes\n");
            else
                printf("\tWINS Proxy Enabled: No\n");
    
            if (pFixedInfo->EnableDns)
                printf("\tNetBIOS Resolution Uses DNS: Yes\n");
            else
                printf("\tNetBIOS Resolution Uses DNS: No\n");
    
  6. Free any memory allocated for the pFixedInfo structure.

        if (pFixedInfo) {
            free(pFixedInfo);
            pFixedInfo = NULL;
        }
    

Next Step: Managing Network Adapters Using GetAdaptersInfo

Previous Step: Creating a Basic IP Helper Application

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。