简体   繁体   中英

Moving Folder Windows C++: Works Vista and up, not XP

I've been working on getting this right for a few hours now, and it works perfectly on Windows Vista & Windows 7, but when I run it on Windows XP it fails without any message. Unfortunately I don't have a development environment under XP so I can't just run it through a debugger to check, have I missed something blindingly obvious? The same piece of code does actually use a CopyFile and a few commands to write out data to C:\\ so if it's a permissions error it's an odd one.

EDIT: The return value is 1223, ERROR_CANCELLED which means cancelled by user.

EDIT 2: I disabled the s.fFlags and it immediately popped a dialog box up asking if it should create the dir-test folder, so I switched to FOF_NOCONFIRMATION and it appeared to ignore the flag. I do also use that during a deletion using the same SHFileOperation method so it either doesn't apply to file copies.

LPTSTR source = L"dir-test\\*\0";               
LPTSTR dest = L"C:\\dir-test\0";


SHFILEOPSTRUCT s = { 0 };
s.hwnd = 0;
s.wFunc = FO_COPY;
s.fFlags = FOF_SILENT;
s.pTo = dest;
s.pFrom = source;
int n;
n = SHFileOperation(&s);`

So it turns out for some odd reason that using the SHFileOperation will force a confirm dialog for Windows XP (but not Vista or 7) and ignore the flags to tell it to just confirm. Simple fix of using CreateDirectory() prior to running the copy, which doesn't require a confirmation dialog.

The documentation for SHFILEOPSTRUCT has this warning:

It cannot be overstated that your paths should always be full paths. If the pFrom or pTo members are unqualified names, the current directories are taken from the global current drive and directory settings as managed by the GetCurrentDirectory and SetCurrentDirectory functions.

Your source specification is an unqualified name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM