server: Use a less hackish approach to not POLLIN when we have a hung up socket.
[wine/multimedia.git] / dlls / shell32 / shlexec.c
blobff07550214e87f1f066d3dc29fa3e700b3edcac6
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33 #include <assert.h>
35 #define COBJMACROS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winuser.h"
42 #include "shlwapi.h"
43 #include "ddeml.h"
45 #include "shell32_main.h"
46 #include "pidl.h"
47 #include "shresdef.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(exec);
53 static const WCHAR wszOpen[] = {'o','p','e','n',0};
54 static const WCHAR wszExe[] = {'.','e','x','e',0};
55 static const WCHAR wszILPtr[] = {':','%','p',0};
56 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
57 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
58 static const WCHAR wszEmpty[] = {0};
60 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
62 typedef UINT_PTR (*SHELL_ExecuteW32)(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
63 const SHELLEXECUTEINFOW *sei, LPSHELLEXECUTEINFOW sei_out);
66 /***********************************************************************
67 * SHELL_ArgifyW [Internal]
69 * this function is supposed to expand the escape sequences found in the registry
70 * some diving reported that the following were used:
71 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
72 * %1 file
73 * %2 printer
74 * %3 driver
75 * %4 port
76 * %I address of a global item ID (explorer switch /idlist)
77 * %L seems to be %1 as long filename followed by the 8+3 variation
78 * %S ???
79 * %* all following parameters (see batfile)
82 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
84 WCHAR xlpFile[1024];
85 BOOL done = FALSE;
86 BOOL found_p1 = FALSE;
87 PWSTR res = out;
88 PCWSTR cmd;
89 DWORD used = 0;
91 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
92 debugstr_w(lpFile), pidl, args);
94 while (*fmt)
96 if (*fmt == '%')
98 switch (*++fmt)
100 case '\0':
101 case '%':
102 used++;
103 if (used < len)
104 *res++ = '%';
105 break;
107 case '2':
108 case '3':
109 case '4':
110 case '5':
111 case '6':
112 case '7':
113 case '8':
114 case '9':
115 case '0':
116 case '*':
117 if (args)
119 if (*fmt == '*')
121 used++;
122 if (used < len)
123 *res++ = '"';
124 while(*args)
126 used++;
127 if (used < len)
128 *res++ = *args++;
129 else
130 args++;
132 used++;
133 if (used < len)
134 *res++ = '"';
136 else
138 while(*args && !isspace(*args))
140 used++;
141 if (used < len)
142 *res++ = *args++;
143 else
144 args++;
147 while(isspace(*args))
148 ++args;
150 break;
152 /* else fall through */
153 case '1':
154 if (!done || (*fmt == '1'))
156 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
157 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
158 cmd = xlpFile;
159 else
160 cmd = lpFile;
162 used += strlenW(cmd);
163 if (used < len)
165 strcpyW(res, cmd);
166 res += strlenW(cmd);
169 found_p1 = TRUE;
170 break;
173 * IE uses this a lot for activating things such as windows media
174 * player. This is not verified to be fully correct but it appears
175 * to work just fine.
177 case 'l':
178 case 'L':
179 if (lpFile) {
180 used += strlenW(lpFile);
181 if (used < len)
183 strcpyW(res, lpFile);
184 res += strlenW(lpFile);
187 found_p1 = TRUE;
188 break;
190 case 'i':
191 case 'I':
192 if (pidl) {
193 INT chars = 0;
194 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
195 * allowing a buffer of 100 should more than exceed all needs */
196 WCHAR buf[100];
197 LPVOID pv;
198 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
199 pv = SHLockShared(hmem, 0);
200 chars = sprintfW(buf, wszILPtr, pv);
201 if (chars >= sizeof(buf)/sizeof(WCHAR))
202 ERR("pidl format buffer too small!\n");
203 used += chars;
204 if (used < len)
206 strcpyW(res,buf);
207 res += chars;
209 SHUnlockShared(pv);
211 found_p1 = TRUE;
212 break;
214 default:
216 * Check if this is an env-variable here...
219 /* Make sure that we have at least one more %.*/
220 if (strchrW(fmt, '%'))
222 WCHAR tmpBuffer[1024];
223 PWSTR tmpB = tmpBuffer;
224 WCHAR tmpEnvBuff[MAX_PATH];
225 DWORD envRet;
227 while (*fmt != '%')
228 *tmpB++ = *fmt++;
229 *tmpB++ = 0;
231 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
233 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
234 if (envRet == 0 || envRet > MAX_PATH)
236 used += strlenW(tmpBuffer);
237 if (used < len)
239 strcpyW( res, tmpBuffer );
240 res += strlenW(tmpBuffer);
243 else
245 used += strlenW(tmpEnvBuff);
246 if (used < len)
248 strcpyW( res, tmpEnvBuff );
249 res += strlenW(tmpEnvBuff);
253 done = TRUE;
254 break;
256 /* Don't skip past terminator (catch a single '%' at the end) */
257 if (*fmt != '\0')
259 fmt++;
262 else
264 used ++;
265 if (used < len)
266 *res++ = *fmt++;
267 else
268 fmt++;
272 *res = '\0';
273 TRACE("used %i of %i space\n",used,len);
274 if (out_len)
275 *out_len = used;
277 return found_p1;
280 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
282 STRRET strret;
283 IShellFolder* desktop;
285 HRESULT hr = SHGetDesktopFolder(&desktop);
287 if (SUCCEEDED(hr)) {
288 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
290 if (SUCCEEDED(hr))
291 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
293 IShellFolder_Release(desktop);
296 return hr;
299 /*************************************************************************
300 * SHELL_ExecuteW [Internal]
303 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
304 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
306 STARTUPINFOW startup;
307 PROCESS_INFORMATION info;
308 UINT_PTR retval = SE_ERR_NOASSOC;
309 UINT gcdret = 0;
310 WCHAR curdir[MAX_PATH];
311 DWORD dwCreationFlags;
312 const WCHAR *lpDirectory = NULL;
314 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
316 /* make sure we don't fail the CreateProcess if the calling app passes in
317 * a bad working directory */
318 if (psei->lpDirectory && psei->lpDirectory[0])
320 DWORD attr = GetFileAttributesW(psei->lpDirectory);
321 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
322 lpDirectory = psei->lpDirectory;
325 /* ShellExecute specifies the command from psei->lpDirectory
326 * if present. Not from the current dir as CreateProcess does */
327 if( lpDirectory )
328 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
329 if( !SetCurrentDirectoryW( lpDirectory))
330 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
331 ZeroMemory(&startup,sizeof(STARTUPINFOW));
332 startup.cb = sizeof(STARTUPINFOW);
333 startup.dwFlags = STARTF_USESHOWWINDOW;
334 startup.wShowWindow = psei->nShow;
335 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
336 if (psei->fMask & SEE_MASK_NO_CONSOLE)
337 dwCreationFlags |= CREATE_NEW_CONSOLE;
338 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
339 lpDirectory, &startup, &info))
341 /* Give 30 seconds to the app to come up, if desired. Probably only needed
342 when starting app immediately before making a DDE connection. */
343 if (shWait)
344 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
345 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
346 retval = 33;
347 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
348 psei_out->hProcess = info.hProcess;
349 else
350 CloseHandle( info.hProcess );
351 CloseHandle( info.hThread );
353 else if ((retval = GetLastError()) >= 32)
355 TRACE("CreateProcess returned error %ld\n", retval);
356 retval = ERROR_BAD_FORMAT;
359 TRACE("returning %lu\n", retval);
361 psei_out->hInstApp = (HINSTANCE)retval;
362 if( gcdret )
363 if( !SetCurrentDirectoryW( curdir))
364 ERR("cannot return to directory %s\n", debugstr_w(curdir));
366 return retval;
370 /***********************************************************************
371 * SHELL_BuildEnvW [Internal]
373 * Build the environment for the new process, adding the specified
374 * path to the PATH variable. Returned pointer must be freed by caller.
376 static void *SHELL_BuildEnvW( const WCHAR *path )
378 static const WCHAR wPath[] = {'P','A','T','H','=',0};
379 WCHAR *strings, *new_env;
380 WCHAR *p, *p2;
381 int total = strlenW(path) + 1;
382 BOOL got_path = FALSE;
384 if (!(strings = GetEnvironmentStringsW())) return NULL;
385 p = strings;
386 while (*p)
388 int len = strlenW(p) + 1;
389 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
390 total += len;
391 p += len;
393 if (!got_path) total += 5; /* we need to create PATH */
394 total++; /* terminating null */
396 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
398 FreeEnvironmentStringsW( strings );
399 return NULL;
401 p = strings;
402 p2 = new_env;
403 while (*p)
405 int len = strlenW(p) + 1;
406 memcpy( p2, p, len * sizeof(WCHAR) );
407 if (!strncmpiW( p, wPath, 5 ))
409 p2[len - 1] = ';';
410 strcpyW( p2 + len, path );
411 p2 += strlenW(path) + 1;
413 p += len;
414 p2 += len;
416 if (!got_path)
418 strcpyW( p2, wPath );
419 strcatW( p2, path );
420 p2 += strlenW(p2) + 1;
422 *p2 = 0;
423 FreeEnvironmentStringsW( strings );
424 return new_env;
428 /***********************************************************************
429 * SHELL_TryAppPathW [Internal]
431 * Helper function for SHELL_FindExecutable
432 * @param lpResult - pointer to a buffer of size MAX_PATH
433 * On entry: szName is a filename (probably without path separators).
434 * On exit: if szName found in "App Path", place full path in lpResult, and return true
436 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
438 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',
439 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
440 static const WCHAR wPath[] = {'P','a','t','h',0};
441 HKEY hkApp = 0;
442 WCHAR buffer[1024];
443 LONG len;
444 LONG res;
445 BOOL found = FALSE;
447 if (env) *env = NULL;
448 strcpyW(buffer, wszKeyAppPaths);
449 strcatW(buffer, szName);
450 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
451 if (res) goto end;
453 len = MAX_PATH*sizeof(WCHAR);
454 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
455 if (res) goto end;
456 found = TRUE;
458 if (env)
460 DWORD count = sizeof(buffer);
461 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
462 *env = SHELL_BuildEnvW( buffer );
465 end:
466 if (hkApp) RegCloseKey(hkApp);
467 return found;
470 /*************************************************************************
471 * SHELL_FindExecutableByOperation [Internal]
473 * called from SHELL_FindExecutable or SHELL_execute_class
474 * in/out:
475 * filetype a buffer, big enough, to get the key name to do actually the
476 * command "WordPad.Document.1\\shell\\open\\command"
477 * passed as "WordPad.Document.1"
478 * in:
479 * lpOperation the operation on it (open)
480 * commandlen the size of command buffer (in bytes)
481 * out:
482 * command a buffer, to store the command to do the
483 * operation on the file
484 * key a buffer, big enough, to get the key name to do actually the
485 * command "WordPad.Document.1\\shell\\open\\command"
486 * Can be NULL
488 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
490 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
491 HKEY hkeyClass;
492 WCHAR verb[MAX_PATH];
494 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, 0x02000000, &hkeyClass))
495 return SE_ERR_NOASSOC;
496 if (!HCR_GetDefaultVerbW(hkeyClass, lpOperation, verb, sizeof(verb)/sizeof(verb[0])))
497 return SE_ERR_NOASSOC;
498 RegCloseKey(hkeyClass);
500 /* Looking for ...buffer\shell\<verb>\command */
501 strcatW(filetype, wszShell);
502 strcatW(filetype, verb);
503 strcatW(filetype, wCommand);
505 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
506 &commandlen) == ERROR_SUCCESS)
508 commandlen /= sizeof(WCHAR);
509 if (key) strcpyW(key, filetype);
510 #if 0
511 LPWSTR tmp;
512 WCHAR param[256];
513 LONG paramlen = sizeof(param);
514 static const WCHAR wSpace[] = {' ',0};
516 /* FIXME: it seems all Windows version don't behave the same here.
517 * the doc states that this ddeexec information can be found after
518 * the exec names.
519 * on Win98, it doesn't appear, but I think it does on Win2k
521 /* Get the parameters needed by the application
522 from the associated ddeexec key */
523 tmp = strstrW(filetype, wCommand);
524 tmp[0] = '\0';
525 strcatW(filetype, wDdeexec);
526 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
527 &paramlen) == ERROR_SUCCESS)
529 paramlen /= sizeof(WCHAR);
530 strcatW(command, wSpace);
531 strcatW(command, param);
532 commandlen += paramlen;
534 #endif
536 command[commandlen] = '\0';
538 return 33; /* FIXME see SHELL_FindExecutable() */
541 return SE_ERR_NOASSOC;
544 /*************************************************************************
545 * SHELL_FindExecutable [Internal]
547 * Utility for code sharing between FindExecutable and ShellExecute
548 * in:
549 * lpFile the name of a file
550 * lpOperation the operation on it (open)
551 * out:
552 * lpResult a buffer, big enough :-(, to store the command to do the
553 * operation on the file
554 * key a buffer, big enough, to get the key name to do actually the
555 * command (it'll be used afterwards for more information
556 * on the operation)
558 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
559 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
561 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
562 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
563 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
564 WCHAR *extension = NULL; /* pointer to file extension */
565 WCHAR filetype[256]; /* registry name for this filetype */
566 LONG filetypelen = sizeof(filetype); /* length of above */
567 WCHAR command[1024]; /* command from registry */
568 WCHAR wBuffer[256]; /* Used to GetProfileString */
569 UINT retval = SE_ERR_NOASSOC;
570 WCHAR *tok; /* token pointer */
571 WCHAR xlpFile[256]; /* result of SearchPath */
572 DWORD attribs; /* file attributes */
574 TRACE("%s\n", debugstr_w(lpFile));
576 if (!lpResult)
577 return ERROR_INVALID_PARAMETER;
579 xlpFile[0] = '\0';
580 lpResult[0] = '\0'; /* Start off with an empty return string */
581 if (key) *key = '\0';
583 /* trap NULL parameters on entry */
584 if (!lpFile)
586 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
587 debugstr_w(lpFile), debugstr_w(lpResult));
588 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
591 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
593 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
594 return 33;
597 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
599 TRACE("SearchPathW returned non-zero\n");
600 lpFile = xlpFile;
601 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
604 attribs = GetFileAttributesW(lpFile);
605 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
607 strcpyW(filetype, wszFolder);
609 else
611 /* Did we get something? Anything? */
612 if (xlpFile[0]==0)
614 TRACE("Returning SE_ERR_FNF\n");
615 return SE_ERR_FNF;
617 /* First thing we need is the file's extension */
618 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
619 /* File->Run in progman uses */
620 /* .\FILE.EXE :( */
621 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
623 if (extension == NULL || extension[1]==0)
625 WARN("Returning SE_ERR_NOASSOC\n");
626 return SE_ERR_NOASSOC;
629 /* Three places to check: */
630 /* 1. win.ini, [windows], programs (NB no leading '.') */
631 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
632 /* 3. win.ini, [extensions], extension (NB no leading '.' */
633 /* All I know of the order is that registry is checked before */
634 /* extensions; however, it'd make sense to check the programs */
635 /* section first, so that's what happens here. */
637 /* See if it's a program - if GetProfileString fails, we skip this
638 * section. Actually, if GetProfileString fails, we've probably
639 * got a lot more to worry about than running a program... */
640 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
642 CharLowerW(wBuffer);
643 tok = wBuffer;
644 while (*tok)
646 WCHAR *p = tok;
647 while (*p && *p != ' ' && *p != '\t') p++;
648 if (*p)
650 *p++ = 0;
651 while (*p == ' ' || *p == '\t') p++;
654 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
656 strcpyW(lpResult, xlpFile);
657 /* Need to perhaps check that the file has a path
658 * attached */
659 TRACE("found %s\n", debugstr_w(lpResult));
660 return 33;
661 /* Greater than 32 to indicate success */
663 tok = p;
667 /* Check registry */
668 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
669 &filetypelen) == ERROR_SUCCESS)
671 filetypelen /= sizeof(WCHAR);
672 if (filetypelen == sizeof(filetype)/sizeof(WCHAR))
673 filetypelen--;
674 filetype[filetypelen] = '\0';
675 TRACE("File type: %s\n", debugstr_w(filetype));
677 else
679 *filetype = '\0';
683 if (*filetype)
685 /* pass the operation string to SHELL_FindExecutableByOperation() */
686 retval = SHELL_FindExecutableByOperation(lpOperation, key, filetype, command, sizeof(command));
688 if (retval > 32)
690 DWORD finishedLen;
691 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
692 if (finishedLen > resultLen)
693 ERR("Argify buffer not large enough.. truncated\n");
695 /* Remove double quotation marks and command line arguments */
696 if (*lpResult == '"')
698 WCHAR *p = lpResult;
699 while (*(p + 1) != '"')
701 *p = *(p + 1);
702 p++;
704 *p = '\0';
706 else
708 /* Truncate on first space */
709 WCHAR *p = lpResult;
710 while (*p != ' ' && *p != '\0')
711 p++;
712 *p='\0';
716 else /* Check win.ini */
718 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
720 /* Toss the leading dot */
721 extension++;
722 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
724 if (strlenW(command) != 0)
726 strcpyW(lpResult, command);
727 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
728 if (tok != NULL)
730 tok[0] = '\0';
731 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
732 tok = strchrW(command, '^'); /* see above */
733 if ((tok != NULL) && (strlenW(tok)>5))
735 strcatW(lpResult, &tok[5]);
738 retval = 33; /* FIXME - see above */
743 TRACE("returning %s\n", debugstr_w(lpResult));
744 return retval;
747 /******************************************************************
748 * dde_cb
750 * callback for the DDE connection. not really useful
752 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
753 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
754 ULONG_PTR dwData1, ULONG_PTR dwData2)
756 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
757 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
758 return NULL;
761 /******************************************************************
762 * dde_connect
764 * ShellExecute helper. Used to do an operation with a DDE connection
766 * Handles both the direct connection (try #1), and if it fails,
767 * launching an application and trying (#2) to connect to it
770 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
771 const WCHAR* lpFile, WCHAR *env,
772 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
773 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
775 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
776 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
777 WCHAR regkey[256];
778 WCHAR * endkey = regkey + strlenW(key);
779 WCHAR app[256], topic[256], ifexec[256], res[256];
780 LONG applen, topiclen, ifexeclen;
781 WCHAR * exec;
782 DWORD ddeInst = 0;
783 DWORD tid;
784 DWORD resultLen;
785 HSZ hszApp, hszTopic;
786 HCONV hConv;
787 HDDEDATA hDdeData;
788 unsigned ret = SE_ERR_NOASSOC;
789 BOOL unicode = !(GetVersion() & 0x80000000);
791 strcpyW(regkey, key);
792 strcpyW(endkey, wApplication);
793 applen = sizeof(app);
794 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
796 WCHAR command[1024], fullpath[MAX_PATH];
797 static const WCHAR wSo[] = { '.','s','o',0 };
798 int sizeSo = sizeof(wSo)/sizeof(WCHAR);
799 LPWSTR ptr = NULL;
800 DWORD ret = 0;
802 /* Get application command from start string and find filename of application */
803 if (*start == '"')
805 strcpyW(command, start+1);
806 if ((ptr = strchrW(command, '"')))
807 *ptr = 0;
808 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
810 else
812 LPCWSTR p;
813 LPWSTR space;
814 for (p=start; (space=strchrW(p, ' ')); p=space+1)
816 int idx = space-start;
817 memcpy(command, start, idx*sizeof(WCHAR));
818 command[idx] = '\0';
819 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr)))
820 break;
822 if (!ret)
823 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
826 if (!ret)
828 ERR("Unable to find application path for command %s\n", debugstr_w(start));
829 return ERROR_ACCESS_DENIED;
831 strcpyW(app, ptr);
833 /* Remove extensions (including .so) */
834 ptr = app + strlenW(app) - (sizeSo-1);
835 if (strlenW(app) >= sizeSo &&
836 !strcmpW(ptr, wSo))
837 *ptr = 0;
839 ptr = strrchrW(app, '.');
840 assert(ptr);
841 *ptr = 0;
844 strcpyW(endkey, wTopic);
845 topiclen = sizeof(topic);
846 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
848 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
849 strcpyW(topic, wSystem);
852 if (unicode)
854 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
855 return 2;
857 else
859 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
860 return 2;
863 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
864 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
866 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
867 exec = ddeexec;
868 if (!hConv)
870 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
871 TRACE("Launching %s\n", debugstr_w(start));
872 ret = execfunc(start, env, TRUE, psei, psei_out);
873 if (ret <= 32)
875 TRACE("Couldn't launch\n");
876 goto error;
878 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
879 if (!hConv)
881 TRACE("Couldn't connect. ret=%d\n", ret);
882 DdeUninitialize(ddeInst);
883 SetLastError(ERROR_DDE_FAIL);
884 return 30; /* whatever */
886 strcpyW(endkey, wIfexec);
887 ifexeclen = sizeof(ifexec);
888 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
890 exec = ifexec;
894 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
895 if (resultLen > sizeof(res)/sizeof(WCHAR))
896 ERR("Argify buffer not large enough, truncated\n");
897 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
899 /* It's documented in the KB 330337 that IE has a bug and returns
900 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
902 if (unicode)
903 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
904 XTYP_EXECUTE, 30000, &tid);
905 else
907 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
908 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
909 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
910 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
911 XTYP_EXECUTE, 10000, &tid );
912 HeapFree(GetProcessHeap(), 0, resA);
914 if (hDdeData)
915 DdeFreeDataHandle(hDdeData);
916 else
917 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
918 ret = 33;
920 DdeDisconnect(hConv);
922 error:
923 DdeUninitialize(ddeInst);
925 return ret;
928 /*************************************************************************
929 * execute_from_key [Internal]
931 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
932 LPCWSTR executable_name,
933 SHELL_ExecuteW32 execfunc,
934 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
936 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
937 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
938 WCHAR cmd[256], param[1024], ddeexec[256];
939 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
940 UINT_PTR retval = SE_ERR_NOASSOC;
941 DWORD resultLen;
942 LPWSTR tmp;
944 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
945 debugstr_w(szCommandline), debugstr_w(executable_name));
947 cmd[0] = '\0';
948 param[0] = '\0';
950 /* Get the application from the registry */
951 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
953 TRACE("got cmd: %s\n", debugstr_w(cmd));
955 /* Is there a replace() function anywhere? */
956 cmdlen /= sizeof(WCHAR);
957 if (cmdlen >= sizeof(cmd)/sizeof(WCHAR))
958 cmdlen = sizeof(cmd)/sizeof(WCHAR)-1;
959 cmd[cmdlen] = '\0';
960 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
961 if (resultLen > sizeof(param)/sizeof(WCHAR))
962 ERR("Argify buffer not large enough, truncating\n");
965 /* Get the parameters needed by the application
966 from the associated ddeexec key */
967 tmp = strstrW(key, wCommand);
968 assert(tmp);
969 strcpyW(tmp, wDdeexec);
971 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
973 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
974 if (!param[0]) strcpyW(param, executable_name);
975 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
977 else if (param[0])
979 TRACE("executing: %s\n", debugstr_w(param));
980 retval = execfunc(param, env, FALSE, psei, psei_out);
982 else
983 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
985 return retval;
988 /*************************************************************************
989 * FindExecutableA [SHELL32.@]
991 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
993 HINSTANCE retval;
994 WCHAR *wFile = NULL, *wDirectory = NULL;
995 WCHAR wResult[MAX_PATH];
997 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
998 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1000 retval = FindExecutableW(wFile, wDirectory, wResult);
1001 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1002 SHFree( wFile );
1003 SHFree( wDirectory );
1005 TRACE("returning %s\n", lpResult);
1006 return retval;
1009 /*************************************************************************
1010 * FindExecutableW [SHELL32.@]
1012 * This function returns the executable associated with the specified file
1013 * for the default verb.
1015 * PARAMS
1016 * lpFile [I] The file to find the association for. This must refer to
1017 * an existing file otherwise FindExecutable fails and returns
1018 * SE_ERR_FNF.
1019 * lpResult [O] Points to a buffer into which the executable path is
1020 * copied. This parameter must not be NULL otherwise
1021 * FindExecutable() segfaults. The buffer must be of size at
1022 * least MAX_PATH characters.
1024 * RETURNS
1025 * A value greater than 32 on success, less than or equal to 32 otherwise.
1026 * See the SE_ERR_* constants.
1028 * NOTES
1029 * On Windows XP and 2003, FindExecutable() seems to first convert the
1030 * filename into 8.3 format, thus taking into account only the first three
1031 * characters of the extension, and expects to find an association for those.
1032 * However other Windows versions behave sanely.
1034 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1036 UINT_PTR retval = SE_ERR_NOASSOC;
1037 WCHAR old_dir[1024];
1039 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1041 lpResult[0] = '\0'; /* Start off with an empty return string */
1042 if (lpFile == NULL)
1043 return (HINSTANCE)SE_ERR_FNF;
1045 if (lpDirectory)
1047 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
1048 SetCurrentDirectoryW(lpDirectory);
1051 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
1053 TRACE("returning %s\n", debugstr_w(lpResult));
1054 if (lpDirectory)
1055 SetCurrentDirectoryW(old_dir);
1056 return (HINSTANCE)retval;
1059 /* FIXME: is this already implemented somewhere else? */
1060 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1062 LPCWSTR ext = NULL, lpClass = NULL;
1063 LPWSTR cls = NULL;
1064 DWORD type = 0, sz = 0;
1065 HKEY hkey = 0;
1066 LONG r;
1068 if (sei->fMask & SEE_MASK_CLASSALL)
1069 return sei->hkeyClass;
1071 if (sei->fMask & SEE_MASK_CLASSNAME)
1072 lpClass = sei->lpClass;
1073 else
1075 ext = PathFindExtensionW( sei->lpFile );
1076 TRACE("ext = %s\n", debugstr_w( ext ) );
1077 if (!ext)
1078 return hkey;
1080 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1081 if (r != ERROR_SUCCESS )
1082 return hkey;
1084 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1085 if ( r == ERROR_SUCCESS && type == REG_SZ )
1087 sz += sizeof (WCHAR);
1088 cls = HeapAlloc( GetProcessHeap(), 0, sz );
1089 cls[0] = 0;
1090 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1093 RegCloseKey( hkey );
1094 lpClass = cls;
1097 TRACE("class = %s\n", debugstr_w(lpClass) );
1099 hkey = 0;
1100 if ( lpClass )
1101 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1103 HeapFree( GetProcessHeap(), 0, cls );
1105 return hkey;
1108 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1110 LPCITEMIDLIST pidllast = NULL;
1111 IDataObject *dataobj = NULL;
1112 IShellFolder *shf = NULL;
1113 LPITEMIDLIST pidl = NULL;
1114 HRESULT r;
1116 if (sei->fMask & SEE_MASK_CLASSALL)
1117 pidl = sei->lpIDList;
1118 else
1120 WCHAR fullpath[MAX_PATH];
1121 BOOL ret;
1123 fullpath[0] = 0;
1124 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1125 if (!ret)
1126 goto end;
1128 pidl = ILCreateFromPathW( fullpath );
1131 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1132 if ( FAILED( r ) )
1133 goto end;
1135 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1136 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1138 end:
1139 if ( pidl != sei->lpIDList )
1140 ILFree( pidl );
1141 if ( shf )
1142 IShellFolder_Release( shf );
1143 return dataobj;
1146 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1147 LPSHELLEXECUTEINFOW sei )
1149 IContextMenu *cm = NULL;
1150 CMINVOKECOMMANDINFOEX ici;
1151 MENUITEMINFOW info;
1152 WCHAR string[0x80];
1153 INT i, n, def = -1;
1154 HMENU hmenu = 0;
1155 HRESULT r;
1157 TRACE("%p %p\n", obj, sei );
1159 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1160 if ( FAILED( r ) )
1161 return r;
1163 hmenu = CreateMenu();
1164 if ( !hmenu )
1165 goto end;
1167 /* the number of the last menu added is returned in r */
1168 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1169 if ( FAILED( r ) )
1170 goto end;
1172 n = GetMenuItemCount( hmenu );
1173 for ( i = 0; i < n; i++ )
1175 memset( &info, 0, sizeof info );
1176 info.cbSize = sizeof info;
1177 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1178 info.dwTypeData = string;
1179 info.cch = sizeof string;
1180 string[0] = 0;
1181 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1183 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1184 info.fState, info.dwItemData, info.fType, info.wID );
1185 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1186 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1188 def = i;
1189 break;
1193 r = E_FAIL;
1194 if ( def == -1 )
1195 goto end;
1197 memset( &ici, 0, sizeof ici );
1198 ici.cbSize = sizeof ici;
1199 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1200 ici.nShow = sei->nShow;
1201 ici.lpVerb = MAKEINTRESOURCEA( def );
1202 ici.hwnd = sei->hwnd;
1203 ici.lpParametersW = sei->lpParameters;
1205 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1207 TRACE("invoke command returned %08x\n", r );
1209 end:
1210 if ( hmenu )
1211 DestroyMenu( hmenu );
1212 if ( cm )
1213 IContextMenu_Release( cm );
1214 return r;
1217 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1219 IDataObject *dataobj = NULL;
1220 IObjectWithSite *ows = NULL;
1221 IShellExtInit *obj = NULL;
1222 HRESULT r;
1224 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1226 r = CoInitialize( NULL );
1227 if ( FAILED( r ) )
1228 goto end;
1230 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1231 &IID_IShellExtInit, (LPVOID*)&obj );
1232 if ( FAILED( r ) )
1234 ERR("failed %08x\n", r );
1235 goto end;
1238 dataobj = shellex_get_dataobj( sei );
1239 if ( !dataobj )
1241 ERR("failed to get data object\n");
1242 goto end;
1245 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1246 if ( FAILED( r ) )
1247 goto end;
1249 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1250 if ( FAILED( r ) )
1251 goto end;
1253 IObjectWithSite_SetSite( ows, NULL );
1255 r = shellex_run_context_menu_default( obj, sei );
1257 end:
1258 if ( ows )
1259 IObjectWithSite_Release( ows );
1260 if ( dataobj )
1261 IDataObject_Release( dataobj );
1262 if ( obj )
1263 IShellExtInit_Release( obj );
1264 CoUninitialize();
1265 return r;
1269 /*************************************************************************
1270 * ShellExecute_FromContextMenu [Internal]
1272 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1274 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1275 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1276 HKEY hkey, hkeycm = 0;
1277 WCHAR szguid[39];
1278 HRESULT hr;
1279 GUID guid;
1280 DWORD i;
1281 LONG r;
1283 TRACE("%s\n", debugstr_w(sei->lpFile) );
1285 hkey = ShellExecute_GetClassKey( sei );
1286 if ( !hkey )
1287 return ERROR_FUNCTION_FAILED;
1289 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1290 if ( r == ERROR_SUCCESS )
1292 i = 0;
1293 while ( 1 )
1295 r = RegEnumKeyW( hkeycm, i++, szguid, sizeof(szguid)/sizeof(szguid[0]) );
1296 if ( r != ERROR_SUCCESS )
1297 break;
1299 hr = CLSIDFromString( szguid, &guid );
1300 if (SUCCEEDED(hr))
1302 /* stop at the first one that succeeds in running */
1303 hr = shellex_load_object_and_run( hkey, &guid, sei );
1304 if ( SUCCEEDED( hr ) )
1305 break;
1308 RegCloseKey( hkeycm );
1311 if ( hkey != sei->hkeyClass )
1312 RegCloseKey( hkey );
1313 return r;
1316 static UINT_PTR SHELL_quote_and_execute( LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR lpstrProtocol, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc );
1318 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1320 static const WCHAR wQuote[] = {'"',0};
1321 static const WCHAR wSpace[] = {' ',0};
1322 WCHAR execCmd[1024], filetype[1024];
1323 /* launch a document by fileclass like 'WordPad.Document.1' */
1324 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1325 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1326 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1327 DWORD resultLen;
1328 BOOL done;
1329 UINT_PTR rslt;
1331 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1332 if (cmask != SEE_MASK_CLASSNAME)
1334 WCHAR wcmd[1024];
1335 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1336 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1337 psei->lpVerb,
1338 execCmd, sizeof(execCmd));
1340 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1341 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1343 wcmd[0] = '\0';
1344 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1345 if (!done && wszApplicationName[0])
1347 strcatW(wcmd, wSpace);
1348 if (*wszApplicationName != '"')
1350 strcatW(wcmd, wQuote);
1351 strcatW(wcmd, wszApplicationName);
1352 strcatW(wcmd, wQuote);
1354 else
1355 strcatW(wcmd, wszApplicationName);
1357 if (resultLen > sizeof(wcmd)/sizeof(WCHAR))
1358 ERR("Argify buffer not large enough... truncating\n");
1359 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1362 strcpyW(filetype, psei->lpClass);
1363 rslt = SHELL_FindExecutableByOperation(psei->lpVerb, NULL, filetype, execCmd, sizeof(execCmd));
1365 TRACE("SHELL_FindExecutableByOperation returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(filetype), debugstr_w(execCmd));
1366 if (33 > rslt)
1367 return rslt;
1368 rslt = SHELL_quote_and_execute( execCmd, wszEmpty, filetype,
1369 wszApplicationName, NULL, psei,
1370 psei_out, execfunc );
1371 return rslt;
1374 static BOOL SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1376 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1377 WCHAR buffer[MAX_PATH];
1378 BOOL appKnownSingular = FALSE;
1380 /* last chance to translate IDList: now also allow CLSID paths */
1381 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, sizeof(buffer)))) {
1382 if (buffer[0]==':' && buffer[1]==':') {
1383 /* open shell folder for the specified class GUID */
1384 if (strlenW(buffer) + 1 > parametersLen)
1385 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1386 lstrlenW(buffer) + 1, parametersLen);
1387 lstrcpynW(wszParameters, buffer, parametersLen);
1388 if (strlenW(wExplorer) > dwApplicationNameLen)
1389 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1390 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1391 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1392 appKnownSingular = TRUE;
1394 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1395 } else {
1396 WCHAR target[MAX_PATH];
1397 DWORD attribs;
1398 DWORD resultLen;
1399 /* Check if we're executing a directory and if so use the
1400 handler for the Folder class */
1401 strcpyW(target, buffer);
1402 attribs = GetFileAttributesW(buffer);
1403 if (attribs != INVALID_FILE_ATTRIBUTES &&
1404 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1405 HCR_GetExecuteCommandW(0, wszFolder,
1406 sei->lpVerb,
1407 buffer, sizeof(buffer))) {
1408 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1409 buffer, target, sei->lpIDList, NULL, &resultLen);
1410 if (resultLen > dwApplicationNameLen)
1411 ERR("Argify buffer not large enough... truncating\n");
1412 appKnownSingular = FALSE;
1414 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1417 return appKnownSingular;
1420 static UINT_PTR SHELL_quote_and_execute( LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR lpstrProtocol, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1422 static const WCHAR wQuote[] = {'"',0};
1423 static const WCHAR wSpace[] = {' ',0};
1424 UINT_PTR retval;
1425 DWORD len;
1426 WCHAR *wszQuotedCmd;
1428 /* Length of quotes plus length of command plus NULL terminator */
1429 len = 2 + lstrlenW(wcmd) + 1;
1430 if (wszParameters[0])
1432 /* Length of space plus length of parameters */
1433 len += 1 + lstrlenW(wszParameters);
1435 wszQuotedCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1436 /* Must quote to handle case where cmd contains spaces,
1437 * else security hole if malicious user creates executable file "C:\\Program"
1439 strcpyW(wszQuotedCmd, wQuote);
1440 strcatW(wszQuotedCmd, wcmd);
1441 strcatW(wszQuotedCmd, wQuote);
1442 if (wszParameters[0]) {
1443 strcatW(wszQuotedCmd, wSpace);
1444 strcatW(wszQuotedCmd, wszParameters);
1446 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1447 if (*lpstrProtocol)
1448 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1449 else
1450 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1451 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1452 return retval;
1455 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1457 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1458 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1459 UINT_PTR retval;
1460 WCHAR *lpstrProtocol;
1461 LPCWSTR lpstrRes;
1462 INT iSize;
1463 DWORD len;
1465 lpstrRes = strchrW(lpFile, ':');
1466 if (lpstrRes)
1467 iSize = lpstrRes - lpFile;
1468 else
1469 iSize = strlenW(lpFile);
1471 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1472 /* Looking for ...protocol\shell\lpOperation\command */
1473 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1474 if (psei->lpVerb)
1475 len += lstrlenW(psei->lpVerb);
1476 else
1477 len += lstrlenW(wszOpen);
1478 lpstrProtocol = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1479 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1480 lpstrProtocol[iSize] = '\0';
1481 strcatW(lpstrProtocol, wShell);
1482 strcatW(lpstrProtocol, psei->lpVerb? psei->lpVerb: wszOpen);
1483 strcatW(lpstrProtocol, wCommand);
1485 /* Remove File Protocol from lpFile */
1486 /* In the case file://path/file */
1487 if (!strncmpiW(lpFile, wFile, iSize))
1489 lpFile += iSize;
1490 while (*lpFile == ':') lpFile++;
1492 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1493 wcmd, execfunc, psei, psei_out);
1494 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1495 return retval;
1498 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1500 WCHAR msg[2048];
1501 int error_code=GetLastError();
1503 if (retval == SE_ERR_NOASSOC)
1504 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, sizeof(msg)/sizeof(WCHAR));
1505 else
1506 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, sizeof(msg)/sizeof(WCHAR), NULL);
1508 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1511 /*************************************************************************
1512 * SHELL_execute [Internal]
1514 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1516 static const WCHAR wSpace[] = {' ',0};
1517 static const WCHAR wWww[] = {'w','w','w',0};
1518 static const WCHAR wFile[] = {'f','i','l','e',0};
1519 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1520 static const DWORD unsupportedFlags =
1521 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1522 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1523 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1525 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1526 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1527 DWORD dwApplicationNameLen = MAX_PATH+2;
1528 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1529 DWORD dirLen = sizeof(dirBuffer) / sizeof(WCHAR);
1530 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1531 DWORD len;
1532 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1533 WCHAR wfileName[MAX_PATH];
1534 WCHAR *env;
1535 WCHAR lpstrProtocol[256];
1536 LPCWSTR lpFile;
1537 UINT_PTR retval = SE_ERR_NOASSOC;
1538 BOOL appKnownSingular = FALSE;
1540 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1541 sei_tmp = *sei;
1543 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1544 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1545 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1546 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1547 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1548 debugstr_w(sei_tmp.lpClass) : "not used");
1550 sei->hProcess = NULL;
1552 /* make copies of all path/command strings */
1553 if (!sei_tmp.lpFile)
1555 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1556 *wszApplicationName = '\0';
1558 else if (*sei_tmp.lpFile == '\"')
1560 DWORD l = strlenW(sei_tmp.lpFile+1);
1561 if(l >= dwApplicationNameLen) dwApplicationNameLen = l+1;
1562 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1563 memcpy(wszApplicationName, sei_tmp.lpFile+1, (l+1)*sizeof(WCHAR));
1564 if (wszApplicationName[l-1] == '\"')
1565 wszApplicationName[l-1] = '\0';
1566 appKnownSingular = TRUE;
1567 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1568 } else {
1569 DWORD l = strlenW(sei_tmp.lpFile)+1;
1570 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1571 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1572 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1575 wszParameters = parametersBuffer;
1576 if (sei_tmp.lpParameters)
1578 len = lstrlenW(sei_tmp.lpParameters) + 1;
1579 if (len > parametersLen)
1581 wszParameters = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1582 parametersLen = len;
1584 strcpyW(wszParameters, sei_tmp.lpParameters);
1586 else
1587 *wszParameters = '\0';
1589 wszDir = dirBuffer;
1590 if (sei_tmp.lpDirectory)
1592 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1593 if (len > dirLen)
1595 wszDir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1596 dirLen = len;
1598 strcpyW(wszDir, sei_tmp.lpDirectory);
1600 else
1601 *wszDir = '\0';
1603 /* adjust string pointers to point to the new buffers */
1604 sei_tmp.lpFile = wszApplicationName;
1605 sei_tmp.lpParameters = wszParameters;
1606 sei_tmp.lpDirectory = wszDir;
1608 if (sei_tmp.fMask & unsupportedFlags)
1610 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1613 /* process the IDList */
1614 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1616 IShellExecuteHookW* pSEH;
1618 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1620 if (SUCCEEDED(hr))
1622 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1624 IShellExecuteHookW_Release(pSEH);
1626 if (hr == S_OK) {
1627 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1628 if (wszParameters != parametersBuffer)
1629 HeapFree(GetProcessHeap(), 0, wszParameters);
1630 if (wszDir != dirBuffer)
1631 HeapFree(GetProcessHeap(), 0, wszDir);
1632 return TRUE;
1636 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1637 appKnownSingular = TRUE;
1638 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1641 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1643 sei->hInstApp = (HINSTANCE) 33;
1644 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1645 if (wszParameters != parametersBuffer)
1646 HeapFree(GetProcessHeap(), 0, wszParameters);
1647 if (wszDir != dirBuffer)
1648 HeapFree(GetProcessHeap(), 0, wszDir);
1649 return TRUE;
1652 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1654 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1655 execfunc );
1656 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1657 do_error_dialog(retval, sei_tmp.hwnd);
1658 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1659 if (wszParameters != parametersBuffer)
1660 HeapFree(GetProcessHeap(), 0, wszParameters);
1661 if (wszDir != dirBuffer)
1662 HeapFree(GetProcessHeap(), 0, wszDir);
1663 return retval > 32;
1666 /* Has the IDList not yet been translated? */
1667 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1669 appKnownSingular = SHELL_translate_idlist( &sei_tmp, wszParameters,
1670 parametersLen,
1671 wszApplicationName,
1672 dwApplicationNameLen );
1675 /* expand environment strings */
1676 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1677 if (len>0)
1679 LPWSTR buf;
1680 buf = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
1682 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len+1);
1683 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1684 dwApplicationNameLen = len+1;
1685 wszApplicationName = buf;
1686 /* appKnownSingular unmodified */
1688 sei_tmp.lpFile = wszApplicationName;
1691 if (*sei_tmp.lpParameters)
1693 len = ExpandEnvironmentStringsW(sei_tmp.lpParameters, NULL, 0);
1694 if (len > 0)
1696 LPWSTR buf;
1697 len++;
1698 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1699 ExpandEnvironmentStringsW(sei_tmp.lpParameters, buf, len);
1700 if (wszParameters != parametersBuffer)
1701 HeapFree(GetProcessHeap(), 0, wszParameters);
1702 wszParameters = buf;
1703 parametersLen = len;
1704 sei_tmp.lpParameters = wszParameters;
1708 if (*sei_tmp.lpDirectory)
1710 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1711 if (len > 0)
1713 LPWSTR buf;
1714 len++;
1715 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1716 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1717 if (wszDir != dirBuffer)
1718 HeapFree(GetProcessHeap(), 0, wszDir);
1719 wszDir = buf;
1720 sei_tmp.lpDirectory = wszDir;
1724 /* Else, try to execute the filename */
1725 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1727 /* separate out command line arguments from executable file name */
1728 if (!*sei_tmp.lpParameters && !appKnownSingular) {
1729 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1730 if (sei_tmp.lpFile[0] == '"') {
1731 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1732 LPWSTR dst = wfileName;
1733 LPWSTR end;
1735 /* copy the unquoted executable path to 'wfileName' */
1736 while(*src && *src!='"')
1737 *dst++ = *src++;
1739 *dst = '\0';
1741 if (*src == '"') {
1742 end = ++src;
1744 while(isspace(*src))
1745 ++src;
1746 } else
1747 end = src;
1749 /* copy the parameter string to 'wszParameters' */
1750 strcpyW(wszParameters, src);
1752 /* terminate previous command string after the quote character */
1753 *end = '\0';
1755 else
1757 /* If the executable name is not quoted, we have to use this search loop here,
1758 that in CreateProcess() is not sufficient because it does not handle shell links. */
1759 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1760 LPWSTR space, s;
1762 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1763 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1764 int idx = space-sei_tmp.lpFile;
1765 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1766 buffer[idx] = '\0';
1768 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1769 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile)/sizeof(xlpFile[0]), xlpFile, NULL))
1771 /* separate out command from parameter string */
1772 LPCWSTR p = space + 1;
1774 while(isspaceW(*p))
1775 ++p;
1777 strcpyW(wszParameters, p);
1778 *space = '\0';
1780 break;
1784 lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
1786 } else
1787 lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
1789 lpFile = wfileName;
1791 wcmd = wcmdBuffer;
1792 len = lstrlenW(wszApplicationName) + 1;
1793 if (sei_tmp.lpParameters[0])
1794 len += 1 + lstrlenW(wszParameters);
1795 if (len > wcmdLen)
1797 wcmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1798 wcmdLen = len;
1800 strcpyW(wcmd, wszApplicationName);
1801 if (sei_tmp.lpParameters[0]) {
1802 strcatW(wcmd, wSpace);
1803 strcatW(wcmd, wszParameters);
1806 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1807 if (retval > 32) {
1808 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1809 if (wszParameters != parametersBuffer)
1810 HeapFree(GetProcessHeap(), 0, wszParameters);
1811 if (wszDir != dirBuffer)
1812 HeapFree(GetProcessHeap(), 0, wszDir);
1813 if (wcmd != wcmdBuffer)
1814 HeapFree(GetProcessHeap(), 0, wcmd);
1815 return TRUE;
1818 /* Else, try to find the executable */
1819 wcmd[0] = '\0';
1820 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1821 if (retval > 32) /* Found */
1823 retval = SHELL_quote_and_execute( wcmd, wszParameters, lpstrProtocol,
1824 wszApplicationName, env, &sei_tmp,
1825 sei, execfunc );
1826 HeapFree( GetProcessHeap(), 0, env );
1828 else if (PathIsDirectoryW(lpFile))
1830 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r',0};
1831 static const WCHAR wQuote[] = {'"',0};
1832 WCHAR wExec[MAX_PATH];
1833 WCHAR * lpQuotedFile = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3) );
1835 if (lpQuotedFile)
1837 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, wExplorer,
1838 wszOpen, wExec, MAX_PATH,
1839 NULL, &env, NULL, NULL );
1840 if (retval > 32)
1842 strcpyW(lpQuotedFile, wQuote);
1843 strcatW(lpQuotedFile, lpFile);
1844 strcatW(lpQuotedFile, wQuote);
1845 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1846 lpstrProtocol,
1847 wszApplicationName, env,
1848 &sei_tmp, sei, execfunc );
1849 HeapFree( GetProcessHeap(), 0, env );
1851 HeapFree( GetProcessHeap(), 0, lpQuotedFile );
1853 else
1854 retval = 0; /* Out of memory */
1856 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1858 retval = SHELL_execute_url( lpFile, wFile, wcmd, &sei_tmp, sei, execfunc );
1860 /* Check if file specified is in the form www.??????.*** */
1861 else if (!strncmpiW(lpFile, wWww, 3))
1863 /* if so, append lpFile http:// and call ShellExecute */
1864 WCHAR lpstrTmpFile[256];
1865 strcpyW(lpstrTmpFile, wHttp);
1866 strcatW(lpstrTmpFile, lpFile);
1867 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1870 TRACE("retval %lu\n", retval);
1872 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1873 if (wszParameters != parametersBuffer)
1874 HeapFree(GetProcessHeap(), 0, wszParameters);
1875 if (wszDir != dirBuffer)
1876 HeapFree(GetProcessHeap(), 0, wszDir);
1877 if (wcmd != wcmdBuffer)
1878 HeapFree(GetProcessHeap(), 0, wcmd);
1880 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1882 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1883 do_error_dialog(retval, sei_tmp.hwnd);
1884 return retval > 32;
1887 /*************************************************************************
1888 * ShellExecuteA [SHELL32.290]
1890 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1891 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1893 SHELLEXECUTEINFOA sei;
1895 TRACE("%p,%s,%s,%s,%s,%d\n",
1896 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
1897 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1899 sei.cbSize = sizeof(sei);
1900 sei.fMask = SEE_MASK_FLAG_NO_UI;
1901 sei.hwnd = hWnd;
1902 sei.lpVerb = lpOperation;
1903 sei.lpFile = lpFile;
1904 sei.lpParameters = lpParameters;
1905 sei.lpDirectory = lpDirectory;
1906 sei.nShow = iShowCmd;
1907 sei.lpIDList = 0;
1908 sei.lpClass = 0;
1909 sei.hkeyClass = 0;
1910 sei.dwHotKey = 0;
1911 sei.hProcess = 0;
1913 ShellExecuteExA (&sei);
1914 return sei.hInstApp;
1917 /*************************************************************************
1918 * ShellExecuteExA [SHELL32.292]
1921 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1923 SHELLEXECUTEINFOW seiW;
1924 BOOL ret;
1925 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1927 TRACE("%p\n", sei);
1929 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1931 if (sei->lpVerb)
1932 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1934 if (sei->lpFile)
1935 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1937 if (sei->lpParameters)
1938 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1940 if (sei->lpDirectory)
1941 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1943 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1944 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1945 else
1946 seiW.lpClass = NULL;
1948 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1950 sei->hInstApp = seiW.hInstApp;
1952 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1953 sei->hProcess = seiW.hProcess;
1955 SHFree(wVerb);
1956 SHFree(wFile);
1957 SHFree(wParameters);
1958 SHFree(wDirectory);
1959 SHFree(wClass);
1961 return ret;
1964 /*************************************************************************
1965 * ShellExecuteExW [SHELL32.293]
1968 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1970 return SHELL_execute( sei, SHELL_ExecuteW );
1973 /*************************************************************************
1974 * ShellExecuteW [SHELL32.294]
1975 * from shellapi.h
1976 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1977 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1979 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1980 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1982 SHELLEXECUTEINFOW sei;
1984 TRACE("\n");
1985 sei.cbSize = sizeof(sei);
1986 sei.fMask = SEE_MASK_FLAG_NO_UI;
1987 sei.hwnd = hwnd;
1988 sei.lpVerb = lpOperation;
1989 sei.lpFile = lpFile;
1990 sei.lpParameters = lpParameters;
1991 sei.lpDirectory = lpDirectory;
1992 sei.nShow = nShowCmd;
1993 sei.lpIDList = 0;
1994 sei.lpClass = 0;
1995 sei.hkeyClass = 0;
1996 sei.dwHotKey = 0;
1997 sei.hProcess = 0;
1999 SHELL_execute( &sei, SHELL_ExecuteW );
2000 return sei.hInstApp;
2003 /*************************************************************************
2004 * WOWShellExecute [SHELL32.@]
2006 * FIXME: the callback function most likely doesn't work the same way on Windows.
2008 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
2009 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
2011 SHELLEXECUTEINFOW seiW;
2012 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2013 HANDLE hProcess = 0;
2015 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
2016 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2017 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2018 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2020 seiW.cbSize = sizeof(seiW);
2021 seiW.fMask = 0;
2022 seiW.hwnd = hWnd;
2023 seiW.nShow = iShowCmd;
2024 seiW.lpIDList = 0;
2025 seiW.lpClass = 0;
2026 seiW.hkeyClass = 0;
2027 seiW.dwHotKey = 0;
2028 seiW.hProcess = hProcess;
2030 SHELL_execute( &seiW, callback );
2032 SHFree(wVerb);
2033 SHFree(wFile);
2034 SHFree(wParameters);
2035 SHFree(wDirectory);
2036 return seiW.hInstApp;
2039 /*************************************************************************
2040 * OpenAs_RunDLLA [SHELL32.@]
2042 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2044 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2047 /*************************************************************************
2048 * OpenAs_RunDLLW [SHELL32.@]
2050 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2052 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);