Fix double-clicking control panel items
[reactos.git] / reactos / lib / shell32 / shlexec.c
index ab2a055..65c0d50 100644 (file)
 #include <ctype.h>
 #include <assert.h>
 
+#define COBJMACROS
+
 #include "windef.h"
 #include "winbase.h"
 #include "winerror.h"
 #include "winreg.h"
-#include "wownt32.h"
-#include "shellapi.h"
-#include "wingdi.h"
 #include "winuser.h"
-#include "shlobj.h"
 #include "shlwapi.h"
 #include "ddeml.h"
 
 #include "wine/winbase16.h"
 #include "shell32_main.h"
-#include "undocshell.h"
 #include "pidl.h"
 
 #include "wine/debug.h"
@@ -60,6 +57,8 @@ static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
 static const WCHAR wszEmpty[] = {0};
 
+#define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
+
 
 /***********************************************************************
  *     SHELL_ArgifyW [Internal]
@@ -77,14 +76,19 @@ static const WCHAR wszEmpty[] = {0};
  * %* all following parameters (see batfile)
  *
  * FIXME: use 'len'
+ * FIXME: Careful of going over string boundaries. No checking is done to 'res'...
  */
 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
 {
     WCHAR   xlpFile[1024];
     BOOL    done = FALSE;
-    LPVOID  pv;
+    BOOL    found_p1 = FALSE;
     PWSTR   res = out;
     PCWSTR  cmd;
+    LPVOID  pv;
+
+    TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
+          debugstr_w(lpFile), pidl, args);
 
     while (*fmt)
     {
@@ -92,93 +96,128 @@ static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lp
         {
             switch (*++fmt)
             {
-              case '\0':
-              case '%':
+            case '\0':
+            case '%':
                 *res++ = '%';
                 break;
 
-              case '2':
-              case '3':
-             case '4':
-             case '5':
-              case '6':
-              case '7':
-             case '8':
-             case '9':
-             case '0':
-              case '*':
-               if (args)
-               {
-                   if (*fmt == '*')
-                   {
-                       *res++ = '"';
-                       while(*args)
-                           *res++ = *args++;
-                       *res++ = '"';
-                   }
-                   else
-                   {
-                       while(*args && !isspace(*args))
-                           *res++ = *args++;
+            case '2':
+            case '3':
+            case '4':
+            case '5':
+            case '6':
+            case '7':
+            case '8':
+            case '9':
+            case '0':
+            case '*':
+                if (args)
+                {
+                    if (*fmt == '*')
+                    {
+                        *res++ = '"';
+                        while(*args)
+                            *res++ = *args++;
+                        *res++ = '"';
+                    }
+                    else
+                    {
+                        while(*args && !isspace(*args))
+                            *res++ = *args++;
 
-                       while(isspace(*args))
-                           ++args;
-                   }
-               }
-               else
-               {
-              case '1':
-                   if (!done || (*fmt == '1'))
-                   {
-                       /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
-                       if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
-                           cmd = xlpFile;
-                       else
-                           cmd = lpFile;
-
-                       /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
-                       if (res != out && *(res - 1) == '"')
-                       {
-                           strcpyW(res, cmd);
-                           res += strlenW(cmd);
-                       }
-                       else
-                       {
-                           strcpyW(res, cmd);
-                           res += strlenW(cmd);
-                       }
-                   }
-               }
+                        while(isspace(*args))
+                            ++args;
+                    }
+                    break;
+                }
+                /* else fall through */
+            case '1':
+                if (!done || (*fmt == '1'))
+                {
+                    /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
+                    if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
+                        cmd = xlpFile;
+                    else
+                        cmd = lpFile;
+
+                    /* Add double quotation marks unless we already have them
+                       (e.g.: "file://%1" %* for exefile) or unless the arg is already
+                       enclosed in double quotation marks */
+                    if ((res == out || *(fmt + 1) != '"') && *cmd != '"')
+                    {
+                        *res++ = '"';
+                        strcpyW(res, cmd);
+                        res += strlenW(cmd);
+                        *res++ = '"';
+                    }
+                    else
+                    {
+                        strcpyW(res, cmd);
+                        res += strlenW(cmd);
+                    }
+                }
+                found_p1 = TRUE;
                 break;
 
-              /*
-               * IE uses this alot for activating things such as windows media
-               * player. This is not verified to be fully correct but it appears
-               * to work just fine.
-               */
-              case 'l':
-              case 'L':
+            /*
+             * IE uses this a lot for activating things such as windows media
+             * player. This is not verified to be fully correct but it appears
+             * to work just fine.
+             */
+            case 'l':
+            case 'L':
                if (lpFile) {
                    strcpyW(res, lpFile);
                    res += strlenW(lpFile);
                }
+                found_p1 = TRUE;
                 break;
 
-              case 'i':
-              case 'I':
+            case 'i':
+            case 'I':
                if (pidl) {
                    HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
                    pv = SHLockShared(hmem, 0);
                    res += sprintfW(res, wszILPtr, pv);
                    SHUnlockShared(pv);
                }
+                found_p1 = TRUE;
                 break;
 
-            default: FIXME("Unknown escape sequence %%%c\n", *fmt);
-            }
+           default:
+                /*
+                 * Check if this is an env-variable here...
+                 */
 
-            fmt++;
-            done = TRUE;
+                /* Make sure that we have at least one more %.*/
+                if (strchrW(fmt, '%'))
+                {
+                    WCHAR   tmpBuffer[1024];
+                    PWSTR   tmpB = tmpBuffer;
+                    WCHAR   tmpEnvBuff[MAX_PATH];
+                    DWORD   envRet;
+
+                    while (*fmt != '%')
+                        *tmpB++ = *fmt++;
+                    *tmpB++ = 0;
+
+                    TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
+
+                    envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
+                    if (envRet == 0 || envRet > MAX_PATH)
+                        strcpyW( res, tmpBuffer );
+                    else
+                        strcpyW( res, tmpEnvBuff );
+                    res += strlenW(res);
+                }
+                done = TRUE;
+                break;
+            }
+            /* Don't skip past terminator (catch a single '%' at the end) */
+            if (*fmt != '\0')
+            {
+                fmt++;
+            }
         }
         else
             *res++ = *fmt++;
