Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Get PID of an excel.exe opened by perl

by Nalina (Monk)
on Jun 06, 2005 at 12:34 UTC ( [id://463896]=perlquestion: print w/replies, xml ) Need Help??

Nalina has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
this is in continuation with my last thread 'End a process in the Task manager'. Thnks all for very usefull suggestions. Now I am trying to get the PID of an excel file which I open through my script.
use Win32; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); use Win32::OLE; use Win32::OLE::Const; $Win32::OLE::Warn = 3; # die on errors... $curr_time = time(); print $curr_time; print "\n"; eval{ $Constant = Win32::OLE::Const->Load('Microsoft Excel'); $Excel = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) || die "Error launching MS Excel ".Win32::OLE->LastError(1); $Excel->{Visible} = 0; die "You must provide the filename with path\n" unless @ARGV; $Book = $Excel->Workbooks->Open($ARGV[0]); use Win32::API; Win32::API->Import("kernel32", "int GetCurrentProcessId()"); $PID = GetCurrentProcessId(); print "\n\n $PID \n\n"; ...... ...
but this gives me PID of 'perl.exe'. I tried to find the child processes of perl. But the excel file which is opened by perl is not a child of perl.exe but it is a child of 'svchost.exe'.
If this is the case how do I get the PID of the particular excel file which is opened by current process(perl.exe)

Replies are listed 'Best First'.
Re: Get PID of an excel.exe opened by perl
by holli (Abbot) on Jun 06, 2005 at 12:53 UTC
    The following shows how to find the PID's of running Excel instances.
    use strict; use warnings; use Win32::Process::Info; use Data::Dumper; my $pi = Win32::Process::Info->new(); for ( $pi->ListPids ) { my ($info) = $pi->GetProcInfo ($_); print "Excel instance at PID $_.\n" if $info->{Name} =~ /excel\.ex +e/i; }
    Sadly, I'm not sure how to find out, which file is opened by a specific instance. That is a bit more difficult, because each instance can open multiple files.

    Update:
    A possible way might be to
    1. Enumerate the windows in the system
    2. Find out which PID belongs to that window
    3. If the window belongs to one of your Excel instances, find out the windows' caption
    4. The part after the first dash of the caption (" - ") must be the filename (because Excels' windows normally have captions like "Microsoft Excel - File.xls")


    holli, /regexed monk/
      Thanks for the reply. Please give me the url (or repository) to install Proc::ProcessTable using ppm on windows.
        Your new node here and your statement you want to kill a process on windows makes me wonder if you ever read the docs. To kill a process:
        Win32::Process::KillProcess($pid, $exitcode);


        holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found