Killing process

Discussion in 'Python' started by Harlin Seritt, Apr 30, 2005.

  1. 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?
     
    Harlin Seritt, Apr 30, 2005
    #1
    1. Advertising

  2. Harlin Seritt

    Roger Upole Guest

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

    Roger

    "Harlin Seritt" <> wrote in message
    news:...
    >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?
    >





    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
     
    Roger Upole, Apr 30, 2005
    #2
    1. Advertising

  3. 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
     
    Harlin Seritt, Apr 30, 2005
    #3
  4. Harlin Seritt

    Peter Hansen Guest

    Harlin Seritt wrote:
    > 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
     
    Peter Hansen, Apr 30, 2005
    #4
  5. 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.
     
    Harlin Seritt, Apr 30, 2005
    #5
  6. Harlin Seritt

    Peter Hansen Guest

    Harlin Seritt wrote:
    > 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
     
    Peter Hansen, Apr 30, 2005
    #6
  7. 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.
     
    Harlin Seritt, May 1, 2005
    #7
  8. Harlin Seritt

    Peter Hansen Guest

    Harlin Seritt wrote:
    > 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
     
    Peter Hansen, May 2, 2005
    #8
  9. # / 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?

    --
    How many Vietnam vets does it take to screw in a light bulb?
    You don't know, man. You don't KNOW.
    Cause you weren't THERE. http://bash.org/?255991
     
    Roman Neuhauser, May 18, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Johnson Sr.

    Process killing

    Michael Johnson Sr., Feb 10, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Michael Johnson Sr.
    Feb 10, 2004
  2. Shruti A via .NET 247
    Replies:
    0
    Views:
    753
    Shruti A via .NET 247
    May 15, 2004
  3. GregO
    Replies:
    0
    Views:
    422
    GregO
    Jul 20, 2004
  4. Anshuman
    Replies:
    0
    Views:
    415
    Anshuman
    Jan 4, 2005
  5. Sting

    killing a process in ms windows - newbie

    Sting, Dec 28, 2003, in forum: C Programming
    Replies:
    2
    Views:
    345
    Richard Bos
    Dec 29, 2003
Loading...

Share This Page