Peterson, Barry (STP) wrote:
> Hi,
> 
> I'm writing a module that will sometimes be used on Windows, and sometimes 
> used on Linux.
> 
> Unfortunately, on Windows, I need to be able to get the Process ID so I make 
> use of Win32:API.  Of course this causes a problem when the module used on 
> Linux.

The process ID should already be in $$.

print "PID = $$\n";

> Here's my "ALMOST works" solution:
> 
>   if($^O =~ /MSWin32/i){
>     require Win32::API;
>     my $GetCurrentProcessId = Win32::API->new("kernel32", "int 
> GetCurrentProcessId()");
>     my $PID = GetCurrentProcessId();
>     :
>     :
>     etc.
>   }
> 
> By pulling Win32::API in using "require" instead of "use" it is only 
> evaluated on demand so Linux is happy, but because I am using "require" 
> instead of "use" something is not quite right with the initialization so that 
> on Windows perl complains:
> 
> Win32::API::parse_prototype: WARNING unknown output parameter type 'int' at 
> C:/perl/site/lib/Win32/API.pm line 273.
> 
> Granted I get the $PID, but I'd like to get it properly, or at least without 
> the annoying warning message.
> 
> If I use "use" instead of "require" then Win32 seems to initialize properly, 
> but Linux cannot use the module at all.
> 
> Having one module for Linux and one for Windows is not an option.
> 
> If there a way or additional steps I can take to initialize Win32 without 
> using "use"?

We just discussed a similar case.  This should work for similar situations:

use strict;
use warnings;

my $windoze;
BEGIN {
        $windoze = 1 if $^O eq 'MSWin32';
        if ($windoze) {
                print "Looks like its Windoze\n";
                require Win32::API;
                import Win32::API;      # probably don't even need for this case
        } else {
                print "Looks like its not Windoze\n";
        }
}

if ($windoze) {
        sleep 5;
        my $GetCurrentProcessId = Win32::API->Import('kernel32',
          'int GetCurrentProcessId ()') or
          die "import GetCurrentProcessId: $! ($^E)";
        my $PID = GetCurrentProcessId ();
        print $PID, "\n";
}

__END__



-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to