Click here to Skip to main content
11,714,345 members (82,901 online)
Click here to Skip to main content

CShellFileOp - Wrapper for SHFileOperation

, 26 Nov 1999 170.8K 3.7K 61
Rate this:
Please Sign up or sign in to vote.
An easy-to-use wrapper for the Win32 SHFileOperation function
  • Download demo project - 23 Kb
  • Download source files - 9 Kb <!-- Article Starts -->

    The CShellFileOp class is designed to be an easy-to-use wrapper for the Win32 SHFileOperation() API. This function is powerful, but using it requires lots of bookkeeping.

    CShellFileOp hides the complex details of the API, and instead lets the programmer use familiar C++ constructs. It also does extensive error-checking of the parameters you use. This decreases headaches, reduces the amount of time the programmer loses wading through the docs, and in general makes everyone happy.

    CShellFileOp was written with MSVC 5.0 and tested with 6.0. The sample project is now in 6.0 format. I have also tested it in Unicode on NT 4.

    Example - Using CShellFileOp to copy files

    Below is example code showing the way CShellFileOp covers SHFileOperation(). I have included complete documentation for the class in each of the zip files accompanying this article. The docs are in the file CShellFileOp_docs.html.

    void CSomeDlg::CopySomeFiles()
    {
    CShellFileOp sfo;
    BOOL         bAPICalled;
    int          nAPIReturnVal;
    
        // This example copies a few files to the A: drive.
    
        // Pass the full paths to the files to be copied.
    
        sfo.AddSourceFile ( _T("c:\\windows\\command\\format.com") );
        sfo.AddSourceFile ( _T("c:\\windows\\command\\fdisk.exe") );
        sfo.AddSourceFile ( _T("c:\\*.com") );
    
        // Pass the destination directory
    
        sfo.AddDestFile ( _T("A:\\") );
    
        // Set up a few flags that control the operation.
    
        sfo.SetOperationFlags
        ( FO_COPY,         // the operation type (copy in this case)
          AfxGetMainWnd(), // pointer to parent window
          FALSE,           // flag - silent mode?
          FALSE,           // flag - allow undo?
          FALSE,           // flag - should wild cards affect files only?
          TRUE,            // flag - suppress confirmation messages?
          TRUE,            // flag - suppress confirmation messages 
                           // when making directories?
          FALSE,           // flag - rename files when name collisions occur?
          FALSE );         // flag - simple progress dialog?
              
        // Start the operation.
    
        if ( sfo.Go ( &bAPICalled, &nAPIReturnVal ) )
            {
            // The operation succeeded!
            }
        else
            {
            if ( !bAPICalled )
                {
                // SHFileOperation() wasn't called - check the info you passed
                // in to the CShellFileOp object.  The DEBUG version will
                // throw ASSERTs and/or show TRACE messages to help you out.
                }
            else
                {
                // SHFileOperation() returned nonzero (failure).  That return
                // value is now in nAPIReturnVal.
                }
            }
    }

    Revision History

    October 11, 1998: Version 1.0. First release.
    February 27, 2000: Version 1.1. Fixed a bug in CShellFileOp::Go() that allocated too much memory in Unicode builds.

    You can get the latest updates to this and my other articles at http://home.inreach.com/mdunn/code/

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    Share

    About the Author

    Michael Dunn
    Software Developer (Senior) VMware
    United States United States
    Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
    Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

    He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

    Mike was a VC MVP from 2005 to 2009.

    You may also be interested in...

    Comments and Discussions

     
    GeneralOnly copying done not replacing !!! Pin
    "_$h@nky_"29-Jul-08 0:02
    member"_$h@nky_"29-Jul-08 0:02 
    QuestionRe: Only copying done not replacing !!! Pin
    DavidCrow29-Apr-10 10:29
    mvpDavidCrow29-Apr-10 10:29 
    GeneralUsing SHFileOperation real return values Pin
    erangi3-Oct-06 2:13
    membererangi3-Oct-06 2:13 
    GeneralGood work Pin
    Franc Morales7-May-06 19:44
    memberFranc Morales7-May-06 19:44 
    QuestionCustom Error message? Pin
    Bernhard8-Jan-06 20:27
    memberBernhard8-Jan-06 20:27 
    AnswerRe: Custom Error message? Pin
    Michael Dunn8-Jan-06 20:55
    sitebuilderMichael Dunn8-Jan-06 20:55 
    GeneralRe: Custom Error message? Pin
    Bernhard8-Jan-06 21:25
    memberBernhard8-Jan-06 21:25 
    GeneralPosition of window Pin
    Fred_12322-Oct-04 2:45
    memberFred_12322-Oct-04 2:45 
    GeneralRe: Position of window Pin
    Michael Dunn27-Oct-04 13:52
    sitebuilderMichael Dunn27-Oct-04 13:52 
    GeneralRe: Position of window Pin
    KenGuru27-Sep-05 3:01
    memberKenGuru27-Sep-05 3:01 
    GeneralLittle improvement Pin
    peterchen11-Aug-04 14:42
    memberpeterchen11-Aug-04 14:42 
    GeneralNotifications Pin
    mailMonty15-Jun-04 23:36
    membermailMonty15-Jun-04 23:36 
    GeneralProgress Dialog Pin
    tyounsi11-May-04 16:18
    membertyounsi11-May-04 16:18 
    GeneralErrors compiling with Visual Studio 2003 Pin
    kimmov28-Feb-04 1:27
    memberkimmov28-Feb-04 1:27 
    GeneralRe: Errors compiling with Visual Studio 2003 Pin
    Michael Dunn28-Feb-04 6:11
    sitebuilderMichael Dunn28-Feb-04 6:11 
    GeneralFilenames missing Pin
    lufutusi17-Nov-03 21:18
    memberlufutusi17-Nov-03 21:18 
    GeneralThe source of my files to copy is a network drive Pin
    IHR18-Dec-02 12:14
    memberIHR18-Dec-02 12:14 
    GeneralRe: The source of my files to copy is a network drive Pin
    IHR13-Aug-03 4:32
    memberIHR13-Aug-03 4:32 
    GeneralRe: The source of my files to copy is a network drive Pin
    veraart24-Feb-06 0:57
    memberveraart24-Feb-06 0:57 
    GeneralConstant Created date Pin
    Naju9-Sep-02 0:25
    sussNaju9-Sep-02 0:25 
    GeneralRe: Constant Created date Pin
    Blake Miller26-Nov-02 5:12
    memberBlake Miller26-Nov-02 5:12 
    GeneralSHFileOperation Error Codes Pin
    Anonymous7-Jul-02 13:58
    memberAnonymous7-Jul-02 13:58 
    GeneralRe: SHFileOperation Error Codes Pin
    noirs213-Apr-05 5:00
    membernoirs213-Apr-05 5:00 
    GeneralRe: SHFileOperation Error Codes Pin
    Bartosz Bien5-Sep-05 1:13
    memberBartosz Bien5-Sep-05 1:13 
    AnswerRe: SHFileOperation Error Codes Pin
    IAteTheWholeThing220-Sep-06 22:27
    memberIAteTheWholeThing220-Sep-06 22:27 
    GeneralRe: SHFileOperation Error Codes Pin
    IAteTheWholeThing23-Oct-06 14:53
    memberIAteTheWholeThing23-Oct-06 14:53 
    GeneralRe: SHFileOperation Error Codes Pin
    Mick Leong12-Jul-09 22:09
    memberMick Leong12-Jul-09 22:09 
    QuestionHow can I disable run some application? Pin
    Anonymous19-Feb-02 0:48
    memberAnonymous19-Feb-02 0:48 
    Generalanandf Pin
    Anonymous24-Jul-01 23:53
    memberAnonymous24-Jul-01 23:53 
    Questionhow can i get a file's icon from this API Pin
    wubarcley14-Jan-00 18:42
    susswubarcley14-Jan-00 18:42 
    AnswerRe: how can i get a file's icon from this API Pin
    Mike Dunn16-Jan-00 13:21
    sussMike Dunn16-Jan-00 13:21 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.150819.1 | Last Updated 27 Nov 1999
    Article Copyright 1999 by Michael Dunn
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid