Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

The behavior of Win32::OLE->new

by marynella (Novice)
on Nov 19, 2003 at 12:01 UTC ( #308273=perlquestion: print w/ replies, xml ) Need Help??
marynella has asked for the wisdom of the Perl Monks concerning the following question:

I have an existing problem with Perl/OLE, that I'd like you to take a look at. In my script there is a call to:
Win32::OLE->new( 'InternetExplorer.Application', sub {$_[0]->Quit;} )
It appears that the behavior of Win32::OLE->new is as follows:
  1. If there is an existing instance of InternetExplorer(IE) running, then it will return a handle to the existing IE application.
  2. If there is not an existing instance of IE running, then it will create a new instance of IE, and return a handle to this new instance.
I seem to be having a concurrency/race condition problem with this call, when multiple scripts get the same instance of IE for the handle. Can you help me to find a way to change this call, so that it ALWAYS returns a new instance of IE and does not try to look for an already existing instance of IE?

Note: I am currently using Win32::Mutex to avoid the concurrency/race condition problems. However, this is not the best solution.
Thanks!

update (broquaint): tidied up formatting

Comment on The behavior of Win32::OLE->new
Download Code
Re: The behavior of Win32::OLE->new
by l3nz (Friar) on Nov 19, 2003 at 17:31 UTC
    This is not much a Perl-ish answer, but I guess it's not mostly a Perl problem - this is probably because of the threading model the object is running under. I have come across the same problem with VisualBasic and JavaScript object creation, and I have found that, to obtain different and separate independent copies, you can:
    • Create an object of type WScript.Shell
    • Call the "run" method on it to launch a separate browser (or whatever program you need to run)
    This is a sample, just not to go completely off-topic and being thrown out of the monastery for messing with the Enemy without providing appropriate Perl code as an offer:
    use Win32::OLE; my $url = 'http://www.perlmonks.com/index.pl?node_id='; # opens five separate instances for (my $i = 0; $i < 5; $i++ ) { my $node_id = 300000 + $i; $obSh = Win32::OLE->new('WScript.Shell'); $rc = $obSh->run("IEXPLORE.EXE ${url}${node_id}", 1, false); undef $obSh; }
    On my Windows box, each copy of explorer is independent and separately prompts for the proxy password.

    You might want to read this too.

        Your solution maybe good, but I have another problem. Maybe someone had this new problem to and can help me.
      I've put in my script the line:
        $obSh = Win32::OLE->new('WScript.Shell', sub {$_[0]->Quit;}) or die "Can't start Shell", Win32::OLE::LastError();
      This line gives me an error:
        Can't start ShellWin32::OLE(0.17) error 0x80040154: "Class not registered"
      Thanks!
Re: The behavior of Win32::OLE->new
by jsprat (Curate) on Nov 19, 2003 at 18:10 UTC
    You want a new instance of IE every time it runs, right?

    Instead of Win32::OLE->new(), what you want to use is CreateObject, like so:

    my $ie = Win32::OLE->CreateObject('InternetExplorer.Application', 'Qui +t') or die "Can't start IE", Win32::OLE::LastError();

    $ie in each script will reference its own instance of IE.

    Update: According to Win32::OLE docs, new and CreateObject are equivalent. You should be getting a new instance of ie with each run.

      If instead of Win32::OLE->new(), I use CreateObject my problem is not solved.
      Thanks any way for the advice
        Is it possible this is related to the "Reuse windows when launching shortcuts" option? In the Internet Options control panel, look in the Advanced tab. Since the ShellExec of a .url file basically does the same thing you're doing, this may have an effect on your problem.


        mhoward - at - hattmoward.org
        Hmm. On Win2K/IE6 and Win98/IE5.5, both l3nz snippet and mine work for me, and you don't have Windows Script Host.

        What version of Windows and IE are you running?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-08-22 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls