Recent

Author Topic: IFileOperation and moving file to trash  (Read 5786 times)

devers000

  • Newbie
  • Posts: 5
IFileOperation and moving file to trash
« on: April 22, 2018, 10:02:08 am »
Hi there!

Have problem with moving file to trash.
At first I tried to work with SHFileOperation
Code: Pascal  [Select][+][-]
  1. function TForm1.remove_to_trash(aFile : PChar):integer;
  2. var
  3.   fileOpStruct : TSHFILEOPSTRUCT;
  4. begin
  5.   fileOpStruct.wnd:=0;
  6.   fileOpStruct.wFunc := FO_DELETE;
  7.   fileOpStruct.pFrom := aFile;
  8.   fileOpStruct.fFlags := FOF_ALLOWUNDO + FOF_NOCONFIRMATION;
  9.   Result := SHFileOperation(fileOpStruct);
  10. end;
  11.  
  12. //calling
  13. err_code := remove_to_trash(PChar(str_file+#0))
  14.  

And I got error 124 if str_file contains cyrillic symbols.
I've read about IFileOperation interface and found example for Delphi
but Lazarus doesn't support this interface.

Could you please help me to solve this task - move file to trash.
Thanks in advance.

P.S. I use last version lazarus-1.8.2-fpc-3.0.4-win32, Windows 7 x64 Russian
« Last Edit: April 22, 2018, 10:10:24 am by devers000 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: IFileOperation and moving file to trash
« Reply #1 on: April 22, 2018, 12:12:21 pm »
but Lazarus doesn't support this interface.
It does. Why do you think it doesn't? Maybe you need the widechar versions:
Code: Pascal  [Select][+][-]
  1. function TForm1.remove_to_trash(aFile : PWideChar):integer;
  2. var
  3.   fileOpStruct : TSHFILEOPSTRUCTW;
  4. begin
  5.   fileOpStruct.wnd:=0; // note: Application.MainForm.Handle
  6.   fileOpStruct.wFunc := FO_DELETE;
  7.   fileOpStruct.pFrom := aFile;
  8.   fileOpStruct.fFlags := FOF_ALLOWUNDO + FOF_NOCONFIRMATION;
  9.   Result := SHFileOperationW(fileOpStruct);
  10. end;
Note there is a slightly different version in the JEDI package.
« Last Edit: April 22, 2018, 12:35:00 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: IFileOperation and moving file to trash
« Reply #2 on: April 22, 2018, 03:02:22 pm »
Do not forget that pFrom is a list of paths separated by the symbol #0 and at the end additional #0 as the end of the list.

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: IFileOperation and moving file to trash
« Reply #3 on: April 22, 2018, 04:20:32 pm »
Indeed #0 as separator and #0#0 as terminator. But I don't think that was the issue here, although it can be.
Btw, I forgot to add that {$mode delphiunicode} will pick the W versions by default.
And these are since NT2000 (maybe even NT4) always the real versions that are called. The AnsiString A versions are mostly stubs and redirect to the W versions anyway.
« Last Edit: April 22, 2018, 04:27:27 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

devers000

  • Newbie
  • Posts: 5
Re: IFileOperation and moving file to trash
« Reply #4 on: April 22, 2018, 07:03:51 pm »
Thank you for response!

I've tried to use the widechar versions but got error
Quote
unit1.pas(79,42) Error: Incompatible type for arg no. 1: Got "_SHFILEOPSTRUCTW", expected "LPSHFILEOPSTRUCTW"

Lazarus doesn't support this interface because I got error
Quote
unit1.pas(61,11) Error: Identifier not found "IFileOperation"

I've added
Code: Pascal  [Select][+][-]
  1. uses ActiveX, ComObj, ShlObj;
and I can't find "IFileOperation" inside Lazarus modules.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: IFileOperation and moving file to trash
« Reply #5 on: April 22, 2018, 07:30:28 pm »
This work:
Code: Pascal  [Select][+][-]
  1. uses ShellApi;
  2.  
  3. function MoveToTrash(const FilePaths: UnicodeString): Boolean;
  4. var
  5.   R: TSHFILEOPSTRUCTW;
  6. begin
  7.   FillChar(R, SizeOf(R), 0);
  8.   R.wFunc := FO_DELETE;
  9.   R.pFrom := PWideChar(FilePaths + #0);
  10.   R.fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION;
  11.   Result := SHFileOperationW(@R) = 0;
  12. end;
  13.  
  14. { TForm1 }
  15.  
  16. procedure TForm1.Button1Click(Sender: TObject);
  17. begin
  18.   if MoveToTrash('c:\temp\1.txt') then
  19.     Caption := 'Work!';
  20. end;

devers000

  • Newbie
  • Posts: 5
Re: IFileOperation and moving file to trash
« Reply #6 on: April 22, 2018, 07:48:23 pm »
Thank you ASerge!

Now I see where I was wrong and made error. I should use @ and UnicodeString type


 

TinyPortal © 2005-2018