Launching C# program from another C# program - c#

Due to me having knowledge of launching apps I am aware that you have multiple ways of launching an application in C# .NET, but I'm running into a issue that occurs when attempting to launch a SDL2 application.
I have attempted the following using the Process class to:
Start the .exe file of the build.
Start the application using "cmd.exe /K" or "cmd.exe /c" followed by "exec" or "call" or "start" followed by "{path to file}" or "{path to batch file to launch the application}". Launching the application via a batch file and CMD works fine. But, whenever I attempt to even launch the application (even in a new instance of Command-Prompt launched from cmd.exe /? start cmd.exe ?params) it will yield no result.
What I can observe is that the application tries to open. It takes forever to launch into the Window mode (starting the 3D environment). After a timeout it will either, render a couple of frames of a blank window before closing or close immediately after opening the window.
So my question is, does anyone have succesfully made a launcher application for a SDL app written in C# .NET? Or knows a way to debug this behaviour? Because unfortunately, the app does not send out a error message and since SDL safely closes the application I can't observe a crash either.
Edit #1
I'm not doing anything fancy with parameters as there shouldn't be any. I already have another one functioning that launches a normal C# application as my launcher requires to open 2 programs. 1 SLD application, 1 COM:VBA controlling application.
Given:
string audioSpectrumProgram = "AudioSpectrum.exe";
string audioSpectrumBatchProgram = "AudioSpectrum.bat";
private void BtnLaunchPPTApp_OnClick()
{
//Powerpoint controlling application
pVBAApp = Process.Start(presenterProgram, $"\"{this.path}\" {this.audioFormatParams[0]} {((this.ckboxGenerate.Checked) ? "--create" : "")} lang={this.languageCodesParams[this.cboxLanguage.SelectedIndex]}");
}
Method 1:
private void BtnLaunchSDLApp_OnClick()
{
pVizualizer = Process.Start(audioSpectrumProgram); //file launched from local path (is correct)
}
Method 2:
pVizualizer = Process.Start(audioSpectrumBatchProgram); //file launched from local path (is correct)
Method 3:
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
FileInfo spectrumFileInfo = new FileInfo(audioSpectrumProgram);
if (spectrumFileInfo.Exists)
info.Arguments = $"/c \"{spectrumFileInfo.FullName}\"";
pVizualizer = Process.Start(info);
Method 4:
based on senario of method 3. You don't have to parse arguments using ProcessStartInfo.
pVizualizer = Process.Start($"cmd.exe /K call \"{spectrumFileInfo.FullName}\"") //to observe what happens to the application
Edit #2
Not affected by changing the UseShellExecute to true or false
private void btnOpenVisualizer_Click(object sender, EventArgs e)
{
FileInfo spectrumFileInfo = new FileInfo(audioSpectrumProgram);
ProcessStartInfo info = new ProcessStartInfo(spectrumFileInfo.FullName);
info.UseShellExecute = true;
pVizualizer = new Process();
pVizualizer.StartInfo = info;
pVizualizer.EnableRaisingEvents = true;
pVizualizer.Exited += new EventHandler(myProcess_Exited);
pVizualizer.Start();
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine(
$"Exit time : {pVizualizer.ExitTime}\n" +
$"Exit code : {pVizualizer.ExitCode}\n"
);
}

A general way of analyzing startup issues is to use SysInternals Process Monitor.
Record the application that is not starting up properly. Use a filter for your application. Then go through all items which don't have SUCCESS in the result column. Typically you want to do that bottom-up, since the last error is the one stopping your application from loading.
Like this you'll find common startup issues like:
missing DLLs or other dependencies
old DLLs or DLLs loaded from wrong location (e.g. registered COM components)
wrong working directory, e.g. access to non-existent config files

