| 1 |
# Author : Bakkiaraj M |
| 2 |
# Script: Get System time from windows OS using GetLocalTime API. |
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use FFI::CheckLib; |
| 6 |
use FFI::Platypus; |
| 7 |
use Convert::Binary::C; |
| 8 |
|
| 9 |
#Get the system time using Kernel32.dll |
| 10 |
|
| 11 |
#find the Kernel32.dll |
| 12 |
my $libPath = find_lib(lib=>'Kernel32'); |
| 13 |
#Create FFI Object |
| 14 |
my $ffiObj = FFI::Platypus->new(); |
| 15 |
$ffiObj->lib($libPath); |
| 16 |
|
| 17 |
#Import the GetLocalTime function |
| 18 |
$ffiObj->attach('GetLocalTime',['record(16)'],'void'); |
| 19 |
|
| 20 |
#Define SYSTEMTIME Struct as per https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx |
| 21 |
#As per, C:\MinGW\include\windef.h, WORD id unsigned short |
| 22 |
my $c = Convert::Binary::C->new->parse(<<ENDC); |
| 23 |
|
| 24 |
struct SYSTEMTIME { |
| 25 |
unsigned short wYear; |
| 26 |
unsigned short wMonth; |
| 27 |
unsigned short wDayOfWeek; |
| 28 |
unsigned short wDay; |
| 29 |
unsigned short wHour; |
| 30 |
unsigned short wMinute; |
| 31 |
unsigned short wSecond; |
| 32 |
unsigned short wMilliseconds; |
| 33 |
}; |
| 34 |
|
| 35 |
ENDC |
| 36 |
|
| 37 |
|
| 38 |
my $dateStruct = { |
| 39 |
wYear=>0, |
| 40 |
wMonth=>0, |
| 41 |
wDayOfWeek=>0, |
| 42 |
wDay=>0, |
| 43 |
wHour=>0, |
| 44 |
wMinute=>0, |
| 45 |
wSecond=>0, |
| 46 |
wMilliseconds=>0, |
| 47 |
}; |
| 48 |
|
| 49 |
my $packed = $c->pack('SYSTEMTIME', $dateStruct); |
| 50 |
|
| 51 |
#Call the function by passing the structure reference |
| 52 |
GetLocalTime($packed); |
| 53 |
|
| 54 |
if (defined ($packed)) |
| 55 |
{ |
| 56 |
#Unpack the structure |
| 57 |
my $sysDate = $c->unpack('SYSTEMTIME', $packed); |
| 58 |
print "\n WINDOWS SYSTEM TIME: ",$$sysDate{'wHour'},':',$$sysDate{'wMinute'},':',$$sysDate{'wSecond'},'.',$$sysDate{'wMilliseconds'},' ',$$sysDate{'wDay'},'/',$$sysDate{'wMonth'},'/',$$sysDate{'wYear'}, "\n"; |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
print "\n Something is wrong\n"; |
| 63 |
} |
| 64 |
|
| 65 |
exit 0; |