Problems using the WMI EnableStatic method - c#

I'm trying to create a tool that converts the dynamic DHCP-provided IPv4 address, gateway and dns-settings into static configuration. I've tried to use WMI to solve this puzzle, but I have a problem I can't figure out.
The application completes, DNS and Gateway is configured, but the EnableStatic method (to set the IP address and subnet) has been unsuccesful which means that the IP is still received from DHCP (with greyed out fields) even though the default gateway has been set. How do I fix this?
The ReturnValue from EnableStatic is 70 (Invalid IP address). The weird thing is that the input parameters are the same that I extracted from the NIC 2 seconds earlier.
Here is the code (except GUI), http://pastebin.com/AE3dGhUz:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace Static_NIC_Settings_Creator
{
public partial class Form1 : Form
{
private ManagementObjectCollection queryCollection;
private string[] networkInterfaces;
private int currentNIC;
private string[] ipAddress;
private string[] subnetMask;
private string[] defaultIPGateway;
private string[] dnsServerSearchOrder;
public Form1()
{
InitializeComponent();
getNICs();
}
private void convertButton_Click(object sender, EventArgs e)
{
if (networkInterfaces.Count() > 0)
{
//Get current NIC settings
if (!getNICSettings())
{
MessageBox.Show("Retrieving current NIC settings failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//Convert to static NIC settings
if (!setNICStatic())
{
MessageBox.Show("Setting NIC settings to static failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
private void nicSelecter_SelectedIndexChanged(object sender, EventArgs e)
{
currentNIC = nicSelecter.SelectedIndex;
}
private void getNICs()
{
//Get NICS
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
queryCollection = query.Get();
//Make nic string array
int i = queryCollection.Count;
networkInterfaces = new string[i];
//Fill nic string array
i = 0;
foreach (ManagementObject mo in queryCollection)
{
networkInterfaces[i] = (String)mo["Description"];
i++;
}
//Fill dropbox with arraylist-data
nicSelecter.DataSource = networkInterfaces;
}
private Boolean getNICSettings()
{
//Get selected NIC
int i = 0;
foreach (ManagementObject mo in queryCollection)
{
//Get settings for specific NIC
if (i == currentNIC)
{
try
{
ipAddress = (String[])mo["IPAddress"];
subnetMask = (String[])mo["IPSubnet"];
defaultIPGateway = (String[])mo["DefaultIPGateway"];
dnsServerSearchOrder = (String[])mo["DNSServerSearchOrder"];
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error");
return false;
}
}
i++;
}
return false;
}
private Boolean setNICStatic()
{
//Get selected NIC
int i = 0;
foreach (ManagementObject mo in queryCollection)
{
//Get settings for specific NIC
if (i == currentNIC)
{
try
{
//Set static IP and subnet mask
ManagementBaseObject setIP;
ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = ipAddress;
newIP["SubnetMask"] = subnetMask;
setIP = mo.InvokeMethod("EnableStatic", newIP, null);
//Set default gateway
ManagementBaseObject setGateway;
ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = defaultIPGateway;
newGateway["GatewayCostMetric"] = new int[] { 1 };
setGateway = mo.InvokeMethod("SetGateways", newGateway, null);
//Set dns servers
ManagementBaseObject setDNS;
ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = dnsServerSearchOrder;
setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
System.Windows.Forms.MessageBox.Show("Setting NIC settings returned: " + setDNS);
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error");
return false;
}
}
i++;
}
//No NICs
return false;
}
} //End class
}
Any ideas?

Could it be that you are inputting IPv6 addresses as well? Just playing around with PowerShell it seems not to like them. Perhaps you can post actual values that are being inputted whilst debugging, it would help a lot. Also maybe try statically inputting some values like:
new string[]{"192.168.0.1"}, new string[] {"255.255.255.255"}
Also unless you really, really need C# and a GUI you may want to consider using PowerShell (requirement is it being installed of course) as WMI is really much simpler to manipulate there (sadly you still have that learning curve though).
This is just an example of how to use PowerShell, you can at the very least use it for some testing:
Get-WmiObject Win32_NetworkAdapterConfiguration
Then get the index of your adapter then run, but replace your index number:
$obj = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.Index -eq 1}
$obj.EnableStatic("192.168.0.1", "255.255.255.0")
To get method parameters just run:
$obj.EnableStatic
It will return:
MemberType : Method
OverloadDefinitions : {System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)
Name : EnableStatic
IsInstance : True

Another tip for those who might stumble across this on search...
If your current IP address is DHCP assigned, and EnableStatic() is returning 2147944122 or 2147944117 error codes, verify the dhcp service (DHCP Client) in Windows is enabled and started.
I found that switching from one static address to another using EnableStatic() works OK. But if you have a dynamic address, and the DHCP service is disabled (possibly due to security reasons), then EnableStatic() will not work.
Adding this solution to the internet, in case it saves someone time in the future.

Related

How to kill remote desktop session for local computer only

I'm using the following code to kill a remote desktop session and the application running in it. It works fine, the only problem is that it kills the specified application for all users.
How do I keep this to just the local machine running a session?
We have multiple users logging in and running this application from a server on their local machines. Most are running using work resources, but some use remote desktop.
No matter how they are logged in when I run my code all users loose their sessions.
private void btnCloseSession_Click(object sender, EventArgs e)
{
if (!runningExclusiveProcess)
{
runningExclusiveProcess = true;
btnCloseSession.Enabled = false;
//check and close Labware if running
if (chkCloseLabware.Checked == true)
{
if (chkExit.Checked == true)
{
KillLabWare();
Close();
}
else
{
KillLabWare();
}
}
Process[] my = Process.GetProcessesByName("mstsc");
//loop thru list to get selected item(s)
ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstOpenSessions);
selectedItems = lstOpenSessions.SelectedItems;
try
{
//remove credentials
string szTestx = "/delete:GOJO.NET/" + cboServer.Text;
ProcessStartInfo infox = new ProcessStartInfo("cmdkey.exe", szTestx);
Process procx = new Process();
procx.StartInfo = infox;
procx.Start();
if (lstOpenSessions.SelectedIndex != -1)
{
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
//loop thru process to match process vs. list selection(s)
foreach (Process remote in my)
{
if (remote.MainWindowTitle == selectedItems[i].ToString())
{
KillRS(remote.MainWindowTitle);
lstOpenSessions.Items.Remove(selectedItems[i]);
}
}
if (lstOpenSessions.Items.Contains(selectedItems[i].ToString()))
{
lstOpenSessions.Items.Remove(selectedItems[i]);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
// If your task is synchronous, then undo your flag here:
runningExclusiveProcess = false;
btnCloseSession.Enabled = true;
}
}
public void KillLabWare()
{
ConnectionOptions con = new ConnectionOptions();
con.Username = cboUserName.Text;
con.Password = txtPassWord.Text;
string strIPAddress = cboServer.Text;
ManagementScope scope = new
ManagementScope(#"\\" + strIPAddress + #"\root\cimv2", con);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='Labware.exe'");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection objectCollection = searcher.Get();
foreach (ManagementObject managementObject in objectCollection)
{
managementObject.InvokeMethod("Terminate", null);
}
}
private void KillRS(string rwt)
{
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle == rwt)
{
p.Kill();
}
}
}
public static void KillRemoteProcess(Process p, string user, string password)
{
new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "TaskKill.exe",
Arguments = string.Format("/pid {0} /s {1} /u {2} /p {3}", p.Id, p.MachineName, user, password),
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true
}
}.Start();
}
It sounds like you are trying to force a specific user to log out? Is this because you find that users are forgetting to log out and constantly consuming licenses?
LabWare Application allows for a time out interval (in minutes) to be set on each user where after the interval has passed, the user will be logged out (licence no longer consumed).
For more information see page 204 of the LabWare 7 Technical Manual.
Alternativley if this is for a scheduler (service or cluster instance) session, this can also be controlled by the application. You can either manually change the shutdown and keep alive flags on the instance record on the Services table (if using Service Manager) or you can write a LIMS Basic event trigger/automation script or scheduled subroutine (or have this as a button on a Visual workflow) to do this for you.
HTH.
If you wanna kill remote desktop session or disconnect current RDP session, please read this article:
WTSDisconnectSession function
but if you logout current user, it also disconnect RDP session, here is the code:
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
class Program
{
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSDisconnectSession(IntPtr hServer, int sessionId, bool bWait);
const int WTS_CURRENT_SESSION = -1;
static readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
static void Main(string[] args)
{
if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE,
WTS_CURRENT_SESSION, false))
throw new Win32Exception();
}
}
I hope it works, fill free for further info, comment plz
Happy Coding 😎

How to EnableDHCP and delete IP from adapter via WMI

Im making some kind of IP setting tool and i have a problem with EnableDHCP method via WMI (Win32_NetworkAdapterConfiguration). Adapter was set to DHCP but there is still IP and gateway. So i am using netsh function which works to me but i want to use only WMI. Any advice?
I try some methods like Lease and send null to static but it doesnt work.
s//THIS ONE I WANT TO USE BUT ITS LET IP AT ADAPTER EVEN DHCP IS ENABLED
public void button1_Click(object sender, EventArgs e)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
//check numbe of aktive ports
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
// write parameters to active port
ManagementBaseObject setIP;
setIP = objMO.InvokeMethod("EnableDHCP", null, null);
}
catch (Exception x)
{
}
}
}
}
Process p = new Process();//netsh work well but i dont want to use
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"" + adapter.Name + "\" dhcp");
p.StartInfo = psi;
p.Start();
I expect using EnableDHCP over WMI and IP adress was cleared, but IP adress is still there
This(Picture)
public Collection<PSObject> Invoke(string Query)
{
Collection<PSObject> ps = new Collection<PSObject>();
try
{
using (PowerShell PSI = PowerShell.Create())
{
PSI.AddScript(Query);
ps = PSI.Invoke();
}
}
catch (Exception ex)
{
ps = null;
throw;
}
return ps;
}
Create a class to invoke PS Query
public List<YourObjectList> GetInfo(string ParametersIFYouWant)
{
List<YourObjectList> ListInfo = new List<YourObjectList>();
InvokePowerShellQuery powerShellQuery = new InvokePowerShellQuery();
string PSQuery = "Query Here as a string";
try
{
var PSOutput = powerShellQuery.Invoke(PSQuery);
//In your case you wont need unless in you PS Query you return a message or some other object you want to report back for success or failure.
foreach (var item in PSOutput)
{
//Fill ListInfo
}
}
catch (Exception ex)
{
Listinfo = null;
//handle however you want here
}
return Listinfo;
}
Send the query to your class
Quick Google search on PS Queries you may use
https://www.pdq.com/blog/using-powershell-to-set-static-and-dhcp-ip-addresses-part-1/
https://4sysops.com/archives/set-an-ip-address-and-configure-dhcp-with-powershell

