Console window still popping up even after ProcessWindowStyle.Hidden; - c#

I have to run a console application from my Windows Application. The console application I want to run is an Embedded Resource in my application, and I am calling it like this:
// Run the updater and grab its output
Process Updater = new Process();
Updater.StartInfo.FileName = "C:\\tmp\\tmp.exe";
Updater.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Updater.StartInfo.UseShellExecute = false;
Updater.StartInfo.RedirectStandardOutput = true;
Updater.Start();
string UpdaterOutput = Updater.StandardOutput.ReadToEnd();
Updater.WaitForExit();
It extracts fine, and it runs fine, and it also grabs its output completely fine... but I can still see the console Window popping open quickly as it's run. I know the console pop up is from this application because the console title is C:\tmp\tmp.exe. Is there any completely fail proof way to hide the console application? I thought using ProcessWindowStyle.Hidden would do it but apparently not.
Thanks.

Set the ProcessStartInfo.CreateNoWindow property to true

Related

C# Console Application fails reading files on restart

Its hard to place everything in the title, but I hope I can explain this as good as I can. Basicly I made a C# Console Application that reads a certain file every now and then. The Application runs on a Virtual Machine from Google Instances. Now I have created another Console Application that is capable of managing the other application, like closing them or restarting the executables.
The Issue:
When I start the Console Application by hand (manual), it works fine and reads the files like it should. I could close and open this as much as I like and it still works. The problem is when my second Console Application tries to restart the first Console Application. The restart works fine and most functions like certain ftp connections work, but it stops reading files and gives a null back as result. There happen to be no debug errors nor does it display an error on the console.
What I want is that the second application could restart my first application without making it run where some functions appear to be blocked.
The Code I use:
string loc = File.ReadAllText(Directory.GetCurrentDirectory() + "\\"+ "location.txt");
Process p = new Process();
p.StartInfo.FileName = loc;
p.StartInfo.UseShellExecute = true;
p.Start();
I tried running it as p.StartInfo.Verb = "runas"; but this has no positive result either. Could this be an issue with the Google Virtual Machine, possible firewall settings or code related issues.
Extra
This code does work on my own laptop and so does it work after restarting.

Launch a command shell from a console application run via Windows Task Scheduler

I have a Windows console application that is launched via a schedule setup in Task Scheduler. This console application, as part of its normal runtime, will launch a command prompt in order to run a java program. No, I have no control over the design of the Java program. It was supplied to me as is and I have no rights or access to make changes to it. I also cannot implement it in another language. I must use what was given to me.
At any rate, when my console application tries to run the command prompt it will work just fine if I'm launching the application manually. However, when I try it as an action within Task Scheduler, my console application will start and run as expected until it needs to launch the command prompt. At this point, the console application exits. No error message or code is provided.
How do I get the command prompt window to start as a new window from within my console application when no one is logged into the server?
Thanks for any hints or suggestions you can provide.
* UPDATE *
Here is the code snippet that launches the program from within my console application:
string parameter_save_path = #"C:\output\folder"
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo start_info = new System.Diagnostics.ProcessStartInfo();
start_info.WorkingDirectory = #"C:\mtselect-client";
start_info.FileName = "cmd.exe";
start_info.Arguments = "/C run.bat \"" + parameter_save_path + "\"";
process.StartInfo = start_info;
process.Start();
process.WaitForExit();
The run.bat is what launches the java program.
I think it's too late for this message, but...
Maybe in your batch file you are running your java application with something like: java -jar ApplicationName
First I would do should be comment out the "#echo off" from the batch file, next trace out the batch lines with one echo "x" (being x a natural number starting from 1 and increasing by 1 in each ocurrence). Next I will add a line with java -version, and so I will be sure java app is installed and accesible.
Maybe java needs be ran by an authenticated user and so have java_home defined. Maybe the application needs some JVM parameters like memory size, etc.
Have good luck, tell me and I will try to help

Invoke console app from windows service