@@ -186,7 +225,7 @@ static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lp
 
     *res = '\0';
 
-    return done;
+    return found_p1;
 }
 
 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
@@ -229,7 +268,7 @@ HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, U
 
 /*************************************************************************
  *     SHELL_ResolveShortCutW [Internal]
- *     read shortcut file at 'wcmd' and fill psei with its content
+ *     read shortcut file at 'wcmd'
  */
 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
 {
@@ -263,7 +302,7 @@ static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HW
 
                            if (SUCCEEDED(hr) && *ppidl) {
                                /* We got a PIDL instead of a file system path - try to translate it. */
-                               if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl, wcmd, MAX_PATH))) {
+                               if (SHGetPathFromIDListW(*ppidl, wcmd)) {
                                    SHFree(*ppidl);
                                    *ppidl = NULL;
                                }
@@ -295,46 +334,54 @@ static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HW
  *     SHELL_ExecuteW [Internal]
  *
  */
-static UINT SHELL_ExecuteW(const WCHAR *lpCmd, void *env, BOOL shWait,
+static UINT SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
                            LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
 {
     STARTUPINFOW  startup;
     PROCESS_INFORMATION info;
     UINT retval = 31;
+    UINT gcdret = 0;
+    WCHAR curdir[MAX_PATH];
 
     TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
-
-    ZeroMemory(&startup, sizeof(startup));
+    /* ShellExecute specifies the command from psei->lpDirectory
+     * if present. Not from the current dir as CreateProcess does */
+    if( psei->lpDirectory && psei->lpDirectory[0] )
+        if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
+            if( !SetCurrentDirectoryW( psei->lpDirectory))
+                ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
+    ZeroMemory(&startup,sizeof(STARTUPINFOW));
     startup.cb = sizeof(STARTUPINFOW);
     startup.dwFlags = STARTF_USESHOWWINDOW;
     startup.wShowWindow = psei->nShow;
-
-    if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, 0,
+    if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT,
                        env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
     {
         /* Give 30 seconds to the app to come up, if desired. Probably only needed
            when starting app immediately before making a DDE connection. */
         if (shWait)
-            if (WaitForInputIdle( info.hProcess, 30000 ) == -1)
+            if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
                 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
         retval = 33;
-
         if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
             psei_out->hProcess = info.hProcess;
         else
-            CloseHandle(info.hProcess);
-
-        CloseHandle(info.hThread);
+            CloseHandle( info.hProcess );
+        CloseHandle( info.hThread );
     }
     else if ((retval = GetLastError()) >= 32)
     {
-        FIXME("Strange error set by CreateProcess: %d\n", retval);
+        TRACE("CreateProcess returned error %d\n", retval);
         retval = ERROR_BAD_FORMAT;
     }
 
     TRACE("returning %u\n", retval);
 
     psei_out->hInstApp = (HINSTANCE)retval;
+    if( gcdret )
+        if( !SetCurrentDirectoryW( curdir))
+            ERR("cannot return to directory %s\n", debugstr_w(curdir));
+
     return retval;
 }
 
@@ -405,12 +452,11 @@ static void *SHELL_BuildEnvW( const WCHAR *path )
  * On entry: szName is a filename (probably without path separators).
  * On exit: if szName found in "App Path", place full path in lpResult, and return true
  */
-static BOOL SHELL_TryAppPathW(LPCWSTR szName, LPWSTR lpResult, void** env)
+static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
 {
     static const WCHAR wszKeyAppPaths[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',
        '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
     static const WCHAR wPath[] = {'P','a','t','h',0};
-
     HKEY hkApp = 0;
     WCHAR buffer[1024];
     LONG len;
@@ -503,34 +549,32 @@ static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCW
  *              on the operation)
  */
 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
-                           LPWSTR lpResult, int resultLen, LPWSTR key, void **env, LPITEMIDLIST pidl, LPCWSTR args)
+                                 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
 {
     static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
     static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
     static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
-
     WCHAR *extension = NULL; /* pointer to file extension */
-    WCHAR wtmpext[5];        /* local copy to mung as we please */
     WCHAR filetype[256];     /* registry name for this filetype */
     LONG  filetypelen = sizeof(filetype); /* length of above */
-    WCHAR command[256];      /* command from registry */
+    WCHAR command[1024];     /* command from registry */
     WCHAR wBuffer[256];      /* Used to GetProfileString */
     UINT  retval = 31;       /* default - 'No association was found' */
     WCHAR *tok;              /* token pointer */
-    WCHAR xlpFile[256];             /* result of SearchPath */
+    WCHAR xlpFile[256];      /* result of SearchPath */
     DWORD attribs;           /* file attributes */
 
     TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
 
+    xlpFile[0] = '\0';
     lpResult[0] = '\0'; /* Start off with an empty return string */
     if (key) *key = '\0';
 
-    xlpFile[0] = '\0';
-
     /* trap NULL parameters on entry */
-    if ((lpFile == NULL) || (lpResult == NULL))
+    if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
     {
-        WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", debugstr_w(lpFile), debugstr_w(lpResult));
+        WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
+             debugstr_w(lpFile), debugstr_w(lpOperation), debugstr_w(lpResult));
         return 2; /* File not found. Close enough, I guess. */
     }
 
@@ -550,77 +594,73 @@ UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
     attribs = GetFileAttributesW(lpFile);
     if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
     {
-       strcpyW(filetype, wszFolder);
-       filetypelen = 6;    /* strlen("Folder") */
+       strcpyW(filetype, wszFolder);
+       filetypelen = 6;    /* strlen("Folder") */
     }
     else
     {
-       /* First thing we need is the file's extension */
-       extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
-                                          /* File->Run in progman uses */
-                                          /* .\FILE.EXE :( */
-       TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
+        /* First thing we need is the file's extension */
+        extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
+        /* File->Run in progman uses */
+        /* .\FILE.EXE :( */
+        TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
 
-       if ((extension == NULL) || (extension == &xlpFile[strlenW(xlpFile)]))
-       {
-           WARN("Returning 31 - No association\n");
-           return 31; /* no association */
-       }
-       /* Make local copy & lowercase it for reg & 'programs=' lookup */
-       lstrcpynW(wtmpext, extension, 5);
-       CharLowerW(wtmpext);
-       TRACE("%s file\n", debugstr_w(wtmpext));
-
-       /* Three places to check: */
-       /* 1. win.ini, [windows], programs (NB no leading '.') */
-       /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
-       /* 3. win.ini, [extensions], extension (NB no leading '.' */
-       /* All I know of the order is that registry is checked before */
-       /* extensions; however, it'd make sense to check the programs */
-       /* section first, so that's what happens here. */
-
-       /* See if it's a program - if GetProfileString fails, we skip this
-        * section. Actually, if GetProfileString fails, we've probably
-        * got a lot more to worry about than running a program... */
-       if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
-       {
-           CharLowerW(wBuffer);
-           tok = wBuffer;
-           while (*tok)
-           {
-               WCHAR *p = tok;
-               while (*p && *p != ' ' && *p != '\t') p++;
-               if (*p)
-               {
-                   *p++ = 0;
-                   while (*p == ' ' || *p == '\t') p++;
-               }
+        if (extension == NULL || extension[1]==0)
+        {
+            WARN("Returning 31 - No association\n");
+            return 31; /* no association */
+        }
 
-               if (strcmpW(tok, &wtmpext[1]) == 0) /* have to skip the leading "." */
-               {
-                   strcpyW(lpResult, xlpFile);
-                   /* Need to perhaps check that the file has a path
-                    * attached */
-                   TRACE("found %s\n", debugstr_w(lpResult));
-                   return 33;
-
-                   /* Greater than 32 to indicate success FIXME According to the
-                    * docs, I should be returning a handle for the
-                    * executable. Does this mean I'm supposed to open the
-                    * executable file or something? More RTFM, I guess... */
-               }
-               tok = p;
-           }
-       }
+        /* Three places to check: */
+        /* 1. win.ini, [windows], programs (NB no leading '.') */
+        /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
+        /* 3. win.ini, [extensions], extension (NB no leading '.' */
+        /* All I know of the order is that registry is checked before */
+        /* extensions; however, it'd make sense to check the programs */
+        /* section first, so that's what happens here. */
+
+        /* See if it's a program - if GetProfileString fails, we skip this
+         * section. Actually, if GetProfileString fails, we've probably
+         * got a lot more to worry about than running a program... */
+        if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
+        {
+            CharLowerW(wBuffer);
+            tok = wBuffer;
+            while (*tok)
+            {
+                WCHAR *p = tok;
+                while (*p && *p != ' ' && *p != '\t') p++;
+                if (*p)
+                {
+                    *p++ = 0;
+                    while (*p == ' ' || *p == '\t') p++;
+                }
 
-       /* Check registry */
-       if (RegQueryValueW(HKEY_CLASSES_ROOT, wtmpext, filetype, 
-                           &filetypelen) == ERROR_SUCCESS)
-       {
-           filetypelen /= sizeof(WCHAR);
-           filetype[filetypelen] = '\0';
-           TRACE("File type: %s\n", debugstr_w(filetype));
-       }
+                if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
+                {
+                    strcpyW(lpResult, xlpFile);
+                    /* Need to perhaps check that the file has a path
+                     * attached */
+                    TRACE("found %s\n", debugstr_w(lpResult));
+                    return 33;
+
+                    /* Greater than 32 to indicate success FIXME According to the
+                     * docs, I should be returning a handle for the
+                     * executable. Does this mean I'm supposed to open the
+                     * executable file or something? More RTFM, I guess... */
+                }
+                tok = p;
+            }
+        }
+
+        /* Check registry */
+        if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
+                           &filetypelen) == ERROR_SUCCESS)
+        {
+            filetypelen /= sizeof(WCHAR);
+            filetype[filetypelen] = '\0';
+            TRACE("File type: %s\n", debugstr_w(filetype));
+        }
     }
 
     if (*filetype)
