forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Winapi program to kill running process

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
DanRRight



Joined: 10 Mar 2008
Posts: 2815
Location: South Pole, Antarctica

PostPosted: Tue Jun 10, 2008 9:45 pm    Post subject: Winapi program to kill running process Reply with quote

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 ?


Last edited by DanRRight on Sat Jun 14, 2008 6:36 am; edited 3 times in total
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7924
Location: Salford, UK

PostPosted: Wed Jun 11, 2008 10:19 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
DanRRight



Joined: 10 Mar 2008
Posts: 2815
Location: South Pole, Antarctica

PostPosted: Thu Jun 12, 2008 3:24 am    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7924
Location: Salford, UK

PostPosted: Thu Jun 12, 2008 7:40 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2388
Location: Yateley, Hants, UK

PostPosted: Fri Jun 13, 2008 2:16 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2815
Location: South Pole, Antarctica

PostPosted: Sun Jun 15, 2008 12:51 pm    Post subject: Reply with quote

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 Smile


Last edited by DanRRight on Tue Jun 17, 2008 10:11 am; edited 1 time in total
Back to top
View user's profile Send private message
JohnHorspool



Joined: 26 Sep 2005
Posts: 270
Location: Gloucestershire UK

PostPosted: Tue Jun 17, 2008 8:26 am    Post subject: Reply with quote

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 !!
Back to top
View user's profile Send private message Visit poster's website
DanRRight



Joined: 10 Mar 2008
Posts: 2815
Location: South Pole, Antarctica

PostPosted: Fri Jul 25, 2008 6:28 am    Post subject: Reply with quote

Here is the final code:
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>
   character*64 commandline, CMNAM
   character*256 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
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2815
Location: South Pole, Antarctica

PostPosted: Wed May 19, 2010 9:10 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Thu May 20, 2010 1:55 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2815
Location: South Pole, Antarctica

PostPosted: Thu May 20, 2010 11:30 pm    Post subject: Reply with quote

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 Smile

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group