[Windows] Associate file extensions with a program

new topic     » topic index » view thread      » older message » newer message

Hi all,

I wrote two small routines, that allow to associate file extensions with
a program on Windows.
Does anyone see a mistake/bug in the code? The code seems to work fine
on Win 98. Is it also appropriate for other Windows versions? How can I
"uninstall" these associations? Can it be done in a similar simple way,
or will I have to use Windows API functions for that purpose?

-- tested with Eu 2.4
include wildcard.e                         -- for lower()

function escape (sequence fileName)
   sequence ret
   integer char

   ret = ""
   for i = 1 to length(fileName) do
      char = fileName[i]
      if char = '\\' then
         ret &= "\\\\"
      else
         ret &= char
      end if
   end for
   return ret
end function

global function registry_associate (
                                    sequence regFile,
                                    sequence extensionList,
                                    sequence fileType,
                                    sequence description,
                                    sequence iconFile,
                                    integer  iconNo,
                                    sequence progFile
                                   )
   -- Produces the file <regFile>.
   -- When on Windows then <regFile> is double-clicked, files with
   -- an extension contained in <extensionList> will be associated with
   -- the denoted icon and with program <progFile>.

   integer fw

   if not equal(lower(regFile[length(regFile)-3..length(regFile)]), ".reg") then
      regFile &= ".reg"
   end if

   fw = open(regFile, "w")
   if fw = -1 then
      return -1                            -- error
   end if

   puts(fw, "REGEDIT4\n\n")

   for i = 1 to length(extensionList) do
      printf(fw, "[HKEY_CLASSES_ROOT\\.%s]\n", {extensionList[i]})
      printf(fw, "@=\"%s\"\n\n", {fileType})
   end for

   printf(fw, "[HKEY_CLASSES_ROOT\\%s]\n", {fileType})
   printf(fw, "@=\"%s\"\n\n", {description})

   printf(fw, "[HKEY_CLASSES_ROOT\\%s\\DefaultIcon]\n", {fileType})
   printf(fw, "@=\"%s,%d\"\n\n", {escape(iconFile), iconNo})

   printf(fw, "[HKEY_CLASSES_ROOT\\%s\\shell\\open\\command]\n", {fileType})
   printf(fw, "@=\"\\\"%s\\\" \\\"%%1\\\"\"\n\n", {escape(progFile)})

   close(fw)
   return 0                                -- success
end function


-- Demo: Write a REG file, that associates the extension "exw" with
--       "C:\\Programs\\Euphoria\\Bin\\exw.exe"
? registry_associate(
                     "test.reg",
                     {"exw"},
                     "EuWinApp",
                     "Euphoria Windows App",
                     "C:\\Programs\\Euphoria\\Bin\\exw.exe",
                     0,
                     "C:\\Programs\\Euphoria\\Bin\\exw.exe"
                    )


TIA,
   Juergen

-- 
We don't know where to GOTO if we don't know where we've COME FROM.
http://www.fortran.com/fortran/come_from.html

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu