File: show_processes.rb

package info (click to toggle)
ruby-cstruct 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 412 kB
  • sloc: ruby: 1,008; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,134 bytes parent folder | download | duplicates (2)
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
# CStruct Examples
require 'windows/tool_helper'
require 'cstruct/win32struct'
include Windows::ToolHelper

module Windows
  MAX_PATH = 260
  module ToolHelper 
    class PROCESSENTRY32 < Win32Struct
      DWORD   :dwSize
      DWORD   :cntUsage
      DWORD   :th32ProcessID 
      DWORD   :th32DefaultHeapID
      DWORD   :th32ModuleID 
      DWORD   :cntThreads
      DWORD   :th32ParentProcessID
      LONG    :pcPriClassBase        
      DWORD   :dwFlags
      CHAR    :szExeFile,[MAX_PATH]  
    end
  end 
end

def show_all_processes
  proc_enrty32 = PROCESSENTRY32.new
  proc_enrty32.dwSize = PROCESSENTRY32.__size__
  snap_handle  = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS,0)
  
  if Process32First(snap_handle,proc_enrty32.data)
    show_process proc_enrty32 
    while true
      break if !Process32Next( snap_handle,proc_enrty32.data ) 
      show_process proc_enrty32
    end   
  end
  
end

def show_process proc_enrty32
    printf"%20s 0x%08X\n",proc_enrty32.szExeFile.to_cstr,proc_enrty32.th32ProcessID
end

printf"%20s %s\n","<Process Name>","<PID>"
printf"%20s %s\n","--------------","-----"
show_all_processes