Ok For Future reference:
Pathing to the files can be correct and everything might be in order but if you are using DLLs for imports. Change the process's working directory.
The project will run, libs can "sometimes" be found but can cause a weird unknown bug like this one. So the most optimal way of running another C# instance with SDL or any other kind of library:
private void RunSDLProgram()
{
FileInfo spectrumFileInfo = new FileInfo("pathToFile.exe");
ProcessStartInfo info = new ProcessStartInfo(spectrumFileInfo.FullName);
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.WorkingDirectory = spectrumFileInfo.DirectoryName;
pVizualizer = new Process();
pVizualizer.StartInfo = info;
pVizualizer.EnableRaisingEvents = true;
pVizualizer.Exited += new EventHandler(myProcess_Exited);
pVizualizer.Start();
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine(
$"Exit time : {pVizualizer.ExitTime}\n" +
$"Exit code : {pVizualizer.ExitCode}\n" +
$"output : {pVizualizer.StandardOutput}\n" +
$"err : {pVizualizer.StandardError}\n"
);
}
Running a batch file will look at it's own directory and makes all references local, but it won't alter the working directory. (already had my suspicions about changing the work directory but I didn't see a way to call 2 opperations in process.start("cmd.exe");)

Related

.net core can't open file from python script

Someone i know has made a python script that checks validity of some files. Right now that only works local, but he wants it to work cloud based. He gave me the .exe file with a few other files that i can use. I'm using .net core 2 for my backend. I can start the .exe file just fine. But when the python code tries to open the config file he made, the program returns it cannot find the file.
try:
json_data=open('config.txt').read()
except FileNotFoundError:
sys.exit('FATAL ERROR: config.txt not found!') <--returns this
except Exception as e:
sys.exit('FATAL ERROR: {}'.format(e))
The file is in the same folder as the .exe. Whenever i just run it straight from windows it works perfect and it can find the file. But whenever i run the program from within my .net core webapp it gives back that error. I'm using visual studio 2017 for development and debugging.
Here is my c# code i got from a post on stackoverflow to run the exe:
public IActionResult Index(string cmd, string args)
{
try
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "Python\\Windows 64-bit\\mdqt.exe";
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
start.UseShellExecute = false;
start.CreateNoWindow = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
string result = "";
while (!process.HasExited)
{
while (!process.StandardOutput.EndOfStream)
{
result += process.StandardOutput.ReadLine();
}
while (!process.StandardError.EndOfStream)
{
result += process.StandardError.ReadLine();
}
}
return Ok(result);
}
}
catch (Exception e)
{
return Ok(e);
}
}
Could it be some file permissions issue? If so how can i fix it?
EDIT:
I have set the working directory using the following code:
start.WorkingDirectory = Path.Combine(hostingEnvironment.ContentRootPath.ToString(), "Python\\Windows 64-bit");
It now gives me an error it cannot find the folder. Yet when I put the link into the filemanager(and ofcourse delete 1 backslash everywhere), it directs me to the right folder. Could this be a permissions issue? The folder I'm linking to is within the project folder, so IIS should be able to reach that folder as it has permission to interact with the parent folders. What am I overlooking?

How do I force standard output on a C# Process when UseShellExecute == false?

I am running processes from C# using the following code;
private static void ExecuteShellCMD(string workingDir, string commandWithArgs, bool bWait = true)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "runas";
info.FileName = "cmd.exe";
info.WorkingDirectory = workingDir;
info.Arguments = "/C " + commandWithArgs;
info.UseShellExecute = false;
using (Process myProcess = Process.Start(info))
{
if (bWait)
{
myProcess.WaitForExit();
}
int ExitCode = myProcess.ExitCode;
//Log exit code here.
}
}
It loads an elevated command window and executes the code/bat file I pass it, but without logging anything to the console. This doesn't appear to be consistent on other machines, and has worked in the past on my machine, and I wondered if anyone had any ideas about how I can consistently make this Process just print logs into the command window the process makes.
I can see logs if I set UseShellExecute = true but then can't use Verb without accepting the elevation prompt which is undesirable.
I have tried looking for solutions around the web, and I am aware that I can redirect the output using other settings. Most of the questions and tutorials on this subject seem to deal with redirecting the ouput to somewhere else but I want to be able to keep track of the progress in the command window itself.
Perhaps I have missed an command line argument or similar?
Turns out this was actually a bug in Unity Hub. The process and output were working fine, however when ran from a Unity instance that was launched from Unity Hub it took control of the output and didn't release it. This was solved by just launching Unity directly and a bug report has been filed against Unity hub.

Elevated executable can't find OS Tool

