Class: ChildProcess::Windows::Handle

Inherits:
Object
  • Object
show all
Defined in:
lib/childprocess/windows/handle.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle, pid) ⇒ Handle

Returns a new instance of Handle.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/childprocess/windows/handle.rb', line 26

def initialize(handle, pid)
  unless handle.kind_of?(FFI::Pointer)
    raise TypeError, "invalid handle: #{handle.inspect}"
  end

  if handle.null?
    raise ArgumentError, "handle is null: #{handle.inspect}"
  end

  @pid    = pid
  @handle = handle
  @closed = false
end

Class Method Details

.open(pid, access = PROCESS_ALL_ACCESS) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/childprocess/windows/handle.rb', line 8

def open(pid, access = PROCESS_ALL_ACCESS)
  handle = Lib.open_process(access, false, pid)

  if handle.null?
    raise Error, Lib.last_error_message
  end

  h = new(handle, pid)
  return h unless block_given?

  begin
    yield h
  ensure
    h.close
  end
end

Instance Method Details

#closeObject



76
77
78
79
80
81
# File 'lib/childprocess/windows/handle.rb', line 76

def close
  return if @closed

  Lib.close_handle(@handle)
  @closed = true
end

#exit_codeObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/childprocess/windows/handle.rb', line 40

def exit_code
  code_pointer = FFI::MemoryPointer.new :ulong
  ok = Lib.get_exit_code(@handle, code_pointer)

  if ok
    code_pointer.get_ulong(0)
  else
    close
    raise Error, Lib.last_error_message
  end
end

#send(signal) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/childprocess/windows/handle.rb', line 52

def send(signal)
  case signal
  when 0
    exit_code == PROCESS_STILL_ALIVE
  when WIN_SIGINT
    Lib.generate_console_ctrl_event(CTRL_C_EVENT, @pid)
  when WIN_SIGBREAK
    Lib.generate_console_ctrl_event(CTRL_BREAK_EVENT, @pid)
  when WIN_SIGKILL
    ok = Lib.terminate_process(@handle, @pid)
    ok or raise Error, Lib.last_error_message
  else
    thread_id     = FFI::MemoryPointer.new(:ulong)
    module_handle = Lib.get_module_handle("kernel32")
    proc_address  = Lib.get_proc_address(module_handle, "ExitProcess")

    thread = Lib.create_remote_thread(@handle, 0, 0, proc_address, 0, 0, thread_id)
    thread or raise Error, Lib.last_error_message

    Lib.wait_for_single_object(thread, 5)
    true
  end
end

#wait(milliseconds = nil) ⇒ Object



83
84
85
# File 'lib/childprocess/windows/handle.rb', line 83

def wait(milliseconds = nil)
  Lib.wait_for_single_object(@handle, milliseconds || INFINITE)
end