Power up Hyper_V client in C# using WMI

I am new to coding the Hyper-V within WMI, and always welcome to a learning opportunity in this area.
There is a need for me to create a winform application that lists all VMs available within a computer. When a user clicks on one VM, it will launch the Hyper-V client window.
My codes below could pretty much start or stop any specific VM. However, it doesn't launch the hyper-v client window.
Here are my prototype codes (in command lines for now):
using System;
using System.Management;
namespace HyperVSamples
{
public class RequestStateChangeClass
{
public static void RequestStateChange(string vmName, string action)
{
ManagementScope scope = new ManagementScope(#"\\.\root\virtualization\v2", null);
ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
if (null == vm)
{
throw new ArgumentException(
string.Format(
"The virtual machine '{0}' could not be found.",
vmName));
}
ManagementBaseObject inParams = vm.GetMethodParameters("RequestStateChange");
const int Enabled = 2;
const int Disabled = 3;
if (action.ToLower() == "start")
{
inParams["RequestedState"] = Enabled;
}
else if (action.ToLower() == "stop")
{
inParams["RequestedState"] = Disabled;
}
else
{
throw new Exception("Wrong action is specified");
}
ManagementBaseObject outParams = vm.InvokeMethod(
"RequestStateChange",
inParams,
null);
if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
{
if (Utility.JobCompleted(outParams, scope))
{
Console.WriteLine(
"{0} state was changed successfully.",
vmName);
}
else
{
Console.WriteLine("Failed to change virtual system state");
}
}
else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
{
Console.WriteLine(
"{0} state was changed successfully.",
vmName);
}
else
{
Console.WriteLine(
"Change virtual system state failed with error {0}",
outParams["ReturnValue"]);
}
}
public static void Main(string[] args)
{
if (args != null && args.Length != 2)
{
Console.WriteLine("Usage: <application> vmName action");
Console.WriteLine("action: start|stop");
return;
}
RequestStateChange(args[0], args[1]);
}
}
}
Given:
The computer has Hyper-V manager installed with several pre-populated VMs.
Question:
How would I fire up the hyper-v client window from a winform?
Thanks
After taking some research, it appears firing up the hyper-v client is quite simple.... Below is the full function just in case anyone looks for it in the future...
public static string ConnectVM(string VMName)
{
var error = string.Empty;
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
//create a pipeline
var path = ConfigurationManager.AppSettings["VMConnectPath"];
var pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript($"& \"{path}\" localhost '{VMName}'");
try
{
pipeline.Invoke();
}
catch (Exception e)
{
error = e.Message;
}
runspace.Close();
return error;
}

C# WMI: Throws an error when I try to enable/disable PPPoE adapter

I'm trying to enable/disable my PPPoE adapter according to this answer.
It works great with normal adapters but not with PPPoE which throws an error saying :
An error occurred while querying for WMI data: Invalid method Parameter(s)
The adapter name is correct I used WMI Query tool for that purpose but I have no idea what params need to be set. Any help would be much appreciated.
Edit
Here's the code I used:
static void Main(string[] args)
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_NetworkAdapter WHERE Name = 'WAN Miniport (PPPOE)'");
foreach (ManagementObject queryObj in searcher.Get())
{
queryObj.InvokeMethod("Enable", null);
//Console.WriteLine("Name: {0}", queryObj["Name"]);
}
}
catch (ManagementException e)
{
Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
}
Console.ReadKey();
}
Ok I've found my way around with DotRas here's the code to connect/disconnect PPPoE connection (AKA dial up):
using System;
using System.Linq;
using System.Net;
using DotRas;
namespace Test_Reconnect_PPPoE
{
class Program
{
public static void Main(string[] args)
{
// Connect
using (RasDialer dialer = new RasDialer())
{
dialer.EntryName = "Your Entry (Connection Name)";
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
dialer.Credentials = new NetworkCredential("username", "password");
dialer.Dial();
Console.WriteLine("Connected");
}
// Disconnect
RasConnection conn = RasConnection.GetActiveConnections().Where(o => o.EntryName == "Your Entry (Connection Name)").FirstOrDefault();
if (conn != null)
{
conn.HangUp();
Console.WriteLine("Disconnected");
}
Console.ReadKey();
}
}
}
Hope this will help someone.