I'm trying to create a shortcut in my C# WinForm application for users to launch Microsoft's "Digitizer to Monitor Mapping Tool" (MultiDigiMon.exe). However it seems that even if ran as an administrator my applications or cmd processes launched from it can not find the executable in question and I have no idea why.
My Short cut consists of a simple button with the click event hooked to the fallowing function.
private void TouchButton_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo("C:\\Windows\\System32\\cmd.exe");
start.WorkingDirectory = "C:\\Windows\\System32";
start.CreateNoWindow = false;
start.UseShellExecute = false;
//start.Arguments = "/K \"cd C:\\Windows\\System32 && MultiDigiMon.exe -touch\"";
Process.Start(start);
}
In my experience on Win10 systems, MultiDigiMon.exe can be always be found in C:\Windows\System32 (assuming the C drive is your root). Additionally the arguments line above was commented out to make the following screen shots as identical as possible.
On the left is the command prompt launched using the above code and on the right is a command prompt launched by typing cmd in to the OS's taskbar and and running the first result as administrator. As you can see both promps are marked as administrator but the left fails to launch MultiDigiMon.exe and won't even list the file exist using the dir command. the right seems to have no issue with either command. (MultiDigiMon.exe dose not seem to do anything at all if multiple touch screen devices are not connected and I do not believe that it give any text output especially if successful).
Finally I have also checked the permission on MultiDigiMon.exe and (with the exception of TrustedInstaller who has full control) everybody has read and execute right. Could someone explain why I can't launch MultiDigimon.exe form the left prompt or directly from my application?
As commented on by RbMm the issue was that since my application was being compiled to run on any cpu it was treated as a 32 bit application by the file system and redirected all access to the System32 directory to SysWOW64.
To get around this you can substitute "System32" width "Sysnative" as discussed here. In my case my final code ended up looking like the fallowing.
private void TouchButton_Click(object sender, EventArgs e)
{
ProcessStartInfo start = new ProcessStartInfo("C:\\Windows\\Sysnative\\MultiDigiMon.exe");
start.WorkingDirectory = "C:\\Windows\\System32";
start.CreateNoWindow = false;
start.UseShellExecute = false;
start.Arguments = "-touch";
Process.Start(start);
}

clear icon cache in win 7 programmatically - execute ie4uinit.exe-ClearIconCache using C# or Visual Basic

We changed the logo-icon of our WPF application, and then the icon of the main executable. On my PC with Win 7, there is a problem with the refresh of the icon cache: the desktop shortcut to the main executable, and the preview of the icon of the executable, in Windows Explorer still shows the old icon.
Even restarting the system the problem persists.
I found that running this command solves the problem:
ie4uinit.exe-ClearIconCache
My problem is that I can't run it from code. I made two attempts.
First:
Dim si As New ProcessStartInfo()
si.CreateNoWindow = False
si.UseShellExecute = False
si.FileName = "ie4uinit.exe"
si.WindowStyle = ProcessWindowStyle.Hidden
si.Arguments = "-ClearIconCache"
Dim p As Process = Process.Start(si)
error: "Could not find the specified file" - I tried to input the full path but it still doesn't find the file
Second:
I put the command in a batch file
Dim si As New ProcessStartInfo("C:\test.bat")
si.UseShellExecute = False
si.RedirectStandardError = True
si.RedirectStandardInput = True
si.RedirectStandardOutput = True
si.CreateNoWindow = True
si.ErrorDialog = False
si.WindowStyle = ProcessWindowStyle.Hidden
Dim p As Process = Process.Start(si)
This time I get no errors, but not even the desired effect. If I double-click on the batch file instead, everything is working fine.
I'd like to adjust one of these procedure otherwise finding a new one to clear the windows icon cache. C# or Visual Basic is not important...
Pileggi
maybe it doesn't search for it in the path try using:
as the path "%WINDIR%\System32\ie4uinit.exe",
if this doesnt work try "C:\Windows\System32\ie4uinit.exe"
I found the solution: I had to build the executable that runs the batch file for "Any CPU", otherwise it has not sufficient permissions to run ie4unit.
Before I was trying building for "x86" and I was running the process on a Win7 64 bit...
I had a similar issue, trying to call ie4uinit from an Inno installer. The PATH did include the right system directories; however, doing a "dir" did not show that the file exists. In fact, there were over 100 *.exe files that could not be found from whatever shell was executing the command. Opening Explorer or a command window reveals the file is there (which of course we know). I think it is a permissions or access issue. I didn't have the patience to trace it further, but just copied ie4uinit.exe to a local directory and had my installer execute it there.
Enables or disables file system redirection for the calling thread.
[DllImport("Kernel32.dll")]
private static extern bool Wow64EnableWow64FsRedirection(bool Wow64FsEnableRedirection);
//.....
Wow64EnableWow64FsRedirection(false);
Dim p As Process = Process.Start(si)
Wow64EnableWow64FsRedirection(true);
You can try this:
Dim objProcess As System.Diagnostics.Process
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.FileName = "ie4uinit.exe"
objProcess.StartInfo.Arguments = "-ClearIconCache"
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
objProcess.Start()
objProcess.WaitForExit()
objProcess.Close()

