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