Mega Code Archive

 
Categories / Delphi / Examples
 

Fileassocone

unit Assoc; {Richard Ebbs March '99} {a small unit to text the setting up of 'file associations' (so that by clicking on a file with a .wtf extension, this brings up the Writer's Toolkit with the file loaded into the Writer's Toolkit Editor)...} {See below program code for detailed notes on what we're doing...} interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Registry, IniFiles; const wtExeFileName = 'WT.EXE'; instDirName = 'C:\WTOOLKIT'; type TAssocForm = class(TForm) GoButton: TButton; ExitButton: TButton; procedure GoButtonClick(Sender: TObject); procedure CreateFileAssociations(fullEXEPath, winDirPath: String); procedure ExitButtonClick(Sender: TObject); private {private declarations} public {public declarations} end; var AssocForm: TAssocForm; implementation {$R *.DFM} procedure TAssocForm.GoButtonClick(Sender: TObject); var winDirName: array[0..144] of Char; fullEXEPathName: String; begin GetWindowsDirectory(winDirName, sizeof(winDirName)); fullEXEPathName := instDirName + '\' + wtEXEFileName; CreateFileAssociations(fullEXEPathName, winDirName); ShowMessage('Done'); end; procedure TAssocForm.CreateFileAssociations(fullEXEPath, winDirPath: String); var reg: TRegistry; fullINIPathName, newValueName, newValueData, iniDataString: String; winINIFile: TIniFile; begin {1) add an entry to WIN.INI...} fullINIPathName := winDirPath + '\WIN.INI'; winINIFile := TIniFile.Create(fullINIPathName); with winINIFile do begin try {here the first parameter in WriteString writes a 'key name', if you like, to the INI file. This is a value that's found at the left of a line in the INI file. The IniFile.WriteString routine itself inserts a '=' character between that and the next parameter, which is the 'value' if you like...} iniDataString := fullEXEPath + ' ^.wtf'; WriteString('Extensions', 'wtf', iniDataString); finally {free up memory used in creating the INI file...} Free; end; end; {2) make a new registry entry under HKEY_CLASSES_ROOT...} reg := TRegistry.Create; reg.RootKey := HKEY_CLASSES_ROOT; {reg.LazyWrite := False;} reg.CreateKey('\.wtf'); reg.OpenKey('.wtf', False); newValueName := ''; newValueData := 'WritersToolkit'; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; {3) make another key with a subkey for icon double-clicking(?) under HKEY_CLASSES_ROOT...} reg := TRegistry.Create; reg.RootKey := HKEY_CLASSES_ROOT; reg.CreateKey('\WritersToolkit'); reg.CreateKey('\WritersToolkit\DefaultIcon'); reg.OpenKey('WritersToolkit\DefaultIcon', False); newValueName := ''; newValueData := fullEXEPath; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; {4) make another key with a subkey for 'open' under HKEY_CLASSES_ROOT...} reg := TRegistry.Create; reg.RootKey := HKEY_CLASSES_ROOT; reg.CreateKey('\WritersToolkit'); reg.CreateKey('\WritersToolkit\Shell'); reg.CreateKey('\WritersToolkit\Shell\Open'); reg.CreateKey('\WritersToolkit\Shell\Open\Command'); reg.OpenKey('WritersToolkit\Shell\Open\Command', False); newValueName := ''; {note the space in string here...} newValueData := fullEXEPath + ' %1'; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; {5) make a new registry entry under HKEY_LOCAL_MACHINE...} reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; {reg.LazyWrite := False;} reg.CreateKey('\Software\Classes\.wtf'); reg.OpenKey('\Software\Classes\.wtf', False); newValueName := ''; newValueData := 'WritersToolkit'; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; {6) make another key with a subkey for 'icon double-clicking(?) under HKEY_LOCAL_MACHINE...} reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; reg.CreateKey('\Software\Classes\WritersToolkit'); reg.CreateKey('\Software\Classes\WritersToolkit\DefaultIcon'); reg.OpenKey('\Software\Classes\WritersToolkit\DefaultIcon', False); newValueName := ''; newValueData := fullEXEPath; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; {7) make another key with a subkey for 'open' under HKEY_LOCAL_MACHINE...} reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; reg.CreateKey('\Software\Classes\WritersToolkit'); reg.CreateKey('\Software\Classes\WritersToolkit\Shell'); reg.CreateKey('\Software\Classes\WritersToolkit\Shell\Open'); reg.CreateKey('\Software\Classes\WritersToolkit\Shell\Open\Command'); reg.OpenKey('\Software\Classes\WritersToolkit\Shell\Open\Command', False); newValueName := ''; newValueData := fullEXEPath + ' %1'; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; {this next may well not be necessary...} reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Extensions', False); newValueName := '.wtf'; newValueData := fullEXEPath + ' ^.WTF'; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free; end; procedure TAssocForm.ExitButtonClick(Sender: TObject); begin Close; end; end. {To associate a file extension with an application (so that the user can double click on a file or icon in Explorer to run the application with that file loaded) seemingly it's necessary to do SEVEN things- (and for this example we set up the association of the Writer's Toolkit application with it's default file format of .wtf). Note that in essence what we're doing is adding a line to the WIN.INI file, then adding two virtually identical sets of values in different places in the registry, one set under HKEY_CLASSES_ROOT, the other set under HKEY_LOCAL_MACHINE. i) add a new line to the WIN.INI file, under the 'Extensions' category so that the entry looks like this wtf=C:\WTOOLKIT\WT.EXE ^.wtf ii) add a new folder (key) and value to the registry under HKEY_CLASSES_ROOT. For this example the folder (key) will be called simply .wtf and the (two-part) value in there will be name: default value (ie an empty string written to the 'name' field) and, data: WritersToolkit. Note that values in the registry are in two parts, 'name' and 'data'. The Delphi WriteString routine for writing to the registry takes two parameters, one for each of these two parts of the value. iii) add another new folder (key) and value to the registry under HKEY_CLASSES_ROOT. This one we will call WritersToolkit (and this entry must be spelt in exactly the same way as it's spelt in ii) above). Under this new key ('folder') we now create a subfolder. The subfolder (or 'subkey' if you prefer) we will call DefaultIcon. The value within this is name: default value, and data: application exe name (including the full path). Note that we add [space] %1 to the EXE name (so that the file in question is opened with the application). iv) now we create more subfolders under the HKEY_CLASSES_ROOT rootkey, also under WritersToolkit as above. These subfolders we will call Shell\Open\Command. We don't need to write anything to the 'Shell' or 'Open' keys, so we now ignore them, but in the Command key we again write the application exe name (including the full path). v) here we add a new value to the registry under HKEY_LOCAL_MACHINE that is the same as the entry under HKEY_CLASSES_ROOT in ii) above. The only difference here is that the key is under the rootkey of HKEY_LOCAL_MACHINE and \Software\Classes (not HKEY_CLASSES_ROOT). (See ii) above for the value we use). vi) similarly, here we add a new key under HKEY_LOCAL_MACHINE that is the same as in iii) above, except that here we create a new key under the HKEY_LOCAL_MACHINE rootkey called WritersToolkit, and we also create a subkey called DefaultIcon, writing name: default value (ie nothing) and data: full_exe_name_inc_path to this. vii) lastly we create extra subkeys called Shell\Open\Command under the rootkey of HKEY_LOCAL_MACHINE and also under the key of WritersToolkit (similar to iv) above, but in a different place). Again we create subkeys called Shell\Open\Command. We don't need to write anything to the 'Shell' or 'Open' keys, so we ignore them, but in the Command key we again write the application exe name (including the full path). OK? Note 1. I guess that by adding %1 to the exe name variously here we are setting things up so that the application takes the filename of a file to open as a parameter. This will only work, of course, if the application itself HAS the code to handle exe file parameters... Note 2: if icon double-click no worky try adding ',1' to the end of each full_EXE_name value (under both DefaultIcon keys/folders)..?} {maybe we need to do this as well... reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Extensions', False); newValueName := '.wtf'; newValueData := fullEXEPath + ' ^.WTF'; reg.WriteString(newValueName, newValueData); reg.CloseKey; reg.free;} ********************************************* It is complicated enough that I wouldn't want to write an explanation here, but Neil Rubenking does a good job of explaining it all in the docs for his PC Magazine utility "Freedom of Association". I suggest you use the www.pcmag.com web site and read the explanation of the utility. ********************************************* Check this out. http://x53.deja.com/getdoc.xp?AN=459725089&CONTEXT=972400484.1695154184&hitn um=32 Mark Bracey ---------------- Having a Delphi Day ... ********************************************** It's a real pain in the butt to do. Look for "shcmdint.zip" on DSP or maybe Torry's. It's by Ken Miles "Allow your application to interact with the Windows Explorer, open documents in existing instances of the application, create and respond to right mouse menu items for file types in the Windows Explorer and simply process the command line parameters and switches." -- Daniel J. Wojcik http://www.genjerdan.com ******************************************************** i did some checking and ended up writing a wrapper component for the api. these are the steps i followed: 1) you'll need to consult the MSDN dox here: http://msdn.microsoft.com/library/psdk/shellcc/shell/Functions/URLAssociatio nDialog.htm 2) unfortunately the MSDN dox are "not completely" correct. they point you to shell32.dll - but really the api is in url.dll. 3) do a dump on the dll to get the info you need: tdump.exe c:\winnt\system32\url.dll > c:\junk.txt 4) write your export unit for the dll. look at something like wininet.pas to see how it's done. 5) write your component to wrap the api calls.