[Win32utils-devel] New win32-dir

Berger, Daniel Daniel.Berger at qwest.com
Wed May 4 10:43:10 EDT 2005


Hi all,

In lieu of Timothy's pure Ruby junction code, and Zach's VC++ 6 issues,
I decided to just rewrite the dang thing in pure Ruby using Win32API.
That will solve's Zach's problem and make it easier to add Timothy's
code, which I plan to add (slightly modified) to the 0.2.0 release.

The code is below.  One thing I have a question about is how to handle
the situation where SHGetFolderPath is not found.  I just wrapped it in
a begin/rescue clause.  Does that seem a reasonable approach?

Regards,

Dan

PS - There are a few extra constants to boot (I dug them out of the
header files).

require "Win32API"

module Win32

   # SHGetFolderPath may not be defined
   # If not, use SHGetSpecialFolderPath instead
   begin
      SHGetFolderPath =
Win32API.new("shell32","SHGetFolderPath","LLLLP","L")
   rescue RuntimeError
      SHGetSpecialFolderPath =
         Win32API.new("shell32","SHGetSpecialFolderPath", "LPLL","L")
   end

   CSIDL_DESKTOP          =  0x0000 # <desktop>
   CSIDL_INTERNET         =  0x0001 # Internet Explorer (icon on
desktop)
   CSIDL_PROGRAMS         =  0x0002 # Start Menu\Programs
   CSIDL_CONTROLS         =  0x0003 # My Computer\Control Panel
   CSIDL_PRINTERS         =  0x0004 # My Computer\Printers
   CSIDL_PERSONAL         =  0x0005 # My Documents
   CSIDL_FAVORITES        =  0x0006 # <user name>\Favorites
   CSIDL_STARTUP          =  0x0007 # Start Menu\Programs\Startup
   CSIDL_RECENT           =  0x0008 # <user name>\Recent
   CSIDL_SENDTO           =  0x0009 # <user name>\SendTo
   CSIDL_BITBUCKET        =  0x000a # <desktop>\Recycle Bin
   CSIDL_STARTMENU        =  0x000b # <user name>\Start Menu
   CSIDL_MYDOCUMENTS      =  0x000c # logical "My Documents" desktop
icon
   CSIDL_MYMUSIC          =  0x000d # "My Music" folder
   CSIDL_MYVIDEO          =  0x000e # "My Videos" folder
   CSIDL_DESKTOPDIRECTORY =  0x0010 # <user name>\Desktop
   CSIDL_DRIVES           =  0x0011 # My Computer
   CSIDL_NETWORK          =  0x0012 # Network Neighborhood (My Network
Places)
   CSIDL_NETHOOD          =  0x0013 # <user name>\nethood
   CSIDL_FONTS            =  0x0014 # windows\fonts
   CSIDL_TEMPLATES        =  0x0015
   CSIDL_COMMON_STARTMENU =  0x0016 # All Users\Start Menu
   CSIDL_COMMON_PROGRAMS  =  0X0017 # All Users\Start Menu\Programs
   CSIDL_COMMON_STARTUP   =  0x0018 # All Users\Startup
   CSIDL_COMMON_FAVORITES =  0x001f

   CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019 # All Users\Desktop

   CSIDL_APPDATA        = 0x001a # <user name>\Application Data
   CSIDL_PRINTHOOD      = 0x001b # <user name>\PrintHood
   CSIDL_LOCAL_APPDATA  = 0x001c # <user name>\Local
Settings\Application Data
   CSIDL_ALTSTARTUP     = 0x001d # non localized startup

   CSIDL_COMMON_ALTSTARTUP       = 0x001e # non localized common startup
   CSIDL_INTERNET_CACHE          = 0x0020
   CSIDL_COOKIES                 = 0x0021
   CSIDL_HISTORY                 = 0x0022
   CSIDL_COMMON_APPDATA          = 0x0023 # All Users\Application Data
   CSIDL_WINDOWS                 = 0x0024 # GetWindowsDirectory()
   CSIDL_SYSTEM                  = 0x0025 # GetSystemDirectory()
   CSIDL_PROGRAM_FILES           = 0x0026 # C:\Program Files
   CSIDL_MYPICTURES              = 0x0027 # C:\Program Files\My Pictures
   CSIDL_PROFILE                 = 0x0028 # USERPROFILE
   CSIDL_SYSTEMX86               = 0x0029 # x86 system directory on RISC
   CSIDL_PROGRAM_FILESX86        = 0x002a # x86 C:\Program Files on RISC
   CSIDL_PROGRAM_FILES_COMMON    = 0x002b # C:\Program Files\Common
   CSIDL_PROGRAM_FILES_COMMONX86 = 0x002c # x86 Program Files\Common on
RISC
   CSIDL_COMMON_TEMPLATES        = 0x002d # All Users\Templates
   CSIDL_COMMON_DOCUMENTS        = 0x002e # All Users\Documents
   CSIDL_CONNECTIONS             = 0x0031 # Network and Dial-up
Connections
   CSIDL_COMMON_MUSIC            = 0x0035 # All Users\My Music
   CSIDL_COMMON_PICTURES         = 0x0036 # All Users\My Pictures
   CSIDL_COMMON_VIDEO            = 0x0037 # All Users\My Video
   CSIDL_RESOURCES               = 0x0038 # Resource Direcotry
   CSIDL_RESOURCES_LOCALIZED     = 0x0039 # Localized Resource Direcotry
   CSIDL_COMMON_OEM_LINKS        = 0x003a # Links to All Users OEM
