Run exe with an input file and close upon process exit
Afficher commentaires plus anciens
Hi,
I am creating a GUI where the user is to input parameters, MATLAB then creates an input file (exp.in) that is to be read into an exe. Once the cfd computations have completed, I would like the exe to close itself (if it is successful). I have the following created using similar queries by others however do not know how to go about passing the exe an input file.
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'P:\path...cfd.exe';
%proc.StartInfo.Arguments = 'P:\path...exp.in';
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
StreamWriter = proc.StandardInput;
sIn.AutoFlush = true;
sIn.write("dir P:\path...exp.in")
sIn.write("exit")
sIn.Close()
if isempty(proc)
error('Failed to launch process');
end
while true
if proc.HasExited
fprintf('\nProcess exited with status %d\n', proc.ExitCode);
break
end
fprintf('.');
pause(1.0);
end
Is there a way to pass on an input file for the exe to use. I have tried to use arguments but it doesn't work. Research suggests controlling process input via streamwriter.
I can use the system command to do so but then don't know how to close the exe once it finishes (have used taskkill to no avail; it closes before the process finishes).
Thanks in advance!
4 commentaires
OCDER
le 14 Août 2018
Does the exe file have the ability to take an input like this?
system('cfd.exe -i inputfile.in')
Or must you actively put in the inputs after cfd.exe is opened up?
>> system('cfd.exe')
cfd] -i inputfile.in
cfd] exit
>>
Muhammad Munib
le 3 Sep 2018
Modifié(e) : Muhammad Munib
le 3 Sep 2018
Guillaume
le 3 Sep 2018
"in a more conventional way"
Not that conventional. There's a big difference between:
C:\> cfd.exe -i inputfile.in
which pass the name of a file to the executable and leave it up to the executable to read the file
and
C:\> cfd.exe < inputfile.in &
which tells the command prompt to read the inputfile and redirect its content to the executable (which would normally take its input from the keyboard). No argument is actually passed to the executable.
By the way, I'm not sure what that final & is by the way, as far as I know it's not legal or has no effect on its own.
As far as I can tell, the .Net code you show replicate that streaming process so if there's a problem you need to clarify what it actually is. Perhaps, what you write to the input stream is not correct.
As for, "With the above I get the error 'Struct contents reference from a non-struct array object.'". Of course, as documented status is numeric, not a structure. I've no idea where you picked up
if status.HasExited
it's not going to work since as said status is numeric (either 0 or 1) and additionally, does not get updated once you've received its value.
Anyway, the way to pass arguments to your executable and the way to terminate it once it's done what you need is going to depend entirely on the way that executable is designed. Hence you need to read its documentation and explain to us how that works, or if its documentation is available publicly give us a link.
Muhammad Munib
le 3 Sep 2018
Réponse acceptée
Plus de réponses (1)
Fangjun Jiang
le 14 Août 2018
Try to run system() manually first or open a command window and then type in line by line
cfd.exe <exp.in
exit
1 commentaire
Muhammad Munib
le 3 Sep 2018
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!