How To : Using Win32::Package with Perl 5

Misc. functions & Tips

Last update 22 sept 97

Checked with build 306


GetLastError function

Returns the calling thread's last-error code value. Most Win32 functions set their calling thread's last-error value when they fail; a few functions set it when they succeed.

Syntax

GetLastError();

Example

use Win32;
my $name;

$! = 0; #first clear $! from previous error code
$name = Win32::LoginName(); # or whatever function you'd like
$! = Win32::GetLastError();
if ($!) then { print "An error occured : $! \n"; }

Remarks

Some of the extended error codes returned by the GetLastError function are not the same for NT & 95. You should call the GetLastError function immediately and set it to $! after calling a win32:: function, as they will not generate any error or warning under Perl. You can find description of the errors codes in Visual C++ or Visual Basic.

GetCwd function

Returns the current active drive. This function does not return a UNC path.

Syntax

GetCwd();

Example

use Win32;
my $MyDir;

$MyDir = Win32::GetCwd();
print "$MyDir \n";

FsType function

Returns a string naming the filesystem type of the currently active partition.

Syntax

FsType();

Example

use Win32;
my $filesyst;
$filesyst = Win32::FsType();

if ($filesys == 'FAT')  {
	print "NTFS is better than FAT !\n";
} elsif ($filesys == 'NTFS') {
	print "NTFS : Never Tweak your File System !\n";
}

PerlVersion function

Returns a string with the build of Perl.

Syntax

PerlVersion();

Example

use Win32;
my $build;

$build=Win32::PerlVersion();
print "This is a Perl 5 $build.\n";

LoginName function

Returns a string with the username of the owner of the current perl process. By default, it's your login name.

Syntax

LoginName();

Example

use Win32;

my $name;
$name=Win32::LoginName();
print "Process launched by user : $name\n";

NodeName function

Returns a string with the computer node-name of the computer.

Syntax

NodeName();

Example

use Win32;

my $computer;
$computer=Win32::NodeName();
print "Your computer's name is : $computer\n";

DomainName function

Returns a string with the name of the microsoft network domain that the owner of the current perl process is logged into.

Syntax

DomainName();

Example

use Win32;
my $domain;
my $computer

$computer=Win32::NodeName();
$domain=Win32::DomainName();

if ($domain == $computer) {
	print "You're logged locally.\n"; }
else {
	print "You've logged into domain : $domain\n"; }

InitiateSystemShutdown function

Initiates a shutdown and optional restart of the specified computer.

Syntax

InitiateSystemShutdown($MachineName,$Message,$Timeout,$ForceAppsClosed,$RebootAfterShutdown);

$MachineName String that specifies the network name of the computer to shut down. No unc.
$Message String that specifies a message to display in the shutdown dialog box. This parameter can be NULL if no message is required.
$Timeout Specifies the time (in seconds) that the dialog box should be displayed. While this dialog box is displayed, the shutdown can be stopped by the AbortSystemShutdown function.If zero, the computer shuts down without displaying the dialog box, and the shutdown cannot be stopped by AbortSystemShutdown.
$ForceAppsClosed Specifies whether applications with unsaved changes are to be forcibly closed. If this parameter is TRUE, such applications are closed. If this parameter is FALSE, a dialog box is displayed prompting the user to close the applications.
$RebootAfterShutdown Specifies whether the computer is to restart immediately after shutting down. If this parameter is TRUE, the computer is to restart.

Example

use Win32;

Win32::InitiateSystemShutdown ('MONTY_PYTHON',300,0,0);

$! = Win32::GetLastError();

if ($!) then {
	print "Can't get it down : $! \n"; }
else {
	print "on is way ...\n"; }

Remarks

This function only works with NT. I just can't get this function shutting down a local computer. To shut down a remote computer, the calling process must have the REMOTE_SHUTDOWN privilege on the remote computer.

AbortSystemShutdown function

Aborts a shutdown of the specified computer.

Syntax

AbortSystemShutdown($MachineName);

Example

use Win32;

Win32::InitiateSystemShutdown ('MONTY_PYTHON',300,0,0);

$! = Win32::GetLastError();
if ($!) then {
	print "Can't get it down : $! \n"; }
else {
	print "on is way ...\n";

	Win32::AbortSystemShutdown ('MONTY_PYTHON');

	print "server is safe !\n"; }

IsWin95 function