@@ -653,29 +693,29 @@ UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
 
                    if (retval > 32)
                        break;
-               }
+           }
                RegCloseKey(hkey);
            }
        }
 
        if (retval > 32)
-        {
-            SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
+       {
+           SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
 
-            /* Remove double quotation marks and command line arguments */
-            if (*lpResult == '"')
-            {
-                WCHAR *p = lpResult;
-                while (*(p + 1) != '"')
-                {
-                    *p = *(p + 1);
-                    p++;
-                }
-                *p = '\0';
-            }
-        }
+           /* Remove double quotation marks and command line arguments */
+           if (*lpResult == '"')
+           {
+               WCHAR *p = lpResult;
+               while (*(p + 1) != '"')
+               {
+                   *p = *(p + 1);
+                   p++;
+               }
+               *p = '\0';
+           }
+       }
     }
-    else if (extension) /* Check win.ini */
+    else /* Check win.ini */
     {
        static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
 
@@ -687,7 +727,6 @@ UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
             {
                 strcpyW(lpResult, command);
                 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
-
                 if (tok != NULL)
                 {
                     tok[0] = '\0';
@@ -698,7 +737,6 @@ UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
                         strcatW(lpResult, &tok[5]);
                     }
                 }
-
                 retval = 33; /* FIXME - see above */
             }
         }
