Skip to content

Editing the system registry with Delphi

I must say that this is extremely easy to do, and reallly can make any delphi application feel like a “proper” windows application.

Firstly I must point out that all this tutorial does (at the moment) is teach you how to make windows open documents with a certain extension automatically, as well as given them an icon of your choosing. A basic knowledge of Delphi would be a great help for this tutorial, but is NOT NECESSARY. I will point out everything clearly so that any programmer who knows how to edit registry keys in his language and access parameters can use this (i will show you how to do this if you are a delphi programmer).

If you do not know what the system registry is, then think of it as a massive database of information which you can mess around with that windows uses. When you double-click on a file, windows checks the registry to see what application has registered that extension, and open the document within it.

The registry settings you should alter and what you should put in them are given below for all you non delphi programmers(I’ll explain more afters). For the example below i have just created a program called Hoding, which opens all “.hod” files automatically. It is located at c:\hoding.exe (you do need to add some code to your program for handling the autoopen parameters, a simple editing of the registry won’t do (in Delphi anyway)).

Directory or key Data
HKEY_CLASSES_ROOT\.hod\ Hoding.Document
HKEY_CLASSES_ROOT\Hoding.Document\ Hoding Document
HKEY_CLASSES_ROOT\Hoding.Document\shell\open\command\ “C:\Hoding.exe” %1
HKEY_CLASSES_ROOT\Hoding.Document\DefaultIcon\ “C:\Hoding.exe” ,0

If any of the above even looks a little like it makes sense to you, you will have no problem in modifying the code to your own progam’s ends.

Before I get into how to change the registry settings, I want to show you how to handle them. Look at the data held in the HKEY_CLASSES_ROOT\Hoding.Document\shell\open\command\ folder. It says “C:\Hoding.exe” %1 . This means that when a person trys to open one of the file types you have registered, windows just runs your program and attaches the file name onto the end of it as a parameter. Eg. you double click on c:\my documents\testfile.hod after messing with the registry. Windows will run this– “C:\Hoding.exe” “c:\my documents\testfile.hod” –. Basically, all windows does is replace the %1 in the registry data with the document’s file name.

Now I’m going to go into the code needed in delphi to access this parameter. It’s dead simple actually. To acces the 1st parameter, just type paramstr(1) (in case you care, paramstr(0) is the applications directory and filename). If there is none, then it will be blank. Likewise, you can, if you have more, open the second, or third parameter by using the paramstr(x:integer) function. I’ll give you an example piece of code below for handeling the code (I’m just going to load the file into a tmemo, but naturally you should be able to write your own procedure).

procedure TForm1.FormCreate(Sender: TObject);

Was that tough? I don’t think so. Now on to the editing of the registry…

I’m just going to paste the code I use below, add a few comments, and hope that you will have the cop on to be able to modify it yourself. (also, don’t forget that you have to add “registry” to your uses list at the top)

procedure TMainForm.SetRegistry;
var reg: tregistry;
reg.writestring(”,’Hoding Document’);
reg.WriteString(”,'”‘+appfilename +'” “%1” ‘);
reg.writestring(”,appfilename +’,0′);

I would love to add loads of more stuff, but I don’t have enough time right now so you’ll have to figure everything out yourself (not as tutorial like as I would like, but I don’t have the time to make it so). All I will say is that if you use the code above, the document icon will have the same icon as the program. Ideally you should add another icon to the resource file, change the line that says this reg.writestring(”,appfilename +’,0′); to this reg.writestring(”,appfilename +’,1′); and the program will use the second icon istead of the first :).

Have fun making loads of nice registry-integrated apps!

-Stephen Lavelle-


  1. sam wrote:

    This appears to work for XP but I cannot get it to work in Vista

    Saturday, June 2, 2007 at 5:14 am | Permalink
  2. Icecube wrote:

    Hmmm…dont know if I can help you there dude; it’s been many years since I’ve done anything with Delphi (and, indeed, windows)

    Saturday, June 2, 2007 at 6:19 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *