-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.rb
executable file
·48 lines (40 loc) · 1.44 KB
/
process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def get_process_list
process_list = []
hsnapshot = Win.CreateToolhelp32Snapshot(3, 0)
return process_list if !hsnapshot
pe_pointer = FFI::MemoryPointer.new :char, Win::ProcessEntry32.size, true
pe = Win::ProcessEntry32.new pe_pointer
pe[:dwSize] = Win::ProcessEntry32.size
Win.Process32First(hsnapshot, pe_pointer)
proc_hash = {}
proc_hash[:name] = pe[:szExeFile].to_s
proc_hash[:pid] = pe[:th32ProcessID].to_i
process_list << proc_hash
while(Win.Process32Next(hsnapshot, pe_pointer) != 0)
proc_hash = {}
proc_hash[:name] = pe[:szExeFile].to_s
proc_hash[:pid] = pe[:th32ProcessID].to_i
process_list << proc_hash
end
process_list
end
def get_pid_by_name(process_name)
process_name = process_name.downcase.gsub(/\.exe$/, '')
process_list = get_process_list
process_list.each do |process|
name = process[:name].downcase.gsub(/\.exe$/, '')
return process[:pid] if name == process_name
end
nil
end
def create_process(cmd_line)
si_pointer = FFI::MemoryPointer.new :char, Win::StartupInfo.size, true
si = Win::StartupInfo.new si_pointer
si[:cb] = Win::StartupInfo.size
si[:dwFlags] = 1
si[:wShowWindow] = 0
pi_pointer = FFI::MemoryPointer.new :char, Win::ProcessInfo.size, true
pi = Win::ProcessInfo.new pi_pointer
Win.CreateProcessA(nil, cmd_line, nil, nil, false, 8,
nil, nil, si_pointer, pi_pointer)
end