Killing process

H

Harlin Seritt

I am using os.getpid() to get the pid value for a script running. I
store that value (as a string) to a file. Later when I try to kill that
pid (i pull this from the file as a string value) I get errors.

Using the following lines I get the subsequent error.

(pid is a string value)
win32api.TerminateProcess(int(pid), 0)

OR

ctypes.windll.kernel32.TerminateProcess(int(pid), 0)

Errors:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, i
return self.func(*args)
File "vngconsole.py", line 27, in StopVngSvc
win32api.TerminateProcess(int(pid), 0)
error: (6, 'TerminateProcess', 'The handle is invalid.')

How exactly do I kill a pid using a string value?
 
R

Roger Upole

TerminateProcess takes a process handle, not a pid.
win32api.OpenProcess will give you a handle.

Roger
 
H

Harlin Seritt

Yeah I've kind of figured that. I was just wanting to know what I could
use to kill a pid that is a string value. Thanks though.

Harlin
 
P

Peter Hansen

Harlin said:
I am using os.getpid() to get the pid value for a script running. I
store that value (as a string) to a file. Later when I try to kill that
pid (i pull this from the file as a string value) I get errors. [...]
File "vngconsole.py", line 27, in StopVngSvc
win32api.TerminateProcess(int(pid), 0)
error: (6, 'TerminateProcess', 'The handle is invalid.')

I believe the problem is you are assuming TerminateProcess() takes a
PID, when in fact it takes a "handle". See this recipe for some hints:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462
and
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp
to learn more about the key function you're missing.

-Peter
 
H

Harlin Seritt

I think I need something besides TerminateProcess(). Is there anyway
possible to terminate a process by just passing a string value to the
function? Honestly, I am not interesting in terminating a process by
its handle.
 
P

Peter Hansen

Harlin said:
I think I need something besides TerminateProcess(). Is there anyway
possible to terminate a process by just passing a string value to the
function? Honestly, I am not interesting in terminating a process by
its handle.

This is a bizarre request. Why can't you just call int() as you did in
your example to turn the string into a number? And if you can do that,
why would you have a problem using the *defined mechanism* to convert
from a PID to a handle, which is what the Windows API routine to kill
processes requires?

-Peter
 
H

Harlin Seritt

I actually tried mapping the PID to an integer value and it still
didn't work. At any rate, I found another way to do it. Thanks anyways.
 
P

Peter Hansen

Harlin said:
I actually tried mapping the PID to an integer value and it still
didn't work. At any rate, I found another way to do it. Thanks anyways.

For those who might follow in your footsteps, it might be nice to
describe your solution... (and to reduce the likelihood somebody will
have to spend time repeating all those links and ideas again...)

Thanks,
-Peter
 
R

Roman Neuhauser

# (e-mail address removed) / 2005-05-01 05:07:27 -0700:
I actually tried mapping the PID to an integer value and it still
didn't work. At any rate, I found another way to do it. Thanks anyways.

What the kind people have been trying to get through is that the
win32api.TerminateProcess() does *NOT* take a pid (whether integer
or numeric string).

Got it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top