@@ -711,12 +749,14 @@ UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
 /******************************************************************
  *             dde_cb
  *
- * callback for the DDE connection. not really usefull
+ * callback for the DDE connection. not really useful
  */
 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
-                                HSZ hsz1, HSZ hsz2,
-                                HDDEDATA hData, DWORD dwData1, DWORD dwData2)
+                                HSZ hsz1, HSZ hsz2, HDDEDATA hData,
+                                ULONG_PTR dwData1, ULONG_PTR dwData2)
 {
+    TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
+           uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
     return NULL;
 }
 
@@ -729,23 +769,22 @@ static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
  * launching an application and trying (#2) to connect to it
  *
  */
-static unsigned dde_connect(WCHAR * key, WCHAR* start, WCHAR* ddeexec,
-                            const WCHAR* lpFile, void *env,
+static unsigned dde_connect(WCHAR* key, WCHAR* start, WCHAR* ddeexec,
+                            const WCHAR* lpFile, WCHAR *env,
                            LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
                            LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
 {
     static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
     static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
-
-    WCHAR*      endkey = key + strlenW(key);
-    WCHAR      app[256], topic[256], ifexec[256];
-    WCHAR      res[1024];
+    WCHAR *     endkey = key + strlenW(key);
+    WCHAR       app[256], topic[256], ifexec[256], res[256];
     LONG        applen, topiclen, ifexeclen;
     WCHAR *     exec;
     DWORD       ddeInst = 0;
     DWORD       tid;
     HSZ         hszApp, hszTopic;
     HCONV       hConv;
+    HDDEDATA    hDdeData;
     unsigned    ret = 31;
 
     strcpyW(endkey, wApplication);
@@ -769,8 +808,8 @@ static unsigned dde_connect(WCHAR * key, WCHAR* start, WCHAR* ddeexec,
         return 2;
     }
 
-    hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINANSI);
-    hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINANSI);
+    hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
+    hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
 
     hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
     exec = ddeexec;
@@ -790,7 +829,7 @@ static unsigned dde_connect(WCHAR * key, WCHAR* start, WCHAR* ddeexec,
             TRACE("Couldn't connect. ret=%d\n", ret);
             DdeUninitialize(ddeInst);
             SetLastError(ERROR_DDE_FAIL);
-            return ret = 30; /* whatever */
+            return 30; /* whatever */
         }
         strcpyW(endkey, wIfexec);
         ifexeclen = sizeof(ifexec);
@@ -800,21 +839,32 @@ static unsigned dde_connect(WCHAR * key, WCHAR* start, WCHAR* ddeexec,
         }
     }
 
-    SHELL_ArgifyW(res, sizeof(res), exec, lpFile, pidl, szCommandline);
+    SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
     TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
 
-    ret = (DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
-                                XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
+    /* It's documented in the KB 330337 that IE has a bug and returns
+     * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
+     */
+    hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
+                                     XTYP_EXECUTE, 10000, &tid);
+    if (hDdeData)
+        DdeFreeDataHandle(hDdeData);
+    else
+        WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
+    ret = 33;
+
     DdeDisconnect(hConv);
+
  error:
     DdeUninitialize(ddeInst);
+
     return ret;
 }
 
 /*************************************************************************
  *     execute_from_key [Internal]
  */