I have a simple windows service which i need to use to invoke a console application.The console app generates pdf to print by opening the adobe reader window.Running the console app works fine to print pdf.But invoking it from service not successful.It doesnt even show up the console window where i log events.
Process pdfprocess = new Process();
pdfprocess.StartInfo.FileName = #"C:\Documents and Settings\xyz\Desktop\dgdfg\PdfReportGeneration\bin\Debug\PdfReportGeneration.exe";
pdfprocess.StartInfo.UseShellExecute = false;
pdfprocess.StartInfo.RedirectStandardOutput = true;
pdfprocess.Start();
But invoking other application like
pdfprocess.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe";
works fine.
What will be the reason?
There is probably some permissions issue there (PdfReportGeneration.exe inaccessible under service account or maybe something that it uses...)
I would advise to capture Process Monitor log to see where exactly it fails.
Windows services run in a different window station and cannot interact with the desktop, unless you're using an older version of Windows and tick a checkbox in the service properties in the service manager.

How to call buggy .dll in new process?

I have a c# windows service application that is crashing without throwing an exception when processing certain files using a third-party .dll. What I decided to do was create a new console application which replicates a small portion of the windows service code, particularly the part the causes the service to crash. What I want to do is call the new .exe program from the windows service, and if it crashes, I throw an exception myself.
So, I need to call this .exe program (not in the background as I can't allow the windows service to continue until I know the file to be processed is safe), and then determine if it exited successfully or not. How do I go about doing this? The examples I've seen run the .exe in a background process which is not what I want.
Thanks.
Look at this SO answer how to run console application from windows service. Just add WaitForExit, like this :
ProcessStartInfo info = new ProcessStartInfo(#"c:\myprogram.exe");
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.ErrorDialog = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(info);
process.WaitForExit();
In console application you can set exit code if you exit with Environment.Exit(statusCode) or return int value from main function of console applicaiton. Or you can write to output and then in your service examine exit code (process.ExitCode) or output stream so you can determine is process was exited successfully.

Windows SDK - C# - Debugging process exiting with error code -1073741502

SHORT VERSION
How do you figure out which DLL is failing to load (and potentially why) when a process exits with error code -1073741502?
LONG VERSION
I'm trying to write a pretxnchangegroup hook for Mercurial, and as a part of that hook I need to get the output of running the command:
hg log
The code that I'm using to start and run the hg.exe process is as follows:
string Command = "log";
Process p = new Process();
ProcessStartInfo psi = p.StartInfo;
p.StartInfo.FileName = #"C:\Program Files (x86)\Mercurial\hg.exe";
psi.CreateNoWindow = true;
psi.LoadUserProfile = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.Arguments = Command;
// Pass-through environment variables
psi.UserName = Properties.Settings.Default.HG_User;
psi.Domain = Properties.Settings.Default.HG_Domain;
psi.Password = new System.Security.SecureString();
foreach (char c in Properties.Settings.Default.HG_Pass)
{
psi.Password.AppendChar(c);
}
p.Start();
p.WaitForExit();
The problem is that the process keeps exiting with error code -1073741502, without outputting anything on Standard Output or Standard Error. After some research online, I discovered that this error code has something to do with the application failing to initialize properly (couldn't find DLL's, maybe?), but I have no idea how to go about figuring out how to fix it.
Keep in mind that this hook is being called for when I'm pushing to the repository over the web (so, IIS is calling the Mercurial CGI via Python, which has this program configured as a hook).
In a totally different web application, I'm able to run HG commands just fine, and I'm also able to run this by doing
runas /user:<same account as in the code> /noprofile cmd.exe and then manually typing in the hg command line.
Also, if I set UseShellExecute = true, then it executes just fine, but then I can't get the Standard Output. I'm really tempted to just make a web service call to the web app which is able to execute this command successfully, but that'd be a really ugly solution.
Any ideas why this thing isn't executing?
I was able to resolve this by disabling UAC so it sounds like a permissions problem even though I do not know the exact details.

Categories