ShawnAdministrator
(KiX Supporter)
2004-11-19 06:11 PM
Challenge for a slow day ...

Since it's a slow day, thought I would pump the korg brain-trust with a challenge ...

Can anybody think of a way, using RUN or SHELL or maybe even COM (wsh what have you) to be able to do the following ...

At the beginning of the script, start an instance of an exe (say - notepad.exe for example), have notepad running "in the background", yet get control returned back to your script (because the script needs to do stuff while notepad is displayed), then (and here's the trick) ... be able to AUTOMATICALLY close-down notepad when the script terminates (without explicitly killing the notepad process) ...

Say for example, the assumption would be that notepad would be some kind of "child" process that would automatically terminate when the parent process (kix32) terminated.

If this is not possible (not sure if it is), then kill the notepad process somehow, without alot of muss and fuss. But ideally, if this child process could be killed by default, that would be the best.

Thoughts anyone ?

For example... the script would start like this. The following is the script ... see the script below ... the script is coming next, here it is ...

Code:

Break On
; Your code here
Exit 0




-Shawn


LonkeroAdministrator
(KiX Master Guru)
2004-11-19 07:24 PM
Re: Challenge for a slow day ...

doesn't START have such a switch?

ShawnAdministrator
(KiX Supporter)
2004-11-19 08:04 PM
Re: Challenge for a slow day ...

sooo ... show me the money !

LonkeroAdministrator
(KiX Master Guru)
2004-11-19 09:01 PM
Re: Challenge for a slow day ...

too busy, you know

eriqjaffe
(Hey THIS is FUN)
2004-11-19 09:23 PM
Re: Challenge for a slow day ...

Well, it may not be the most elegant way to do it, but...

Code:
Break on

Run "%comspec% /c start /min notepad.exe"

Sleep(5) ;or whatever code you'd like

If SetFocus ("Untitled - Notepad") = 0
$rc = SendKeys("~{F4}Y")
EndIf



ShawnAdministrator
(KiX Supporter)
2004-11-19 10:39 PM
Re: Challenge for a slow day ...

hehee, good one - clever! What i was hoping to accomplish was that if the "user" canceled the script (however), that the child process would automatically terminate as well ... ja

Howard Bullock
(KiX Supporter)
2004-11-19 10:41 PM
Re: Challenge for a slow day ...

If you have "Break OFF" then when the user canceled the script the user session would close (logoff) closing notepad.

ShawnAdministrator
(KiX Supporter)
2004-11-19 10:48 PM
Re: Challenge for a slow day ...

hmmm, was hoping not to go to that extreme ...

LonkeroAdministrator
(KiX Master Guru)
2004-11-20 12:31 AM
Re: Challenge for a slow day ...

cool!
so, hoby only one with acceptable answer.
really nice one.
I have one other already.
it's long but a nicey...
just need to get it written.


Sealeopard
(KiX Master)
2004-11-20 01:06 AM
Re: Challenge for a slow day ...

Shawn, you old mod', forgot the Scripts Forum rules about including a script in the initial post? ;-)

Les
(KiX Master)
2004-11-20 01:10 AM
Re: Challenge for a slow day ...

Shawn, the old mod', got moderated. LOL

Sealeopard
(KiX Master)
2004-11-20 01:12 AM
Re: Challenge for a slow day ...

Happens to the best of us ;-)

ShawnAdministrator
(KiX Supporter)
2004-11-20 01:14 AM
Re: Challenge for a slow day ...

I posted here very much on purpose ...

LonkeroAdministrator
(KiX Master Guru)
2004-11-20 01:14 AM
Re: Challenge for a slow day ...

this moved?
can't see any notes...


ShawnAdministrator
(KiX Supporter)
2004-11-20 01:19 AM
Re: Challenge for a slow day ...

ja, general discussion is a much better place to be talking about a kixtart script.

LonkeroAdministrator
(KiX Master Guru)
2004-11-20 01:30 AM
Re: Challenge for a slow day ...

maybe...
anyway, back to the challenge...
a direct child-parent stuff is little "eh":
Quote:

The Win32 specification states that no relationship exists between a parent process and any of its child processes once a child process has been created. Many other operating systems support a parent/child relationship policy, so if you kill a process, the system automatically kills all of the process's descendant processes.




http://www.microsoft.com/msj/0698/win320698.aspx


ShawnAdministrator
(KiX Supporter)
2004-11-20 01:39 AM
Re: Challenge for a slow day ...

Thank you Jooel ... that is the magic information i was looking for. There is no parent-child relationship, so no straight-forward way of doing this.

