Solved

register filetype with application

Posted on 2011-03-03
2
261 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

687 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