Silverfrost Forums

Welcome to our forums

Winapi program to kill running process

10 Jun 2008 8:45 (Edited: 14 Jun 2008 5:36) #3333

Suppose you are recompiling the program. But at the link time the exe is not created because what happen is that same name previous erroneous process did not exited and is is still hanged somewhere. Or sometimes happen that you exited Clearwin program but it is still there as shows the Task Manager. So you go to Task Manager and kill the process manually. Sounds familiar?

Can we write the winapi fortran code which can look at the list of running processes and kill unneaded running exe process ?

11 Jun 2008 9:19 #3335

One way to do this would be to write a function in C/C++ using EnumProcesses, OpenProcess, EnumProcessModules, and GetModuleBaseName etc. This can be built with SCC and accessed from your Fortran code. Alternatively, a process that has a unique Window could be tracked down (directly from Fortran) using FindWindow.

To create a process with a given priority you could also write a function in C/C++ that uses CreateProcess and SetPriorityClass.

We could probably provide functions for you but we would have to charge for this service.

12 Jun 2008 2:24 #3337

No, Paul, forget C or SCC, everything must be done in Fortran! If this is not possible in Fortran, I do not care. This just means that Fortran (sort of, Winapi or that sort of things) is not super-duper. Couple of beers will be of course yours, just tell where to send 😃

12 Jun 2008 6:40 #3338

I am not sure that I understand your humour. A large part of the FTN95 library (and the compiler itself) is written in C/C++. What I am saying is that this particular task needs an extension to the library and that having SCC allows you to do this for yourself. If you want us to do this for you then it will cost a lot more than a couple of beers! Maybe even a few hundred but I guess you have worked this out.

13 Jun 2008 1:16 #3351

Dan, Paul,

I have had to do this procedure that Dan complains of, but have regarded it as an unpleasant fact of life when using Windows. SLINK won't save the EXE if an instance of the EXE is already running - or one of its windows. Since each Clearwin window has an independent existence, you can sometimes during development leave one or more of them lingering on as ghosts.

I can't get on with PLATO - not that there's anything wrong with it, but I'm happy with a batch file. I have a little program that can zap running instances of program X or its windows by sending a message to their 'classname' to shut down. This zapper goes into the batch file before the SLINK command.

If any Window has classname (say) Dan1, then

   REPLY='Not there'
   IA = SEND_TEXT_MESSAGE@('Dan1','Shut Down',REPLY)

If 'Dan1' is found, then Reply is changed, so you can test for it having the initial value or the reply (see below). Programs with classname 'Dan1' handle this message by shutting down - you have to program that.

The code for declaring a window to have classname Dan1 is:

   IA=WINIO@('%nc[Dan1]%rm&',MESSAGE_FN)

This defines the classname of the window, so maybe you need to declare each window with a different classname and send messages to all of them. You can send messages to non-existent windows without doing any harm

The handler for reading a windows message is ....

   INTEGER FUNCTION MESSAGE_FN()
   CHARACTER*(255) MESSAGE
   INCLUDE <WINDOWS.INS>
   MESSAGE = CLEARWIN_STRING@('MESSAGE_TEXT')
   IF (MESSAGE .EQ. 'Close Down') THEN
       CALL REPLY_TO_TEXT_MESSAGE@('YES')
       MESSAGE_FN = 0  ! this is a graceful way of exiting
       RETURN
   ENDIF
   MESSAGE_FN = 1
   RETURN
   END

During the development stage it is normally only one or two windows that live on as ghosts.

This takes some effort, but doesn't cost anything directly. Clearwin, as so often, has the tools - you just need to find them.

Regards

Eddie

15 Jun 2008 11:51 (Edited: 17 Jun 2008 9:11) #3362

Message passing would probably not always work because Clearwin program may hang somewhere and be nonresponsive...

The whole trick can be easily done this way