Bryce
(KiX Supporter)
2004-11-20 01:46 AM
Re: Challenge for a slow day ...

its rude.. crude.... and ugly as all sin to look at.... but....

Code:

break on
$ = setoption("novarsinstrings","on")
if $guarddog guarddog($guarddog) exit endif

$Current = enumprocess("notepad.exe")
run "notepad.exe"
$new = enumprocess("notepad.exe")
$notepad = substr($new,1,instr($new,$current)-2)

run '%comspec% /c start /min kix32 "' + @scriptdir + "\" + @scriptname + '" $guarddog='+@pid+","+$notepad

? "press any key to end script and close NOTEPAD.exe"
get $x

function guarddog($data)
while enumprocess(split($data,",")[0])
sleep 0.5
loop
enumprocess(split($data,",")[1],1)
endfunction

FUNCTION EnumProcess($exe, optional $terminate, optional $Computer)
Dim $winmgmts, $ExecQuery, $Process, $id
if not $computer $computer=@wksta endif
$winmgmts="winmgmts:{impersonationLevel=impersonate}!//" + $COMPUTER
select
case val($exe)>0
$ExecQuery="select * from Win32_Process where ProcessId='" + $exe + "'"
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For each $Process in $GetObject
if $terminate $=$Process.Terminate endif
$EnumProcess = $Process.name
next
$GetObject=''
case vartype($exe)=8
$ExecQuery="select * from Win32_Process where Name='" + $exe + "'"
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For each $Process in $GetObject
if $terminate $=$Process.Terminate endif
$id=$Process.ProcessId
$EnumProcess = "" + $Id + "|" + $EnumProcess
Next
$EnumProcess=left($EnumProcess,len($EnumProcess)-1)
$GetObject=''
case 1
exit 1
endselect
ENDFUNCTION



LonkeroAdministrator
(KiX Master Guru)
2004-11-20 01:56 AM
Re: Challenge for a slow day ...

ja bryce, that looks so much like mine.
different approach but finally the logic behind is same.

sadly shawn said it's too complex.
he wants 1 liner [censored].


ShawnAdministrator
(KiX Supporter)
2004-11-20 02:05 AM
Re: Challenge for a slow day ...

What you saying Bryce ? This script is like that ugly girl at the office (that everyone has) ? The one that for some reason we can't stop fantasizing about ? ;0)

LonkeroAdministrator
(KiX Master Guru)
2004-11-20 02:06 AM
Re: Challenge for a slow day ...

nope.
that would be your swedish-teacher.
"svenska talande pojker och flickor"

huh.


AllenAdministrator
(KiX Supporter)
2004-11-20 02:07 AM
Re: Challenge for a slow day ...

Don't mean to ask a stupid question, but what is this doing?

Code:
 
if $guarddog guarddog($guarddog) exit endif



LonkeroAdministrator
(KiX Master Guru)
2004-11-20 02:10 AM
Re: Challenge for a slow day ...

and to explain.
swedish teacher is the ugliest thing you can imagine.


Bryce
(KiX Supporter)
2004-11-20 06:18 AM
Re: Challenge for a slow day ...

Quote:

Don't mean to ask a stupid question, but what is this doing?

Code:
 
if $guarddog guarddog($guarddog) exit endif






the script has 2 modes....

the first mode will launch notepad.exe identify nopepads's PID and then the script calles it self again so it can run in the 2nd mode.

the 2nd moce is passed a variable $garddog=<kix pid>,<notepad pid>

when in guarddog mode, this 2nd script watches the original kix PID and when this PID is no longer running, it terminats the notepad PID.

.... confused yet


AllenAdministrator
(KiX Supporter)
2004-11-20 07:27 AM
Re: Challenge for a slow day ...

ahhh... nice idea. There were some discussions on MSN about doing this, but in two scripts... cool

Mat
(Lurker)
2005-01-11 12:18 PM
Re: Challenge for a slow day ...

Hi

We are having the opposite problen, i.e. we need to keep the application (notepad or whatever) running after the script closes.

The really funny thing is that the script behaves differently depending on whether it was invoked (1) through group policy or (2) manually; in (1) all applications called from the script close when the script is closed by the user (there is a progress window that the user can close by clicking OK); in (2) all applications remain active (which is what we want).

The win32 specification quoted earlier on in this thread doesn't seem to explain the (1) scenario... but I read somewhere that the GPO thing starts a WSH session in which the login script runs and I am guessing that closing the script also closes the WSH and everything running in it.


LonkeroAdministrator
(KiX Master Guru)
2005-01-11 12:47 PM
Re: Challenge for a slow day ...

and someone said wintendos don't understand inheritance!