Solved

shfileoperation  with complete exception

Posted on 2003-11-10
5
437 Views
Last Modified: 2010-04-05
Hi i was wondering could you please give me a complete code in handling the exception of the shfileopearation api?
0
Comment
Question by:oracle_beginner
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:jconde
ID: 9720343
Hi!

Maybe this will help:

uses
  Windows, SysUtils, ShellAPI, FileCtrl;

procedure SHCopyFile(hWndOwner: HWND; const SourceFile, TargetFile: string);
var Info: TSHFileOpStruct;
    Aborted : Bool;
begin
  Aborted := False;
  with Info do
  begin
    Wnd := hWndOwner;
    wFunc := FO_COPY;

{ From Microsoft's Help:
  wFunc = Operation to perform. This member can be one of the following values:
  FO_COPY Copies the files specified by pFrom to the location specified by pTo.
  FO_DELETE Deletes the files specified by pFrom (pTo is ignored).
  FO_MOVE Moves the files specified by pFrom to the location specified by pTo.
  FO_RENAME Renames the files specified by pFrom. }

    pFrom := pChar(SourceFile);
    pTo := pChar(TargetFile);
    fFlags := 0;
    fAnyOperationsAborted := Aborted;
  end;
  try
    SHFileOperation(Info);
  finally
    if Aborted then; { enact upon any user cancellations }
  end;
end;
0
 
LVL 7

Expert Comment

by:jconde
ID: 9720359
BTW, TSHFileOpStruct is defined in ShellAPI as follows:

TSHFileOpStruct = record
   Wnd: HWND;
   wFunc: UINT;
   pFrom: PAnsiChar;
   pTo: PAnsiChar;
   fFlags: FILEOP_FLAGS;
   fAnyOperationsAborted: BOOL;
   hNameMappings: Pointer;
   lpszProgressTitle: PAnsiChar;
 end;

0
 
LVL 6

Expert Comment

by:DaFox
ID: 9725065
oracle_beginner,

what kind of error do you want to handle? If you are not interested in one specific error try it like this:

if ShFileOperation(AFileOpStruct) <> 0 then
begin
  ShowMessage(SysErrorMessage(GetLastError));
  exit;
end;

Win32 APIs don't raise exceptions, they return result codes!

Markus
0
 

Author Comment

by:oracle_beginner
ID: 9738168
I would like to trap especially when no diskette is in the drive, diskfull and so on..
0
 
LVL 6

Accepted Solution

by:
DaFox earned 250 total points
ID: 9738718
Then take a look at the System Error Codes page in the PSDK:

// ...
26 The specified disk or diskette cannot be accessed. ERROR_NOT_DOS_DISK
39 The disk is full. ERROR_HANDLE_DISK_FULL
112 There is not enough space on the disk. ERROR_DISK_FULL
// ...

Markus

PS: It seems like SHFileOperation doesn't fail (return value <> 0) when there's no disk in drive A:. You have to check it on your own.
0

Featured Post

Industry Leaders: 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

Suggested Solutions

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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