home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / site / lib / Win32.pm < prev   
Encoding:
Perl POD Document  |  2002-10-23  |  9.0 KB  |  262 lines

  1. package Win32;
  2.  
  3. #
  4. #  Documentation for all Win32:: functions are in Win32.pod, which is a
  5. #  standard part of Perl 5.6, and later.
  6. #
  7.  
  8. BEGIN {
  9.     use strict;
  10.     use vars qw|$VERSION @ISA @EXPORT @EXPORT_OK|;
  11.  
  12.     require Exporter;
  13.     require DynaLoader;
  14.  
  15.     @ISA = qw|Exporter DynaLoader|;
  16.     $VERSION = '0.20';
  17.  
  18.     @EXPORT = qw(
  19.     NULL
  20.     WIN31_CLASS
  21.     OWNER_SECURITY_INFORMATION
  22.     GROUP_SECURITY_INFORMATION
  23.     DACL_SECURITY_INFORMATION
  24.     SACL_SECURITY_INFORMATION
  25.     MB_ICONHAND
  26.     MB_ICONQUESTION
  27.     MB_ICONEXCLAMATION
  28.     MB_ICONASTERISK
  29.     MB_ICONWARNING
  30.     MB_ICONERROR
  31.     MB_ICONINFORMATION
  32.     MB_ICONSTOP
  33.     );
  34.     @EXPORT_OK = qw(
  35.         GetOSName
  36.         SW_HIDE
  37.         SW_SHOWNORMAL
  38.         SW_SHOWMINIMIZED
  39.         SW_SHOWMAXIMIZED
  40.         SW_SHOWNOACTIVATE
  41.  
  42.         CSIDL_DESKTOP
  43.         CSIDL_PROGRAMS
  44.         CSIDL_PERSONAL
  45.         CSIDL_FAVORITES
  46.         CSIDL_STARTUP
  47.         CSIDL_RECENT
  48.         CSIDL_SENDTO
  49.         CSIDL_STARTMENU
  50.         CSIDL_MYMUSIC
  51.         CSIDL_MYVIDEO
  52.         CSIDL_DESKTOPDIRECTORY
  53.         CSIDL_NETHOOD
  54.         CSIDL_FONTS
  55.         CSIDL_TEMPLATES
  56.         CSIDL_COMMON_STARTMENU
  57.         CSIDL_COMMON_PROGRAMS
  58.         CSIDL_COMMON_STARTUP
  59.         CSIDL_COMMON_DESKTOPDIRECTORY
  60.         CSIDL_APPDATA
  61.         CSIDL_PRINTHOOD
  62.         CSIDL_LOCAL_APPDATA
  63.         CSIDL_COMMON_FAVORITES
  64.         CSIDL_INTERNET_CACHE
  65.         CSIDL_COOKIES
  66.         CSIDL_HISTORY
  67.         CSIDL_COMMON_APPDATA
  68.         CSIDL_WINDOWS
  69.         CSIDL_SYSTEM
  70.         CSIDL_PROGRAM_FILES
  71.         CSIDL_MYPICTURES
  72.         CSIDL_PROFILE
  73.         CSIDL_PROGRAM_FILES_COMMON
  74.         CSIDL_COMMON_TEMPLATES
  75.         CSIDL_COMMON_DOCUMENTS
  76.         CSIDL_COMMON_ADMINTOOLS
  77.         CSIDL_ADMINTOOLS
  78.         CSIDL_COMMON_MUSIC
  79.         CSIDL_COMMON_PICTURES
  80.         CSIDL_COMMON_VIDEO
  81.         CSIDL_RESOURCES
  82.         CSIDL_RESOURCES_LOCALIZED
  83.         CSIDL_CDBURN_AREA
  84.     );
  85. }
  86.  
  87. # Routines available in core:
  88. # Win32::GetLastError
  89. # Win32::LoginName
  90. # Win32::NodeName
  91. # Win32::DomainName
  92. # Win32::FsType
  93. # Win32::GetCwd
  94. # Win32::GetOSVersion
  95. # Win32::FormatMessage ERRORCODE
  96. # Win32::Spawn COMMAND, ARGS, PID
  97. # Win32::GetTickCount
  98. # Win32::IsWinNT
  99. # Win32::IsWin95
  100.  
  101. # We won't bother with the constant stuff, too much of a hassle. Just hard
  102. # code it here.
  103.  
  104. sub NULL ()                { 0 }
  105. sub WIN31_CLASS ()            { &NULL }
  106.  
  107. sub OWNER_SECURITY_INFORMATION ()    { 0x00000001 }
  108. sub GROUP_SECURITY_INFORMATION ()    { 0x00000002 }
  109. sub DACL_SECURITY_INFORMATION  ()    { 0x00000004 }
  110. sub SACL_SECURITY_INFORMATION  ()    { 0x00000008 }
  111.  
  112. sub MB_ICONHAND        ()        { 0x00000010 }
  113. sub MB_ICONQUESTION    ()        { 0x00000020 }
  114. sub MB_ICONEXCLAMATION    ()        { 0x00000030 }
  115. sub MB_ICONASTERISK    ()        { 0x00000040 }
  116. sub MB_ICONWARNING    ()        { 0x00000030 }
  117. sub MB_ICONERROR    ()        { 0x00000010 }
  118. sub MB_ICONINFORMATION    ()        { 0x00000040 }
  119. sub MB_ICONSTOP        ()        { 0x00000010 }
  120.  
  121. sub SW_HIDE           ()        { 0 }
  122. sub SW_SHOWNORMAL     ()        { 1 }
  123. sub SW_SHOWMINIMIZED  ()        { 2 }
  124. sub SW_SHOWMAXIMIZED  ()        { 3 }
  125. sub SW_SHOWNOACTIVATE ()        { 4 }
  126.  
  127. sub CSIDL_DESKTOP              ()       { 0x0000 }     # <desktop>
  128. sub CSIDL_PROGRAMS             ()       { 0x0002 }     # Start Menu\Programs
  129. sub CSIDL_PERSONAL             ()       { 0x0005 }     # "My Documents" folder
  130. sub CSIDL_FAVORITES            ()       { 0x0006 }     # <user name>\Favorites
  131. sub CSIDL_STARTUP              ()       { 0x0007 }     # Start Menu\Programs\Startup
  132. sub CSIDL_RECENT               ()       { 0x0008 }     # <user name>\Recent
  133. sub CSIDL_SENDTO               ()       { 0x0009 }     # <user name>\SendTo
  134. sub CSIDL_STARTMENU            ()       { 0x000B }     # <user name>\Start Menu
  135. sub CSIDL_MYMUSIC              ()       { 0x000D }     # "My Music" folder
  136. sub CSIDL_MYVIDEO              ()       { 0x000E }     # "My Videos" folder
  137. sub CSIDL_DESKTOPDIRECTORY     ()       { 0x0010 }     # <user name>\Desktop
  138. sub CSIDL_NETHOOD              ()       { 0x0013 }     # <user name>\nethood
  139. sub CSIDL_FONTS                ()       { 0x0014 }     # windows\fonts
  140. sub CSIDL_TEMPLATES            ()       { 0x0015 }
  141. sub CSIDL_COMMON_STARTMENU     ()       { 0x0016 }     # All Users\Start Menu
  142. sub CSIDL_COMMON_PROGRAMS      ()       { 0x0017 }     # All Users\Start Menu\Programs
  143. sub CSIDL_COMMON_STARTUP       ()       { 0x0018 }     # All Users\Startup
  144. sub CSIDL_COMMON_DESKTOPDIRECTORY ()    { 0x0019 }     # All Users\Desktop
  145. sub CSIDL_APPDATA              ()       { 0x001A }     # Application Data, new for NT4
  146. sub CSIDL_PRINTHOOD            ()       { 0x001B }     # <user name>\PrintHood
  147. sub CSIDL_LOCAL_APPDATA        ()       { 0x001C }     # non roaming, user\Local Settings\Application Data
  148. sub CSIDL_COMMON_FAVORITES     ()       { 0x001F }
  149. sub CSIDL_INTERNET_CACHE       ()       { 0x0020 }
  150. sub CSIDL_COOKIES              ()       { 0x0021 }
  151. sub CSIDL_HISTORY              ()       { 0x0022 }
  152. sub CSIDL_COMMON_APPDATA       ()       { 0x0023 }     # All Users\Application Data
  153. sub CSIDL_WINDOWS              ()       { 0x0024 }     # GetWindowsDirectory()
  154. sub CSIDL_SYSTEM               ()       { 0x0025 }     # GetSystemDirectory()
  155. sub CSIDL_PROGRAM_FILES        ()       { 0x0026 }     # C:\Program Files
  156. sub CSIDL_MYPICTURES           ()       { 0x0027 }     # "My Pictures", new for Win2K
  157. sub CSIDL_PROFILE              ()       { 0x0028 }     # USERPROFILE
  158. sub CSIDL_PROGRAM_FILES_COMMON ()       { 0x002B }     # C:\Program Files\Common
  159. sub CSIDL_COMMON_TEMPLATES     ()       { 0x002D }     # All Users\Templates
  160. sub CSIDL_COMMON_DOCUMENTS     ()       { 0x002E }     # All Users\Documents
  161. sub CSIDL_COMMON_ADMINTOOLS    ()       { 0x002F }     # All Users\Start Menu\Programs\Administrative Tools
  162. sub CSIDL_ADMINTOOLS           ()       { 0x0030 }     # <user name>\Start Menu\Programs\Administrative Tools
  163. sub CSIDL_COMMON_MUSIC         ()       { 0x0035 }     # All Users\My Music
  164. sub CSIDL_COMMON_PICTURES      ()       { 0x0036 }     # All Users\My Pictures
  165. sub CSIDL_COMMON_VIDEO         ()       { 0x0037 }     # All Users\My Video
  166. sub CSIDL_RESOURCES            ()       { 0x0038 }     # %windir%\Resources\, For theme and other windows resources.
  167. sub CSIDL_RESOURCES_LOCALIZED  ()       { 0x0039 }     # %windir%\Resources\<LangID>, for theme and other windows specific resources.
  168. sub CSIDL_CDBURN_AREA          ()       { 0x003B }     # <user name>\Local Settings\Application Data\Microsoft\CD Burning
  169.  
  170. ### This method is just a simple interface into GetOSVersion().  More
  171. ### specific or demanding situations should use that instead.
  172.  
  173. my ($found_os, $found_desc);
  174.  
  175. sub GetOSName {
  176.     my ($os,$desc,$major, $minor, $build, $id)=("","");
  177.     unless (defined $found_os) {
  178.         # If we have a run this already, we have the results cached
  179.         # If so, return them
  180.  
  181.         # Use the standard API call to determine the version
  182.         ($desc, $major, $minor, $build, $id) = Win32::GetOSVersion();
  183.  
  184.         # If id==0 then its a win32s box -- Meaning Win3.11
  185.         #  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/sysinfo_49iw.asp
  186.         unless($id) {
  187.             $os = 'Win32s';
  188.         }
  189.     else {
  190.         # Magic numbers from MSDN documentation of OSVERSIONINFO
  191.         # Here is some mickeysoft code that tells the story as well:
  192.         # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/sysinfo_92jy.asp
  193.         # Caution with the above code as it uses functions unavailable
  194.         # to us in Perl.
  195.         # Most version names can be parsed from just the id and minor
  196.         # version
  197.         $os = {
  198.         1 => {
  199.             0  => "95",
  200.             10 => "98",
  201.             90 => "Me"
  202.         },
  203.         2 => {
  204.             0  => "2000",
  205.             1  => "XP/.Net",
  206.             51 => "NT3.51"
  207.         }
  208.         }->{$id}->{$minor};
  209.     }
  210.  
  211.         # This _really_ shouldnt happen. At least not for quite a while
  212.         # Politely warn and return undef
  213.         unless (defined $os) {
  214.             warn qq[Windows version [$id:$major:$minor] unknown!];
  215.             return undef;
  216.         }
  217.  
  218.         my $tag = "";
  219.  
  220.         # But distinguising W2k from NT4 requires looking at the major version
  221.         if ($os eq "2000" && $major != 5) {
  222.             $os = "NT4";
  223.         }
  224.  
  225.         # For the rest we take a look at the build numbers and try to deduce
  226.     # the exact release name, but we put that in the $desc
  227.         elsif ($os eq "95") {
  228.             if ($build eq '67109814') {
  229.                     $tag = '(a)';
  230.             }
  231.         elsif ($build eq '67306684') {
  232.                     $tag = '(b1)';
  233.             }
  234.         elsif ($build eq '67109975') {
  235.                     $tag = '(b2)';
  236.             }
  237.         }
  238.     elsif ($os eq "98" && $build eq '67766446') {
  239.             $tag = '(2nd ed)';
  240.         }
  241.  
  242.     if (length $tag) {
  243.         if (length $desc) {
  244.             $desc = "$tag $desc";
  245.         }
  246.         else {
  247.             $desc = $tag;
  248.         }
  249.     }
  250.  
  251.         # cache the results, so we dont have to do this again
  252.         $found_os      = "Win$os";
  253.         $found_desc    = $desc;
  254.     }
  255.  
  256.     return wantarray ? ($found_os, $found_desc) : $found_os;
  257. }
  258.  
  259. bootstrap Win32;
  260.  
  261. 1;
  262.