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

Terminating a Created Process

Intel_C_Intel
Employee
1,337 Views
If I use CreateProcess (from kernel32 ) to start up another *.exeprocess, how can I terminate it?
.....
lstatus = CreateProcess(null_character, &
command_line(1:len_trim(command_line)) // " "C, &
NULL_security_attributes, &
NULL_security_attributes, &
.FALSE., &
dwCreate, &
NULL, &
null_character, &
startup_info,process_info)
....
more code and time passes - now Iwant to terminate the executable I started with CreateProcess. What do I use? ExitProcess seems to only kill my program -- leaving the created process still running.
0 Kudos
7 Replies
Jugoslav_Dujic
Valued Contributor II
1,337 Views

TerminateProcess(process_info%hProcess, iWhatever)

Note that it should be used only as the last resort -- is there by any chance a friendlier way to close it? Is it a GUI process or a console one?

Side note: you should call CloseHandle(process_info%hProcess) and CloseHandle(process_info%hThread) when you don't need them.

Jugoslav

0 Kudos
Intel_C_Intel
Employee
1,337 Views
I was trying to advoid having the user click on the OK/Cancel button in the created process (in this case Gnuplot) then clicking on a button in my program to bring up another plot. It would be nice to just select what you want from my progam with out having to manually stop Gnuplot eah time the user select a different variable to be plotted. I looked at TerminateProcess, but was conerned about its use cluttering up memory.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,337 Views

In this case, the "nice" way to close it is to send WM_CLOSE message to GNUPlot's window:

i = SendMessage(hGnuPlot, WM_CLOSE, 0, 0)

However, obtaining handle to the window hGnuPlot can be tricky. You can use:

a) FindWindow API if you know the window's title (i.e. it's always the same & unique), or,

b) a bit more bulletproof, EnumThreadWindows(process_info.dwThreadID, YourEnumProc, LOC(hGnuPlot)). In LOGICAL YourEnumProc(hWnd, hGnuPlot), where hWnd should have VALUE attribute, test whether

IF (IAND(GetWindowLong(hWnd, GWL_STYLE), WS_OVERLAPPED) /= 0) THEN
hGnuPlot = hWnd
YourEnumProc = .FALSE.
ELSE
YourEnumProc = .TRUE.
END IF

In other words, the window with WS_OVERLAPPED style within the GnuPlot's main thread is the one.

Jugoslav

0 Kudos
Intel_C_Intel
Employee
1,337 Views
I did get TerminateProcess to work. Haven't looked at what it does to memory, but at least you gave me enough nerve to try it! Thanks!
0 Kudos
Intel_C_Intel
Employee
1,337 Views
Just our of curiosity, have you looked into using the Array Visualizer rather than using GnuPlot? You would be able to launch the Viewer as a seperate process (using avNewViewer) and not have to use TerminateProcess to close it.

Is there a gnuPlot feature that you need that is missing from the AV product?

John
0 Kudos
Intel_C_Intel
Employee
1,337 Views
I've been using GnuPlot for many years -- sometimes its just not easy to change. Someday, I may switch to MatlLab, but Gnuplot is cheap, works and produces far better plots that Excel. I'll take another look at Array Visualizer also when I have some spare time. It would always be nice to have eveyrthing in one package.
0 Kudos
Intel_C_Intel
Employee
1,337 Views
I know it's really hard finding the time to try out new stuff...

But try running one of the AV Fortran samples when you get a chance.
(such as: /Program Files/Intel/Fortran/Array Visualizer/Samples/Fortran/Simple)

One nice feature (which I don't think you can do from gnuPlot or Excel), is that as your program runs you can save the graphs+data to a file. The file can be opened later in the Viewer for off-line viewing.

John
0 Kudos
Reply