views:

53

answers:

1

I use following code to delete a file. it wroks well.

SHFILEOPSTRUCT FileOp;
ZeroMemory(&FileOp, sizeof(SHFILEOPSTRUCT));
FileOp.hwnd = m_hAppHandle;
FileOp.wFunc = FO_DELETE; //delete
FileOp.pFrom = szPath; 
FileOp.pTo = NULL; 
FileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI;
FileOp.fAnyOperationsAborted = FALSE; 
FileOp.hNameMappings = NULL; 
FileOp.lpszProgressTitle = NULL;  

SHFileOperation(&FileOp);

But rcently I found a strang thing, the reproduction step as follow: 1. install win2000 2. install MS office2003

the at the first time (and only the first time) I call "SHFileOperation" function to delete a file. then a system warnning message box show

"Windows cannot create a shortcut here. Do you want the shortcut to be placed on the desktop instead? "

no matter I choose "OK" or "NO" button to close the warnning message box. then, such warnning message box never show again, i.e. , after I close the warnning message box, I can call "SHFileOperation" function to delete a file without such system warnning message.

It is truely magic, It just happens one time.

Any guy kown some related information? Thanks.

+3  A: 

The pFrom member of SHFILEOPSTRUCT is a list of null-terminated strings and should end with a double null. It looks like szPath is a single null-terminated string with only one path in it. This will meanSHFileOperation will read beyond your intended end of pFrom.

MSDN Library for SHFILEOPSTRUCT: http://msdn.microsoft.com/en-us/library/bb759795%28VS.85%29.aspx

Blog article about this: Don't forget to double-null-terminate those strings you pass to SHFileOperation

Otherside
Thanks for you remind. I have already use double null and the function works well except the first time after I installed MS Office.see"THCAR szPath[MAX_PATH];memset("
Chandler