WMI EnableDHCP fails on disconnected adapter
Asked Answered
A

3

11

I'm using the WMI Win32_NetworkAdapterConfiguration Class EnableDHCP Method to enable the DHCP service on a network adapter. I'm using XP Embedded with SP2 but it also happens on my desktop (XP SP3).

Everything works great except when the network cable is unplugged.

If the cable is disconnected, EnableDHCP returns 94 (Path, file, or object not found) and when I look in the network control panel properties, it is set to manual configuration with address 0.0.0.0 and mask 255.0.0.0.

Curiously, when in this state, EnableStatic fails with code 94 or 81 (Unable to configure DHCP service) the first time but then works the second time.

Enabling DHCP works fine using netsh or the windows GUI even with the cable unplugged.

Anybody know how to work around this issue?

Alcoholic answered 12/8, 2009 at 14:35 Comment(0)
A
8

It seems at least one other person is interested so here's what I found:

As far as I can tell it's a bug in WMI. To get around it, what seems to work is to directly set the appropriate registry values for the adapter. These are stored in the registry under

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\<guid>

The guid is contained in the IP_ADAPTER_INFO structure filled in by GetAdaptersInfo.

Set the EnableDHCP DWORD value to 1.

I also found it useful to clear the gateways using WMI SetGateways otherwise the old gateway(s) hang around.

I also found that when EnableStatic says it failed, it actually works.

This stuff is only relevant when the network is disconnected.

Alcoholic answered 26/8, 2009 at 10:41 Comment(2)
Can you tell me how i excatly "set the appropriate registry values for the adapter. These are stored in the registry under ...."Britnibrito
Wow, it was over 10 years ago I did that. As far as I remember, I used the Win32 registry functions (RegOpenKey and friends).Alcoholic
S
2

markh44's answer is still relevant 11 years later.

I found I had to add one additional step though: Set the DisableDhcpOnConnect value to 0. If I only EnableDHCP to 1, I found in the IPv4 Properties window that the IP address and subnet mask were gone, but "Use the following address" was still selected. Setting DisableDhcpOnConnect to 0 caused "Obtain an IP address automatically" to be selected.

Stockstill answered 22/6, 2020 at 20:10 Comment(0)
D
2

markh44's question (AND answer) are also still relevant 13+ years later, however, I wanted to bring some attention to a similar issue related to using WMI to change the adapter setting, but using the "old standby" of NetSh to do this. . .

I know O.P. is using WMI to do this, but when I read his statement that NetSh could do this without the network cable connected, I was completely flabbergasted because I had apparently ran into a situation where this was NOT the case and had developed a mindset that this could never be done with the network cable disconnected, so I wanted to thank josh2112 for his answer on this as well as it was the DisableDhcpOnConnect parameter that led me to this article which explains some bugs with the NetSh method that I encountered and could not explain nor "accurately" reproduce.

When using NetSh to set an IP Address to Static this command has a faulty condition in where it will NOT set the EnableDHCP value to a 0 when it runs! So what you end up having is this condition where NetSh can no longer set it to DHCP either - see screenshots below. DisableDhcpOnConnect present, EnableDHCP set to 1 DHCP is already enabled on this interface with NetSh attempt

So - if anyone wants a way to do this without changing more than one Registry value (and also without using the WMI methods), you can simply change the EnableDHCP value to 0, then use your favorite process invoker (VB.Net's example would be Process.Start(info) with the relevant data provided to call NetSh via cmd.exe) or any other method to call cmd.exe with the necessary parameters, and it will set DHCP appropriately for the Adapter in question, AND remove the static IPv4 entry as shown in the next screenshots:

EnableDHCP set to 0, some of the details redacted for privacy. EnableDHCP set to 1, DisableDhcpOnConnect deleted, and DefaultGateway cleared automatically

Dwarfish answered 28/7, 2022 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.