Home > front end >  GetInterfaceDnsSettings failed with 87
GetInterfaceDnsSettings failed with 87

Time:08-08

I have a program to set DNS server automatically, but when I used GetInterfaceDnsSettings to get current DNS settings, it always return 87. The code is as followings.

nsafe fn get_adapters<F>(mut callback: F)
where
    F: FnMut(&IP_ADAPTER_INFO) -> bool {...}
#[test]
    fn adapters() {
        unsafe {
            get_adapters(|adapter| {
                if adapter.Type != 6 || adapter.LeaseObtained == 0 {
                    return false;
                }
                let name = get_string(&adapter.AdapterName);
                println!(
                    "name:{}, dhcp:{}, type:{}, lease:{}",
                    name, adapter.DhcpEnabled, adapter.Type, adapter.LeaseObtained
                );
                let mut guid = GUID::default();
                let iid: Vec<u16> = adapter
                    .AdapterName
                    .as_slice()
                    .iter()
                    .map(|w| *w as u16)
                    .collect();
                if S_OK != IIDFromString(iid.as_ptr(), &mut guid) {
                    println!("IIDFromString failed");
                    return false;
                }
                println!("guid:{:?}", guid);
                let mut setting = DNS_INTERFACE_SETTINGS::default();
                let code = GetInterfaceDnsSettings(guid, &mut setting);
                if code == NO_ERROR {
                    println!("dns:{:?}", setting.NameServer);
                } else {
                    println!("get interface dns failed:{}", get_error_message(code));
                    return false;
                }
                let code = SetInterfaceDnsSettings(guid, &setting);
                if code != NO_ERROR {
                    println!("set failed:{}", get_error_message(code));
                    false
                } else {
                    true
                }
            });
        }
    }

Also you can get a full code example in https://github.com/lazytiger/winapi_test.git Need some help, thank you all.

CodePudding user response:

As indicated in the GetInterfaceDnsSettings function documentation

You should set only the Version member; the Flags field must be empty.

If you set the Version member to DNS_INTERFACE_SETTINGS_VERSION1, then the Settings parameter must point to a valid DNS_INTERFACE_SETTINGS structure.

If you set the Version member to DNS_INTERFACE_SETTINGS_VERSION3, then the Settings parameter must point to a valid DNS_INTERFACE_SETTINGS3 structure.

  • Related