Recent

Author Topic: TFileAssociation component  (Read 11536 times)

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: TFileAssociation component
« Reply #15 on: December 16, 2019, 12:31:26 am »
@lainz,

Thanks again for your prompt reply.

I had already suspected that a full path was needed, but in fact that is not the only problem I am running into. It happens that no parameter is being passed to my application, therefore the expression below:

Code: Pascal  [Select][+][-]
  1. if ParamCount > 0 then
  2.   begin
  3.     s := ParamStr(1);
  4.     if ExtractFileExt(s) = '.dtz' then LoadFile(s);
  5.   end;
  6.  

always evaluates as False, even if I hardcode the full pathname in the Action property:

Code: Pascal  [Select][+][-]
  1. FileAssociation1.Action := "C:\Users\mauro\My Documents\Projetos\FreeDELTA\fde.exe" "%1"

Best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: TFileAssociation component
« Reply #16 on: December 16, 2019, 12:54:39 am »
Maybe this

  // notice that using RegisterForAllUsers as True requires Administrator Privileges
  // if you want to run without privileges set it to false

Try running it as admin or set registerforallusers to false.

If that doesn't work upload a small test project I can compile and test to see what's happening.

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: TFileAssociation component
« Reply #17 on: December 16, 2019, 01:21:13 am »
@lainz,

Running as admin or setting RegisterForAllUsers to False do not work either.

I will create a test project soon.

Best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: TFileAssociation component
« Reply #18 on: December 16, 2019, 01:28:07 am »
 Ok. Yes is the way to go.
Remember  me what version of Windows you're using so when you upload your program I will try to test on same version

I recently tested the component and worked. But anything can fail as well

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: TFileAssociation component
« Reply #19 on: December 22, 2019, 03:19:48 pm »
If this component for some reason doesn't works for you, or you need to run it as administrator for all users without elevating your application, an Inno Setup Script will fit your needs best.

https://wiki.lazarus.freepascal.org/Inno_Setup_Usage#File_Association

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: TFileAssociation component
« Reply #20 on: December 23, 2019, 03:11:28 pm »
Hi, @lainz,

Sorry for the delay in replying. In the end, I had success with the procedure below:

Code: Pascal  [Select][+][-]
  1. {$IFDEF WINDOWS}
  2. uses Registry, ShlObj;
  3.  
  4. procedure TMainForm.RegisterFileType(ExtName: string; AppName: string);
  5. var
  6.   reg: TRegistry;
  7. begin
  8.   reg := TRegistry.Create;
  9.   try
  10.     reg.RootKey := HKEY_CLASSES_ROOT;
  11.     reg.OpenKey('.' + ExtName, True);
  12.     reg.WriteString('', ExtName + 'file');
  13.     reg.CloseKey;
  14.     reg.CreateKey(ExtName + 'file');
  15.     reg.OpenKey(ExtName + 'file\DefaultIcon', True);
  16.     reg.WriteString('', AppName + ',0');
  17.     reg.CloseKey;
  18.     reg.OpenKey(ExtName + 'file\shell\open\command', True);
  19.     reg.WriteString('', AppName + ' "%1"');
  20.     reg.CloseKey;
  21.   finally
  22.     reg.Free;
  23.   end;
  24.   SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  25. end;
  26. {$ENDIF}

Now, the name of the associated file is correctly passed to my application.

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: TFileAssociation component
« Reply #21 on: December 23, 2019, 04:35:17 pm »
« Last Edit: December 23, 2019, 04:37:57 pm by lainz »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: TFileAssociation component
« Reply #22 on: December 23, 2019, 08:57:29 pm »
@lainz,

Thanks again for your prompt reply.

I had already suspected that a full path was needed, but in fact that is not the only problem I am running into. It happens that no parameter is being passed to my application, therefore the expression below:

Code: Pascal  [Select][+][-]
  1. if ParamCount > 0 then
  2.   begin
  3.     s := ParamStr(1);
  4.     if ExtractFileExt(s) = '.dtz' then LoadFile(s);
  5.   end;
  6.  

always evaluates as False, even if I hardcode the full pathname in the Action property:

Code: Pascal  [Select][+][-]
  1. FileAssociation1.Action := "C:\Users\mauro\My Documents\Projetos\FreeDELTA\fde.exe" "%1"

Best regards,

I'm trying to figure what happened.

Maybe you did not setup a parameter. All parameters are mandatory. That should be on the wiki as well.

Specially ActionName, that must be 'Open' to work with double click. This is useful as well for default commands like 'Edit' and 'Print'. This must be in english for 'edit', 'open' and 'print', so it can access the right registry entry. But you can customize the translation with ActionText.
« Last Edit: December 23, 2019, 08:59:06 pm by lainz »

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: TFileAssociation component
« Reply #23 on: December 23, 2019, 10:01:18 pm »
@lainz,

I am using the above procedure as follows, it the OnCreate event of my application:

Code: Pascal  [Select][+][-]
  1. var
  2.    reg: TRegistry;
  3.        
  4. begin
  5.   {$IFDEF WINDOWS}
  6.   reg := TRegistry.Create;
  7.   try
  8.     reg.RootKey := HKEY_CLASSES_ROOT;
  9.     if not reg.KeyExists('ext' + 'file\shell\open\command') then
  10.       RegisterFileType('ext', ExtractFilePath(Application.ExeName) + 'my_app.exe');
  11.   finally
  12.     reg.Free;
  13.   end;
  14.  
  15.  if ParamCount > 0 then
  16.   begin
  17.     s := ParamStr(1);
  18.     if ExtractFileExt(s) = '.ext' then
  19.       LoadFile(s);
  20.   end;
  21.   {$ENDIF}

Where 'ext' is the extension associated with my application.

Cheers.
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

 

TinyPortal © 2005-2018