[WIN] Process running?

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

Hi,

···

----- Original Message -----
From: “Shashank Date” sdate@everestkc.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, February 20, 2003 11:04 PM
Subject: [WIN] Process running ?

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]

Refer to my code and modify for your purpose.

Park Heesob.

require ‘Win32API’

PROCESSENTRY32_SIZE = 296
TH32CS_SNAPPROCESS = 0x2
INVALID_HANDLE_VALUE = -1

CreateToolhelp32Snapshot = Win32API.new(“kernel32”,
“CreateToolhelp32Snapshot”,[‘L’,‘L’], ‘L’)
Process32First = Win32API.new(“kernel32”, “Process32First”, [‘L’,‘P’], ‘L’)
Process32Next = Win32API.new(“kernel32”, “Process32Next”, [‘L’,‘P’], ‘L’)
CloseHandle = Win32API.new(“kernel32”, “CloseHandle”, [‘L’], ‘L’)

def getProcessList ()

#  Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot.Call(TH32CS_SNAPPROCESS, 0)

return nil if (hProcessSnap == INVALID_HANDLE_VALUE)

#  Fill in the size of the structure before using it.
# pe32.dwSize = sizeof(PROCESSENTRY32)
pe32 = [PROCESSENTRY32_SIZE].pack("L") + "\0" * (PROCESSENTRY32_SIZE-4)

#  Walk the snapshot of the processes, and for each process,
#  display information.

if (Process32First.Call(hProcessSnap, pe32)!=0)
    begin
      th32ProcessID = pe32.unpack("LLLLLLL")[2]
      th32ParentProcessID = pe32.unpack("LLLLLLL")[6]
      printf( "PID(PPID)\t\t\t%d(%d)\n",

th32ProcessID,th32ParentProcessID)
end while (Process32Next.Call(hProcessSnap, pe32)!=0)
bRet = true
else
bRet = nil # could not walk the list of processes
end
# Do not forget to clean up the snapshot object.
CloseHandle.Call(hProcessSnap)
return (bRet)
end

if $0 == FILE
getProcessList
end

“Park Heesob” phasis@bcline.com wrote in message

Refer to my code and modify for your purpose.
Park Heesob.

That worked fine …
Thanks a lot, Park !

In article 002801c2d94b$32dc05d0$8d1b31d3@ADSL, phasis@bcline.com
poosted code using ToolHelp32…

IMHO, these lib doesn’t exist on WinNT. Although OP wrote he’s using
W2k, it may be possible cold should be run sometimes on such a machine,
which will fail. I’d suppose using psapi.dll (EnumProcesses() etc.).
In fact, my customers regularly use code which was written for W2k on
WNT or W98 or …

My point was: Is there a Ruby extension which adresses this process-
problem in a portable way?

Michael B.

“Michael Bruschkewitz” brusch2@gmx.net wrote in message

IMHO, these lib doesn’t exist on WinNT.

You are right ! I was not paying attention to the help on MSDN where I read
about it.
Thanks for pointing it out to me.

I’d suppose using psapi.dll (EnumProcesses() etc.).

But from what I have read about psapi.dll it is available mainly for Win NT
which I did not care too much.
I may have missed the important part. Do you know if it is truly cross
platform over all Wins?
If yes, I will certainly consider using it.

In fact, my customers regularly use code which was written for W2k on
WNT or W98 or …

Yes, that possibility exists. Incidentally, CreateToolhelp32Snapshot is
available on:

Client: Included in Windows XP, Windows 2000 Professional, Windows Me,
Windows 98, and Windows 95.
Server: Included in Windows Server 2003 and Windows 2000 Server.

My point was: Is there a Ruby extension which adresses this process-
problem in a portable way?

Not yet, to my knowledge. I am sure there are plans to make it available in
the future releases.

Michael B.
Thanks,
– shanko

In article 5fe8970718196f958823bd5af5d6ba66@news.teranews.com,
sdate@everestkc.net says…

But from what I have read about psapi.dll it is available mainly for Win NT
which I did not care too much.
I may have missed the important part. Do you know if it is truly cross
platform over all Wins?
If yes, I will certainly consider using it.
I know it works on 2k(Prof. and Server) and on NT. I’ve currently no
other Box available (still haven’t bought these VM-Ware license…) to
check 95/98/Me.

Michael B.

Michael Bruschkewitz brusch2@gmx.net wrote in message news:MPG.18c33973b3eb58cf989694@192.168.0.61

In article 5fe8970718196f958823bd5af5d6ba66@news.teranews.com,
sdate@everestkc.net says…

But from what I have read about psapi.dll it is available mainly for Win NT
which I did not care too much.
I may have missed the important part. Do you know if it is truly cross
platform over all Wins?
If yes, I will certainly consider using it.
I know it works on 2k(Prof. and Server) and on NT. I’ve currently no
other Box available (still haven’t bought these VM-Ware license…) to
check 95/98/Me.

Michael B.

This weekend I tried a few of the sample programs from MSDN
(EnumProcesses), linked against psapi.lib using VC++ 6.0, on my
Windows XP (Home) laptop, and they seemed to work ok.

That makes my life a bit easier, since I’m planning on adding Windows
support to sys-proctable.

Regards,

Dan

Dan