Hi,
try this, its working on Win32 and perl doesn't print a error on HP-UX:

################# cut here #####################

use strict;

BEGIN {
     if ($^O eq 'MSWin32') {
          die "Unable to load Win32::OLE" unless eval 'require Win32::OLE';
          eval 'import Win32::OLE qw ( in )';
     }
}


sub getWinProcessId {
# to call with getWinProcessId(ProcName)
# returns the process id
# returns -1 if no process found
# returns -2 if WMI not available
     my $ProcName=$_[0];
     my $found=0;
     my $Class = "Win32_Process";
     (my $Machine = shift @ARGV || "." ) =~ s/^[\\\/]+//;
     if( my $WMIServices = Win32::OLE-> GetObject(

"winmgmts:{impersonationLevel=impersonate,(security)}//$Machine" ) ) {
          foreach my $Processes ( in( $WMIServices-> InstancesOf( $Class )
) ) {
#print "$Processes->{Name}: $Processes->{ProcessId}\n";
               if ( "$Processes->{Name}"=~/$ProcName/i ) {
                    $found=1;
                    return ($Processes-> {ProcessId});
               }
          }
          return -1;
     }
     else
     {
# WMI does not work
          return -2;
     }
}


if ( $^O eq "MSWin32" ) {
     my $test=getWinProcessId("cmd.exe");
     print "$test\n";
} else {
     print "This is not MSWin32\n";
}

################# cut here #####################



                                                                                
                                                      
                    [EMAIL PROTECTED]                                           
                                               
                    Sent by:                                 To:     
[email protected]                              
                    [EMAIL PROTECTED]        cc:                                
                                      
                    eState.com                               Subject:     Same 
script running on UNIX/WIN32, how?                     
                                                                                
                                                      
                                                                                
                                                      
                    18.07.2005 10:14                                            
                                                      
                                                                                
                                                      
                                                                                
                                                      





Hello,

I need for a bigger script (1000 lines) the capability to run the same
script on UNIX and WIN32.

As yet, I'm successfull with constructs like in the example. But now I have
a problem with the function from the example. With "use Win32::OLE qw( in
);", in the second line this example works fine, but not on UNIX ;-(  With
"require Win32::OLE qw( in );", like in the example, I get an error.
Somebody with an idea?

use strict;
#use Win32::OLE qw( in );

if ( $^O eq "MSWin32" ) {
  require Win32::OLE qw( in );
  my $test=&getWinProcessId("cmd.exe");
  print "$test\n";
}

sub getWinProcessId {
  # to call with &getWinProcessId(ProcName)
  # returns the process id
  # returns -1 if no process found
  # returns -2 if WMI not available
  my $ProcName=$_[0];
  my $found=0;
  my $Class = "Win32_Process";
  (my $Machine = shift @ARGV || "." ) =~ s/^[\\\/]+//;
  if( my $WMIServices = Win32::OLE-> GetObject(
  "winmgmts:{impersonationLevel=impersonate,(security)}//$Machine" ) ) {
     foreach my $Processes ( in( $WMIServices-> InstancesOf( $Class ) ) ) {
       #print "$Processes->{Name}: $Processes->{ProcessId}\n";
       if ( "$Processes->{Name}"=~/$ProcName/i ) {
         $found=1;
         return ($Processes-> {ProcessId});
       }
     }
     return -1;
  }
  else
  {
    # WMI does not work
    return -2;
  }
}


Kind regards,
Walter Laub

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs




_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to