Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

[VB6] Use IFileOperation to replace SHFileOperation for modern Copy/Move box/prompts

$
0
0
cFileOperation 0.1

Display the latest version of the copy/move/delete progress dialog and the related prompts.

SHFileOperation has been superseded by IFileOperation on Windows Vista and above. At least the basic parts of it are easy to access in VB6- showing the standard Windows dialog/progress boxes to Move, Copy, or Delete files. While the class handles it, this function also requires us to bring in the IShellItem interface and its relatives into VB, so take a look at the class module code if you ever wanted to use other functions that required this.

From MSDN, advantages to IFileOperation:
Quote:

Use of IShellItem to identify items rather than string paths. SHFileOperation required path and destination strings to terminate in two null characters rather than the standard single null character, which itself was used to delimit multiple paths in the string. Identifying an item through IShellItem is more robust and less prone to programming errors. It also allows you to access non-file system items such as virtual folders. Multiple items in one operation can be passed as an IShellItemArray, IDataObject, or a collection accessed through IEnumShellItems rather than as a string.
More accurate error reporting through HRESULT values in conjunction with an API such as FormatMessage. Return codes from SHFileOperation could be misleading or inaccurate.
Extensibility. As a Component Object Model (COM) interface, IFileOperation can have its capabilities extended by a third-party to meet their specific needs, although this should be a very rare case. Windows provides a default implementation of IFileOperation that should meet the needs of most users.
Better progress feedback. Detailed operation progress, including notifications when specific operations begin and end on individual items as well as the overall progress, can be received during the operation. While SHFileOperation did provide progress UI, it was not as detailed.
More functionality. In addition to the copy, delete, move, and rename functionality provided by SHFileOperation, IFileOperation allows you to apply property values and create new items.
More control over the operation. In addition to the operation flags recognized by SHFileOperation, new flags are recognized in IFileOperation::SetOperationFlags that specify extended operation options.
Different operations can be performed in one call. For instance, you can move a set of files, copy others, rename a folder, and apply properties to yet another item all in one operation. SHFileOperation could only do one operation—copy, move, rename, or delete—at a time.
Right now this projects just supports the basic calls to Copy/Move/Delete; look for more options, like customizing actions using the .Advise sink method, in future releases.

Requirements
IFileOperation is only available on Windows Vista and higher; this project will not work on XP.
The included olelib.tlb is an upgrade of the standard one and needs to be added as a reference.

Usage Summary
Using the class is fairly straight forward;
-Add a reference to the upgraded olelib.tlb
-Add the class module to the project and go nuts.
-Sample project included to show how the class is called.

Part 1: The Type Library
The easiest way to go about this, due to the extensive dependency tree, was to start with Eduardo Morcillo's olelib. Everything is too tightly interrelated to have separate projects; the conflicts and hours spent re-doing things would simply be unmanageable. So what I've done is take this excellent work, and add in a number of modern interfaces. Old interfaces are the same; if you already use this file in your projects, you can replace it without making any changes to existing code. There's lots to be done with all the new interfaces I've added, and more projects will be forthcoming.

This project contains, at least for the time being while I await an answer on whether its allowed, the upgraded olelib.tlb and the full source to it. You can compile it yourself with the included mk.bat (if your MKTYPLIB.EXE isn't in the standard folder you'll have to edit it).
Among the interfaces added:
IShellItem
IShellItem2
IShellItemArray
IEnumShellItems
IFileOperation
IPropertyChange
IPropertyChangeArray
IObjectWithPropertyKey
IOperationsProgressDialog
IShellLibrary*
ITaskbarList3*
ITaskbarList4*
IActionProgress*
IShellItemImageFactory*
IThumbnailProvider*
* - Not related to the current project, but look for new projects showing their use soon.

Might be a few more I added a long time ago and forgot about, this update is years in the making.

Add olelib.tlb as a reference to your project.

Part 2 - The Class
Once you've added olelib, you're ready to start using cFileOperation. Since this calls the native methods, everything functions the same as in Explorer, including prompts about overwriting, confirmation deletion, etc. No extra code is needed to handle that.
Here are the currently supported calls:

.ParentWindow - Specify the parent window (e.g. Form1.hWnd) to keep the dialogs on top of it.
.SingleFile - For performing operations on a single file.
.SetFileList - For multiple files, specify an array containing a single full path to a file in each item.
.FileList - Retrieve the current file list.
.DestFolder - The destination folder; don't need to set for Delete.
.Flags - Set flags for the operation; uses the standard FileOperationFlags enum (see below).
.CopyFile - Copies the single file.
.CopyFiles - Copies the file list.
.MoveFile - Moves the single file.
.MoveFiles - Moves the file list.
.DeleteFile - Deletes the single file.
.DeleteFiles - Deletes the file list.

File Operation Flags

See MSDN Description of Flags

Can't put it much better than MSDN.


-------
All bug reports, comments, criticisms, and suggestions welcome.
PLEASE NOTE: I don't have access to multiple test systems; everything works on Win7 x64, and everything should work from Vista through 10, but please let met know if there's an issue.
Attached Files

Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>