?
Solved

register filetype with application

Posted on 2011-03-03
2
Medium Priority
?
264 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 400 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 1600 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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