Closing Windows Application

Discussion in 'Perl Misc' started by Shandyman, Mar 25, 2006.

  1. Shandyman

    Shandyman Guest

    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.
    Shandyman, Mar 25, 2006
    #1
    1. Advertising

  2. Shandyman

    Matt Garrish Guest

    "Shandyman" <> wrote in message
    news:...
    > 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
    Matt Garrish, Mar 25, 2006
    #2
    1. Advertising

  3. Shandyman

    Shandyman Guest

    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?
    Shandyman, Mar 26, 2006
    #3
  4. Shandyman

    Sisyphus Guest

    "Shandyman" <> wrote in message
    news:...
    > 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
    Sisyphus, Mar 26, 2006
    #4
  5. Shandyman

    Shandyman Guest

    Rob --

    Thanks. That's very helpful.
    Shandyman, Mar 26, 2006
    #5
  6. Shandyman

    Bart Lateur Guest

    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.
    Bart Lateur, Mar 26, 2006
    #6
  7. Shandyman

    Dr.Ruud Guest

    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,/'
    Dr.Ruud, Mar 26, 2006
    #7
  8. Shandyman

    Shandyman Guest

    Thank you all for your help.

    I think I have things under control now.
    Shandyman, Mar 27, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Uk1M?=

    Closing an ASP.NET application

    =?Utf-8?B?Uk1M?=, Aug 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,247
    =?Utf-8?B?Uk1M?=
    Aug 12, 2004
  2. Hillary Hill
    Replies:
    2
    Views:
    521
    Hillary Hill
    May 13, 2005
  3. federico

    closing excel application

    federico, Oct 17, 2003, in forum: Python
    Replies:
    4
    Views:
    596
    federico
    Oct 20, 2003
  4. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    649
    Thomas Hansen
    Feb 15, 2007
  5. thomas
    Replies:
    0
    Views:
    225
    thomas
    Oct 23, 2003
Loading...

Share This Page