Getting the Drive letter of USB drive inserted

I have detected the insertion of USB drive in my WPF application by following suggestions in given link How do I detect when a removable disk is inserted using C#?
but I am unable to figure out the Drive letter of the detected USB drive; my Code is given below
static ManagementEventWatcher w = null;
static void AddInsertUSBHandler()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += USBInserted;
w.Start();
}
catch (Exception e) {
Console.WriteLine(e.Message);
if (w != null)
{
w.Stop();
}
}
}
static void USBInserted(object sender, EventArgs e)
{
Console.WriteLine("A USB device inserted");
}
Kindly guide me if possible.
Hope this helps, its code based on Neeraj Dubbey’s answer.
We run code in a continuous multithreaded loop that keeps checking for a change in connected USB devices. Because we keep track of previously connected devices we can compare it against a new list of devices. We can also determine if a device is removed using the same method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Usb_Test
{
class Program
{
private static List<USBDeviceInfo> previousDecices = new List<USBDeviceInfo>();
static void Main(string[] args)
{
Thread thread = new Thread(DeviceDetection);
thread.Start();
Console.Read();
}
static void DeviceDetection()
{
while (true)
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID")
));
}
if (previousDecices == null || !previousDecices.Any()) // So we don't detect already plugged in devices the first time.
previousDecices = devices;
var insertionDevices = devices.Where(d => !previousDecices.Any(d2 => d2.DeviceID == d.DeviceID));
if (insertionDevices != null && insertionDevices.Any())
{
foreach(var value in insertionDevices)
{
Console.WriteLine("Inserted: " + value.DeviceID); // Add your own event for the insertion of devices.
}
}
var removedDevices = previousDecices.Where(d => !devices.Any(d2 => d2.DeviceID == d.DeviceID));
if (removedDevices != null && removedDevices.Any())
{
foreach (var value in removedDevices)
{
Console.WriteLine("Removed: " + value.DeviceID); // Add your own event for the removal of devices.
}
}
previousDecices = devices;
collection.Dispose();
}
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}
}
However this is not a great solution, you're a lot better off listening for WM_DEVICECHANGE.
Windows will send WM_DEVICECHANGE message to all applications whenever
some hardware change occurs, including when a flash drive (or other
removable device) is inserted or removed. The WParam parameter of this
message contains code which specifies exactly what event occurred. For
our purpose only the following events are interesting:
DBT_DEVICEARRIVAL - sent after a device or piece of media has been
inserted. Your program will receive this message when the device is
ready for use, at about the time when Explorer displays the dialog
which lets you choose what to do with the inserted media.
DBT_DEVICEQUERYREMOVE - sent when the system requests permission to
remove a device or piece of media. Any application can deny this
request and cancel the removal. This is the important event if you
need to perform some action on the flash drive before it is removed,
e.g. encrypt some files on it. Your program can deny this request
which will cause Windows to display the well-known message saying that
the device cannot be removed now.
DBT_DEVICEREMOVECOMPLETE - sent after a device has been removed. When
your program receives this event, the device is no longer available —
at the time when Windows display its "device has been removed" bubble
to the user.
Here are some links that each detail an answer on how to do this in C#:
http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Program
http://www.codeproject.com/Articles/60579/A-USB-Library-to-Detect-USB-Devices
Both solution should work, and you can always adapt them to fit your needs.
Hey try this console based code afetr adding the reference of System.Management in project
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}", usbDevice.DeviceID);
}
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}

Categories