Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Handling MSWin Script Output -- ParentPID

by Discipulus (Canon)
on Sep 11, 2019 at 09:32 UTC ( [id://11106009]=note: print w/replies, xml ) Need Help??


in reply to Handling MSWin Script Output

Hello kcott,

> Is there a way to identify when the output is being presented in a separate window (such that I could change the code so that the "Enter to close window:" prompt only appears in these cases)?

Yes you can, and the solotion was already at pm: Re: Re: Re: Parent process name but is slow because it processes all PIDs to get one matching $$ (I wonder if this can be shortened..) using the cpan Win32::Process::Info module.

Here an adapted solution:

use strict; use Win32::Process::Info; my $pihandle = Win32::Process::Info->new(); my @procinfo = $pihandle->GetProcInfo(); my $ParentPID; my %ProcNames; foreach my $PIDInfo (@procinfo) { $ProcNames{$PIDInfo->{ProcessId}} = $PIDInfo->{Name}; if ($PIDInfo->{ProcessId} == $$) { $ParentPID = $PIDInfo->{ParentProcessId}; last; } } print "Parent's name is [", $ProcNames{$ParentPID}, "]\n"; if ( $ProcNames{$ParentPID} eq 'OpenWith.exe' or $ProcNames{$ParentPID +} eq 'explorer.exe') { print 'Press ENTER to close the window: '; <>; } else{ print "..exiting normally\n"; }

Calling the above program from command line (I always use the longhish form: perl program.pl because I do not want to mess with assoc and friends and .pl is associated just with the editor ;) the program exit directly:

Discipulus@works:D>perl parentpid01.pl Parent's name is [cmd.exe] ..exiting normally

But right clicking the file and using Open with and browsing to the current perl.exe I get:

Parent's name is [explorer.exe] Press ENTER to close the window:

Weirdly (well nothing is normal on this OS) the first time I got that the program was called by OpenWith.exe but once perl.exe is in the cached list of Open With.. the caller became explorer.exe which I suppose is the same if you double click it and you have its extension associated to perl.exe

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Handling MSWin Script Output -- ParentPID
by kcott (Archbishop) on Sep 16, 2019 at 05:06 UTC

    G'day Discipulus,

    Firstly, sorry for the late response. I had some fairly major dental work last week: I've spent the last few days recovering and doing little else.

    Thanks for the script and examples. I imagine that code could be put into a module; perhaps used something (very roughly) like:

    ... use if $^O eq 'MSWin32', Win32::Exit::Prompt => 'exit_prompt'; ... exit_prompt() if $^O eq 'MSWin32'; exit;

    That's still extra code for every script and, as you say, it "is slow".

    As running Perl scripts directly from Win10, instead of from Cygwin, is likely to be an infrequent requirement for a $work task — at least for the time being — I'll probably be looking at handling this in the following order:

    1. Run scripts without extra code from cmd.exe (see discussion under VinsWorldcom's response).
    2. Run scripts without extra code from a possibly reconfigured PowerShell (see discussion under Orangutan's reply).
    3. Run scripts with your extra code from anywhere.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-03-28 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found