WMI + EnableStatic, Error 0x80041003 (Access Denied)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im writing some software for a device that plugs into USB. The interface is a composite device, as it advertises both a serial port and a network adapter. Upon connection, its my job to assign an IP to the adapter on the local PC and also to
tell the device what its IP will be.
The following code, which I use to assign an IP works when I run the program as administrator, but does not work when Im running as User. 0x80041003 is returned as user, which I think means access denied. Also I am a user, Im a local administrator
on this PC, so I assumed I would be able to set the IP of a network adapter. The machine Im currently on is running in a domain, but there are no policies set for anything like this.
Have I done something wrong here, or is it impossible to ever set a network adapters IP and subnet mask as User? If anyone can help me I would really appreciate it.

Heres my SetIP function. The description field identifies the network adapter to set the IP address of:
<pre class="prettyprint /// <summary>
/// Sets 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)
{
try
{
ConnectionOptions _Options = new ConnectionOptions();

_Options.Impersonation = ImpersonationLevel.Impersonate;

ManagementScope _Scope = new ManagementScope("root\cimv2", _Options);

_Scope.Connect();

ManagementPath _Path = new ManagementPath();

ManagementObjectSearcher findAdapters = new ManagementObjectSearcher
(
_Scope,
new ObjectQuery
(
string.Format("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Description={0}", description)
)
); foreach (ManagementObject adapter in findAdapters.Get())
{
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"]); <span class="x_Apple-tab-span" style="white-space:pre break;
}
catch (Exception)
{
// Some sensible logging here...
}
}
}
catch (Exception)
{
// Some sensible logging here...
}
} [/code]
<br/>

<br/>
<br/>
<br/>

View the full article
 
Back
Top