Share via

SetInterfaceDnsSettings function (netioapi.h) does not work?

gary allen 0 Reputation points
2023-06-26T04:51:43.5633333+00:00

I want to change the DNS of a network adapter with C++

I came across this SetInterfaceDnsSettings function https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-setinterfacednssettings

This is my code.

The ret value returned is 0, so I guess it means no error. But the DNS setting of the interface did not change.

The interface guid is got from GetAdaptersInfo() function.

GUID interfaceGUID;
    LPCOLESTR lpsz = L"{1CA18531-B5FA-4A92-837A-AE12298FFF7F}";
    HRESULT hr = CLSIDFromString(lpsz, &interfaceGUID);
    if (hr != S_OK)
    {
        printf("get guid error!!!\n");
        exit(1);
    }

    DNS_INTERFACE_SETTINGS settings = {DNS_INTERFACE_SETTINGS_VERSION1, 0, NULL, L"8.8.8.8", NULL, 0, 0, 0, 0, NULL};
    printf("NameServer         : %ls\n", settings.NameServer);

    DWORD ret = SetInterfaceDnsSettings(interfaceGUID, &settings);
    printf("result: %lu\n", ret);
Windows development | Windows API - Win32
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.


1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,736 Reputation points Microsoft External Staff
    2023-06-26T07:06:47.8366667+00:00

    Try to specify DNS_INTERFACE_SETTINGS.Flags with DNS_SETTING_NAMESERVER.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.