Getting handle is invalid message in startprocess cmd.exe on windows10 - c#

Hello i have a c# programm that opens a cmd.exe redirect its standardinput and runs a command, after that i take a screenshot of the console and close the window.
This works fine on windows 7 but on windows 10 i get lots of "The handle is invalid" messages in the console instead of my command.
This occurs only when i do that in a windowsforms application a console application works fine even on windows 10
Here is a sample code that reproduces the problem
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/K";// chcp 65001";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.WorkingDirectory = #"c:\";
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
StreamWriter encodedWriter = new StreamWriter(myProcess.StandardInput.BaseStream, Encoding.GetEncoding(850));
encodedWriter.WriteLine("abcdef gä öü");
encodedWriter.Flush();
Thread.Sleep(2500);
myProcess.Kill();
it is in some way connected between redirecting the input, windows forms and windows 10. If one of these conditions is false their are no invalid handle messages

Related

Run File on Server from WPF Button Press

Some application code using Process.Start() and PsExec.exe to start a video on a serverpc is running OK when run from a Console application, but not when run from a button press in a WPF application. This sent me nuts today.
So:
I am running a small WPF app on a PC which, once a button is pressed, will send a command to a server PC to run a video file. I am using PsExec.exe to run the process on the server interactively (did not manage with WMI)
This is the code I'm using:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = #"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4";
p.StartInfo.CreateNoWindow = true;
p.Start();
No, this exact same code can open video file Photoslideshow.mp4 on the server PC (192.168.1.3) when run from a normal console App. However when I try to run it after pressing a button in a WPF app, p.Start() gives me a "The system cannot find the file specified" error. Here is the WPF code snippet (it's the same as above):
private void Video1_btn_Click(object sender, RoutedEventArgs e)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = #"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4 //fullscreen";
p.StartInfo.CreateNoWindow = true;
p.Start();
}
}
When I tried to use p.Start on a file that is local to my PC, that opened as expected. It's just the server that is not 'seeing' the FileName. As I said originally, the same code only fails on p.Start when accessing it after a button click.
What am I doing wrong? Please someone tell me that this is the result of hours in front of the PC and that it's only a stupid mistake which I cannot see!.
EDIT:
After more playing around,I realised that the error "The system cannot find the file specified" related to this line:
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
enter image description here
Upon changing this line to:
p.StartInfo.FileName =#"C:\Windows\System32\Notepad.exe";
and removing the next line, Notepad opens up on my local PC. However, when I change the 2 lines back to something like:
p.StartInfo.FileName = #"Notepad.exe";
p.StartInfo.Arguments = #"\192.168.1.3 -u Administrator -p pass-i cmd.exe /c START C:\realtek.txt";...
notepad opens on my local PC but an error "Network path not found". (which is a similar error to when I run the 'non-button- code.)
Thus I know the problem is something to do with either the WPF/Button application. But I dont know what the problem is!
thanks a lot,
Mario
I managed to figure it out.
For some reason, the system was not finding PSExec from this path (even though it existed in that folder).
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
Upon copying the File to another directory and using the full path, it finally worked.

I remaked console app to WinForm app. I can't get output from another console app now

I had C# console app, which runs another Borland C console app. It worked okay.
But after I remake the first app to WinForm app, I can't get output from Borland C console app.
Code is here:
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = name1;
p.Start();
StreamReader sr = p.StandardOutput;
progOutput = sr.ReadToEnd();
//progOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
The string progOutput is empty. I get the same result if I use line
progOutput = p.StandardOutput.ReadToEnd(); instead of StreamReader
Borland C console app really started and created it's output files normally.
But it's output disappeared in WinForm app.
As I said this code worked okay in C# console app,
but does not work now in C# Windows Form app.
BTW, if I run C# console app in WinForm app, output is okay.
Somehing wrong with Borland C output?
But it worked when I ran Borland C app in C# console app.
So I am puzzled.
Thanks
In some scenarios I found beneficial to launch executables indirectly like so..
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/C " + name1;
..especially when having output redirect issues. But you say it worked as a console app?!
check
var errorOut = p.StandardError.ReadToEnd();
you might get output as StandardError. You can find many questions related to this, we expect output from StandardOutput but it redirect to StandardError.

C# WinForms Process Encoding problem

I am writing a windows forms application in C#
I have a Process Object which runs a cmd command and returns it's output.
Process Pro = new Process();
Pro.StartInfo.FileName = "cmd.exe";
Pro.StartInfo.Arguments = "<Dos Command here>";
Pro.StartInfo.CreateNoWindow = true;
Pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Pro.StartInfo.RedirectStandardOutput = true;
Pro.StartInfo.UseShellExecute = false;
Pro.Start();
Which works fine! However if the output of the command is not ASCII(in my case Greek), the Output are random symbols. Surely an encoding problem.
If i run the same code on a console application everything runs smoothly.
I tried reading the Base stream as UTF-8, but no luck!
System.IO.StreamReader Rdr = new System.IO.StreamReader(Pro.StandardOutput.BaseStream, Encoding.UTF8);
Is there any way to read the output properly in a winform application?
Thnx!
The real solution is base on this:
unicode-characters-in-windows-command-line-how
check here:
Wiki code page
for the code page you need.
you can also do an ugly hack, writing the command to a batch file (f.e foo.bat)
then running it as foo.bat > log.txt
then you can read the output from log.txt.

Redirect StandardIn when opening a shortcut

Due to the joys of UAC, I need to open an elevated command prompt programmatically and then redirect the standard input so I can use the time command.
I can open the link (a .lnk file) if I use
Process ecp = System.Diagnostics.Process.Start("c:/ecp.lnk");
however, if I use this method, I can't redirect the standardIn.
If I use the StartProcessInformation method (which works wonderfully if you are calling an exe)
ProcessStartInfo processStartInfo = new ProcessStartInfo("c:/ecp.lnk");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
StreamWriter inp = process.StandardInput;
StreamReader oup = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();
I get the error message:
The specified executable is not a valid Win32 application.
Can anyone help me create an elevated command prompt which I can capture the standard input of? Or if anyone knows how to programatically escalate a command prompt?
In case no-one comes up with a better idea (pretty please), here is the work around one of the more devious in my office just came up with:
Copy cmd.exe (the link it pointing at this file)
Paste this file into a different directory
Rename the newly pasted file to something different
Set the permissions on this new file to Run As Administrator
You will still get the escalation dialog popping up, but at least you can capture the standardIn of this valid Win32 app!

Permission issues when running JScript from C# Console application

I'm trying to run a Jscript task from a C# console application.
The Jscipt file is not mine so I can't change it. The script moves some files and this is what is causing the issues.
When I run the script manually, i.e. form the shell it executes correctly. When I try and run the script from my console application the bulk of the process runs but I get a ":Error = Permission denied" error when it tries to move the files.
I've tried every permutation of the Diagnostics.Process class that I can think of but I've had no luck.
My current code:
Process process = new Process();
process.StartInfo.WorkingDirectory = Path.GetDirectoryName((string)path);
process.StartInfo.FileName = #"cmd.exe";
process.StartInfo.Arguments = "/C " + (string)path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Verb = "runas";
process.StartInfo.LoadUserProfile = true;
process.StartInfo.Domain = "admin";
process.StartInfo.UserName = #"cardax_sync_test";
process.StartInfo.Password = GetSecureString("abc123");
process.Start();
process.WaitForExit();
Any ideas?
Thanx
Rookie Mistake!
I forgot to close the text reader that creates one of the input files for the jscript.
I'll submit this question for deletion when it get's old enough. Don't want more useless info clogging up the net!

Categories

Resources