specific apps

   # USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
   CSIDL_CDBURN_AREA  = 0x003b
   
   # All Users\Start Menu\Programs\Administrative Tools 
   CSIDL_COMMON_ADMINTOOLS = 0x002f
   
   # <user name>\Start Menu\Programs\Administrative Tools
   CSIDL_ADMINTOOLS = 0x0030 
   
   def cget(const)
      path = " " * 255
      
      if SHGetFolderPath     
         result = SHGetFolderPath.call(0, const, 0, 1, path)
      else
         result = SHGetSpecialFolderPath.call(0, path, const, 0)
      end
         
      if result != 0
         path = nil
      else 
         path.gsub!(/\000/, '')
         path.strip!
      end
      
      path
   end
   module_function :cget
end

class Dir
   include Win32
   undef_method :cget # Not meant to be public
   
   DESKTOP     = Win32.cget(CSIDL_DESKTOP)
   INTERNET    = Win32.cget(CSIDL_INTERNET)
   PROGRAMS    = Win32.cget(CSIDL_PROGRAMS)
   CONTROLS    = Win32.cget(CSIDL_CONTROLS)
   PRINTERS    = Win32.cget(CSIDL_PRINTERS)
   PERSONAL    = Win32.cget(CSIDL_PERSONAL)
   FAVORITES   = Win32.cget(CSIDL_FAVORITES)
   STARTUP     = Win32.cget(CSIDL_STARTUP)
   RECENT      = Win32.cget(CSIDL_RECENT)
   SENDTO      = Win32.cget(CSIDL_SENDTO)
   BITBUCKET   = Win32.cget(CSIDL_BITBUCKET)
   STARTMENU   = Win32.cget(CSIDL_STARTMENU)
   MYDOCUMENTS = Win32.cget(CSIDL_MYDOCUMENTS)
   MYMUSIC     = Win32.cget(CSIDL_MYMUSIC)
   MYVIDEO     = Win32.cget(CSIDL_MYVIDEO)
   
   DESKTOPDIRECTORY = Win32.cget(CSIDL_DESKTOPDIRECTORY)
   DRIVES           = Win32.cget(CSIDL_DRIVES)
   NETWORK          = Win32.cget(CSIDL_NETWORK)
   NETHOOD          = Win32.cget(CSIDL_NETHOOD)
   FONTS            = Win32.cget(CSIDL_FONTS)
   TEMPLATES        = Win32.cget(CSIDL_TEMPLATES)
   COMMON_STARTMENU = Win32.cget(CSIDL_COMMON_STARTMENU)
   COMMON_PROGRAMS  = Win32.cget(CSIDL_COMMON_PROGRAMS)
   COMMON_STARTUP   = Win32.cget(CSIDL_COMMON_STARTUP)
   COMMON_FAVORITES = Win32.cget(CSIDL_COMMON_FAVORITES)

   COMMON_DESKTOPDIRECTORY = Win32.cget(CSIDL_COMMON_DESKTOPDIRECTORY)

   APPDATA       = Win32.cget(CSIDL_APPDATA)
   PRINTHOOD     = Win32.cget(CSIDL_PRINTHOOD)
   LOCAL_APPDATA = Win32.cget(CSIDL_LOCAL_APPDATA)
   ALTSTARTUP    = Win32.cget(CSIDL_ALTSTARTUP)

   COMMON_ALTSTARTUP       = Win32.cget(CSIDL_COMMON_ALTSTARTUP)
   INTERNET_CACHE          = Win32.cget(CSIDL_INTERNET_CACHE)
   COOKIES                 = Win32.cget(CSIDL_COOKIES)
   HISTORY                 = Win32.cget(CSIDL_HISTORY)
   COMMON_APPDATA          = Win32.cget(CSIDL_COMMON_APPDATA)
   WINDOWS                 = Win32.cget(CSIDL_WINDOWS)
   SYSTEM                  = Win32.cget(CSIDL_SYSTEM)
   PROGRAM_FILES           = Win32.cget(CSIDL_PROGRAM_FILES)
   MYPICTURES              = Win32.cget(CSIDL_MYPICTURES)
   PROFILE                 = Win32.cget(CSIDL_PROFILE)
   SYSTEMX86               = Win32.cget(CSIDL_SYSTEMX86)
   PROGRAM_FILESX86        = Win32.cget(CSIDL_PROGRAM_FILESX86)
   PROGRAM_FILES_COMMON    = Win32.cget(CSIDL_PROGRAM_FILES_COMMON)
   PROGRAM_FILES_COMMONX86 = Win32.cget(CSIDL_PROGRAM_FILES_COMMONX86)
   COMMON_TEMPLATES        = Win32.cget(CSIDL_COMMON_TEMPLATES)
   COMMON_DOCUMENTS        = Win32.cget(CSIDL_COMMON_DOCUMENTS)
   CONNECTIONS             = Win32.cget(CSIDL_CONNECTIONS)
   COMMON_MUSIC            = Win32.cget(CSIDL_COMMON_MUSIC)
   COMMON_PICTURES         = Win32.cget(CSIDL_COMMON_PICTURES)
   COMMON_VIDEO            = Win32.cget(CSIDL_COMMON_VIDEO)
   RESOURCES               = Win32.cget(CSIDL_RESOURCES)
   RESOURCES_LOCALIZED     = Win32.cget(CSIDL_RESOURCES_LOCALIZED)
   COMMON_OEM_LINKS        = Win32.cget(CSIDL_COMMON_OEM_LINKS)

   # USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
   CDBURN_AREA = Win32.cget(CSIDL_CDBURN_AREA)
   
   # All Users\Start Menu\Programs\Administrative Tools 
   COMMON_ADMINTOOLS = Win32.cget(CSIDL_COMMON_ADMINTOOLS)
   
   # <user name>\Start Menu\Programs\Administrative Tools
   ADMINTOOLS = Win32.cget(CSIDL_ADMINTOOLS)
end



More information about the win32utils-devel mailing list