Gettig the pid of a process started with Win32_popen

Hi,

I have a problem with getting the pid of a program I opened with the
popen2-method of the Win32_popen-Module. When I start my program with
popen2 (on NT 4.0) I see two new processes in the process list. One is
the program I started the other is a CMD.exe.
Now I can communicate with the programm via pipes and it works fine. But
now I want to kill the program again (with the kill-method) of the Module
and I need the pid for it.
I tried to get the pid with IO#pid (using one of the pipes) but it returns
the pid of the CMD.exe.
Does anybody know a way to get the right pid (or better to avoid to start
the CMD.exe while starting my program)?

Thanks

Oliver

···

Erster Klick - SMS versenden, zweiter Klick - die Telefonnummer im
Adressbuch speichern bei: http://freemail.web.de/features/?mc=021151

Hi,
“Oliver Osterholz” oliverosterholz@web.de wrote in message
news:200303180820.h2I8Kt332142@mailgate5.cinetic.de

I have a problem with getting the pid of a program I opened with the
popen2-method of the Win32_popen-Module. When I start my program with
popen2 (on NT 4.0) I see two new processes in the process list. One is
the program I started the other is a CMD.exe.
Now I can communicate with the programm via pipes and it works fine. But
now I want to kill the program again (with the kill-method) of the Module
and I need the pid for it.
I tried to get the pid with IO#pid (using one of the pipes) but it returns
the pid of the CMD.exe.
Does anybody know a way to get the right pid (or better to avoid to start
the CMD.exe while starting my program)?

Your program is attached to CMD.exe.
If you want to kill your program, just kill CMD.exe.
So you don’t need pid of your program.
If you really want to know pid of your program,
you can do it like following.

···

======================================
require ‘win32_popen’
require ‘Win32API’

RegCloseKey = Win32API.new(“advapi32”,“RegCloseKey”,[‘L’],‘L’)
RegOpenKeyEx =
Win32API.new(“advapi32”,“RegOpenKeyEx”,[‘L’,‘P’,‘L’,‘L’,‘P’],‘L’)
RegQueryValueEx =
Win32API.new(“advapi32”,“RegQueryValueEx”,[‘L’,‘P’,‘L’,‘P’,‘P’,‘P’],‘L’)

INITIAL_SIZE = 51200
EXTEND_SIZE = 25600
REGKEY_PERF = “SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Perflib\009”
REGSUBKEY_COUNTERS = “Counters”
KEY_READ = 0x20019;
ERROR_MORE_DATA = 234
HKEY_LOCAL_MACHINE = 0x80000002
HKEY_PERFORMANCE_DATA = 0x80000004

return hash key : parent pids, value : array of child pids

def getpids()
result = Hash.new
hKeyNames = “\0”*4
rc = RegOpenKeyEx.Call(HKEY_LOCAL_MACHINE,REGKEY_PERF,0,KEY_READ,hKeyNames)
hKeyNames = hKeyNames.unpack(‘L’)[0]
dwSize = “\0”*4
rc = RegQueryValueEx.Call( hKeyNames,REGSUBKEY_COUNTERS,0,0,0,dwSize)
dwSize2 = dwSize.unpack(‘L’)[0]
buf = “\0” * dwSize2
rc = RegQueryValueEx.Call( hKeyNames,REGSUBKEY_COUNTERS,0,0,buf,dwSize)

buf2 = Hash[*buf.split(“\0”)]

proidx = buf2.index(“Process”).to_i
pididx = buf2.index(“ID Process”).to_i
ppididx = buf2.index(“Creating Process ID”).to_i

buf = “\0” * INITIAL_SIZE
szSubKey = proidx.to_s
dwSize = [INITIAL_SIZE]

while true
dwSize2 = dwSize.pack(‘L’)
rc = RegQueryValueEx.Call( HKEY_PERFORMANCE_DATA,szSubKey,0,0,buf,dwSize2)

break if rc==0

if rc == ERROR_MORE_DATA
dwSize[0] += EXTEND_SIZE;
buf = “\0” * dwSize[0]
else
exit
end
end

pObj = buf[buf[24,4].unpack(‘L’)[0] … -1]

pCounterDef = pObj[pObj[8,4].unpack(‘L’)[0] … -1]
numCounters = pObj[32,4].unpack(‘L’)[0]

for i in 0 … numCounters
counterNameTitleIndex = pCounterDef[4,4].unpack(‘L’)[0]
if counterNameTitleIndex == pididx
pidcounter = pCounterDef[36,4].unpack(‘L’)[0]
end
if counterNameTitleIndex == ppididx
ppidcounter = pCounterDef[36,4].unpack(‘L’)[0]
end
pCounterDef = pCounterDef[40…-1]
end

dwNumTasks = pObj[40,4].unpack(‘L’)[0]

pInst = pObj[pObj[4,4].unpack(‘L’)[0] … -1]

for i in 0 … dwNumTasks
pCounter = pInst[pInst[0,4].unpack(‘L’)[0] … -1]
pid = pCounter[pidcounter,4].unpack(‘L’)[0]
ppid = pCounter[ppidcounter,4].unpack(‘L’)[0]

if ppid>0
if result[ppid] == nil
result[ppid] = [pid]
else
result[ppid].push(pid)
end
end
pInst = pCounter[pCounter[0,4].unpack(‘L’)[0] … -1]
end
RegCloseKey.Call( hKeyNames )
RegCloseKey.Call( HKEY_PERFORMANCE_DATA )
result
end

fin,fout = IO.win32_popen2(“yourprogram”)
sleep 0.1 while getpids[fin.pid]==nil
pid = getpids[fin.pid][0]

==============================================

Park Heesob

Hi,

···

At Tue, 18 Mar 2003 17:21:00 +0900, Oliver Osterholz wrote:

Does anybody know a way to get the right pid (or better to avoid to start
the CMD.exe while starting my program)?

Unless you use redirection, IO.popen in 1.8 works fine for R/W
mode without intermediate cmd.exe.


Nobu Nakada