Solved

shfileoperation  with complete exception

Posted on 2003-11-10
5
431 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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