Module: MCLimit::Win

Defined in:
lib/mc-limit/winfns.rb

Constant Summary collapse

WM_CLOSE =
0x10
SendMessage =
Win32::API.new('SendMessage', 'LLLL', 'I', 'user32')
EnumWindows =
Win32::API.new('EnumWindows', 'KP', 'L', 'user32')
GetWindowText =
Win32::API.new('GetWindowText', 'LPI', 'I', 'user32')
GetWindowThreadProcessId =
Win32::API.new('GetWindowThreadProcessId', 'LP', 'L', 'user32')

Class Method Summary collapse

Class Method Details

.close_process(pids, text) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/mc-limit/winfns.rb', line 28

def self.close_process(pids, text)
  pids.each do |pid|
    handle = window_for_pid(pid, 'Minecraft')
    next if handle.nil?
    SendMessage.call(handle, WM_CLOSE, 0, 0)
    break
  end
end

.window_for_pid(pid, text) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mc-limit/winfns.rb', line 12

def self.window_for_pid(pid, text)
  result = nil
  finder = Win32::API::Callback.new('LP', 'I') do |handle, param|
    pidp = [0].pack('L')
    GetWindowThreadProcessId.call(handle, pidp)
    if pid == pidp.unpack('L')[0]
      buf = "\0" * 200
      GetWindowText.call(handle, buf, 200)
      result = handle if buf.strip == text
    end
    true
  end
  EnumWindows.call(finder, nil)
  result
end