-static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, void *env, LPCWSTR szCommandline,
+static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
                             SHELL_ExecuteW32 execfunc,
                             LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
 {
@@ -829,12 +879,11 @@ static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, void *env, LPCWSTR szCo
     {
        static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
        static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
-
         LPWSTR tmp;
         WCHAR param[256];
         LONG paramlen = sizeof(param);
 
-       param[0] = '\0';
+        param[0] = '\0';
 
         /* Get the parameters needed by the application
            from the associated ddeexec key */
@@ -919,7 +968,7 @@ HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpR
 /*************************************************************************
  *     ShellExecuteExW32 [Internal]
  */
-BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfunc)
+BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
 {
     static const WCHAR wQuote[] = {'"',0};
     static const WCHAR wSpace[] = {' ',0};
@@ -928,30 +977,50 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
     static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
     static const WCHAR wExtLnk[] = {'.','l','n','k',0};
     static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
-
-    WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wfileName[MAX_PATH], wszDir[MAX_PATH];
-    SHELLEXECUTEINFOW sei_tmp; /* modifyable copy of SHELLEXECUTEINFO struct */
-    void *env;
-    WCHAR wszProtocol[256];
+    static const DWORD unsupportedFlags =
+        SEE_MASK_INVOKEIDLIST  | SEE_MASK_ICON         | SEE_MASK_HOTKEY |
+        SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI |
+        SEE_MASK_UNICODE       | SEE_MASK_NO_CONSOLE   | SEE_MASK_ASYNCOK |
+        SEE_MASK_HMONITOR;
+
+    WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
+    SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
+    WCHAR wfileName[MAX_PATH];
+    WCHAR *env;
+    WCHAR lpstrProtocol[256];
     LPCWSTR lpFile;
     UINT retval = 31;
-    WCHAR buffer[1024];
+    WCHAR wcmd[1024];
+    WCHAR buffer[MAX_PATH];
     const WCHAR* ext;
+    BOOL done;
 
-    memcpy(&sei_tmp, psei, sizeof(sei_tmp));
+    /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
+    memcpy(&sei_tmp, sei, sizeof(sei_tmp));
 
     TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
             sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
             debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
             debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
-            (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei_tmp.lpClass) : "not used");
+            ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
+                debugstr_w(sei_tmp.lpClass) : "not used");
 
-    psei->hProcess = NULL;
+    sei->hProcess = NULL;
 
-    if (sei_tmp.lpFile)
-       strcpyW(wszApplicationName, sei_tmp.lpFile);
+    /* make copies of all path/command strings */
+    if (!sei_tmp.lpFile)
+        *wszApplicationName = '\0';
+    else if (*sei_tmp.lpFile == '\"')
+    {
+        UINT l;
+        strcpyW(wszApplicationName, sei_tmp.lpFile+1);
+        l=lstrlenW(wszApplicationName);
+        if (wszApplicationName[l-1] == '\"')
+            wszApplicationName[l-1] = '\0';
+        TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
+    }
     else
-       *wszApplicationName = '\0';
+        strcpyW(wszApplicationName, sei_tmp.lpFile);
 
     if (sei_tmp.lpParameters)
        strcpyW(wszParameters, sei_tmp.lpParameters);
@@ -963,20 +1032,18 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
     else
        *wszDir = '\0';
 
+    /* adjust string pointers to point to the new buffers */
     sei_tmp.lpFile = wszApplicationName;
     sei_tmp.lpParameters = wszParameters;
     sei_tmp.lpDirectory = wszDir;
 