Indicate if your running the script is running under Windows 95.

Syntax

IsWin95();

Example

use Win32;

if( Win32::IsWin95() ){
	die "EventLog is not supported on Windows 95 or Win32s\n";
}

IsWinNT function

Indicate if you running the script under Windows NT.

Syntax

IsWinNT( );

Example

use Win32;

if( Win32::IsWinNT() ){
	print "Great, you do have an EventLog !\n";
}

GetShortPathName function

Retrieves the short name of file given the long path.

Syntax

GetShortPathName($LongPathName);

Example

use Win32;

$shortpath = Win32::GetShortPathName( "c:/program files" );
print "$shortpath\n";

GetNextAvailDrive function

Returns the next available drive letter ??

Syntax

GetNextAvailDrive();

Example

use Win32;

$nextletter = Win32::GetNextAvailDrive( );
print "The next available drive letter : $nextletter\n";

GetChipName function

Returns the CPU type of the computer.

Syntax

GetChipName();

Example

use Win32;

$chip = Win32::GetChipName( );
$chip =~ s/586/Pentium/;
print "Perl is running on a : $chip class computer.\n";

GetArchName function

Returns the processor architecture like x86 or alpha.

Syntax

GetArchName();

Example

use Win32;

$arch = Win32::GetArchName( );
print "Perl is running on a : $arch CPU type.\n";

GetTickCount function

Retrieves the number of milliseconds that have elapsed since Windows was started. The time will wrap around to zero if Windows is run continuously for 49.7 days.

Syntax

GetTickCount();

Example

use Win32;

$time = Win32::GetTickCount( );
$day  	= 	int($time/86400000);
$hour 	=	int( ($time = ($time - $day*86400000)) /3600000);
$minutes = 	int( ($time = ($time - $hour*3600000)) /60000);
$second =	int( ($time = ($time -$minutes*60000))/1000);
print "Your box have been running for : $day days $hour hours $minutes minutes $second seconds $time milliseconds.\n";

MsgBox function

Display a message box.

Syntax

MsgBox($Message,$Type,$Caption);

$Message Text to be displayed in the dialog box.
$Type Defines the type (Icon & Buttons) of the message box. To define the value sum the desired Buttons option and Icon option.
$Caption Text to be displayed in window header.
Buttons Option Icon Option Returned Value
0 Ok 16 Hand 1 Ok
1 Ok, Cancel 32 Question 2 Cancel
2 Abort, Retry, Ignore 48 Exclamation 3 Abort
3 Yes, No, Cancel 64 Asterisk 4 Retry
4 Yes, No 5 Ignore
5 Retry, Cancel 6 Yes
7 No

Example

use Win32;

Win32::MsgBox("A first step for Perl in the Windows GUI world !");

Win32::MsgBox('Bug detected',48,'Alert ! Alert');

$result = Win32::MsgBox('Meet me at the coffee machine ?', 16+4, 'Tired ?');

(($result == 6) ? print "I am on my way !\n" : print "bye !\n");
	

Tips

%ENV

%ENV, when not execute thru CGI, contains the current environnement :

USERNAME the user name used to log.
PROMPT the prompt (ex : $p$g).
PROCESSOR_IDENTIFIER a string with information on your CPU (ex : x86 Family 5 Model 2 Stepping 12, GenuineIntel).
PROCESSOR_ARCHITECTURE indicate the family of your CPU (ex : x86).
OS a string with the name of the OS (ex : Windows_NT).
HOMEDRIVE letter of the home drive (ex : C:).
INCLUDE path of the include.
CPU kind of CPU (ex : i386).
SYSTEMROOT the place of windows (ex : c:\WINNT).
COMSPEC the place of command interpreter (ex : c:\WINNT\system32\cmd32.exe)
PATH the path ...
PROCESSOR_REVISION is your pentium buggy ?
USERPROFILE path of the profile for the current user.
COMPUTERNAME name of the computer (ex : UC651)
HOMEPATH your place (ex : /users/bill)
USERDOMAIN name of the domain you logged in.
PROCESSOR_LEVEL level of CPU in its family.
OS2LIBPATH path for OS2 library.
SYSTEMDRIVE letter of the system drive.

Example

foreach $i (keys %ENV) {
	print "$i : $ENV{$i} \n";
}

Copyright © 1997 Philippe Le Berre

Any rights not expressly granted herein are reserved.