WMI Win32_NetworkAdapterConfiguration "EnableStatic" call fails with return result 0x80041003

0

I'm having trouble assigning a static IP address to a device that gets attached to my system and advertises a Network Adapter. Whenever I call EnableStatic on the device, with the IP and subnet mask, it returns the error code 0x80041003, which I believe means "Access Denied". I can change the IP address in Windows without requiring elevation.

The code I'm using to set the IP address looks like this:

 /// <summary>
    /// Set's a new IP Address and subnet mask for the adapter with the given description.
    /// </summary>
    /// <param name="description">Description string used to ID the adapter.</param>
    /// <param name="ip_address">The IP Address.</param>
    /// <param name="subnet_mask">The Submask IP Address</param>
    public static void SetIP(string description, string ip, string mask)
    {
        ManagementClass findAdapters = 
                                    new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection adapters = findAdapters.GetInstances();

        foreach (ManagementObject adapter in adapters)
        {
            string name = (string)adapter["Description"];

            if (name != description)
            {
                continue;
            }

            try
            {
                ManagementBaseObject newIP = 
                                           adapter.GetMethodParameters("EnableStatic");

                newIP["IPAddress"] = new string[] { ip };
                newIP["SubnetMask"] = new string[] { mask };

                ManagementBaseObject setIP = 
                                            adapter.InvokeMethod("EnableStatic", newIP, null);

                UInt32 result = (UInt32)(setIP["returnValue"]);
            }
            catch (Exception)
            {
                throw;
            }                
        }
    }

I have hunted around the internets looking for solutions. The one that looked most likely was that I needed to do something like CoInitializeSecurity before everything got up and running. But this seems odd to me. I'm using the WMI namespace, if WMI is somehow dependent on things like this, surely that should be in the namespace too?

There is something I'm missing here....

btw running Windows 7 on a domain as User but am a local administrator if that helps!

c#
wmi
asked on Stack Overflow May 16, 2012 by Robinson

1 Answer

1

You need to be running as Administrator.

If you wanted to avoid a UAC prompt you could do something like

make & install a service that has the ability to change the IP & runs as administrator

Tell the service the new IP as necessary

answered on Stack Overflow May 18, 2012 by Sam

User contributions licensed under CC BY-SA 3.0