Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Programming Registering Extensions YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

Registering Extensions
Another text based on search strings the lead to these pages... :)

Ok, so if your application uses specific file extensions (one or more), you would probably want to associate those extensions with your program so that when the user double clicks on such a file, it will get automatically opened in your app.

Note that for this to work, you have to first assure that your application will open files that are passed to it via (command-line) parameters; that is, through ParamStr(n).

Also, installation programs usually can handle this for you, but the code below is for registering extensions straight from a Delphi application.

Registered extensions are kept in the system registry under two keys in HKEY_CLASSES_ROOT:
  • extension (such as .yib) where the identifier of the extension is kept in the key's default value,
  • extension identifier (such as YIB.main) where the default icon and various command configurations are kept.
So, the RegisterExtension procedure may looks as follows:
  procedure RegisterExtension(AExt, AID, ADesc, AProg: string; AIcon: string = '');
  var
    LReg: TRegistry;
  begin
    LReg := TRegistry.Create;
    try
      LReg.LazyWrite := FALSE;
      LReg.RootKey := HKEY_CLASSES_ROOT;
  
      // Register the extension identifier:
      if LReg.OpenKey(AExt, TRUE) then
        try
          LReg.WriteString('', AID);
        finally
          LReg.CloseKey;
        end;
  
      // Register the extension properties:
      // Icon:
      if LReg.OpenKey(AID, TRUE) then
        try
          LReg.WriteString('', ADesc);
          LReg.OpenKey('DefaultIcon', TRUE);
          if AIcon = '' then
            LReg.WriteString('', AProg + ', 0')
          else
            LReg.WriteString('', AIcon);
        finally
          LReg.CloseKey;
        end;
  
      // Open command:
      if LReg.OpenKey(AID, TRUE) then
        try
          LReg.OpenKey('Shell', TRUE);
          LReg.OpenKey('Open', TRUE);
          LReg.OpenKey('Command', TRUE);
          LReg.WriteString('', AProg + ' "%1"');
        finally
          LReg.CloseKey;
        end;
    finally
      FreeAndNIL(LReg);
    end;
  end;
So, let's take a look at what's here.

TRegistry is a standard object in Delphi's RTL that handles access to the Windows registry. We're setting LazyWrite here to FALSE so that all settings are written to the registry as soon as the respective CloseKey is called. HKEY_CLASSES_ROOT is where file extension settings are kept.

Next, we define the extension to extension ID mapping by writing the default value of the ID in the key equal to the extension (for instance, AExt can be '.yib' here, and AID - 'YIB.main').

Next, we assign an icon to that extension. This icon will be shown for the file type in Windows Explorer and any other applications that show file type icons. Usually, it will be enough just to pass an empty parameter here, thus setting the default value of the DefaultIcon key to AProg + ', 0'. This will make the first icon (in the list of all icons in the program's resources) as the file type's icon. If you want to take any other icon (not necessarily from the program's .exe. file), pass the file with icons and the icon's index in that file (0-based) in the AIcon parameter.

Finally, in the Shell\Open\Command key, the command for opening the file with the program passed in AProg is defined. In this case, we're telling Windows that it should execute the program in AProg and pass the file's name (that the user clicked on) as the first parameter to the program. Obviously, you can define many other behaviors here...

One additional note: in the Shell key you can define many other commands, such as the Print command, ddeexec for dynamic data exchange (for opening, printing, and creating new files), and many others. But I guess that the most often used one is Open.

And how can you call this function? Well, for instance like this:
  RegisterExtension('.yib', 'YIB.main', 'YAC Interview Builder Script', ParamStr(0));
When called from inside YIB.exe, this will tell Windows to run .yib files using YIB.exe.

HTH

Top

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Programming

Delphi


Related pages

TFS - The underlying connection was closed: an unexpected error occurred on a receive.

WCF - The underlying connection was closed: an unexpected error occurred on a receive.

Delphi interfaces... again

Saving / restoring window placements in .NET

Checking "Dangling" Event Handlers in Delphi Forms

Meaningful identifiers

Public fields vs. properties

Drag-n-drop files onto the application window

Intraweb and MaxConnections

A Case for FreeAndNIL

Intraweb as an Apache DSO module

"Device not supported" in Intraweb

Automated GUI Testing

Random()'s Determinism

Rounding and precision on the 8087 FPU

SessionTimeout in Intraweb

Using TChart with Intraweb

Unknown driver: MySQL

TIdMessage's CharSet

Software Guarantees

Automated Testing of Window Forms

TChart - Missing Labels in Axes

Memory Leaks and Connection Explosions in DBExpress

Controlling Conditional Defines and Compilation Switches

Detecting Memory Leaks with DUnit

last_insert_id() and DBExpress

DBExpress and Thread Safety

Forms as Frames

Checking Dangling Pointers vs. the New Memory Manager

Accessing Protected Members

Objects, interfaces, and memory management in Delphi