Connecting C# to a Named Pipe from Hyper-V Permissions - c#

I'm trying to write a very simple client in c# for a named pipe created by hyper-v on windows 8 pro.
The hyper-v named pipe is connected to the com port of a virtual machine.
The code i have written is:
static void PipeClient()
{
NamedPipeClientStream npc = new NamedPipeClientStream(".", "DebianCom1", PipeDirection.InOut);
npc.Connect();
var s = new StreamReader(npc);
var cont = true;
while (cont)
{
Console.Write(s.Read());
}
s.Close();
npc.Close();
}
It throws a System.UnauthorizedAccessException on the instantiation of the named pipe client.
Any pointers?

Try running your C# code as an elevated admin.

Related

Auto Detect The Application on LAN using C#.net

I have an Open Source Voice Chatting application which works fine on LAN.
But the problem is with connecting two PCs Manually.
Let's suppose there are two PCs (Application instances), PC A and PC B.
To make a connection I have to put the IP Address of the PC A into PC B and IP Address of the PC B into PC A.
I want to make the small code change where if the application is running on two PCs and they both are connected via LAN then both sides get the IP address automatically. Like auto-detection.
So my logic was to first know the IP Address on the LAN using arp -a command then writes the output in a text file and only obtain the IP addresses that start with 192... and check the instance of the application on each address using This Solution.
Unfortunately, I end up getting an error which states that.
Unhandled Exception: System.Runtime.InteropServices.COMException: The
RPC server is unavailable.
at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32
errorCode, IntPtr errorInfo) at
System.Management.ManagementScope.InitializeGuts(Object o) at
System.Management.ManagementScope.Initialize() at
System.Management.ManagementObjectSearcher.Initialize() at
System.Management.ManagementObjectSearcher.Get()
This is the code which i used to accomplish this task.
//Write the cmd Output in a text file
string strCmdText;
strCmdText = #"/K arp -a > C:\Test\Result.txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//To get the IP address which starts with the 192.
const string ipPattern = #"^\s*(192\.168\.\d{1,3}\.\d{1,3}\b)";
var ipRegex = new Regex(ipPattern);
var ipAddresses192168 = File.ReadAllLines(#"C:\Test\Result.txt")
.Skip(3) // Skip 3 lines
.Where(line => ipRegex.IsMatch(line))
.Select(line => ipRegex.Match(line).Groups[1].Value);
foreach (var ipAddress in ipAddresses192168)
{
Console.WriteLine(ipAddress);
// Check for the running instance of the application on LAN network.
ManagementScope scope = new ManagementScope(#"\\" + ipAddress + #"\root\cimv2");
string query = "SELECT * FROM Win32_Process WHERE Name='WavPlayer.exe'";
var searcher = new ManagementObjectSearcher(query);
searcher.Scope = scope;
bool isRunning = searcher.Get().Count > 0;
}
So my question is, Is there any other straight forward process to accomplish this task using C#.net?
I am available to provide more info about this question so any help is appreciated.

Failed to open virtual machine

I want to Power Off a VM, I am able to connect to vm using ConnectToVMWareVIServer method of VIX but I am getting error on when I try to Open the connection.
I am getting an exception Failed to open Virtaul machine..
with inner exception "One of the paramters was invalid"
I am new to VMWare hence I used the c# code mentioned in https://github.com/dblock/vmwaretasks
using (VMWareVirtualHost virtualHost = new VMWareVirtualHost())
{
// connect to a remove (VMWare ESX) virtual machine
virtualHost.ConnectToVMWareVIServer(vmIPAddress, vmUserID, vmPassword);
// open an existing virtual machine
using (VMWareVirtualMachine virtualMachine = virtualHost.Open(vmVMXPath))
{
// power on this virtual machine
virtualMachine.PowerOff();
// wait for VMWare Tools
}
}

DCOM Object Reference C#

I am trying to add a reference of the remote computer in a local network (computer name or IP address) to a DCOM object. Just for example:
When I do the following it works on the local machine:
private void btnOpnDWSFT_Click(object sender, EventArgs e)
{
DEWEsoft.IApp x = new
DEWEsoft.App();
x.Init();
x.Width = 1000;
x.Height = 800;
x.Visible = true;
}
But where shall I add the reference to the Remote Machine? BTW: The DCOM server is running and is registered on the remote machine and I got it working in LabView somehow (with ActiveX ObjectRef) but I couldn't get it work in C#.
I would like to remote controll SW called DEWEsoft on a different Machine.
Thank you!

I want create a Connect forwarding to SSH server with library on website www.bitvise.com

I created a connect to SSH server forwarding with library "FlowSshNet64_Framework40.dll" (my operating system is 64 bit) in C# but while I'm running error after import library to application and follow the instructions:
https://www.bitvise.com/fsd-client
Bitvise.FlowSshNet.Client t = new Client();
t.SetProxyHost("66.172.203.200");
t.SetProxyOptions(true);
t.SetProxyUserName("admin");
t.SetProxyPassword("admin");
t.SetProxyPort(22);
t.SetProxyType(ProxyType.Socks5);
ForwardingRule setup = new ForwardingRule();
setup.ClientToServer = true;
setup.ListInterface = "127.0.0.1";
setup.ListPort = 1080;
ForwardingHandler setup1 = new ForwardingHandler();
if (setup1.IsDisposed)
{
t.AddForwarding(setup, setup1);
label1.Text = "connected";
}
Picture of the error:
you can use FlowSshNet32_Framework40.dll on the os 64 bit
to run this you must copy all file to same directory such as bin\Debug. I do it and it doesn't have error
Binaries\CiProv32.dll
Binaries\CryptoPP530Fips32.dll
Binaries\FlowSshC32.dll
Binaries\FlowSshNet32_Framework40.dll -> RENAME THIS FILE TO: FlowSshNet32.dll
But i use your code and it not forwarding to ssh

How to execute process on remote machine, in C#

How can I start a process on a remote computer in c#, say computer name = "someComputer", using System.Diagnostics.Process class?
I created a small console app on that remote computer that just writes "Hello world" to a txt file, and I would like to call it remotely.
Console app path: c:\MyAppFolder\MyApp.exe
Currently I have this:
ProcessStartInfo startInfo = new ProcessStartInfo(string.Format(#"\\{0}\{1}", someComputer, somePath);
startInfo.UserName = "MyUserName";
SecureString sec = new SecureString();
string pwd = "MyPassword";
foreach (char item in pwd)
{
sec.AppendChar(item);
}
sec.MakeReadOnly();
startInfo.Password = sec;
startInfo.UseShellExecute = false;
Process.Start(startInfo);
I keep getting "Network path was not found".
Can can use PsExec from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
Or WMI:
object theProcessToRun() = { "YourFileHere" };
ManagementClass theClass = new ManagementClass(#"\\server\root\cimv2:Win32_Process");
theClass.InvokeMethod("Create", theProcessToRun);
Use one of the following:
(EDIT) Remote Powershell
WMI (see Ivan G's answer)
Task Scheduler API (http://msdn.microsoft.com/en-us/library/windows/desktop/aa383606%28v=vs.85%29.aspx)
PsExec
WshRemote object with a dummy script. Chances are, it works via DCOM, activating some of scripting objects remotely.
Or if you feel like it, inject your own service or COM component. That would be very close to what PsExec does.
Of all these methods, I prefer task scheduler. The cleanest API of them all, I think. Connect to the remote task scheduler, create a new task for the executable, run it. Note: the executable name should be local to that machine. Not \servername\path\file.exe, but c:\path\file.exe. Delete the task if you feel like it.
All those methods require that you have administrative access to the target machine.
ProcessStartInfo is not capable of launching remote processes.
According to MSDN, a Process object only allows access to remote processes not the ability to start or stop remote processes. So to answer your question with respect to using this class, you can't.
An example with WMI and other credentials as the current process, on default it used the same user as the process runs.
var hostname = "server"; //hostname or a IpAddress
var connection = new ConnectionOptions();
//The '.\' is for a local user on the remote machine
//Or 'mydomain\user' for a domain user
connection.Username = #".\Administrator";
connection.Password = "passwordOfAdministrator";
object[] theProcessToRun = { "YourFileHere" }; //for example notepad.exe
var wmiScope = new ManagementScope($#"\\{hostname}\root\cimv2", connection);
wmiScope.Connect();
using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
managementClass.InvokeMethod("Create", theProcessToRun);
}
I don't believe you can start a process through a UNC path directly; that is, if System.Process uses the windows comspec to launch the application... how about you test this theory by mapping a drive to "\someComputer\somePath", then changing your creation of the ProcessStartInfo to that? If it works that way, then you may want to consider temporarily mapping a drive programmatically, launch your app, then remove the mapping (much like pushd/popd works from a command window).

Categories

Resources