haskell / win32

Haskell support for the Win32 API
http://hackage.haskell.org/package/Win32
Other
97 stars 63 forks source link

add terminateProcess #91

Closed joeyh closed 7 years ago

joeyh commented 7 years ago

There currently seems to be no way to use this library to terminate an arbitrary process by ProcessID.

Some code that has worked for me to do this in the past is this:

import System.Win32.Process
import Control.Exception (bracket)
import Control.Monad

terminateProcess :: ProcessID -> IO ()
terminateProcess p = bracket 
        (openProcess pROCESS_TERMINATE False p)
        (void . c_closeProcess)
       (\h -> void $ c_TerminateProcess h 1)

 foreign import ccall unsafe "windows.h TerminateProcess"
        c_TerminateProcess :: ProcessHandle -> Int -> IO Int
joeyh commented 7 years ago

Might want to call it terminateProcessID instead to avoid name conflict with System.Process.terminateProcess