--- w32.h.original	2008-01-10 21:16:16.000000000 +0900
+++ w32.h	2008-04-22 05:52:24.056379500 +0900
@@ -129,6 +129,9 @@
 
 extern void init_ntproc (void);
 extern void term_ntproc (void);
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+extern void syms_of_w32 (void);
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
 extern void globals_of_w32 (void);
 extern void syms_of_w32term (void);
 extern void syms_of_w32fns (void);
--- w32.c.original	2008-02-23 22:49:09.000000000 +0900
+++ w32.c	2008-04-22 05:51:43.056197700 +0900
@@ -75,6 +75,10 @@
 #include <windows.h>
 #include <shlobj.h>
 
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+#include <shellapi.h>
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
+
 #ifdef HAVE_SOCKETS	/* TCP connection support, if kernel can do it */
 #include <sys/socket.h>
 #undef socket
@@ -104,6 +108,9 @@
 typedef HRESULT (WINAPI * ShGetFolderPath_fn)
   (IN HWND, IN int, IN HANDLE, IN DWORD, OUT char *);
 
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+void syms_of_w32 ();
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
 void globals_of_w32 ();
 
 extern Lisp_Object Vw32_downcase_file_names;
@@ -111,6 +118,10 @@
 extern Lisp_Object Vw32_get_true_file_attributes;
 extern int w32_num_mouse_buttons;
 
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+int w32_sys_unlink_use_shellapi;
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
+
 
 /*
   Initialization states
@@ -2240,6 +2251,71 @@
   return result;
 }
 
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+
+int
+sys_rmdir (const char * path)
+{
+  if (w32_sys_unlink_use_shellapi)
+    return (sys_unlink ((const char *)path));
+  else
+    return _rmdir (map_w32_filename (path, NULL));
+}
+
+int
+sys_unlink (const char * path)
+{
+  if (w32_sys_unlink_use_shellapi)
+    {
+      /* Each file name must be terminated by a single NULL
+	 character. An additional NULL character must be appended to the
+	 end of the final name to indicate the end of pFrom. */
+      TCHAR tmp_path[MAX_PATH + 1 + 1];
+      SHFILEOPSTRUCT stFileOp;
+
+      path = map_w32_filename (path, NULL);
+
+      /* On Unix, unlink works without write permission. */
+      _chmod (path, 0666);
+
+#if 0
+      *(tmp_path + lstrlen(tmp_path) + 1) = '\0';
+#else
+      ZeroMemory(tmp_path, sizeof(tmp_path));
+#endif
+      lstrcpy(tmp_path, path);
+
+      ZeroMemory(&stFileOp, sizeof(SHFILEOPSTRUCT));
+      stFileOp.hwnd = HWND_DESKTOP;
+      stFileOp.wFunc = FO_DELETE;
+      stFileOp.pFrom = tmp_path;
+      stFileOp.pTo = NULL;
+      stFileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT;
+      stFileOp.fAnyOperationsAborted = FALSE;
+      stFileOp.hNameMappings = NULL;
+      stFileOp.lpszProgressTitle = NULL;
+
+#if 0
+      SHFileOperation(&stFileOp);
+
+      return (stFileOp.fAnyOperationsAborted);
+#else
+      /* value returned by SHFileOperation() must match value returned by _unlink() */
+      return (SHFileOperation(&stFileOp) == 0 ? 0 : -1);
+#endif
+    }
+  else
+    {
+      path = map_w32_filename (path, NULL);
+
+      /* On Unix, unlink works without write permission. */
+      _chmod (path, 0666);
+      return _unlink (path);
+    }
+}
+
+#else /* W32_SYS_UNLINK_USE_SHELLAPI */
+
 int
 sys_rmdir (const char * path)
 {
@@ -2256,6 +2332,8 @@
   return _unlink (path);
 }
 
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
+
 static FILETIME utc_base_ft;
 static long double utc_base;
 static int init = 0;
@@ -4160,6 +4238,18 @@
   SetConsoleCtrlHandler(shutdown_handler, TRUE);
 }
 
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+
+void
+syms_of_w32 ()
+{
+  DEFVAR_BOOL ("w32-sys-unlink-use-shellapi", &w32_sys_unlink_use_shellapi,
+	       "Non-nil means using shellapi for sys_unlink(), sys_rmdir().");
+  w32_sys_unlink_use_shellapi = 1;
+}
+
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
+
 /* end of w32.c */
 
 /* arch-tag: 90442dd3-37be-482b-b272-ac752e3049f1
--- emacs.c.original	2008-01-10 21:16:14.000000000 +0900
+++ emacs.c	2008-04-22 05:49:48.407912100 +0900
@@ -1588,6 +1588,9 @@
       syms_of_vmsproc ();
 #endif /* VMS */
 #ifdef WINDOWSNT
+#ifdef W32_SYS_UNLINK_USE_SHELLAPI
+      syms_of_w32 ();
+#endif /* W32_SYS_UNLINK_USE_SHELLAPI */
       syms_of_ntproc ();
 #endif /* WINDOWSNT */
       syms_of_window ();
