blib/lib/Resources.pm untested

filter: Statementstmt 0.0% 0 / 291 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 536 CC 44 SCAR 40.4
CC
44
Cov
0%
IsEXE16
90.0
IsEXE32
90.0
ExeType
90.0
CRAP 56.9
1 # Resource.pm - Use Win32::API to retrieve popular
2 # resources from Kernel32.dll and others
3 #
4 # Created by Brad Turner
5 # bsturner@sprintparanet.com
6 # Friday, April 09, 1999
7 #
8 # Updated on Wednesday, August 25, 1999
9 #
10 # All functions verfied on Win95a and WinNT 4.0
11 # (Some functions are WinNT only)
12 ###############################################
13 package Win32API::Resources;
14
15 use Win32;
16 use Win32::API;
17 use strict qw(vars);
18 $Win32API::Resources::VERSION = '0.07';
19
20 #*********************************************************
21 #* IsEXE16 wrapper around GetBinaryType API - based on code by Aldo Calpini
22 #*********************************************************
23 sub IsEXE16
24 {
25 # Simple file test - returns 1 if file is 16-bit, 0 if file is 32 bit
26 # SHGetFileInfo will work on both WinNT and Win95a systems
27
28 my $SHGFI = new Win32::API("shell32", "SHGetFileInfo", [qw(P L P I I)], 'L');
29 my($file) = @_;
30 my $result;
31 my $type = undef;
32
33 if($SHGFI)
34 {
35 $result = $SHGFI->Call($file, 0, 0, 0, 0x2000);
36 if($result)
37 {
38 $type .= ", " if $type;
39
40 my $hi = $result >> 16;
41 my $lo = $result & 0x0000FFFF;
42 $lo = sprintf("%c%c", $lo & 0x00FF, $lo >> 8);
43
44 if($hi)
45 {
46 $hi = sprintf("%d.%02d", $hi >> 8, $hi & 0x00FF);
47 }
48 if (($lo eq "NE") and ($hi ge "3.0"))
49 {
50 $type = 1;
51 }
52 elsif (($lo eq "PE") and ($hi ge "3.0"))
53 {
54 $type = 0;
55 }
56 elsif (($lo eq "PE") and ($hi eq NULL))
57 {
58 $type = 0;
59 }
60 elsif (($lo eq "MZ") and ($hi eq NULL))
61 {
62 $type = 1;
63 }
64 }
65 }
66 return $type;
67 }
68 #*********************************************************
69 #* IsEXE32 wrapper around GetBinaryType API - based on code by Aldo Calpini
70 #*********************************************************
71 sub IsEXE32
72 {
73 # Simple file test - returns 1 if file is 32-bit, 0 if file is 16 bit
74 # SHGetFileInfo will work on both WinNT and Win95a systems
75
76 my $SHGFI = new Win32::API("shell32", "SHGetFileInfo", [qw(P L P I I)], 'L');
77 my($file) = @_;
78 my $result;
79 my $type = undef;
80
81 if($SHGFI)
82 {
83 $result = $SHGFI->Call($file, 0, 0, 0, 0x2000);
84 if($result)
85 {
86 $type .= ", " if $type;
87
88 my $hi = $result >> 16;
89 my $lo = $result & 0x0000FFFF;
90 $lo = sprintf("%c%c", $lo & 0x00FF, $lo >> 8);
91
92 if($hi)
93 {
94 $hi = sprintf("%d.%02d", $hi >> 8, $hi & 0x00FF);
95 }
96 if (($lo eq "NE") and ($hi ge "3.0"))
97 {
98 $type = 0;
99 }
100 elsif (($lo eq "PE") and ($hi ge "3.0"))
101 {
102 $type = 1;
103 }
104 elsif (($lo eq "PE") and ($hi eq NULL))
105 {
106 $type = 1;
107 }
108 elsif (($lo eq "MZ") and ($hi eq NULL))
109 {
110 $type = 0;
111 }
112 }
113 }
114 return $type;
115 }
116 #*********************************************************
117 #* GetBinaryType API direct - by Aldo Calpini
118 #*********************************************************
119 sub GetBinaryType
120 {
121 # GetBinaryType has the advantage of detecting POSIX and OS/2 based applications
122 # however it cannot differentiate between 32-bit apps that are console or window based
123
124 # BOOL GetBinaryType (
125 # LPCTSTR lpApplicationName, // pointer to fully qualified path of file to test
126 # LPDWORD lpBinaryType // pointer to variable to receive binary type information);
127
128 my $GBT = new Win32::API("kernel32", "GetBinaryType", [qw(P P)], 'N');
129 my($file) = @_;
130 my $result;
131 my $type = undef;
132 if(Win32::IsWinNT)
133 {
134 my @typename = (
135 "Win32 based application",
136 "MS-DOS based application",
137 "16-bit Windows based application",
138 "PIF file that executes an MS-DOS based application",
139 "POSIX based application",
140 "16-bit OS/2 based application");
141
142 my $typeindex = pack("L", 0);
143 $result = $GBT->Call($file, $typeindex);
144 $type = $typename[unpack("L", $typeindex)] if $result;
145 }
146 else {
147 print "Win32API::Resources::GetBinaryType only works in WinNT\n";
148 return 0;
149 }
150 return $type;
151 }
152 #*********************************************************
153 #* ExeType Wrapper around SHGetFileInfo API - by Aldo Calpini
154 #*********************************************************
155 sub ExeType
156 {
157 # GetBinaryType has the advantage of detecting POSIX and OS/2 based applications
158 # however it cannot differentiate between 32-bit apps that are console or window based
159 #
160 # SHGetFileInfo will work on both WinNT and Win95a systems
161
162 # WINSHELLAPI DWORD WINAPI SHGetFileInfo(
163 # LPCTSTR pszPath,
164 # DWORD dwFileAttributes,
165 # SHFILEINFO FAR *psfi,
166 # UINT cbFileInfo,
167 # UINT uFlags);
168
169 my $SHGFI = new Win32::API("shell32", "SHGetFileInfo", [qw(P L P I I)], 'L');
170 my($file) = @_;
171 my $result;
172 my $type = undef;
173
174 if($SHGFI)
175 {
176 $result = $SHGFI->Call($file, 0, 0, 0, 0x2000);
177 if($result)
178 {
179 $type .= ", " if $type;
180
181 my $hi = $result >> 16;
182 my $lo = $result & 0x0000FFFF;
183 $lo = sprintf("%c%c", $lo & 0x00FF, $lo >> 8);
184
185 if($hi)
186 {
187 $hi = sprintf("%d.%02d", $hi >> 8, $hi & 0x00FF);
188 }
189 if (($lo eq "NE") and ($hi ge "3.0"))
190 {
191 $type = "16-bit Windows based application, $lo $hi";
192 }
193 elsif (($lo eq "PE") and ($hi ge "3.0"))
194 {
195 $type = "32-bit Windows based application, $lo $hi";
196 }
197 elsif (($lo eq "PE") and ($hi eq NULL))
198 {
199 $type = "32-bit Win32 console based application, $lo $hi";
200 }
201 elsif (($lo eq "MZ") and ($hi eq NULL))
202 {
203 $type = "MS-DOS based application, $lo $hi";
204 }
205 else {
206 $type = "Unknown, $lo $hi";
207 }
208 }
209 }
210 return $type;
211 }
212 #*********************************************************
213 #* GetDriveSpace API sub - this calls the right API depending on the OS Version
214 #*********************************************************
215 sub GetDriveSpace
216 {
217 # Frontend to GetDiskFreeSpace and GetDiskFreeSpaceEx -
218 # Call the correct function depending on which version of the OS you have
219 my $drive = $_[0];
220 my (%DSpace);
221
222 if (Win32::IsWinNT)
223 {
224 my $OS = "Windows NT";
225
226 # We're on NT so we can call the good function
227 %DSpace = Win32API::Resources::GetDiskFreeSpaceEx($drive) or return 0;
228 }
229 elsif (Win32::IsWin95)
230 {
231 my $OS = "Windows 95";
232 my ($servicepack, $major, $minor, $buildnum, $platformid, $l, $m);
233
234 # We're on 95 so we first need check if we're OSR2
235 ($servicepack, $major, $minor, $buildnum, $platformid) = Win32::GetOSVersion;
236 $l = $buildnum & 0xFFFF; #get only the least significant 16
237 $m = $buildnum >> 16; #get only the most significant 16
238 if (("$major, $minor, $l, $m") gt ("4, 0, 950, 1024"))
239 {
240 # We're OSR2 - Calling GetDiskFreeSpaceEx
241 %DSpace = Win32API::Resources::GetDiskFreeSpaceEx($drive) or return 0;
242 }
243 elsif (("$major, $minor, $l, $m") le ("4, 0, 950, 1024"))
244 {
245 # We're OSR1 - Calling GetDiskFreeSpace
246 %DSpace = Win32API::Resources::GetDiskFreeSpace($drive) or return 0;
247 }
248 else {
249 # Not sure what it is - call the safe one
250 %DSpace = Win32API::Resources::GetDiskFreeSpace($drive) or return 0;
251 }
252 }
253 return %DSpace;
254 }
255 #*********************************************************
256 #* GetDiskFreeSpace API sub
257 #*********************************************************
258 sub GetDiskFreeSpace
259 {
260 # The GetDiskFreeSpaceEx function lets you avoid the arithmetic required by the
261 # GetDiskFreeSpace function. However, GetDiskFreeSpaceEx will not work on Win95 OSR1
262
263 # Windows 95:
264 # The GetDiskFreeSpace function returns incorrect values for volumes that are
265 # larger than 2 gigabytes. The function caps the values stored into *lpNumberOfFreeClusters
266 # and *lpTotalNumberOfClusters so as to never report volume sizes that are greater
267 # than 2 gigabytes.
268 # Even on volumes that are smaller than 2 gigabytes, the values stored into
269 # *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values
270 # may be incorrect. That is because the operating system manipulates the values so that
271 # computations with them yield the correct volume size.
272
273 # Windows 95 OSR2 and later:
274 # The GetDiskFreeSpaceEx function is available on Windows 95 systems beginning with OEM
275 # Service Release 2 (OSR2). The GetDiskFreeSpaceEx function returns correct values for
276 # all volumes, including those that are greater than 2 gigabytes.
277
278 # BOOL GetDiskFreeSpace(
279 # LPCTSTR lpRootPathName, // pointer to root path
280 # LPDWORD lpSectorsPerCluster, // pointer to sectors per cluster
281 # LPDWORD lpBytesPerSector, // pointer to bytes per sector
282 # LPDWORD lpNumberOfFreeClusters, // pointer to number of free clusters
283 # LPDWORD lpTotalNumberOfClusters // pointer to total number of clusters);
284
285 my $lpRootPathName = $_[0];
286 # Windows 95: The initial release of Windows 95 does not support UNC paths for
287 # the lpRootPathName parameter. To query the free disk space using a UNC path,
288 # temporarily map the UNC path to a drive letter, query the free disk space on
289 # the drive, then remove the temporary mapping.
290 # Windows 95 OSR2 and later: UNC paths are supported.
291
292 my $lpSectorsPerCluster = "\0" x 32;
293 my $lpBytesPerSector = "\0" x 32;
294 my $lpNumberOfFreeClusters = "\0" x 32;
295 my $lpTotalNumberOfClusters = "\0" x 32;
296
297 # GetDiskFreeSpace API direct
298 my $GetDiskFreeSpace = new Win32::API("kernel32", "GetDiskFreeSpaceA", [qw(P P P P P)], 'N') or return 0;
299 $GetDiskFreeSpace->Call($lpRootPathName, $lpSectorsPerCluster, $lpBytesPerSector, $lpNumberOfFreeClusters, $lpTotalNumberOfClusters) or return 0;
300 $lpSectorsPerCluster = unpack("L", $lpSectorsPerCluster);
301 $lpBytesPerSector = unpack("L", $lpBytesPerSector);
302 $lpNumberOfFreeClusters = unpack("L", $lpNumberOfFreeClusters);
303 $lpTotalNumberOfClusters = unpack("L", $lpTotalNumberOfClusters);
304 my $DriveSpaceTotal = $lpTotalNumberOfClusters * $lpSectorsPerCluster * $lpBytesPerSector;
305 my $DriveSpaceFree = $lpNumberOfFreeClusters * $lpSectorsPerCluster * $lpBytesPerSector;
306
307 my %DSpace = ( SectorsPerCluster => $lpSectorsPerCluster,
308 BytesPerSector => $lpBytesPerSector,
309 NumberOfFreeClusters => $lpNumberOfFreeClusters,
310 TotalNumberOfClusters => $lpTotalNumberOfClusters,
311 DriveSpaceTotal => $DriveSpaceTotal,
312 DriveSpaceFree => $DriveSpaceFree);
313 return %DSpace;
314 }
315 #*********************************************************
316 #* GetDiskFreeSpaceEx API sub
317 #*********************************************************
318 sub GetDiskFreeSpaceEx
319 {
320 # Windows 95 OSR2: The GetDiskFreeSpaceEx function is available on Windows 95
321 # systems beginning with OEM Service Release 2 (OSR2).
322
323 # BOOL GetDiskFreeSpaceEx(
324 # LPCTSTR lpDirectoryName, // pointer to the directory name
325 # PULARGE_INTEGER lpFreeBytesAvailableToCaller, // receives the number of bytes on disk available to the caller
326 # PULARGE_INTEGER lpTotalNumberOfBytes, // receives the number of bytes on disk
327 # PULARGE_INTEGER lpTotalNumberOfFreeBytes // receives the free bytes on disk);
328
329 my $lpDirectoryName = $_[0];
330 my $lpFreeBytesAvailableToCaller = "\0" x 32;
331 my $lpTotalNumberOfBytes = "\0" x 32;
332 my $lpTotalNumberOfFreeBytes = "\0" x 32;
333
334 # GetDiskFreeSpace API direct
335 my $GetDiskFreeSpaceEx = new Win32::API("kernel32", "GetDiskFreeSpaceExA", [qw(P P P P)], 'N') or return 0;
336 $GetDiskFreeSpaceEx->Call($lpDirectoryName, $lpFreeBytesAvailableToCaller, $lpTotalNumberOfBytes, $lpTotalNumberOfFreeBytes) or return 0;
337 $lpFreeBytesAvailableToCaller = UnpackLargeInt($lpFreeBytesAvailableToCaller);
338 $lpTotalNumberOfBytes = UnpackLargeInt($lpTotalNumberOfBytes);
339 $lpTotalNumberOfFreeBytes = UnpackLargeInt($lpTotalNumberOfFreeBytes);
340
341 my %DSpace = ( DriveSpaceQuotaFree => $lpFreeBytesAvailableToCaller,
342 DriveSpaceTotal => $lpTotalNumberOfBytes,
343 DriveSpaceFree => $lpTotalNumberOfFreeBytes);
344 return %DSpace;
345 }
346 #*********************************************************
347 #* GlobalMemoryStatus API sub
348 #*********************************************************
349 sub GlobalMemoryStatus
350 {
351 # VOID GlobalMemoryStatus(
352 # LPMEMORYSTATUS lpBuffer // pointer to the memory status structure);
353
354 my $GMSBuffer = "\0" x 32;
355 my %GMStatus;
356
357 # GlobalMemoryStatus API direct
358 my $GlobalMemoryStatus = new Win32::API("kernel32", "GlobalMemoryStatus", [qw(P)], 'N') or return 0;
359 $GlobalMemoryStatus->Call($GMSBuffer) or return 0;
360 my @GMSBuffer= unpack("L*", $GMSBuffer);
361
362 # MEMORYSTATUS structure - tip o' the hat to Win32::AdminMisc
363 %GMStatus = ( Load => $GMSBuffer[1],
364 RAMTotal => $GMSBuffer[2],
365 RAMAvail => $GMSBuffer[3],
366 PageTotal => $GMSBuffer[4],
367 PageAvail => $GMSBuffer[5],
368 VirtTotal => $GMSBuffer[6],
369 VirtAvail => $GMSBuffer[7]);
370
371 return %GMStatus;
372 }
373 #*********************************************************
374 #* LoadString API sub
375 #*********************************************************
376 sub LoadString
377 {
378 # int LoadString(
379 # HINSTANCE hInstance, // handle to module containing string resource
380 # UINT uID, // resource identifier
381 # LPTSTR lpBuffer, // pointer to buffer for resource
382 # int nBufferMax // size of buffer);
383
384 my $file = $_[0]; # The file passed from main (DLL/EXE)
385 my $uID = $_[1]; # The resource identifier
386 my $LLHandle = "\0" x 32;
387 my $lpBuffer = "\0" x 64;
388 my ($LSBuffer);
389
390 # Win32::LoadLibrary method - requires use Win32
391 $LLHandle = Win32::LoadLibrary($file); # Map the DLL into memory
392
393 # LoadString API direct
394 my $LoadString = new Win32::API("user32", "LoadStringA", [qw(N I P I)], 'I') or return 0;
395 $LSBuffer = $LoadString->Call($LLHandle, $uID, $lpBuffer, 64) or return 0;
396 $lpBuffer = ((split(/\0/, $lpBuffer))[0]); # Strip out the excess buffer
397
398 return $lpBuffer;
399 }
400 #*********************************************************
401 #* EnumString API wrapper around LoadString to Enumerate table
402 #*********************************************************
403 sub EnumString
404 {
405 # Place a wrapper around LoadString to Enumerate all entries
406
407 # int LoadString(
408 # HINSTANCE hInstance, // handle to module containing string resource
409 # UINT uID, // resource identifier
410 # LPTSTR lpBuffer, // pointer to buffer for resource
411 # int nBufferMax // size of buffer);
412
413 my $file = $_[0]; # The file passed from main (DLL/EXE)
414 my $uID = 1; # The resource identifier
415 my $L = 1;
416 my $LLHandle = "\0" x 32;
417 my $lpBuffer = "\0" x 64;
418 my (@lpBuffer, $LSBuffer);
419
420 # Win32::LoadLibrary method - requires use Win32
421 $LLHandle = Win32::LoadLibrary($file); # Map the DLL into memory
422
423 # LoadString API direct
424 my $LoadString = new Win32::API("user32", "LoadStringA", [qw(N I P I)], 'I') or return 0;
425
426 while ($L)
427 {
428 $LSBuffer = $LoadString->Call($LLHandle, $uID, $lpBuffer, 64) or $L = undef;
429 push(@lpBuffer, ((split(/\0/, $lpBuffer))[0])); # Strip out the excess buffer and place it in the list
430 $uID++;
431 }
432
433 return @lpBuffer;
434 }
435 #*********************************************************
436 #* GetFileVersion sub
437 #* with help from blazer@mail.nevalink.ru
438 #*********************************************************
439 sub GetFileVersion
440 {
441 # GetFileVersionInfoSize is used to find the size of record to retrieve
442 # DWORD GetFileVersionInfoSize(
443 # LPTSTR lptstrFilename, // pointer to filename string
444 # LPDWORD lpdwHandle // pointer to variable to receive zero);
445
446 # GetFileVersionInfo is used to retrieve the record itself
447 # BOOL GetFileVersionInfo(
448 # LPTSTR lptstrFilename, // pointer to filename string
449 # DWORD dwHandle, // ignored
450 # DWORD dwLen, // size of buffer
451 # LPVOID lpData // pointer to buffer to receive file-version info.);
452
453 my $filename = shift(@_);
454 my $switch = shift(@_);
455 my (%PAT);
456 my ($lpBufferSize, $lpHandle, $lpBuffer);
457
458 my $GetFileVersionInfoSize = new Win32::API("version", "GetFileVersionInfoSizeA", [qw(P P)], 'N');
459 $lpHandle = DWORD_NULL;
460 $lpBufferSize = $GetFileVersionInfoSize->Call($filename, $lpHandle);
461 $lpBuffer = "\0" x $lpBufferSize;
462
463 my $GetFileVersionInfo = new Win32::API("version", "GetFileVersionInfoA", [qw(P N N P)], 'N');
464 if (!-e $filename and $switch)
465 {
466 # If the filename is not found and the Optional switch is used,
467 # return an array of zero valued keys
468 return %PAT = ( Comments => 0,
469 CharacterSet => 0,
470 Language => 0,
471 ProductName => 0,
472 OriginalFilename => 0,
473 LegalCopyright => 0,
474 LegalTrademarks => 0,
475 InternalName => 0,
476 FileDescription => 0,
477 CompanyName => 0,
478 SpecialBuild => 0,
479 PrivateBuild => 0,
480 FileVersion => 0,
481 FileVersionMS => 0,
482 FileVersionLS => 0,
483 FileVersion64 => 0,
484 ProductVersion => 0,
485 ProductVersionMS => 0,
486 ProductVersionLS => 0,
487 ProductVersion64 => 0,
488 FileType => 0,
489 FileSubtype => 0,
490 FileOS => 0,
491 FileFlags => 0);
492 }
493 elsif (!-e $filename)
494 {
495 # If the filename is not found and no switch is specified
496 # return undefined
497 return;
498 }
499 elsif (-e $filename)
500 {
501 if(!$GetFileVersionInfo->Call($filename, 0, $lpBufferSize, $lpBuffer))
502 {
503 # The file does not contain a FixedFile or StringFile Info block
504 # return undefined
505 return;
506 }
507 }
508
509 # VerQueryValue function returns selected version information from the specified
510 # version-information resource
511 # BOOL VerQueryValue(
512 # const LPVOID pBlock, // address of buffer for version resource
513 # LPTSTR lpSubBlock, // address of value to retrieve
514 # LPVOID *lplpBuffer, // address of buffer for version value pointer
515 # PUINT puLen // address of length buffer);
516
517 my ($lplpBuffer, $puLen) = (DWORD_NULL, DWORD_NULL);
518 my $VerQueryValue = new Win32::API("version", "VerQueryValueA", [qw(P P P P)], 'N');
519
520 # Searching "\\" gets us the VS_FIXEDFILEINFO block which has to be unpacked carefully
521 $VerQueryValue->Call($lpBuffer, "\\", $lplpBuffer, $puLen);
522
523 $puLen = unpack("L", $puLen);
524 my $buffer = unpack("P$puLen", $lplpBuffer);
525 my ( $dwFileVersionMS, $dwFileVersionLS, $dwProductVersionMS, $dwProductVersionLS,
526 $dwFileFlagsMask, $dwFileFlags, $dwFileOS, $dwFileType, $dwFileSubtype,
527 $dwFileDateMS, $dwFileDateLS) = unpack("x8LLLLLLLLLLL", $buffer);
528
529 # Reverse Lookup Tables
530 my %FileOS = ( 0x00000000 => VOS_UNKNOWN,
531 0x00010000 => VOS_DOS,
532 0x00020000 => VOS_OS216,
533 0x00030000 => VOS_OS232,
534 0x00040000 => VOS_NT,
535 0x00010001 => VOS_DOS_WINDOWS16,
536 0x00010004 => VOS_DOS_WINDOWS32,
537 0x00020002 => VOS_OS216_PM16,
538 0x00030003 => VOS_OS232_PM32,
539 0x00040004 => VOS_NT_WINDOWS32,
540 0x00000000 => VOS__BASE,
541 0x00000001 => VOS__WINDOWS16,
542 0x00000002 => VOS__PM16,
543 0x00000003 => VOS__PM32,
544 0x00000004 => VOS__WINDOWS32);
545 my %FileFlags = (0x00000000 => VS_UNKNOWN,
546 0xFEEF04BD => VS_FFI_SIGNATURE,
547 0x00010000 => VS_FFI_STRUCVERSION,
548 0x0000003F => VS_FFI_FILEFLAGSMASK,
549 0x00000001 => VS_FF_DEBUG,
550 0x00000002 => VS_FF_PRERELEASE,
551 0x00000004 => VS_FF_PATCHED,
552 0x00000008 => VS_FF_PRIVATEBUILD,
553 0x00000010 => VS_FF_INFOINFERRED,
554 0x00000020 => VS_FF_SPECIALBUILD);
555 my %FileType = (0x00000000 => VFT_UNKNOWN,
556 0x00000001 => VFT_APP,
557 0x00000002 => VFT_DLL,
558 0x00000003 => VFT_DRV,
559 0x00000004 => VFT_FONT,
560 0x00000005 => VFT_VXD,
561 0x00000007 => VFT_STATIC_LIB);
562 my %VFT_DRV = ( 0x00000000 => VFT2_UNKNOWN,
563 0x00000001 => VFT2_DRV_PRINTER,
564 0x00000002 => VFT2_DRV_KEYBOARD,
565 0x00000003 => VFT2_DRV_LANGUAGE,
566 0x00000004 => VFT2_DRV_DISPLAY,
567 0x00000005 => VFT2_DRV_MOUSE,
568 0x00000006 => VFT2_DRV_NETWORK,
569 0x00000007 => VFT2_DRV_SYSTEM,
570 0x00000008 => VFT2_DRV_INSTALLABLE,
571 0x00000009 => VFT2_DRV_SOUND,
572 0x0000000A => VFT2_DRV_COMM,
573 0x0000000B => VFT2_DRV_INPUTMETHOD);
574 my %VFT_FONT = (0x00000001 => VFT2_FONT_RASTER,
575 0x00000002 => VFT2_FONT_VECTOR,
576 0x00000003 => VFT2_FONT_TRUETYPE);
577
578 my ($t);
579 $PAT{FileVersionMS} = $dwFileVersionMS;
580 $PAT{FileVersionLS} = $dwFileVersionLS;
581 $PAT{FileVersion64} = sprintf ("%.1d.%.2d", ($dwFileVersionMS >> 16),($dwFileVersionMS & 0xFFFF)).(($t = $dwFileVersionLS >> 16) ? sprintf (".%.1d", $t) : "").sprintf (".%.1d",($dwFileVersionLS & 0xFFFF));
582 $PAT{ProductVersionMS} = $dwProductVersionMS;
583 $PAT{ProductVersionLS} = $dwProductVersionLS;
584 $PAT{ProductVersion64} = sprintf ("%.1d.%.2d", ($dwProductVersionMS >> 16),($dwProductVersionMS & 0xFFFF)).(($t = $dwProductVersionLS >> 16) ? sprintf (".%.1d", $t) : "").sprintf (".%.1d",($dwProductVersionLS & 0xFFFF));
585 $PAT{FileType} = $FileType{$dwFileType};
586 $PAT{FileSubtype} = ${$FileType{$dwFileType}}{$dwFileSubtype} if (${$FileType{$dwFileType}}{$dwFileSubtype});
587 $PAT{FileOS} = $FileOS{$dwFileOS};
588 $PAT{FileFlags} = $FileFlags{$dwFileFlags};
589
590 ($lplpBuffer, $puLen) = (DWORD_NULL, DWORD_NULL);
591
592 # Searching "\\VarFileInfo\\Translation" is an attempt to find the Language and Character Set of the file
593 $VerQueryValue->Call($lpBuffer, "\\VarFileInfo\\Translation", $lplpBuffer, $puLen);
594
595 $puLen = unpack("L", $puLen);
596 my $lang_charset = unpack("P$puLen", $lplpBuffer);
597 $lang_charset = unpack("H*", $lang_charset);
598 $lang_charset =~ s/(..)(..)/$2$1/g;
599
600 # If the Language is "Language Neutral" (0000) then the following call will fail, so set it artificially to
601 # English (United States) (0409)
602 # NOTE: This may not work if another language is configured, may have to ask the OS for Language and
603 # append here...
604 $lang_charset = '040904e4' if (substr($lang_charset, 0, 4) == 0000);
605 my $Language = substr($lang_charset, 0, 4);
606 my $CharSet = substr($lang_charset, 4, 8);
607
608 # Reverse lookup tables for Character Set and Language
609 my %CharSet = ( '0000' => '7-bit ASCII',
610 '03a4' => 'Japan (Shift - JIS X-0208)',
611 '03b5' => 'Korea (Shift - KSC 5601)',
612 '03b6' => 'Taiwan (Big5)',
613 '04b0' => 'Unicode',
614 '04e2' => 'Latin-2 (Eastern European)',
615 '04e3' => 'Cyrillic',
616 '04e4' => 'Multilingual',
617 '04e5' => 'Greek',
618 '04e6' => 'Turkish',
619 '04e7' => 'Hebrew',
620 '04e8' => 'Arabic');
621 $PAT{CharacterSet} = $CharSet{$CharSet};
622 my %Language = ('0000' => 'Language Neutral',
623 '0400' => 'Process Default Language',
624 '0401' => 'Arabic (Saudi Arabia)',
625 '0801' => 'Arabic (Iraq)',
626 '0c01' => 'Arabic (Egypt)',
627 '1001' => 'Arabic (Libya)',
628 '1401' => 'Arabic (Algeria)',
629 '1801' => 'Arabic (Morocco)',
630 '1c01' => 'Arabic (Tunisia)',
631 '2001' => 'Arabic (Oman)',
632 '2401' => 'Arabic (Yemen)',
633 '2801' => 'Arabic (Syria)',
634 '2c01' => 'Arabic (Jordan)',
635 '3001' => 'Arabic (Lebanon)',
636 '3401' => 'Arabic (Kuwait)',
637 '3801' => 'Arabic (U.A.E.)',
638 '3c01' => 'Arabic (Bahrain)',
639 '4001' => 'Arabic (Qatar)',
640 '0402' => 'Bulgarian',
641 '0403' => 'Catalan',
642 '0404' => 'Chinese (Taiwan Region)',
643 '0804' => 'Chinese (PRC)',
644 '0c04' => 'Chinese (Hong Kong SAR, PRC)',
645 '1004' => 'Chinese (Singapore)',
646 '0405' => 'Czech',
647 '0406' => 'Danish',
648 '0407' => 'German (Standard)',
649 '0807' => 'German (Swiss)',
650 '0c07' => 'German (Austrian)',
651 '1007' => 'German (Luxembourg)',
652 '1407' => 'German (Liechtenstein)',
653 '0408' => 'Greek',
654 '0409' => 'English (United States)',
655 '0809' => 'English (United Kingdom)',
656 '0c09' => 'English (Australian)',
657 '1009' => 'English (Canadian)',
658 '1409' => 'English (New Zealand)',
659 '1809' => 'English (Ireland)',
660 '1c09' => 'English (South Africa)',
661 '2009' => 'English (Jamaica)',
662 '2409' => 'English (Caribbean)',
663 '2809' => 'English (Belize)',
664 '2c09' => 'English (Trinidad)',
665 '040a' => 'Spanish (Traditional Sort)',
666 '080a' => 'Spanish (Mexican)',
667 '0c0a' => 'Spanish (Modern Sort)',
668 '100a' => 'Spanish (Guatemala)',
669 '140a' => 'Spanish (Costa Rica)',
670 '180a' => 'Spanish (Panama)',
671 '1c0a' => 'Spanish (Dominican Republic)',
672 '200a' => 'Spanish (Venezuela)',
673 '240a' => 'Spanish (Colombia)',
674 '280a' => 'Spanish (Peru)',
675 '2c0a' => 'Spanish (Argentina)',
676 '300a' => 'Spanish (Ecuador)',
677 '340a' => 'Spanish (Chile)',
678 '380a' => 'Spanish (Uruguay)',
679 '3c0a' => 'Spanish (Paraguay)',
680 '400a' => 'Spanish (Bolivia)',
681 '440a' => 'Spanish (El Salvador)',
682 '480a' => 'Spanish (Honduras)',
683 '4c0a' => 'Spanish (Nicaragua)',
684 '500a' => 'Spanish (Puerto Rico)',
685 '040b' => 'Finnish',
686 '040c' => 'French (Standard)',
687 '080c' => 'French (Belgian)',
688 '0c0c' => 'French (Canadian)',
689 '100c' => 'French (Swiss)',
690 '140c' => 'French (Luxembourg)',
691 '040d' => 'Hebrew',
692 '040e' => 'Hungarian',
693 '040f' => 'Icelandic',
694 '0410' => 'Italian (Standard)',
695 '0810' => 'Italian (Swiss)',
696 '0411' => 'Japanese',
697 '0412' => 'Korean',
698 '0812' => 'Korean (Johab)',
699 '0413' => 'Dutch (Standard)',
700 '0813' => 'Dutch (Belgian)',
701 '0414' => 'Norwegian (Bokmal)',
702 '0814' => 'Norwegian (Nynorsk)',
703 '0415' => 'Polish',
704 '0416' => 'Portuguese (Brazilian)',
705 '0816' => 'Portuguese (Standard)',
706 '0418' => 'Romanian',
707 '0419' => 'Russian',
708 '041a' => 'Croatian',
709 '081a' => 'Serbian (Latin)',
710 '0c1a' => 'Serbian (Cyrillic)',
711 '041b' => 'Slovak',
712 '041c' => 'Albanian',
713 '041d' => 'Swedish',
714 '081d' => 'Swedish (Finland)',
715 '041e' => 'Thai',
716 '041f' => 'Turkish',
717 '0421' => 'Indonesian',
718 '0422' => 'Ukrainian',
719 '0423 '=> 'Belarusian',
720 '0424 '=> 'Slovenian',
721 '0425 '=> 'Estonian',
722 '0426' => 'Latvian',
723 '0427' => 'Lithuanian',
724 '0429' => 'Farsi',
725 '042a' => 'Vietnamese',
726 '042d' => 'Basque',
727 '0436' => 'Afrikaans',
728 '0438' => 'Faeroese');
729 $PAT{Language} = $Language{$Language};
730
731 my $String;
732 my @StringFileInfo = qw(Comments CompanyName FileDescription FileVersion InternalName LegalCopyright LegalTrademarks OriginalFilename PrivateBuild ProductName ProductVersion SpecialBuild);
733 foreach $String (@StringFileInfo)
734 {
735 ($lplpBuffer, $puLen) = (DWORD_NULL, DWORD_NULL);
736 $VerQueryValue->Call($lpBuffer, "\\StringFileInfo\\$lang_charset\\$String", $lplpBuffer, $puLen) or next;
737 $puLen = unpack("L", $puLen);
738 $PAT{$String} = unpack("P$puLen", $lplpBuffer);
739 $PAT{$String} =~ s/\0+$//;
740 }
741 return %PAT;
742 }
743 #*********************************************************
744 #* ShowKeys subroutine - list the contents of a hash with optional sorting
745 #*********************************************************
746 sub ShowKeys
747 {
748 # A quick and easy sub to display the contents of a Hash
749 # sorted or unsorted
750
751 my $key;
752 my $title = $_[0]; # Prints the title of the Hash
753 my $sort = $_[1]; # 0 = no sort, 1 = sort
754 my %main = %{$_[2]}; # the reference to the Hash (\%hash)
755
756 print "\n$title\n\n";
757 if ($sort)
758 {
759 foreach $key (sort keys (%main))
760 {
761 print "$key = $main{$key}\n";
762 }
763 }
764 else {
765 foreach $key (keys (%main))
766 {
767 print "$key = $main{$key}\n";
768 }
769 }
770 }
771 #*********************************************************
772 #* GetDrives
773 #*********************************************************
774 sub GetDrives
775 {
776 # In List context GetDrives returns a list of drive assignments on the local system
777 # In Scalar context GetDrives returns the number of valid drive assignments
778 # Passing GetDrives one of the Drive Type Constants will return:
779 # In List context, a list of drive assignments that match the passed constant
780 # In Scalar context, the number of valid drive assignments that match the passed constant
781
782 # The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.
783
784 # DWORD GetLogicalDriveStrings(
785 # DWORD nBufferLength, // size of buffer
786 # LPTSTR lpBuffer // pointer to buffer for drive strings);
787
788 # The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
789
790 # UINT GetDriveType(
791 # LPCTSTR lpRootPathName // pointer to root path);
792
793 # DRIVE_UNKNOWN The drive type cannot be determined.
794 # DRIVE_NO_ROOT_DIR The root directory does not exist.
795 # DRIVE_REMOVABLE The disk can be removed from the drive.
796 # DRIVE_FIXED The disk cannot be removed from the drive.
797 # DRIVE_REMOTE The drive is a remote (network) drive.
798 # DRIVE_CDROM The drive is a CD-ROM drive.
799 # DRIVE_RAMDISK The drive is a RAM disk.
800
801 my $DriveType = $_[0]; # Pass a drive type string to parse by
802 my $nBufferLength = "128";
803 my $lpBuffer = "\0" x 128;
804 my @matchlist;
805 my %DriveType = ( 0x00000000 => "DRIVE_UNKNOWN",
806 0x00000001 => "DRIVE_NO_ROOT_DIR",
807 0x00000002 => "DRIVE_REMOVABLE",
808 0x00000003 => "DRIVE_FIXED",
809 0x00000004 => "DRIVE_REMOTE",
810 0x00000005 => "DRIVE_CDROM",
811 0x00000006 => "DRIVE_RAMDISK");
812
813 # GetLogicalDriveStrings API direct
814 my $GetLogicalDriveStrings = new Win32::API("kernel32", "GetLogicalDriveStringsA", [qw(L P)], 'N') or return 0;
815 $GetLogicalDriveStrings->Call($nBufferLength, $lpBuffer) or return 0;
816 my @lpBuffer = split(/\0/, $lpBuffer); # Strip out the excess buffer and convert to list context
817
818 # GetDriveType API direct
819 my $GetDriveType = new Win32::API("kernel32", "GetDriveTypeA", [qw(P)], 'N') or return 0;
820 my ($lpRootPathName);
821 foreach $lpRootPathName (@lpBuffer)
822 {
823 my $Return = $GetDriveType->Call($lpRootPathName) or return 0;
824 if ($DriveType)
825 {
826 if ($DriveType eq $DriveType{$Return})
827 {
828 push (@matchlist, $lpRootPathName);
829 }
830 else {
831 next;
832 }
833 return @matchlist;
834 }
835 else {
836 return @lpBuffer;
837 }
838 }
839 }
840 #*********************************************************
841 # Enumerate the Open RAS Connections
842 #*********************************************************
843 sub RasEnumConnections
844 {
845 # DWORD RasEnumConnections(
846 # LPRASCONN lprasconn, // buffer to receive connections data
847 # LPDWORD lpcb, // size in bytes of buffer
848 # LPDWORD lpcConnections // number of connections written to buffer);
849
850 my $lprasconn = pack("L*", 32, 0, 0, 0, 0);
851 my $lpcb = pack("L", 32);
852 my $lpcConnections = pack("L", 0);
853 my $Return;
854
855 # RasEnumConnections API direct
856 my $RasEnumConnections = new Win32::API("rasapi32", "RasEnumConnectionsA", [qw(P P P)], 'N');
857 if ($RasEnumConnections)
858 {
859 $Return = $RasEnumConnections->Call($lprasconn, $lpcb, $lpcConnections);
860 }
861 else {
862 return;
863 }
864 if ($Return == 0)
865 {
866 $lpcConnections = unpack("L", $lpcConnections);
867 return $lpcConnections;
868 }
869 else {
870 return;
871 }
872 }
873 #*********************************************************
874 #* DWORD_NULL - packs a NULL string
875 #* based on code by blazer@mail.nevalink.ru
876 #*********************************************************
877 sub DWORD_NULL {pack("L",0)}
878 #*********************************************************
879 #* UnpackLargInt sub - unpacks a LARGE_INTEGER value
880 #* based on code by blazer@mail.nevalink.ru
881 #*********************************************************
882 sub UnpackLargeInt
883 {
884 my $PackedValue = shift(@_);
885
886 my ($b, $a) = unpack("LL", $PackedValue);
887 my $UnpackedValue = $a*2**32+$b;
888
889 return $UnpackedValue
890 }
891 1;
892 __END__