Solved

shfileoperation  with complete exception

Posted on 2003-11-10
5
424 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now