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

Hi monks,
Is there any way to get the PID of an Excel instance as soon as it is opened by perl. for example in the following 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... $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; $Book = $Excel->Workbooks->Add; my $filename="C:\\perlscripts\\test.xls"; $Book->SaveAs($filename); $Book->Close({SaveChanges=>0});
I am opening an excel file & then saving it. Can I get the pid of this excel file opened by passing some reference of it (like $Excel or $Book)? I am trying this since many days. I am really frustrated. Kindly help.
Thanks in Advance

Replies are listed 'Best First'.
Re: Get PID of a particular instance of Excel
by Corion (Patriarch) on Jun 09, 2005 at 06:49 UTC

    How is this question different from your last question about this? Did you follow the advice given there?

    Why do you want to kill Excel.exe at all? The Excel instance goes away if you use the DESTROY callback, as your sample code does.

      Is there any way to findout the state of a process (waiting, running, deadlock)
Re: Get PID of a particular instance of Excel
by gellyfish (Monsignor) on Jun 09, 2005 at 09:03 UTC

    When you are using Win32::OLE like this Perl is not opening Excel, it is asking the OLE sub system to open it on its behalf. You don't need to and infact shouldn't know the PID of the Excel instance, it could be that the Excel is being used by more than one program or is being run as a user different to the one that is running your program. I think you are looking at some problem with a preconceived notion of how it needs to be addressed - maybe if you started from the beginning and explained what problem it is that you are trying to solve we might be able to help you more.

    /J\

      As I said in my last thread, I have an ASP page running on the remote server. This ASP uses the Shell object to run the Perl script as a process and wait for its completion? This Perl Script inturn uses OLE to open excel file & create graph. Different users from different hosts access this ASP page & try to create graphs. Now the problem is when 2 or more users try to generate graph simultaneously, the excel file opened by perl is not getting closed. Over a period of time there will be many instances of excel in the task manager. So I just need to find out what session has opened Excel.exe in task manager, get its pid & then kill it. I am trying this since a week. Kindly help.
Re: Get PID of a particular instance of Excel
by tchatzi (Acolyte) on Jun 09, 2005 at 10:44 UTC
    Something like this maybe......
    use Win32::Process::List; my $P = Win32::Process::List->new(); #constructor my @list = $P->GetProcesses(); #returns an array of hashes my %h = %{ $list[0] }; #get the hash with processname and pid foreach my $pr ( keys %h ) { print "Process $pr has PID " . $h{$pr} . "\n"; }
    Right from Win32::Process::List

    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI