I have tried translating VB code to Ruby which tells if a process with given
pid is running on Win 2K.
The VB code works. But my translated Ruby code does not. What am I missing ?
I suspect my assigment to uProcess in the code below.
I using ruby 1.7.3 (2002-11-17) [i386-mswin32]
TIA,
– shanko
=begin
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long ’ This process
th32DefaultHeapID As Long
th32ModuleID As Long ’ Associated exe
cntThreads As Long
th32ParentProcessID As Long ’ This process’s parent process
pcPriClassBase As Long ’ Base priority of process threads
dwFlags As Long
szExeFile As String * 260 ’ MAX_PATH
End Type
Dim uProcess As PROCESSENTRY32
=end
require ‘Win32API’
STDOUT.sync = true
PROCESSENTRY32_SIZE = 296
def process_running?(pid,ppid = -1)
params = [ ‘L’, # IN DWORD dwFlags
’L’ # IN DWORD th32ProcessID
]
retVal = ‘L’ # HANDLE
getProcessSnapShot = Win32API.new(“kernel32”, “CreateToolhelp32Snapshot”,
params, retVal)
hProcess = getProcessSnapShot.call(8,0)
params = [ ‘L’, # hSnapshot [in] Handle to the snapshot returned from a
previous call to the CreateToolhelp32Snapshot function.
‘P’ # lppe [in, out] Pointer to a PROCESSENTRY32 structure
]
retVal = ‘I’ # BOOL
uProcess.dwSize = Len(uProcess) <<< how to translate this line of VB code
in Ruby ?
uProcess = [PROCESSENTRY32_SIZE].pack(‘I’) + ([0].pack(‘I’) *
(PROCESSENTRY32_SIZE - 4))
getProcessFirst = Win32API.new(“kernel32”, “Process32First”, params,
retVal)
getProcessNext = Win32API.new(“kernel32”, “Process32Next”, params,
retVal)
r = getProcessFirst.call(hProcess,uProcess)
found = false
while (r != 0)
puts r
gotPid = uProcess.th32ProcessID
gotPpid = uProcess.th32ParentProcessID
(found = true; break) if (gotPid == pid) and ((ppid >= 0) ? (ppid ==
gotPpid) : true)
r = getProcessNext.call(hProcess,uProcess)
end
found
end
if $0 == FILE
puts process_running?(1792)
end