Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

register filetype with application

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

Independent Software Vendors: 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

609 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