-    if (sei_tmp.fMask & (SEE_MASK_ICON | SEE_MASK_HOTKEY |
-        SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
-        SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
-        SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
+    if (sei_tmp.fMask & unsupportedFlags)
     {
-        FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask);
+        FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask & unsupportedFlags);
     }
 
     /* process the IDList */
-    if (sei_tmp.fMask & SEE_MASK_INVOKEIDLIST) /* 0x0c: includes SEE_MASK_IDLIST */
+    if (sei_tmp.fMask & SEE_MASK_IDLIST)
     {
        IShellExecuteHookW* pSEH;
 
@@ -984,7 +1051,7 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
 
        if (SUCCEEDED(hr))
        {
-           hr = IShellExecuteHookW_Execute(pSEH, psei);
+           hr = IShellExecuteHookW_Execute(pSEH, sei);
 
            IShellExecuteHookW_Release(pSEH);
 
@@ -992,58 +1059,86 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
                return TRUE;
        }
 
-       /* try to translate PIDL directly into the corresponding file system path */
-        if (SUCCEEDED(SHELL_GetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName/*sei_tmp.lpFile*/, sizeof(wszApplicationName)/sizeof(WCHAR))))
-       {
-           sei_tmp.lpIDList = NULL;
-           sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
-       }
-
+        wszApplicationName[0] = '"';
+        SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName+1);
+        strcatW(wszApplicationName, wQuote);
         TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
     }
 
-    if (sei_tmp.fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
+    if (sei_tmp.fMask & SEE_MASK_CLASSALL)
     {
-        /* launch a document by fileclass like 'WordPad.Document.1' */
+       /* launch a document by fileclass like 'WordPad.Document.1' */
         /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
-        /* FIXME: wszParameters should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
-        HCR_GetExecuteCommandW((sei_tmp.fMask & SEE_MASK_CLASSKEY)? sei_tmp.hkeyClass: NULL,
-                               (sei_tmp.fMask & SEE_MASK_CLASSNAME)? sei_tmp.lpClass: NULL,
-                               (sei_tmp.lpVerb)? sei_tmp.lpVerb: wszOpen,
-                               wszParameters/*sei_tmp.lpParameters*/, sizeof(wszParameters)/sizeof(WCHAR));
+        /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
+        ULONG cmask=(sei_tmp.fMask & SEE_MASK_CLASSALL);
+        HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
+                               (cmask == SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
+                               (sei_tmp.lpVerb) ? sei_tmp.lpVerb : wszOpen,
+                               wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
 
         /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
-        TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(sei_tmp.lpParameters), debugstr_w(sei_tmp.lpFile));
+        TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
 
-        buffer[0] = '\0';
-
-        if (!SHELL_ArgifyW(buffer, sizeof(buffer)/sizeof(WCHAR), sei_tmp.lpParameters, sei_tmp.lpFile, sei_tmp.lpIDList, NULL) && sei_tmp.lpFile[0])
+        wcmd[0] = '\0';
+        done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL);
+        if (!done && wszApplicationName[0])
         {
-            strcatW(buffer, wExtLnk);
-            strcatW(buffer, sei_tmp.lpFile);
+            strcatW(wcmd, wSpace);
+            strcatW(wcmd, wszApplicationName);
         }
-
-        retval = execfunc(buffer, NULL, FALSE, &sei_tmp, psei);
+        retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
         if (retval > 32)
             return TRUE;
         else
             return FALSE;
     }
 
-    /* Else, try to execute the filename */
-    TRACE("execute:'%s','%s'\n", debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters));
-
 
     /* resolve shell shortcuts */
     ext = PathFindExtensionW(sei_tmp.lpFile);
 
