Solved

register filetype with application

Posted on 2011-03-03
2
259 Views
Last Modified: 2012-05-11
found that code below here at EE, but

a) using WIN 7 and application has no admin rights this code makes an AV !

b) Application with ADIM Rights, no AV but also no effect

c) how to unregister a Filetype and a extension  
{**************************************************************
 *
 *  source : http://www.experts-exchange.com/Programming/Editors_IDEs/Delphi/Q_24973381.html
 *
 **************************************************************}

procedure RegisterFileType(ExtName:String; AppName:String) ;
var
   reg:TRegistry;
begin
  reg := TRegistry.Create;
  try
   reg.RootKey:=HKEY_CLASSES_ROOT;
   reg.OpenKey('.' + ExtName, True) ;
   reg.WriteString('', ExtName + 'file') ;
   reg.CloseKey;
   reg.CreateKey(ExtName + 'file') ;
   reg.OpenKey(ExtName + 'file\DefaultIcon', True) ;
   reg.WriteString('', AppName + ',0') ;
   reg.CloseKey;
   reg.OpenKey(ExtName + 'file\shell\open\command', True) ;
   reg.WriteString('',AppName+' "%1"') ;
   reg.CloseKey;
  finally
   reg.Free;
  end;

  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil) ;
end;

Open in new window

0
Comment
Question by:BdLm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 100 total points
ID: 35026998
In Win Vista and Win 7 you need to setup a manifest to be able to do that (besides user must be admin to alter the Registry):

http://delphi.about.com/od/delphitips2009/qt/delphi-vista-registry-run-on-startup.htm
0
 
LVL 3

Accepted Solution

by:
sYk0 earned 400 total points
ID: 35034983
You'll need admin rights to register a file globally in Vista and Seven, you can however register a file for the current ("logged in") user without the need for admin rights (but this is pointless if you need the file registered for all user accounts).

Here's a unit I made a while back, it tries to register a file type globally, failing that it registers the file for the current user (you can modify for your own use if this isn't what you require).

unit uDeRegFileExtentions;

interface

uses
  Registry, SysUtils, Windows, ShlObj;

  procedure DeRegisterExt(const sExtName: AnsiString; bAll: Boolean = False);
  procedure RegisterExt(const sExtName, sAppPath: AnsiString; sExtDescription: AnsiString = ''; IconIndex: Byte = 0);
  
implementation

procedure DeRegisterExt(const sExtName: AnsiString; bAll: Boolean = False);
label
  lRepeat;
const
  sDummyKeyPath = '.%s\xxx\yyy\zzz';
var
  rRegistry : TRegistry;
  bHCRAccess: Boolean;
  sRegPath  : AnsiString;

  procedure DeleteRegKeys();
  begin
    with rRegistry do
      if DeleteKey(Format('%s.%s', [sRegPath, sExtName])) then
        DeleteKey(Format('%s%s_file', [sRegPath, sExtName]));
  end;
  
begin
  sRegPath := '';
  rRegistry := TRegistry.Create;
  with rRegistry do
    try
      RootKey := HKEY_CLASSES_ROOT;
      Access := KEY_CREATE_SUB_KEY or KEY_WRITE or KEY_SET_VALUE;
      bHCRAccess := rRegistry.OpenKey(Format(sDummyKeyPath, [sExtName]), True);
      Access := KEY_ALL_ACCESS;
    lRepeat:
      if bHCRAccess then
      begin
        CloseKey;
        DeleteKey(Format(sDummyKeyPath, [sExtName]));
        DeleteRegKeys;
        if bAll then
        begin
          bHCRAccess := not bHCRAccess;
          goto lRepeat;
        end;
      end
      else
      begin
        RootKey := HKEY_CURRENT_USER;
        sRegPath := 'Software\Classes\';
        DeleteRegKeys;
      end;
    finally
      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil) ;
      FreeAndNil(rRegistry);
    end;
end;

procedure RegisterExt(const sExtName, sAppPath: AnsiString; sExtDescription: AnsiString = ''; IconIndex: Byte = 0);
const
  sDummyKeyPath = '.%s\xxx\yyy\zzz';
var
  rRegistry : TRegistry;
  bHCRAccess: Boolean;
  sRegPath  : AnsiString;
begin
  sRegPath := '';
  rRegistry := TRegistry.Create;
  with rRegistry do
    try
      RootKey := HKEY_CLASSES_ROOT;
      Access := KEY_CREATE_SUB_KEY or KEY_WRITE or KEY_SET_VALUE;
      bHCRAccess := rRegistry.OpenKey(Format(sDummyKeyPath, [sExtName]), True);
      if bHCRAccess then
      begin
        CloseKey;
        DeleteKey(Format(sDummyKeyPath, [sExtName]));
      end
      else
      begin
        RootKey := HKEY_CURRENT_USER;
        sRegPath := 'Software\Classes\';
      end;
      Access := KEY_ALL_ACCESS;
      if OpenKey(Format('%s.%s', [sRegPath, sExtName]), True) then
      begin
        WriteString('', Format('%s_file', [sExtName]));
        CloseKey;
        if OpenKey(Format('%s%s_file', [sRegPath, sExtName]), True) then
        begin
          WriteString('', sExtDescription);
          CloseKey;
          if OpenKey(Format('%s%s_file\DefaultIcon', [sRegPath, sExtName]), True) then
          begin
            WriteString('', Format('%s,%d', [sAppPath, IconIndex]));
            CloseKey;
          end;
          if OpenKey(Format('%s%s_file\shell', [sRegPath, sExtName]), True) then
          begin
            WriteString('', 'open');
            if OpenKey('open\command', True) then
            begin
              WriteString('', sAppPath+' "%1"');
              CloseKey;
            end;
            CloseKey;
          end;
        end;
      end;
    finally
      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil) ;
      FreeAndNil(rRegistry);
    end;
end;

end.

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically Created Query 3 70
RESTRequest Parameter 4 54
Tidtcpserver listening on multiports? 1 45
MS Access from Delphi 31 81
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question