Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28996 Discussions

Console output capture using SystemQQ/RunQQ/etc

longden_loo
Beginner
882 Views
I have a need to run a 3rd party program inside Fortran (CVF) which I've been able to do using SYSTEM, SYSTEMQQ and RUNQQ. However the 3rd party program writes a lot of console messages which I'd like to both suppress from the user's view, and also to capture so I can scan it programmatically for key messages in my CVF app running on Windows XP.

Is there a simple way of doing this in CVF? I see references to CreateProcess() and haven't tried it yet, but reading the CVF helps on the other commands didn't seem to show any special mechanism/options for output control. When the program is run manually from the commandline, I can redirect the output to a file(s), and it shows that the program is making use of both stdout and stderr ... so I need some way to control these outputs when running inside a CVF app.

Any suggestions, sample code, or doc links appreciated. Thanks.

- Longden
0 Kudos
6 Replies
Steven_L_Intel1
Employee
882 Views

Well, CreateProcess will certainly do it - you can specify a handle of your choice for standard output. Or you could do it as simply as:

i = SYSTEM('dir > dir.txt')

0 Kudos
longden_loo
Beginner
882 Views
How embarrassing.

Maybe I should ask a more challenging question like "what's your favorite color" :)

Thx.

BTW, is there any advantage to using SYSTEM vs SYSTEMQQ vs RUNQQ?
0 Kudos
Steven_L_Intel1
Employee
882 Views
Well, don't be hard on yourself - some of these routines don't execute in a shell so can't do redirection. I think RUNQQ is one of them. SYSTEMQQ is pretty much the same as SYSTEM, I think. There may be differences as to whether any wait for completion or not. Then there's my favorite, ShellExecute, a Win32 API routine, perfect for opening documents or running programs without a command window opening.
0 Kudos
longden_loo
Beginner
882 Views
The Fortran Help mentions using Win API CreateProcess(). Is there a reason for your preference of ShellExecute() over CreateProcess()?

Also, there is a possibility that the command being executed may lock or otherwise take a long time, in which case it'd be nice to be able to monitor the execution time from the calling CVF code and cancel the shell (or thread?) if it exceeds a specific time without returning control (ie, ending). Are any of the candidate functions/WinAPIs better suited for something like this?
0 Kudos
Steven_L_Intel1
Employee
882 Views
ShellExecute is for when you just want to start a program in the background and keep going. It's especially handy for opening documents such as web pages as you don't have to specify a program to open the document. It's much easier to use than CreateProcess and does not create a console window the way that RUNQQ and friends do. But once the process is created, you have no control over it and can't even identify it.

If you want to monitor the process you would most likely want to use CreateProcess to get the process handle.
0 Kudos
anthonyrichards
New Contributor III
882 Views

Use ShellExecuteEx and you get the process ID returned in the ShellExecuteInfo structure you have to use.

0 Kudos
Reply