?
Solved

shfileoperation  with complete exception

Posted on 2003-11-10
5
Medium Priority
?
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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