i=start_process@('tasklist >ZZZZZ,' ') i=start_process@('taskkill /pid 270772',' ')

(where 270772 is an example of process ID or PID found by first command, and ZZZZZ the file where PIDs are written and then read to fortran program).

But by some reason start_process@ works strange way. i=start_process@('tasklist >ZZZZZ,' ') does not write anything to file ZZZZZ. Though kills process fine if tell it PID manually. Even such DIR command does not work

use clrwin
INTEGER i, START_PROCESS@ 
i = START_PROCESS@('dir > ZZZZZ',' ')
end

Any idea why? The bug? The WinExec command

i = WinExec('tasklist /v >zzzzzz',0)

also produces empty zzzzz file. The only subroutine which works is

call cissue@('tasklist /v >zzzzz',ifail)

Whole 'killing' code is 40 lines. Though I would prefer to see all this done more elegant way - not to write process list to the disk and read it from there into program to find PID but use WinAPI.

Problem is that Salford's Winapi does not have of normal description of arguments of Winapi functions suitable for Fortran user. What info one can get from this description of TerminateProcess fror example?

  STDCALL TERMINATEPROCESS 'TerminateProcess' (VAL,VAL):LOGICAL*4 or 

STDCALL GETSYSTEMINFO 'GetSystemInfo' (REF) ???

That makes the use of WinAPI with Salford FTN95 a plain pure hell.

Can anyone send me the Intel/Compaq/DEC fortran Winapi description, I lost it somewhere and also lazy to search for compiler CD and install it ?

The whole solution is now elevated to 3 beers 😃

17 Jun 2008 7:26 #3369

Having never encountered 'tasklist' or 'taskkill' before, I found out that indeed they are valid commands in a DOS window on a PC with XP as the OS, however an older PC running Windows 2000 produces the familiar error message of an unknown command !!

25 Jul 2008 5:28 #3554

Here is the final code: ! Program TSKKILL ! Dan R Right, 2008 ! No warranties. ! ! USAGE at command prompt or in batch file: TSKKILL <name.exe> ! IT USES: System programs Tasklist.exe, Taskkill.exe ! GOOD FOR: killing of stuck in memory EXE when compiling new EXE ! USED THERMINOLOGY: PID - process ID ! ! Tasklist.exe shows the list of running processes and their PID ! Taskkill.exe kills the process with given PID. ! You define the needed name of killed process, like that ! C:\> TSKKILL.EXE notepad.exe ! This code saves the list of all processes on disk (file ! zzzzz), finds PID, and kills the process with found PID. ! ! The tasklist /? and taskkill /? commands will ! show all the options and explain you more ! what this program is doing ! include <clearwin.ins> character64 commandline, CMNAM character256 text256(1000), killline INTEGER START_PROCESS@, ifail

        commandline = CMNAM()

	call cissue@('tasklist /v >zzzzz',ifail)

	open(unit=1111,file='zzzzz')
	do i=1,1000
	read(1111,'(A)',err=1001,end=1001)text256(i)
	enddo

1001	close(1111,err=1002)

	nprocesses = i-1  ! nprocesses running (in tasklist zzzzz)
	do i=1,nprocesses
	k=index(text256(i),trim(commandline))
	if(k.gt.0) goto 2000
	enddo
	goto 1003

2000	neededLineNo=i   
!       neededLineNo - line number in tasklist with the defined name which we will kill 
!       search for start and end positions in found line where PID number is 
	k=index(text256(neededLineNo),'.exe')
	do i=k+4,256
	  if(text256(neededLineNo)(i:i).ne.' ') then
	  iStartPID =i  ! first position of PID number
	  GOTO 2001
	  endif
	enddo
	GOTO 1004

2001	do i=iStartPID,256
	  if(text256(neededLineNo)(i:i).eq.' ') then
	  iEndPID =I
	  goto 2002
	  endif
	enddo
	GOTO 1004

2002	read(text256(neededLineNo)
     *  (iStartPID:iEndPID),'(i10)') iPID
	print*, iPID

!       We found PID. Now we will kill the process with given PID

	killline='taskkill /F /pid '//
     *  text256(neededLineNo)(iStartPID:iEndPID)
	i=start_process@(killline,' ')
	goto 10000

1002	print*, 'Error 1002, no file to close'
	goto 10000
1003	print*, 'Error 1003, no such process found'
	goto 10000
1004	print*, 'Error 1004, no pid in the line'
	goto 10000

10000	continue
	end
19 May 2010 8:10 #6385

What folks think here, if we implement the same way the function which will permanently overview the Task Manager Process IDs in real time, can it be used as a kind of anti-hack sniffer?

I mean if you will get a virus or any other program (admittedly not too sophisticated ones which will be able to hide themselves and not appear in Task Manager, i do not know, if this is even possible of course) running on your PC this might catch them.

20 May 2010 12:55 #6392

There is (I'm told of !) a recent 'virus' that runs two processes, both hidden from task manager, and if one is killed, the other one restarts it. I sometimes wonder what my computer is doing when task manager says little is happening, indicating to me there are hidden processes! The process tab in task manager should also report bytes transferred over network connections, differentiating between file I/O and internet activity. Anyway, why are processes hidden from task manager ? Or am I becoming paranoid!

John

20 May 2010 10:30 #6399

Some paranoia is OK, 'Only the Paranoid Survive'(A.Grove). In extreme cases the solution from paranoia is just one: two computers, one is not connected to the internet or anything else 😃

I think i will try to make this small code, at least it will help to resolve quickly what the heck appears sometimes in Task Manager.

BTW, for such programs which temporally write data to harddrive because of use DOS shell, i found useful such things as RAM-drives. They are superfast and do not scramble the disk drive surface.

Please login to reply.