Closing Windows Application

S

Shandyman

Hi --

I'm new to the area of interprocess communication, so I apologize if
this question seems rather elementary.

I'm interested in repeatedly opening and closing a Windows app (e.g.,
notepad.exe).

I put together the following script to try and do this:

#!/usr/bin/perl -w

#use warnings;
#use strict;

use Win32::process::Info;
use Win32;

my $program = "C:\\winnt\\system32\\notepad.exe";

system($program);

my $pi = Win32::process::Info->new ('localhost');
my @info = $pi->GetProcInfo ();

foreach my $process (@info) {
if( $$process{'Caption'} eq 'notepad.exe' ) {
kill 9, $$process{'ProcessId'} and exit;
}
}

What I'm finding is that, once the Windows program opens, I need to
close it manually before I can get to the portion of the script that
attempts to send the kill signal. I assume this is because Perl is
waiting for the Windows program to return, which never happens.

Is there something else I need to do to terminate the Windows program
automatically?

Any help you can provide would be greatly appreciated.

Thanks.
 
M

Matt Garrish

Shandyman said:
Hi --

I'm new to the area of interprocess communication, so I apologize if
this question seems rather elementary.

I'm interested in repeatedly opening and closing a Windows app (e.g.,
notepad.exe).

I put together the following script to try and do this:

#!/usr/bin/perl -w

#use warnings;
#use strict;

use Win32::process::Info;
use Win32;

my $program = "C:\\winnt\\system32\\notepad.exe";

system($program);

my $pi = Win32::process::Info->new ('localhost');
my @info = $pi->GetProcInfo ();

foreach my $process (@info) {
if( $$process{'Caption'} eq 'notepad.exe' ) {
kill 9, $$process{'ProcessId'} and exit;
}
}

What I'm finding is that, once the Windows program opens, I need to
close it manually before I can get to the portion of the script that
attempts to send the kill signal. I assume this is because Perl is
waiting for the Windows program to return, which never happens.

It's always easier and faster to read the docs than hope and pray... : )

You should be using Win32::process. Add the following in place of the system
command and it should work as expected:

Win32::process::Create($ProcessObj, $program, "", 0, NORMAL_PRIORITY_CLASS,
".") || die ErrorReport();

Matt
 
S

Shandyman

Thanks for your response.

I was familiar with Win32::process::Create and had tried it in some of
my previous iterations.

I abandoned it because it produces an error when I try to open the
application, while system didn't.

Is there any theoretical reason why you can't use system instead?
 
S

Sisyphus

Shandyman said:
Thanks for your response.

I was familiar with Win32::process::Create and had tried it in some of
my previous iterations.

I abandoned it because it produces an error when I try to open the
application, while system didn't.

Then you're probably using it incorrectly.
Is there any theoretical reason why you can't use system instead?

One problem with system() is that it doesn't give you a handle on the
process that was started.

Building on what Matt gave you here's a program that opens up notepad, then
3 seconds later kills it:

use warnings;
use strict;
use Win32::process;

my $ProcessObj;
my $program = "C:\\winnt\\system32\\notepad.exe";

Win32::process::Create($ProcessObj, $program, "", 0, NORMAL_PRIORITY_CLASS,
".") || die ErrorReport();

sleep(3); # Give us time to see what happens.

$ProcessObj->Kill( 9 );

__END__

Cheers,
Rob
 
B

Bart Lateur

Shandyman said:
I'm interested in repeatedly opening and closing a Windows app (e.g.,
notepad.exe).
use Win32::process::Info;

Hmm, close. I'd use Win32::process instead. It has both Create() as a
constructor (albeit with a weird syntax, the variable to receive the
object is expected as a first parameter) and the Kill object method. I
think that's all you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top