Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Closing Windows Application (http://www.velocityreviews.com/forums/t897166-closing-windows-application.html)

Shandyman 03-25-2006 11:00 PM

Closing Windows Application
 
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.


Matt Garrish 03-25-2006 11:32 PM

Re: Closing Windows Application
 

"Shandyman" <OtosOne@msn.com> wrote in message
news:1143327609.986125.184360@i40g2000cwc.googlegr oups.com...
> 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



Shandyman 03-26-2006 04:41 AM

Re: Closing Windows Application
 
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?


Sisyphus 03-26-2006 05:30 AM

Re: Closing Windows Application
 

"Shandyman" <OtosOne@msn.com> wrote in message
news:1143348104.806682.201000@v46g2000cwv.googlegr oups.com...
> 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



Shandyman 03-26-2006 06:07 AM

Re: Closing Windows Application
 
Rob --

Thanks. That's very helpful.


Bart Lateur 03-26-2006 08:37 AM

Re: Closing Windows Application
 
Shandyman wrote:

>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.

--
Bart.

Dr.Ruud 03-26-2006 09:59 AM

Re: Closing Windows Application
 
Sisyphus schreef:

> "C:\\winnt\\system32\\notepad.exe";


YM: 'C:/winnt/system32/notepad.exe';

Alternatively, look up the .txt-editor in the registry.

--
Affijn, Ruud

"Gewoon is een tijger."
echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'

Shandyman 03-26-2006 11:36 PM

Re: Closing Windows Application
 
Thank you all for your help.

I think I have things under control now.



All times are GMT. The time now is 01:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.