Using SHFileOperation within a Windows service
Asked Answered
P

4

2

It's possible, but is it appropriate to use SHFileOperation within a Windows service? All those SHxxx API functions in shell32.dll seem to have been written with user level programs in mind. Can I be certain SHFileOperation won't display GUI ever?

Polarimeter answered 14/10, 2008 at 17:13 Comment(0)
G
3

I would say, not it's not appropriate or advisable. Most of the shell32 APIs were written with a basic understanding that they would be used in interactive processes. I don't think there is any way you can guarantee that SHFileOperation will never display a UI component. In fact, if you look at IFileOperation (which is the new Vista interface that replaces SHFileOperation), it clearly states:

Exposes methods to copy, move, rename, create, and delete Shell items as well as methods to provide progress and error dialogs. This interface replaces the SHFileOperation function.

Gibby answered 14/10, 2008 at 17:24 Comment(1)
At this point, I agree. I was thinking of using it because it appears to copy files along with their security descriptor. The standard CopyFile does not, so it requires additional code to get the same behavior.Polarimeter
A
6

According to the SHFILEOPTSTRUCT documentation, you can use the following flags to prevent any UI from appearing:

FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR

or (if you're targeting Windows Vista), FOF_NO_UI, which is the same as the above.

Looking in the ShellAPI.h header file in the Windows SDK, the comment against FOF_NO_UI says "don't display any UI at all", so from this I assume it's OK to use SHFileOperation.

Aiguille answered 14/10, 2008 at 19:36 Comment(0)
G
3

I would say, not it's not appropriate or advisable. Most of the shell32 APIs were written with a basic understanding that they would be used in interactive processes. I don't think there is any way you can guarantee that SHFileOperation will never display a UI component. In fact, if you look at IFileOperation (which is the new Vista interface that replaces SHFileOperation), it clearly states:

Exposes methods to copy, move, rename, create, and delete Shell items as well as methods to provide progress and error dialogs. This interface replaces the SHFileOperation function.

Gibby answered 14/10, 2008 at 17:24 Comment(1)
At this point, I agree. I was thinking of using it because it appears to copy files along with their security descriptor. The standard CopyFile does not, so it requires additional code to get the same behavior.Polarimeter
F
1

I have to agree: not appropriate or advisable.

The prinicpal reason to use SHFileOperation is to perform operations with a UI, and/or which are reversable. I.e. using SHFileOperation to delete files is going to place the files in a recycle bin rather than deleting them allowing the current interactive user to undelete, or undo the operation performed. As the services run on a non interactive desktop, no one will ever be able to clear that recycle bin out.

Featheredge answered 15/10, 2008 at 8:35 Comment(0)
M
0

I had this issue as well and working at implementing a secure and reliable network file copy between servers and network shares ( most of these shares are CIFS / NetApp filer based ) and SHFileOperation fails from time to time.

now started using ROBOCOPY (available by default in all Microsoft OS from Vista/Server 2008 upwards) and really looks interesting and reliable.

this has opened my eyes: https://mcmap.net/q/575059/-how-can-i-copy-network-files-using-robocopy-closed

Messieurs answered 2/11, 2012 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.