Solved

register filetype with application

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now