Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CShellFileOp - Wrapper for SHFileOperation

0.00/5 (No votes)
26 Nov 1999 1  
An easy-to-use wrapper for the Win32 SHFileOperation function
  • Download demo project - 23 Kb
  • Download source files - 9 Kb

    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