Re: Windows File Associations
- From: "Jeff Blakeney" <jeff.blakeney@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 15 Jan 2009 19:14:07 -0500
To: Bill Buckels
On Wed, 14 Jan 2009 21:33:29 -0600, Bill Buckels wrote:
"Jonno Downes" <jonnosan@xxxxxxxxx> wrote:
Of course, it would also be nice if emulator installations then associated
themselves with the appriate file extension, so double clicking a 'dsk'
file for example fired up AppleWin to boot up the dsk in question.
That's basically the way it works on my Machine right now with Apple and
Commdore 64 disk images. My d64's can be called like a command from a
console window. No biggie there.
In my ClipShop application I allow the user to set associations and also to
remove them. This is not rocket-science. The following two functions are
mine and may be used freely, but they are not mine alone. I imagine
everyone's code is about the same (or better:(
I also fire back to Windows ifd I can't open the thing and it goes for
whatver handler the user wants in the next place...
Use and and copy freely provided you don't blame me for it:)
BOOL CreateFileAssociation(char *ext, char *class, char *description, BOOL
bNotify)
Perhaps your routine does more but here is a routine that I just whipped
up that can set filetype associations. You give it a file type
extension, a title or name of the key to hold the other parameters, the
path to an icon, the path to the application to open files of that type
and a description of the file type.
This is written in PowerBASIC so the _ character means that the command
continues on the next line, ' is a short form for a remark, $DQ is an
equate that gets replaced with double quote (") and all the Windows API
equates are preceded by a %. Also, the security attributes section of
the RegCreateKeyEx call is passing a null string ("") but I'm pretty
sure that is a screw up in the declaration for that call in the
WIN32API.INC file that I may have to fix someday. The PBMAIN function
at the end just shows a test call and it uses PB's MSGBOX shortcut for
the Windows' MessageBox API call.
Now I need to write a routine to unregister a file type I created in
this way but I've got to go to my TAP pool league session playoffs now
and doing this has already distracted me for too long but it sure was
fun. :-)
#INCLUDE "WIN32API.INC"
FUNCTION RegisterFileType( _
sExtension AS STRING, _ ' extension
sTitle AS STRING, _ ' extension's title
sIconPath AS STRING, _ ' path to icon
sApplicationPath AS STRING, _ ' path to application
sDescription AS STRING _ ' file type description
) AS LONG
LOCAL hKey AS DWORD
LOCAL lIndex AS LONG
LOCAL lResult AS LONG
LOCAL sKeyName() AS ASCIIZ * %MAX_PATH
LOCAL sValue() AS ASCIIZ * %MAX_PATH
DIM sKeyName(0 TO 3)
DIM sValue(0 TO 3)
IF sExtension = "" OR sTitle = "" THEN
' can't create keys because one or both these values are null so
' return a negative error to differentiate it from the WinError.h
' value that can be generated from the RegCreateKeyEx or
' RegSetValueEx calls in the loop below
FUNCTION = -1
EXIT FUNCTION
END IF
' set array variables to use in our loop making sure we put the any
' paths and passed parameters in double quotes in case there are
' spaces in the paths
' key = filetype, value = title
IF LEFT$(sExtension, 1) = "." THEN
' extension passed in starts with a period so we are okay
sKeyName(0) = sExtension
ELSE
' extension passed in doesn't start with a period so add one
sKeyName(0) = "." + sExtension
END IF
sValue(0) = sTitle
' key = title, value = description
sKeyName(1) = sTitle
' could add a check here to see if the description is null and set
' it to some default value if it is
sValue(1) = sDescription
' key = title\DefaultIcon, value = path to icon
sKeyName(2) = sTitle + "\DefaultIcon"
sValue(2) = $DQ + sIconPath + $DQ
' key = title\shell\open\command, value = path to app
sKeyName(3) = sTitle + "\shell\open\command"
sValue(3) = $DQ+sApplicationPath+$DQ + " " + $DQ+"%1"+$DQ
' loop through array elements and add/update keys and values
FOR lIndex = 0 TO 3
lResult = RegCreateKeyEx(%HKEY_CLASSES_ROOT,_ ' key
sKeyName(lIndex), _ ' subkey
0, _ ' reserved
"", _ ' class
%REG_OPTION_NON_VOLATILE,_ ' options
%KEY_ALL_ACCESS, _ ' security access
"", _ ' security attributes
hKey, _ ' returned handle
lResult) ' disposition
IF lResult = %ERROR_SUCCESS THEN
' created/opened the key successfully so set its value
lResult = RegSetValueEx(hKey, _ ' key
"", _ ' subkey
0, _ ' reserved
%REG_SZ, _ ' type of value
sValue(lIndex), _ ' value
LEN(sValue(lIndex)) + 1)' size of value
RegCloseKey hKey
END IF
' check to see if any errors occurred
IF lResult <> %ERROR_SUCCESS THEN
' an error occurred with either the RegCreateKeyEx or
' RegSetValueEx calls above so return the error
FUNCTION = lResult
EXIT FUNCTION
END IF
NEXT lIndex
FUNCTION = %ERROR_SUCCESS
END FUNCTION
FUNCTION PBMAIN()
LOCAL lError AS LONG
lError = RegisterFileType(".jbt", _ ' extension
"Jeff Blakeney Test File", _ ' title
"C:\Projects\EDM32\Source.ico", _ ' icon path
"C:\Windows\notepad.exe", _ ' app path
"Test filetype for checking program association") ' description
IF lError THEN
MSGBOX "Error: " + FORMAT$(lError), %MB_OK, "RegisterFileType"
ELSE
MSGBOX "Succeeded", %MB_OK, "RegisterFileType"
END IF
END FUNCTION
.
- Follow-Ups:
- Re: Windows File Associations
- From: Bill Buckels
- Re: Windows File Associations
- References:
- Re: Windows File Associations
- From: Bill Buckels
- Re: Windows File Associations
- Prev by Date: Re: Windows File Associations
- Next by Date: Re: Windows File Associations
- Previous by thread: Re: Windows File Associations
- Next by thread: Re: Windows File Associations
- Index(es):
Relevant Pages
|