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

See the "use if" pragma. The following works fine on Win32, Liniux and
Solaris.

-------------------------------------------
use warnings;
use strict;

use if $^O eq "MSWin32", "Win32::API";

print "pid = $$\n";

if ($^O eq "MSWin32") {
    my $GetCurrentProcessId = Win32::API->new("kernel32", "int
GetCurrentProcessId()");
    my $PID = $GetCurrentProcessId->Call();
    print "Win32 PID = $PID\n";
}
---------------------------------------------

You will note, of course, that the Win32::API stuff isn't needed to get
the PID of the current process.

HTH

-- 
Brian Raven



=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the 
intended addressee(s). Unauthorised reproduction, disclosure, modification, 
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message do not 
necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement 
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. 
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail 
vous parvient par erreur, nous vous prions de bien vouloir prevenir 
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre 
systeme. Le contenu de ce message electronique ne represente pas necessairement 
la position ou le point de vue d'Atos Euronext Market Solutions.


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

Reply via email to