-    if (ext && !strcmpiW(ext, wExtLnk))        /* or check for: shell_attribs & SFGAO_LINK */
+    if (ext && !strncmpiW(ext, wExtLnk, sizeof(wExtLnk) / sizeof(WCHAR) - 1) &&
+        (ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '\0' ||
+         (sei_tmp.lpFile[0] == '"' && ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '"')))       /* or check for: shell_attribs & SFGAO_LINK */
     {
-       HRESULT hr = SHELL_ResolveShortCutW((LPWSTR)sei_tmp.lpFile, (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
-                                           sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
+       HRESULT hr;
+       BOOL Quoted;
+
+       if (wszApplicationName[0] == '"')
+       {
+           if (wszApplicationName[strlenW(wszApplicationName) - 1] == '"')
+           {
+               wszApplicationName[strlenW(wszApplicationName) - 1] = '\0';
+               Quoted = TRUE;
+           }
+           else
+           {
+               Quoted = FALSE;
+           }
+       }
+       else
+       {
+           Quoted = FALSE;
+       }
+       /* expand paths before reading shell link */
+       if (ExpandEnvironmentStringsW(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile, buffer, MAX_PATH))
+           lstrcpyW(Quoted ? wszApplicationName + 1 : wszApplicationName/*sei_tmp.lpFile*/, buffer);
+
+       if (*sei_tmp.lpParameters)
+           if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
+               lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
+
+       hr = SHELL_ResolveShortCutW((LPWSTR)(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile),
+                                   (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
+                                   sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
+       if (Quoted)
+       {
+           wszApplicationName[strlenW(wszApplicationName) + 1] = '\0';
+           wszApplicationName[strlenW(wszApplicationName)] = '"';
+       }
 
-       if (psei->lpIDList)
-           psei->fMask |= SEE_MASK_IDLIST;
+       if (sei->lpIDList)
+           sei->fMask |= SEE_MASK_IDLIST;
 
        if (SUCCEEDED(hr))
        {
@@ -1056,7 +1151,7 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
 
                if (SUCCEEDED(hr))
                {
-                   hr = IShellExecuteHookW_Execute(pSEH, psei);
+                   hr = IShellExecuteHookW_Execute(pSEH, sei);
 
                    IShellExecuteHookW_Release(pSEH);
 
@@ -1089,20 +1184,20 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
        }
     }
 
-
     /* expand environment strings */
-
     if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
-       lstrcpyW(wszApplicationName/*sei_tmp.lpFile*/, buffer);
+       lstrcpyW(wszApplicationName, buffer);
 
     if (*sei_tmp.lpParameters)
         if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
-           lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
+           lstrcpyW(wszParameters, buffer);
 
     if (*sei_tmp.lpDirectory)
        if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
-           lstrcpyW(wszDir/*sei_tmp.lpDirectory*/, buffer);
+           lstrcpyW(wszDir, buffer);
 
+    /* Else, try to execute the filename */
+    TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
 
     /* separate out command line arguments from executable file name */
     if (!*sei_tmp.lpParameters) {
@@ -1112,7 +1207,7 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
            LPWSTR dst = wfileName;
            LPWSTR end;
 
-           /* copy the unquoted executabe path to 'wfileName' */
+           /* copy the unquoted executable path to 'wfileName' */
            while(*src && *src!='"')
                *dst++ = *src++;
 
@@ -1142,7 +1237,7 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
            LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
            for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
                int idx = space-sei_tmp.lpFile;
-               strncpyW(buffer, sei_tmp.lpFile, idx);
+               memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
                buffer[idx] = '\0';
 
                /*FIXME This finds directory paths if the targeted file name contains spaces. */
@@ -1168,58 +1263,47 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
 
     lpFile = wfileName;
 
+    strcpyW(wcmd, wszApplicationName);
     if (sei_tmp.lpParameters[0]) {
-        strcatW(wszApplicationName/*sei_tmp.lpFile*/, wSpace);
-        strcatW(wszApplicationName/*sei_tmp.lpFile*/, sei_tmp.lpParameters);
+        strcatW(wcmd, wSpace);
+        strcatW(wcmd, wszParameters);
     }
 
-    retval = execfunc(sei_tmp.lpFile, NULL, FALSE, &sei_tmp, psei);
-    if (retval > 32)
-    {
-       /* Now, that we have successfully launched a process, we can free the PIDL.
-       It may have been used before for %I command line options. */
-       if (sei_tmp.lpIDList!=psei->lpIDList && sei_tmp.lpIDList)
-           SHFree(sei_tmp.lpIDList);
+    /* We set the default to open, and that should generally work.
+       But that is not really the way the MS docs say to do it. */
+    if (!sei_tmp.lpVerb)
+        sei_tmp.lpVerb = wszOpen;
 
-        TRACE("execfunc: retval=%d psei->hInstApp=%p\n", retval, psei->hInstApp);
+    retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
+    if (retval > 32)
         return TRUE;
-    }
 
     /* Else, try to find the executable */
-    buffer[0] = '\0';
-    retval = SHELL_FindExecutable(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, lpFile, sei_tmp.lpVerb, buffer, 1024, wszProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
-
+    wcmd[0] = '\0';
+    retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
     if (retval > 32)  /* Found */
     {
         WCHAR wszQuotedCmd[MAX_PATH+2];
-        /* Must quote to handle case where 'buffer' contains spaces, 
+        /* Must quote to handle case where cmd contains spaces,
          * else security hole if malicious user creates executable file "C:\\Program"
-        *
-        * FIXME: If we don't have set explicitly command line arguments, we must first
-        * split executable path from optional command line arguments. Otherwise we would quote
-        * the complete string with executable path _and_ arguments, which is not what we want.
          */
         strcpyW(wszQuotedCmd, wQuote);
-        strcatW(wszQuotedCmd, buffer);
+        strcatW(wszQuotedCmd, wcmd);
         strcatW(wszQuotedCmd, wQuote);
-
-        if (*sei_tmp.lpParameters) {
+        if (wszParameters[0]) {
             strcatW(wszQuotedCmd, wSpace);
-           strcatW(wszQuotedCmd, sei_tmp.lpParameters);
+            strcatW(wszQuotedCmd, wszParameters);
         }
-
-        TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(buffer), debugstr_w(wszQuotedCmd), debugstr_w(wszProtocol));
-
-        if (*wszProtocol)
-            retval = execute_from_key(wszProtocol, lpFile, env, sei_tmp.lpParameters, execfunc, &sei_tmp, psei);
+        TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
+        if (*lpstrProtocol)
+            retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
         else
-            retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, psei);
-
-        if (env) HeapFree( GetProcessHeap(), 0, env );
+            retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
+        HeapFree( GetProcessHeap(), 0, env );
     }
     else if (PathIsURLW((LPWSTR)lpFile))    /* File not found, check for URL */
     {
-       static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
+       static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
        static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
         LPWSTR lpstrRes;
         INT iSize;
@@ -1231,12 +1315,12 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
             iSize = strlenW(lpFile);
 
         TRACE("Got URL: %s\n", debugstr_w(lpFile));
-        /* Looking for ...protocol\shell\<verb>\command */
-        strncpyW(wszProtocol, lpFile, iSize);
-        wszProtocol[iSize] = '\0';
-        strcatW(wszProtocol, wszShell);
-        strcatW(wszProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
-        strcatW(wszProtocol, wCommand);
+        /* Looking for ...protocol\shell\lpOperation\command */
+        memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
+        lpstrProtocol[iSize] = '\0';
+        strcatW(lpstrProtocol, wShell);
+        strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
+        strcatW(lpstrProtocol, wCommand);
 
         /* Remove File Protocol from lpFile */
         /* In the case file://path/file     */
@@ -1245,8 +1329,7 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
             lpFile += iSize;
             while (*lpFile == ':') lpFile++;
         }
-
-        retval = execute_from_key(wszProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, psei);
+        retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
     }
     /* Check if file specified is in the form www.??????.*** */
     else if (!strncmpiW(lpFile, wWww, 3))
@@ -1258,32 +1341,31 @@ BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfu
         retval = (UINT)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
     }
 
-    /* Now we can free the PIDL. It may have been used before for %I command line options. */
-    if (sei_tmp.lpIDList!=psei->lpIDList && sei_tmp.lpIDList)
-       SHFree(sei_tmp.lpIDList);
-
     TRACE("retval %u\n", retval);
 
     if (retval <= 32)
     {
-        psei->hInstApp = (HINSTANCE)retval;
+        sei->hInstApp = (HINSTANCE)retval;
         return FALSE;
     }
 
-    psei->hInstApp = (HINSTANCE)33;
+    sei->hInstApp = (HINSTANCE)33;
     return TRUE;
 }
 
 /*************************************************************************
  * ShellExecuteA                       [SHELL32.290]
  */
-HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
-                               LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
+HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
+                               LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
 {
     SHELLEXECUTEINFOA sei;
     HANDLE hProcess = 0;
 
-    TRACE("\n");
+    TRACE("%p,%s,%s,%s,%s,%d\n",
+          hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
+          debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
+
     sei.cbSize = sizeof(sei);
     sei.fMask = 0;
     sei.hwnd = hWnd;
@@ -1302,17 +1384,6 @@ HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
     return sei.hInstApp;
 }
 
-/*************************************************************************
- * ShellExecuteEx                              [SHELL32.291]
- *
- */
-BOOL WINAPI ShellExecuteExAW (LPVOID sei)
-{
-    if (SHELL_OsIsUnicode())
-       return ShellExecuteExW32 (sei, SHELL_ExecuteW);
-    return ShellExecuteExA (sei);
-}
-
 /*************************************************************************
  * ShellExecuteExA                             [SHELL32.292]
  *
@@ -1339,7 +1410,7 @@ BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
     if (sei->lpDirectory)
         seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
 
-    if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
+    if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
         seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
     else
         seiW.lpClass = NULL;
@@ -1348,6 +1419,9 @@ BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
 
     sei->hInstApp = seiW.hInstApp;
 
+    if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
+        sei->hProcess = seiW.hProcess;
+
     if (wVerb) SHFree(wVerb);
     if (wFile) SHFree(wFile);
     if (wParameters) SHFree(wParameters);
@@ -1376,6 +1450,7 @@ HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
                                LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
 {
     SHELLEXECUTEINFOW sei;
+    HANDLE hProcess = 0;
 
     TRACE("\n");
     sei.cbSize = sizeof(sei);
@@ -1390,7 +1465,7 @@ HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
     sei.lpClass = 0;
     sei.hkeyClass = 0;
     sei.dwHotKey = 0;
-    sei.hProcess = 0;
+    sei.hProcess = hProcess;
 
     ShellExecuteExW32 (&sei, SHELL_ExecuteW);
     return sei.hInstApp;