tags:

views:

256

answers:

1

I'm trying to add register an exe file with a file extension. The code below works fine with XP, but throws an error in Win Vista/7.

var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CLASSES_ROOT;
    reg.OpenKey('.' + ExtName, True);
    reg.WriteString('', ExtName + 'file');  //error: Failed to set data for ''
    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);

How can I accomplish this same thing in Vista/7?

+9  A: 

Have you tried running this as an admin? You cannot write to HKEY_CLASSES_ROOT as any old user in Vista. You can't in XP, either, unless you are running as a power user/admin. Which many developers are, but that's besides the point.

In other words, you'll need elevation to do this. Here is a good link on how to setup a manifest to mark your application with this characteristic.

Paul-Jan
That's it. I wasn't running the app with elevated status.
croceldon
An alternative is to write to the corresponding key in HKey_Current_User. Then you don't need special privileges, and you don't interfere with other users' file associations.
Rob Kennedy