Self deletable application in C# in one executable

Is it possible to make an application in C# that will be able to delete itself in some condition.
I need to write an updater for my application but I don't want the executable to be left after the update process.
There is an official .Net OneClick but due to some incompatibilities with my HTTP server and some problems of OneClick itself I'm forced to make one myself.
George.
[EDIT]
In more details:
I have:
Application Executable which downloads the updater ("patch", but not exactly) this "patch" updates the application executable itself.
Application executes as folowed:
Application: Start -> Check Version -> Download new Updater -> Start Updater -> exit;
Updater: Start -> do it's work -> start Application Executable -> self delete (this is where I get stuck);
If you use Process.Start you can pass in the Del parameter and the path to the application you wish to delete.
ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info);
Code snippet taken from this article
I suggest you use a batch file as a bootstrap and have it delete itself and the exe afterwards
public static class Updater
{
public static void Main()
{
string path = #"updater.bat";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("updater.exe");
sw.WriteLine("delete updater.exe /y");
sw.WriteLine("delete updater.bat /y");
}
System.Process.Start(path);
}
else
{
RunUpdateProcess();
}
}
private void RunUpdateProcess()
{
.....
}
}
It's tricky without introducing yet another process (that you'd then want to delete as well, no doubt). In your case, you already have 2 processes - updater.exe and application.exe. I'd probably just have the Application delete updater.exe when it's spawned from there - you could use a simple command line arg, or an IPC call from updater.exe to application.exe to trigger it. That's not exactly a self deleting EXE, but fulfills the requirements I think.
For the full treatment, and other options you should read the definitive treatment of self deleting EXEs. Code samples are in C (or ASM), but should be p/invokable.
I'd probably try CreateFile with FILE_FLAG_DELETE_ON_CLOSE for updater.exe with something like (psuedo code):
var h = CreateFile(
"updater.exe",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE
);
byte[] updaterBytes = GetUpdaterBytesFromWeb();
File.WriteAllBytes("updater.exe", updaterBytes);
Process.Start("updater.exe");
Once application.exe exits, updater.exe has a file handle of 1. When updater.exe exits, it drops to 0 and should be deleted.
Couldn't you simply delete the updater from within the application? i.e.:
Application: Start -> [Delete old updater if present] -> Check version -> Download new updater -> Start updater -> exit;
Updater: Start -> Perform update -> Start application -> exit;
Application: Start -> [Delete old updater if present] -> ...
Mhh so let me get this straight. You got some application.exe and your updater application updater.exe?
So when you start your application.exe it checks some webserver for a newer version and then starts updater.exe. And you want updater.exe to delete itself after it has finished updating? Or do you want to delete the downloaded patch (or similar)? Please be a bit more precise.
Consider that when you are deleting updater.exe you must recreate it for the next update process.
your second line can be
Updater: Star -> do it's work -> start Application Executable -> Updater Exits -> Application deletes your Updater.exe
public void uninstall() {
string app_name = Application.StartupPath + "\\" + Application.ProductName + ".exe";
string bat_name = app_name + ".bat";
string bat = "#echo off\n"
+ ":loop\n"
+ "del \"" + app_name + "\"\n"
+ "if Exist \"" + app_name + "\" GOTO loop\n"
+ "del %0";
StreamWriter file = new StreamWriter(bat_name);
file.Write(bat);
file.Close();
Process bat_call = new Process();
bat_call.StartInfo.FileName = bat_name;
bat_call.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
bat_call.StartInfo.UseShellExecute = true;
bat_call.Start();
Application.Exit();
}
self delete by an external executable file ".bat" for windows form applications.

Categories

Resources