Class: SleipnirAPI::Process

Inherits:
Object
  • Object
show all
Extended by:
FFI::Kernel32, FFI::User32, Registry
Defined in:
lib/sleipnir_api/process.rb

Overview

:nodoc:

Constant Summary collapse

@@wbem =
nil

Constants included from FFI::User32

FFI::User32::GW_HWNDFIRST, FFI::User32::GW_HWNDLAST, FFI::User32::GW_HWNDNEXT, FFI::User32::GW_HWNDPREV

Class Method Summary collapse

Methods included from FFI::Kernel32

get_long_path_name

Methods included from FFI::Base

#define_ffi_entry

Methods included from FFI::User32

each_child_window, find_child_window_by_classname, get_class_name, get_first_child, get_first_sibling, get_last_sibling, get_next_sibling, get_prev_sibling, get_top_window, get_window, get_window_process_id, get_window_rect, get_window_thread_id, get_window_thread_process_id, list_window, with_window_dc

Class Method Details

.create(timeout = 3) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/sleipnir_api/process.rb', line 19

def create(timeout = 3)
  exe_path = local_server
  unless File.exist?(exe_path)
    raise_create_error "Sleipnir.exe not found `#{exe_path}'"
  end
  clazz = wbem.Get("Win32_Process")
  r = clazz.Create(exe_path, nil, nil, nil)
  raise_create_error "error=#{r}." unless r.zero?
  wait_for_launch(timeout)
end

.escape(str) ⇒ Object



56
57
58
# File 'lib/sleipnir_api/process.rb', line 56

def escape(str)
  str.gsub(/\\/){ "\\\\" }
end

.exist?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/sleipnir_api/process.rb', line 15

def exist?
  not query_sleipnir.Count.zero?
end

.query_sleipnirObject



52
53
54
# File 'lib/sleipnir_api/process.rb', line 52

def query_sleipnir
  wbem.ExecQuery("select * from Win32_Process where ExecutablePath='%s'" % escape(local_server))
end

.raise_create_error(msg) ⇒ Object



40
41
42
# File 'lib/sleipnir_api/process.rb', line 40

def raise_create_error(msg)
  raise SleipnirAPI::ProcessError, "Failed to create Sleipnir process: #{msg}."
end

.terminate!(handle) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/sleipnir_api/process.rb', line 44

def terminate!(handle)
  proc_id = get_window_process_id(handle)
  query_sleipnir.each do |obj|
    return obj.Terminate if obj.ProcessId == proc_id
  end
  nil
end

.wait_for_launch(timeout) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/sleipnir_api/process.rb', line 30

def wait_for_launch(timeout)
  begin
    timeout(timeout) {
      sleep(0.1) until exist?
    }
  rescue Timeout::Error
    raise_create_error("timed out")
  end
end

.wbemObject



61
62
63
64
65
66
67
# File 'lib/sleipnir_api/process.rb', line 61

def wbem
  unless @@wbem
    locator = WIN32OLE.new("WbemScripting.SWbemLocator")
    @@wbem = locator.ConnectServer(".")
  end
  @@wbem
end