winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / shell32 / shlexec.c
blobce0b8f6d2be3b22879566b1416ba7d05e3e22256
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);
65 static inline BOOL isSpace(WCHAR c)
67 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
70 /***********************************************************************
71 * SHELL_ArgifyW [Internal]
73 * this function is supposed to expand the escape sequences found in the registry
74 * some diving reported that the following were used:
75 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
76 * %1 file
77 * %2 printer
78 * %3 driver
79 * %4 port
80 * %I address of a global item ID (explorer switch /idlist)
81 * %L seems to be %1 as long filename followed by the 8+3 variation
82 * %S ???
83 * %* all following parameters (see batfile)
86 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
88 WCHAR xlpFile[1024];
89 BOOL done = FALSE;
90 BOOL found_p1 = FALSE;
91 PWSTR res = out;
92 PCWSTR cmd;
93 DWORD used = 0;
95 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
96 debugstr_w(lpFile), pidl, args);
98 while (*fmt)
100 if (*fmt == '%')
102 switch (*++fmt)
104 case '\0':
105 case '%':
106 used++;
107 if (used < len)
108 *res++ = '%';
109 break;
111 case '2':
112 case '3':
113 case '4':
114 case '5':
115 case '6':
116 case '7':
117 case '8':
118 case '9':
119 case '0':
120 case '*':
121 if (args)
123 if (*fmt == '*')
125 used++;
126 if (used < len)
127 *res++ = '"';
128 while(*args)
130 used++;
131 if (used < len)
132 *res++ = *args++;
133 else
134 args++;
136 used++;
137 if (used < len)
138 *res++ = '"';
140 else
142 while(*args && !isSpace(*args))
144 used++;
145 if (used < len)
146 *res++ = *args++;
147 else
148 args++;
151 while(isSpace(*args))
152 ++args;
154 break;
156 /* else fall through */
157 case '1':
158 if (!done || (*fmt == '1'))
160 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
161 if (SearchPathW(NULL, lpFile, wszExe, ARRAY_SIZE(xlpFile), xlpFile, NULL))
162 cmd = xlpFile;
163 else
164 cmd = lpFile;
166 used += strlenW(cmd);
167 if (used < len)
169 strcpyW(res, cmd);
170 res += strlenW(cmd);
173 found_p1 = TRUE;
174 break;
177 * IE uses this a lot for activating things such as windows media
178 * player. This is not verified to be fully correct but it appears
179 * to work just fine.
181 case 'l':
182 case 'L':
183 if (lpFile) {
184 used += strlenW(lpFile);
185 if (used < len)
187 strcpyW(res, lpFile);
188 res += strlenW(lpFile);
191 found_p1 = TRUE;
192 break;
194 case 'i':
195 case 'I':
196 if (pidl) {
197 INT chars = 0;
198 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
199 * allowing a buffer of 100 should more than exceed all needs */
200 WCHAR buf[100];
201 LPVOID pv;
202 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
203 pv = SHLockShared(hmem, 0);
204 chars = sprintfW(buf, wszILPtr, pv);
205 if (chars >= ARRAY_SIZE(buf))
206 ERR("pidl format buffer too small!\n");
207 used += chars;
208 if (used < len)
210 strcpyW(res,buf);
211 res += chars;
213 SHUnlockShared(pv);
215 found_p1 = TRUE;
216 break;
218 default:
220 * Check if this is an env-variable here...
223 /* Make sure that we have at least one more %.*/
224 if (strchrW(fmt, '%'))
226 WCHAR tmpBuffer[1024];
227 PWSTR tmpB = tmpBuffer;
228 WCHAR tmpEnvBuff[MAX_PATH];
229 DWORD envRet;
231 while (*fmt != '%')
232 *tmpB++ = *fmt++;
233 *tmpB++ = 0;
235 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
237 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
238 if (envRet == 0 || envRet > MAX_PATH)
240 used += strlenW(tmpBuffer);
241 if (used < len)
243 strcpyW( res, tmpBuffer );
244 res += strlenW(tmpBuffer);
247 else
249 used += strlenW(tmpEnvBuff);
250 if (used < len)
252 strcpyW( res, tmpEnvBuff );
253 res += strlenW(tmpEnvBuff);
257 done = TRUE;
258 break;
260 /* Don't skip past terminator (catch a single '%' at the end) */
261 if (*fmt != '\0')
263 fmt++;
266 else
268 used ++;
269 if (used < len)
270 *res++ = *fmt++;
271 else
272 fmt++;
276 used ++;
277 if (res - out < len)
278 *res = '\0';
279 else
280 out[len-1] = '\0';
282 TRACE("used %i of %i space\n",used,len);
283 if (out_len)
284 *out_len = used;
286 return found_p1;
289 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
291 STRRET strret;
292 IShellFolder* desktop;
294 HRESULT hr = SHGetDesktopFolder(&desktop);
296 if (SUCCEEDED(hr)) {
297 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
299 if (SUCCEEDED(hr))
300 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
302 IShellFolder_Release(desktop);
305 return hr;
308 /*************************************************************************
309 * SHELL_ExecuteW [Internal]
312 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
313 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
315 STARTUPINFOW startup;
316 PROCESS_INFORMATION info;
317 UINT_PTR retval = SE_ERR_NOASSOC;
318 UINT gcdret = 0;
319 WCHAR curdir[MAX_PATH];
320 DWORD dwCreationFlags;
321 const WCHAR *lpDirectory = NULL;
323 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
325 /* make sure we don't fail the CreateProcess if the calling app passes in
326 * a bad working directory */
327 if (psei->lpDirectory && psei->lpDirectory[0])
329 DWORD attr = GetFileAttributesW(psei->lpDirectory);
330 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
331 lpDirectory = psei->lpDirectory;
334 /* ShellExecute specifies the command from psei->lpDirectory
335 * if present. Not from the current dir as CreateProcess does */
336 if( lpDirectory )
337 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
338 if( !SetCurrentDirectoryW( lpDirectory))
339 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
340 ZeroMemory(&startup,sizeof(STARTUPINFOW));
341 startup.cb = sizeof(STARTUPINFOW);
342 startup.dwFlags = STARTF_USESHOWWINDOW;
343 startup.wShowWindow = psei->nShow;
344 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
345 if (!(psei->fMask & SEE_MASK_NO_CONSOLE))
346 dwCreationFlags |= CREATE_NEW_CONSOLE;
347 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
348 lpDirectory, &startup, &info))
350 /* Give 30 seconds to the app to come up, if desired. Probably only needed
351 when starting app immediately before making a DDE connection. */
352 if (shWait)
353 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
354 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
355 retval = 33;
356 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
357 psei_out->hProcess = info.hProcess;
358 else
359 CloseHandle( info.hProcess );
360 CloseHandle( info.hThread );
362 else if ((retval = GetLastError()) >= 32)
364 TRACE("CreateProcess returned error %ld\n", retval);
365 retval = ERROR_BAD_FORMAT;
368 TRACE("returning %lu\n", retval);
370 psei_out->hInstApp = (HINSTANCE)retval;
371 if( gcdret )
372 if( !SetCurrentDirectoryW( curdir))
373 ERR("cannot return to directory %s\n", debugstr_w(curdir));
375 return retval;
379 /***********************************************************************
380 * SHELL_BuildEnvW [Internal]
382 * Build the environment for the new process, adding the specified
383 * path to the PATH variable. Returned pointer must be freed by caller.
385 static void *SHELL_BuildEnvW( const WCHAR *path )
387 static const WCHAR wPath[] = {'P','A','T','H','=',0};
388 WCHAR *strings, *new_env;
389 WCHAR *p, *p2;
390 int total = strlenW(path) + 1;
391 BOOL got_path = FALSE;
393 if (!(strings = GetEnvironmentStringsW())) return NULL;
394 p = strings;
395 while (*p)
397 int len = strlenW(p) + 1;
398 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
399 total += len;
400 p += len;
402 if (!got_path) total += 5; /* we need to create PATH */
403 total++; /* terminating null */
405 if (!(new_env = heap_alloc( total * sizeof(WCHAR) )))
407 FreeEnvironmentStringsW( strings );
408 return NULL;
410 p = strings;
411 p2 = new_env;
412 while (*p)
414 int len = strlenW(p) + 1;
415 memcpy( p2, p, len * sizeof(WCHAR) );
416 if (!strncmpiW( p, wPath, 5 ))
418 p2[len - 1] = ';';
419 strcpyW( p2 + len, path );
420 p2 += strlenW(path) + 1;
422 p += len;
423 p2 += len;
425 if (!got_path)
427 strcpyW( p2, wPath );
428 strcatW( p2, path );
429 p2 += strlenW(p2) + 1;
431 *p2 = 0;
432 FreeEnvironmentStringsW( strings );
433 return new_env;
437 /***********************************************************************
438 * SHELL_TryAppPathW [Internal]
440 * Helper function for SHELL_FindExecutable
441 * @param lpResult - pointer to a buffer of size MAX_PATH
442 * On entry: szName is a filename (probably without path separators).
443 * On exit: if szName found in "App Path", place full path in lpResult, and return true
445 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
447 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',
448 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
449 static const WCHAR wPath[] = {'P','a','t','h',0};
450 HKEY hkApp = 0;
451 WCHAR buffer[1024];
452 LONG len;
453 LONG res;
454 BOOL found = FALSE;
456 if (env) *env = NULL;
457 strcpyW(buffer, wszKeyAppPaths);
458 strcatW(buffer, szName);
459 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
460 if (res) goto end;
462 len = MAX_PATH*sizeof(WCHAR);
463 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
464 if (res) goto end;
465 found = TRUE;
467 if (env)
469 DWORD count = sizeof(buffer);
470 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
471 *env = SHELL_BuildEnvW( buffer );
474 end:
475 if (hkApp) RegCloseKey(hkApp);
476 return found;
479 /*************************************************************************
480 * SHELL_FindExecutableByVerb [Internal]
482 * called from SHELL_FindExecutable or SHELL_execute_class
483 * in/out:
484 * classname a buffer, big enough, to get the key name to do actually the
485 * command "WordPad.Document.1\\shell\\open\\command"
486 * passed as "WordPad.Document.1"
487 * in:
488 * lpVerb the operation on it (open)
489 * commandlen the size of command buffer (in bytes)
490 * out:
491 * command a buffer, to store the command to do the
492 * operation on the file
493 * key a buffer, big enough, to get the key name to do actually the
494 * command "WordPad.Document.1\\shell\\open\\command"
495 * Can be NULL
497 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
499 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
500 HKEY hkeyClass;
501 WCHAR verb[MAX_PATH];
503 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
504 return SE_ERR_NOASSOC;
505 if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, ARRAY_SIZE(verb)))
506 return SE_ERR_NOASSOC;
507 RegCloseKey(hkeyClass);
509 /* Looking for ...buffer\shell\<verb>\command */
510 strcatW(classname, wszShell);
511 strcatW(classname, verb);
512 strcatW(classname, wCommand);
514 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
515 &commandlen) == ERROR_SUCCESS)
517 commandlen /= sizeof(WCHAR);
518 if (key) strcpyW(key, classname);
519 #if 0
520 LPWSTR tmp;
521 WCHAR param[256];
522 LONG paramlen = sizeof(param);
523 static const WCHAR wSpace[] = {' ',0};
525 /* FIXME: it seems all Windows version don't behave the same here.
526 * the doc states that this ddeexec information can be found after
527 * the exec names.
528 * on Win98, it doesn't appear, but I think it does on Win2k
530 /* Get the parameters needed by the application
531 from the associated ddeexec key */
532 tmp = strstrW(classname, wCommand);
533 tmp[0] = '\0';
534 strcatW(classname, wDdeexec);
535 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
536 &paramlen) == ERROR_SUCCESS)
538 paramlen /= sizeof(WCHAR);
539 strcatW(command, wSpace);
540 strcatW(command, param);
541 commandlen += paramlen;
543 #endif
545 command[commandlen] = '\0';
547 return 33; /* FIXME see SHELL_FindExecutable() */
550 return SE_ERR_NOASSOC;
553 /*************************************************************************
554 * SHELL_FindExecutable [Internal]
556 * Utility for code sharing between FindExecutable and ShellExecute
557 * in:
558 * lpFile the name of a file
559 * lpVerb the operation on it (open)
560 * out:
561 * lpResult a buffer, big enough :-(, to store the command to do the
562 * operation on the file
563 * key a buffer, big enough, to get the key name to do actually the
564 * command (it'll be used afterwards for more information
565 * on the operation)
567 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
568 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
570 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
571 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
572 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
573 WCHAR *extension = NULL; /* pointer to file extension */
574 WCHAR classname[256]; /* registry name for this file type */
575 LONG classnamelen = sizeof(classname); /* length of above */
576 WCHAR command[1024]; /* command from registry */
577 WCHAR wBuffer[256]; /* Used to GetProfileString */
578 UINT retval = SE_ERR_NOASSOC;
579 WCHAR *tok; /* token pointer */
580 WCHAR xlpFile[256]; /* result of SearchPath */
581 DWORD attribs; /* file attributes */
583 TRACE("%s\n", debugstr_w(lpFile));
585 if (!lpResult)
586 return ERROR_INVALID_PARAMETER;
588 xlpFile[0] = '\0';
589 lpResult[0] = '\0'; /* Start off with an empty return string */
590 if (key) *key = '\0';
592 /* trap NULL parameters on entry */
593 if (!lpFile)
595 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
596 debugstr_w(lpFile), debugstr_w(lpResult));
597 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
600 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
602 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
603 return 33;
606 if (SearchPathW(lpPath, lpFile, wszExe, ARRAY_SIZE(xlpFile), xlpFile, NULL))
608 TRACE("SearchPathW returned non-zero\n");
609 lpFile = xlpFile;
610 /* The file was found in the application-supplied default directory (or the system search path) */
612 else if (lpPath && SearchPathW(NULL, lpFile, wszExe, ARRAY_SIZE(xlpFile), xlpFile, NULL))
614 TRACE("SearchPathW returned non-zero\n");
615 lpFile = xlpFile;
616 /* The file was found in one of the directories in the system-wide search path */
619 attribs = GetFileAttributesW(lpFile);
620 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
622 strcpyW(classname, wszFolder);
624 else
626 /* Did we get something? Anything? */
627 if (xlpFile[0]==0)
629 TRACE("Returning SE_ERR_FNF\n");
630 return SE_ERR_FNF;
632 /* First thing we need is the file's extension */
633 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
634 /* File->Run in progman uses */
635 /* .\FILE.EXE :( */
636 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
638 if (extension == NULL || extension[1]==0)
640 WARN("Returning SE_ERR_NOASSOC\n");
641 return SE_ERR_NOASSOC;
644 /* Three places to check: */
645 /* 1. win.ini, [windows], programs (NB no leading '.') */
646 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
647 /* 3. win.ini, [extensions], extension (NB no leading '.' */
648 /* All I know of the order is that registry is checked before */
649 /* extensions; however, it'd make sense to check the programs */
650 /* section first, so that's what happens here. */
652 /* See if it's a program - if GetProfileString fails, we skip this
653 * section. Actually, if GetProfileString fails, we've probably
654 * got a lot more to worry about than running a program... */
655 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, ARRAY_SIZE(wBuffer)) > 0)
657 CharLowerW(wBuffer);
658 tok = wBuffer;
659 while (*tok)
661 WCHAR *p = tok;
662 while (*p && *p != ' ' && *p != '\t') p++;
663 if (*p)
665 *p++ = 0;
666 while (*p == ' ' || *p == '\t') p++;
669 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
671 strcpyW(lpResult, xlpFile);
672 /* Need to perhaps check that the file has a path
673 * attached */
674 TRACE("found %s\n", debugstr_w(lpResult));
675 return 33;
676 /* Greater than 32 to indicate success */
678 tok = p;
682 /* Check registry */
683 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
684 &classnamelen) == ERROR_SUCCESS)
686 classnamelen /= sizeof(WCHAR);
687 if (classnamelen == ARRAY_SIZE(classname))
688 classnamelen--;
689 classname[classnamelen] = '\0';
690 TRACE("File type: %s\n", debugstr_w(classname));
692 else
694 *classname = '\0';
698 if (*classname)
700 /* pass the verb string to SHELL_FindExecutableByVerb() */
701 retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
703 if (retval > 32)
705 DWORD finishedLen;
706 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
707 if (finishedLen > resultLen)
708 ERR("Argify buffer not large enough.. truncated\n");
710 /* Remove double quotation marks and command line arguments */
711 if (*lpResult == '"')
713 WCHAR *p = lpResult;
714 while (*(p + 1) != '"')
716 *p = *(p + 1);
717 p++;
719 *p = '\0';
721 else
723 /* Truncate on first space */
724 WCHAR *p = lpResult;
725 while (*p != ' ' && *p != '\0')
726 p++;
727 *p='\0';
731 else /* Check win.ini */
733 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
735 /* Toss the leading dot */
736 extension++;
737 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, ARRAY_SIZE(command)) > 0)
739 if (*command)
741 strcpyW(lpResult, command);
742 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
743 if (tok != NULL)
745 tok[0] = '\0';
746 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
747 tok = strchrW(command, '^'); /* see above */
748 if ((tok != NULL) && (strlenW(tok)>5))
750 strcatW(lpResult, &tok[5]);
753 retval = 33; /* FIXME - see above */
758 TRACE("returning %s\n", debugstr_w(lpResult));
759 return retval;
762 /******************************************************************
763 * dde_cb
765 * callback for the DDE connection. not really useful
767 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
768 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
769 ULONG_PTR dwData1, ULONG_PTR dwData2)
771 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
772 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
773 return NULL;
776 /******************************************************************
777 * dde_connect
779 * ShellExecute helper. Used to do an operation with a DDE connection
781 * Handles both the direct connection (try #1), and if it fails,
782 * launching an application and trying (#2) to connect to it
785 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
786 const WCHAR* lpFile, WCHAR *env,
787 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
788 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
790 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
791 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
792 WCHAR regkey[256];
793 WCHAR * endkey = regkey + strlenW(key);
794 WCHAR app[256], topic[256], ifexec[256], static_res[256];
795 WCHAR * dynamic_res=NULL;
796 WCHAR * res;
797 LONG applen, topiclen, ifexeclen;
798 WCHAR * exec;
799 DWORD ddeInst = 0;
800 DWORD tid;
801 DWORD resultLen, endkeyLen;
802 HSZ hszApp, hszTopic;
803 HCONV hConv;
804 HDDEDATA hDdeData;
805 unsigned ret = SE_ERR_NOASSOC;
806 BOOL unicode = !(GetVersion() & 0x80000000);
808 if (strlenW(key) + 1 > ARRAY_SIZE(regkey))
810 FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
811 return 2;
813 strcpyW(regkey, key);
814 endkeyLen = ARRAY_SIZE(regkey) - (endkey - regkey);
815 if (strlenW(wApplication) + 1 > endkeyLen)
817 FIXME("endkey %s overruns buffer\n", debugstr_w(wApplication));
818 return 2;
820 strcpyW(endkey, wApplication);
821 applen = sizeof(app);
822 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
824 WCHAR command[1024], fullpath[MAX_PATH];
825 static const WCHAR wSo[] = { '.','s','o',0 };
826 int sizeSo = ARRAY_SIZE(wSo);
827 LPWSTR ptr = NULL;
828 DWORD ret = 0;
830 /* Get application command from start string and find filename of application */
831 if (*start == '"')
833 if (strlenW(start + 1) + 1 > ARRAY_SIZE(command))
835 FIXME("size of input parameter %s larger than buffer\n",
836 debugstr_w(start + 1));
837 return 2;
839 strcpyW(command, start+1);
840 if ((ptr = strchrW(command, '"')))
841 *ptr = 0;
842 ret = SearchPathW(NULL, command, wszExe, ARRAY_SIZE(fullpath), fullpath, &ptr);
844 else
846 LPCWSTR p;
847 LPWSTR space;
848 for (p=start; (space=strchrW(p, ' ')); p=space+1)
850 int idx = space-start;
851 memcpy(command, start, idx*sizeof(WCHAR));
852 command[idx] = '\0';
853 if ((ret = SearchPathW(NULL, command, wszExe, ARRAY_SIZE(fullpath), fullpath, &ptr)))
854 break;
856 if (!ret)
857 ret = SearchPathW(NULL, start, wszExe, ARRAY_SIZE(fullpath), fullpath, &ptr);
860 if (!ret)
862 ERR("Unable to find application path for command %s\n", debugstr_w(start));
863 return ERROR_ACCESS_DENIED;
865 if (strlenW(ptr) + 1 > ARRAY_SIZE(app))
867 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
868 return 2;
870 strcpyW(app, ptr);
872 /* Remove extensions (including .so) */
873 ptr = app + strlenW(app) - (sizeSo-1);
874 if (strlenW(app) >= sizeSo &&
875 !strcmpW(ptr, wSo))
876 *ptr = 0;
878 ptr = strrchrW(app, '.');
879 assert(ptr);
880 *ptr = 0;
883 if (strlenW(wTopic) + 1 > endkeyLen)
885 FIXME("endkey %s overruns buffer\n", debugstr_w(wTopic));
886 return 2;
888 strcpyW(endkey, wTopic);
889 topiclen = sizeof(topic);
890 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
892 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
893 strcpyW(topic, wSystem);
896 if (unicode)
898 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
899 return 2;
901 else
903 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
904 return 2;
907 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
908 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
910 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
911 exec = ddeexec;
912 if (!hConv)
914 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
915 TRACE("Launching %s\n", debugstr_w(start));
916 ret = execfunc(start, env, TRUE, psei, psei_out);
917 if (ret <= 32)
919 TRACE("Couldn't launch\n");
920 goto error;
922 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
923 if (!hConv)
925 TRACE("Couldn't connect. ret=%d\n", ret);
926 DdeUninitialize(ddeInst);
927 SetLastError(ERROR_DDE_FAIL);
928 return 30; /* whatever */
930 if (strlenW(wIfexec) + 1 > endkeyLen)
932 FIXME("endkey %s overruns buffer\n", debugstr_w(wIfexec));
933 return 2;
935 strcpyW(endkey, wIfexec);
936 ifexeclen = sizeof(ifexec);
937 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
939 exec = ifexec;
943 SHELL_ArgifyW(static_res, ARRAY_SIZE(static_res), exec, lpFile, pidl, szCommandline, &resultLen);
944 if (resultLen > ARRAY_SIZE(static_res))
946 res = dynamic_res = heap_alloc(resultLen * sizeof(WCHAR));
947 SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL);
949 else
950 res = static_res;
951 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
953 /* It's documented in the KB 330337 that IE has a bug and returns
954 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
956 if (unicode)
957 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
958 XTYP_EXECUTE, 30000, &tid);
959 else
961 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
962 char *resA = heap_alloc(lenA);
963 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
964 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
965 XTYP_EXECUTE, 10000, &tid );
966 heap_free(resA);
968 if (hDdeData)
969 DdeFreeDataHandle(hDdeData);
970 else
971 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
972 ret = 33;
974 heap_free(dynamic_res);
976 DdeDisconnect(hConv);
978 error:
979 DdeUninitialize(ddeInst);
981 return ret;
984 /*************************************************************************
985 * execute_from_key [Internal]
987 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
988 LPCWSTR executable_name,
989 SHELL_ExecuteW32 execfunc,
990 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
992 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
993 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
994 WCHAR cmd[256], param[1024], ddeexec[256];
995 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
996 UINT_PTR retval = SE_ERR_NOASSOC;
997 DWORD resultLen;
998 LPWSTR tmp;
1000 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
1001 debugstr_w(szCommandline), debugstr_w(executable_name));
1003 cmd[0] = '\0';
1004 param[0] = '\0';
1006 /* Get the application from the registry */
1007 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
1009 TRACE("got cmd: %s\n", debugstr_w(cmd));
1011 /* Is there a replace() function anywhere? */
1012 cmdlen /= sizeof(WCHAR);
1013 if (cmdlen >= ARRAY_SIZE(cmd))
1014 cmdlen = ARRAY_SIZE(cmd) - 1;
1015 cmd[cmdlen] = '\0';
1016 SHELL_ArgifyW(param, ARRAY_SIZE(param), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
1017 if (resultLen > ARRAY_SIZE(param))
1018 ERR("Argify buffer not large enough, truncating\n");
1021 /* Get the parameters needed by the application
1022 from the associated ddeexec key */
1023 tmp = strstrW(key, wCommand);
1024 assert(tmp);
1025 strcpyW(tmp, wDdeexec);
1027 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
1029 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
1030 if (!param[0]) strcpyW(param, executable_name);
1031 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
1033 else if (param[0])
1035 TRACE("executing: %s\n", debugstr_w(param));
1036 retval = execfunc(param, env, FALSE, psei, psei_out);
1038 else
1039 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1041 return retval;
1044 /*************************************************************************
1045 * FindExecutableA [SHELL32.@]
1047 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1049 HINSTANCE retval;
1050 WCHAR *wFile = NULL, *wDirectory = NULL;
1051 WCHAR wResult[MAX_PATH];
1053 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1054 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1056 retval = FindExecutableW(wFile, wDirectory, wResult);
1057 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1058 SHFree( wFile );
1059 SHFree( wDirectory );
1061 TRACE("returning %s\n", lpResult);
1062 return retval;
1065 /*************************************************************************
1066 * FindExecutableW [SHELL32.@]
1068 * This function returns the executable associated with the specified file
1069 * for the default verb.
1071 * PARAMS
1072 * lpFile [I] The file to find the association for. This must refer to
1073 * an existing file otherwise FindExecutable fails and returns
1074 * SE_ERR_FNF.
1075 * lpResult [O] Points to a buffer into which the executable path is
1076 * copied. This parameter must not be NULL otherwise
1077 * FindExecutable() segfaults. The buffer must be of size at
1078 * least MAX_PATH characters.
1080 * RETURNS
1081 * A value greater than 32 on success, less than or equal to 32 otherwise.
1082 * See the SE_ERR_* constants.
1084 * NOTES
1085 * On Windows XP and 2003, FindExecutable() seems to first convert the
1086 * filename into 8.3 format, thus taking into account only the first three
1087 * characters of the extension, and expects to find an association for those.
1088 * However other Windows versions behave sanely.
1090 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1092 UINT_PTR retval = SE_ERR_NOASSOC;
1093 WCHAR old_dir[1024];
1094 WCHAR res[MAX_PATH];
1096 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1098 lpResult[0] = '\0'; /* Start off with an empty return string */
1099 if (lpFile == NULL)
1100 return (HINSTANCE)SE_ERR_FNF;
1102 if (lpDirectory)
1104 GetCurrentDirectoryW(ARRAY_SIZE(old_dir), old_dir);
1105 SetCurrentDirectoryW(lpDirectory);
1108 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, res, MAX_PATH, NULL, NULL, NULL, NULL);
1110 if (retval > 32)
1111 strcpyW(lpResult, res);
1113 TRACE("returning %s\n", debugstr_w(lpResult));
1114 if (lpDirectory)
1115 SetCurrentDirectoryW(old_dir);
1116 return (HINSTANCE)retval;
1119 /* FIXME: is this already implemented somewhere else? */
1120 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1122 LPCWSTR ext = NULL, lpClass = NULL;
1123 LPWSTR cls = NULL;
1124 DWORD type = 0, sz = 0;
1125 HKEY hkey = 0;
1126 LONG r;
1128 if (sei->fMask & SEE_MASK_CLASSALL)
1129 return sei->hkeyClass;
1131 if (sei->fMask & SEE_MASK_CLASSNAME)
1132 lpClass = sei->lpClass;
1133 else
1135 ext = PathFindExtensionW( sei->lpFile );
1136 TRACE("ext = %s\n", debugstr_w( ext ) );
1137 if (!ext)
1138 return hkey;
1140 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1141 if (r != ERROR_SUCCESS )
1142 return hkey;
1144 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1145 if ( r == ERROR_SUCCESS && type == REG_SZ )
1147 sz += sizeof (WCHAR);
1148 cls = heap_alloc( sz );
1149 cls[0] = 0;
1150 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1153 RegCloseKey( hkey );
1154 lpClass = cls;
1157 TRACE("class = %s\n", debugstr_w(lpClass) );
1159 hkey = 0;
1160 if ( lpClass )
1161 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1163 heap_free( cls );
1165 return hkey;
1168 static HRESULT shellex_get_dataobj( LPSHELLEXECUTEINFOW sei, IDataObject **dataobj )
1170 LPCITEMIDLIST pidllast = NULL;
1171 IShellFolder *shf = NULL;
1172 LPITEMIDLIST pidl = NULL;
1173 HRESULT r = SE_ERR_DLLNOTFOUND;
1175 if (sei->fMask & SEE_MASK_CLASSALL)
1176 pidl = sei->lpIDList;
1177 else
1179 WCHAR fullpath[MAX_PATH];
1180 BOOL ret;
1182 fullpath[0] = 0;
1183 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1184 if (!ret)
1185 goto end;
1187 pidl = ILCreateFromPathW( fullpath );
1190 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1191 if ( FAILED( r ) )
1192 goto end;
1194 r = IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1195 &IID_IDataObject, NULL, (void**)dataobj );
1197 end:
1198 if ( pidl != sei->lpIDList )
1199 ILFree( pidl );
1200 if ( shf )
1201 IShellFolder_Release( shf );
1202 return r;
1205 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1206 LPSHELLEXECUTEINFOW sei )
1208 IContextMenu *cm = NULL;
1209 CMINVOKECOMMANDINFOEX ici;
1210 MENUITEMINFOW info;
1211 WCHAR string[0x80];
1212 INT i, n, def = -1;
1213 HMENU hmenu = 0;
1214 HRESULT r;
1216 TRACE("%p %p\n", obj, sei );
1218 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1219 if ( FAILED( r ) )
1220 return r;
1222 hmenu = CreateMenu();
1223 if ( !hmenu )
1224 goto end;
1226 /* the number of the last menu added is returned in r */
1227 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1228 if ( FAILED( r ) )
1229 goto end;
1231 n = GetMenuItemCount( hmenu );
1232 for ( i = 0; i < n; i++ )
1234 memset( &info, 0, sizeof info );
1235 info.cbSize = sizeof info;
1236 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1237 info.dwTypeData = string;
1238 info.cch = ARRAY_SIZE(string);
1239 string[0] = 0;
1240 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1242 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1243 info.fState, info.dwItemData, info.fType, info.wID );
1244 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1245 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1247 def = i;
1248 break;
1252 r = E_FAIL;
1253 if ( def == -1 )
1254 goto end;
1256 memset( &ici, 0, sizeof ici );
1257 ici.cbSize = sizeof ici;
1258 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE|SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1259 ici.nShow = sei->nShow;
1260 ici.lpVerb = MAKEINTRESOURCEA( def );
1261 ici.hwnd = sei->hwnd;
1262 ici.lpParametersW = sei->lpParameters;
1264 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1266 TRACE("invoke command returned %08x\n", r );
1268 end:
1269 if ( hmenu )
1270 DestroyMenu( hmenu );
1271 if ( cm )
1272 IContextMenu_Release( cm );
1273 return r;
1276 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1278 IDataObject *dataobj = NULL;
1279 IObjectWithSite *ows = NULL;
1280 IShellExtInit *obj = NULL;
1281 HRESULT r;
1283 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1285 r = CoInitialize( NULL );
1286 if ( FAILED( r ) )
1287 goto end;
1289 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1290 &IID_IShellExtInit, (LPVOID*)&obj );
1291 if ( FAILED( r ) )
1293 ERR("failed %08x\n", r );
1294 goto end;
1297 r = shellex_get_dataobj( sei, &dataobj );
1298 if ( FAILED( r ) )
1300 ERR("failed to get data object\n");
1301 goto end;
1304 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1305 if ( FAILED( r ) )
1306 goto end;
1308 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1309 if ( FAILED( r ) )
1310 goto end;
1312 IObjectWithSite_SetSite( ows, NULL );
1314 r = shellex_run_context_menu_default( obj, sei );
1316 end:
1317 if ( ows )
1318 IObjectWithSite_Release( ows );
1319 if ( dataobj )
1320 IDataObject_Release( dataobj );
1321 if ( obj )
1322 IShellExtInit_Release( obj );
1323 CoUninitialize();
1324 return r;
1328 /*************************************************************************
1329 * ShellExecute_FromContextMenu [Internal]
1331 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1333 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1334 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1335 HKEY hkey, hkeycm = 0;
1336 WCHAR szguid[39];
1337 HRESULT hr;
1338 GUID guid;
1339 DWORD i;
1340 LONG r;
1342 TRACE("%s\n", debugstr_w(sei->lpFile) );
1344 hkey = ShellExecute_GetClassKey( sei );
1345 if ( !hkey )
1346 return ERROR_FUNCTION_FAILED;
1348 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1349 if ( r == ERROR_SUCCESS )
1351 i = 0;
1352 while ( 1 )
1354 r = RegEnumKeyW( hkeycm, i++, szguid, ARRAY_SIZE(szguid));
1355 if ( r != ERROR_SUCCESS )
1356 break;
1358 hr = CLSIDFromString( szguid, &guid );
1359 if (SUCCEEDED(hr))
1361 /* stop at the first one that succeeds in running */
1362 hr = shellex_load_object_and_run( hkey, &guid, sei );
1363 if ( SUCCEEDED( hr ) )
1364 break;
1367 RegCloseKey( hkeycm );
1370 if ( hkey != sei->hkeyClass )
1371 RegCloseKey( hkey );
1372 return r;
1375 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 );
1377 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1379 static const WCHAR wQuote[] = {'"',0};
1380 static const WCHAR wSpace[] = {' ',0};
1381 WCHAR execCmd[1024], classname[1024];
1382 /* launch a document by fileclass like 'WordPad.Document.1' */
1383 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1384 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1385 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1386 DWORD resultLen;
1387 BOOL done;
1388 UINT_PTR rslt;
1390 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1391 if (cmask != SEE_MASK_CLASSNAME)
1393 WCHAR wcmd[1024];
1394 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1395 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1396 psei->lpVerb,
1397 execCmd, sizeof(execCmd));
1399 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1400 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1402 wcmd[0] = '\0';
1403 done = SHELL_ArgifyW(wcmd, ARRAY_SIZE(wcmd), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1404 if (!done && wszApplicationName[0])
1406 strcatW(wcmd, wSpace);
1407 if (*wszApplicationName != '"')
1409 strcatW(wcmd, wQuote);
1410 strcatW(wcmd, wszApplicationName);
1411 strcatW(wcmd, wQuote);
1413 else
1414 strcatW(wcmd, wszApplicationName);
1416 if (resultLen > ARRAY_SIZE(wcmd))
1417 ERR("Argify buffer not large enough... truncating\n");
1418 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1421 strcpyW(classname, psei->lpClass);
1422 rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1424 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1425 if (33 > rslt)
1426 return rslt;
1427 rslt = SHELL_quote_and_execute( execCmd, wszEmpty, classname,
1428 wszApplicationName, NULL, psei,
1429 psei_out, execfunc );
1430 return rslt;
1433 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1435 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1436 WCHAR buffer[MAX_PATH];
1438 /* last chance to translate IDList: now also allow CLSID paths */
1439 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, ARRAY_SIZE(buffer)))) {
1440 if (buffer[0]==':' && buffer[1]==':') {
1441 /* open shell folder for the specified class GUID */
1442 if (strlenW(buffer) + 1 > parametersLen)
1443 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1444 lstrlenW(buffer) + 1, parametersLen);
1445 lstrcpynW(wszParameters, buffer, parametersLen);
1446 if (strlenW(wExplorer) > dwApplicationNameLen)
1447 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1448 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1449 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1451 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1452 } else {
1453 WCHAR target[MAX_PATH];
1454 DWORD attribs;
1455 DWORD resultLen;
1456 /* Check if we're executing a directory and if so use the
1457 handler for the Folder class */
1458 strcpyW(target, buffer);
1459 attribs = GetFileAttributesW(buffer);
1460 if (attribs != INVALID_FILE_ATTRIBUTES &&
1461 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1462 HCR_GetExecuteCommandW(0, wszFolder,
1463 sei->lpVerb,
1464 buffer, sizeof(buffer))) {
1465 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1466 buffer, target, sei->lpIDList, NULL, &resultLen);
1467 if (resultLen > dwApplicationNameLen)
1468 ERR("Argify buffer not large enough... truncating\n");
1470 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1475 static UINT_PTR SHELL_quote_and_execute( LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR wszKeyname, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1477 static const WCHAR wQuote[] = {'"',0};
1478 static const WCHAR wSpace[] = {' ',0};
1479 UINT_PTR retval;
1480 DWORD len;
1481 WCHAR *wszQuotedCmd;
1483 /* Length of quotes plus length of command plus NULL terminator */
1484 len = 2 + lstrlenW(wcmd) + 1;
1485 if (wszParameters[0])
1487 /* Length of space plus length of parameters */
1488 len += 1 + lstrlenW(wszParameters);
1490 wszQuotedCmd = heap_alloc(len * sizeof(WCHAR));
1491 /* Must quote to handle case where cmd contains spaces,
1492 * else security hole if malicious user creates executable file "C:\\Program"
1494 strcpyW(wszQuotedCmd, wQuote);
1495 strcatW(wszQuotedCmd, wcmd);
1496 strcatW(wszQuotedCmd, wQuote);
1497 if (wszParameters[0]) {
1498 strcatW(wszQuotedCmd, wSpace);
1499 strcatW(wszQuotedCmd, wszParameters);
1501 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1502 if (*wszKeyname)
1503 retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1504 else
1505 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1506 heap_free(wszQuotedCmd);
1507 return retval;
1510 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1512 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1513 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1514 UINT_PTR retval;
1515 WCHAR *lpstrProtocol;
1516 LPCWSTR lpstrRes;
1517 INT iSize;
1518 DWORD len;
1520 lpstrRes = strchrW(lpFile, ':');
1521 if (lpstrRes)
1522 iSize = lpstrRes - lpFile;
1523 else
1524 iSize = strlenW(lpFile);
1526 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1527 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1528 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1529 if (psei->lpVerb && *psei->lpVerb)
1530 len += lstrlenW(psei->lpVerb);
1531 else
1532 len += lstrlenW(wszOpen);
1533 lpstrProtocol = heap_alloc(len * sizeof(WCHAR));
1534 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1535 lpstrProtocol[iSize] = '\0';
1536 strcatW(lpstrProtocol, wShell);
1537 strcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb: wszOpen);
1538 strcatW(lpstrProtocol, wCommand);
1540 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1541 wcmd, execfunc, psei, psei_out);
1542 heap_free(lpstrProtocol);
1543 return retval;
1546 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1548 WCHAR msg[2048];
1549 int error_code=GetLastError();
1551 if (retval == SE_ERR_NOASSOC)
1552 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, ARRAY_SIZE(msg));
1553 else
1554 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, ARRAY_SIZE(msg), NULL);
1556 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1559 static WCHAR *expand_environment( const WCHAR *str )
1561 WCHAR *buf;
1562 DWORD len;
1564 len = ExpandEnvironmentStringsW(str, NULL, 0);
1565 if (!len) return NULL;
1567 buf = heap_alloc(len * sizeof(WCHAR));
1568 if (!buf) return NULL;
1570 len = ExpandEnvironmentStringsW(str, buf, len);
1571 if (!len)
1573 heap_free(buf);
1574 return NULL;
1576 return buf;
1579 /*************************************************************************
1580 * SHELL_execute [Internal]
1582 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1584 static const WCHAR wWww[] = {'w','w','w',0};
1585 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1586 static const DWORD unsupportedFlags =
1587 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1588 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1589 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1591 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1592 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd = NULL;
1593 DWORD dwApplicationNameLen = MAX_PATH+2;
1594 DWORD parametersLen = ARRAY_SIZE(parametersBuffer);
1595 DWORD wcmdLen = ARRAY_SIZE(wcmdBuffer);
1596 DWORD len;
1597 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1598 WCHAR *env;
1599 WCHAR wszKeyname[256];
1600 LPCWSTR lpFile;
1601 UINT_PTR retval = SE_ERR_NOASSOC;
1603 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1604 sei_tmp = *sei;
1606 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1607 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1608 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1609 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1610 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1611 debugstr_w(sei_tmp.lpClass) : "not used");
1613 sei->hProcess = NULL;
1615 /* make copies of all path/command strings */
1616 if (!sei_tmp.lpFile)
1618 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1619 *wszApplicationName = '\0';
1621 else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = strlenW(sei_tmp.lpFile))-1] == '\"')
1623 if(len-1 >= dwApplicationNameLen) dwApplicationNameLen = len;
1624 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1625 memcpy(wszApplicationName, sei_tmp.lpFile+1, len*sizeof(WCHAR));
1626 if(len > 2)
1627 wszApplicationName[len-2] = '\0';
1628 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1629 } else {
1630 DWORD l = strlenW(sei_tmp.lpFile)+1;
1631 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1632 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1633 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1636 wszParameters = parametersBuffer;
1637 if (sei_tmp.lpParameters)
1639 len = lstrlenW(sei_tmp.lpParameters) + 1;
1640 if (len > parametersLen)
1642 wszParameters = heap_alloc(len * sizeof(WCHAR));
1643 parametersLen = len;
1645 strcpyW(wszParameters, sei_tmp.lpParameters);
1647 else
1648 *wszParameters = '\0';
1650 wszDir = dirBuffer;
1651 if (sei_tmp.lpDirectory)
1653 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1654 if (len > ARRAY_SIZE(dirBuffer))
1655 wszDir = heap_alloc(len * sizeof(WCHAR));
1656 strcpyW(wszDir, sei_tmp.lpDirectory);
1658 else
1659 *wszDir = '\0';
1661 /* adjust string pointers to point to the new buffers */
1662 sei_tmp.lpFile = wszApplicationName;
1663 sei_tmp.lpParameters = wszParameters;
1664 sei_tmp.lpDirectory = wszDir;
1666 if (sei_tmp.fMask & unsupportedFlags)
1668 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1671 /* process the IDList */
1672 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1674 IShellExecuteHookW* pSEH;
1676 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1678 if (SUCCEEDED(hr))
1680 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1682 IShellExecuteHookW_Release(pSEH);
1684 if (hr == S_OK) {
1685 heap_free(wszApplicationName);
1686 if (wszParameters != parametersBuffer)
1687 heap_free(wszParameters);
1688 if (wszDir != dirBuffer)
1689 heap_free(wszDir);
1690 return TRUE;
1694 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1695 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1698 if (sei_tmp.fMask & SEE_MASK_DOENVSUBST)
1700 WCHAR *tmp;
1702 tmp = expand_environment(sei_tmp.lpFile);
1703 if (!tmp)
1705 retval = SE_ERR_OOM;
1706 goto end;
1708 heap_free(wszApplicationName);
1709 sei_tmp.lpFile = wszApplicationName = tmp;
1711 tmp = expand_environment(sei_tmp.lpDirectory);
1712 if (!tmp)
1714 retval = SE_ERR_OOM;
1715 goto end;
1717 if (wszDir != dirBuffer)
1718 heap_free(wszDir);
1719 sei_tmp.lpDirectory = wszDir = tmp;
1722 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1724 sei->hInstApp = (HINSTANCE) 33;
1725 heap_free(wszApplicationName);
1726 if (wszParameters != parametersBuffer)
1727 heap_free(wszParameters);
1728 if (wszDir != dirBuffer)
1729 heap_free(wszDir);
1730 return TRUE;
1733 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1735 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1736 execfunc );
1737 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1738 do_error_dialog(retval, sei_tmp.hwnd);
1739 heap_free(wszApplicationName);
1740 if (wszParameters != parametersBuffer)
1741 heap_free(wszParameters);
1742 if (wszDir != dirBuffer)
1743 heap_free(wszDir);
1744 return retval > 32;
1747 /* Has the IDList not yet been translated? */
1748 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1750 SHELL_translate_idlist( &sei_tmp, wszParameters,
1751 parametersLen,
1752 wszApplicationName,
1753 dwApplicationNameLen );
1756 /* convert file URLs */
1757 if (UrlIsFileUrlW(sei_tmp.lpFile))
1759 LPWSTR buf;
1760 DWORD size;
1762 size = MAX_PATH;
1763 buf = heap_alloc(size * sizeof(WCHAR));
1764 if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0))) {
1765 heap_free(buf);
1766 return SE_ERR_OOM;
1769 heap_free(wszApplicationName);
1770 wszApplicationName = buf;
1771 sei_tmp.lpFile = wszApplicationName;
1773 else /* or expand environment strings (not both!) */
1775 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1776 if (len>0)
1778 LPWSTR buf;
1779 buf = heap_alloc((len + 1) * sizeof(WCHAR));
1781 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1782 heap_free(wszApplicationName);
1783 wszApplicationName = buf;
1785 sei_tmp.lpFile = wszApplicationName;
1789 if (*sei_tmp.lpDirectory)
1791 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1792 if (len > 0)
1794 LPWSTR buf;
1795 len++;
1796 buf = heap_alloc(len * sizeof(WCHAR));
1797 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1798 if (wszDir != dirBuffer)
1799 heap_free(wszDir);
1800 wszDir = buf;
1801 sei_tmp.lpDirectory = wszDir;
1805 /* Else, try to execute the filename */
1806 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1807 lpFile = sei_tmp.lpFile;
1808 wcmd = wcmdBuffer;
1809 strcpyW(wcmd, wszApplicationName);
1810 if (sei_tmp.lpDirectory)
1812 LPCWSTR searchPath[] = {
1813 sei_tmp.lpDirectory,
1814 NULL
1816 PathFindOnPathW(wcmd, searchPath);
1818 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszEmpty,
1819 wszApplicationName, NULL, &sei_tmp,
1820 sei, execfunc );
1821 if (retval > 32) {
1822 heap_free(wszApplicationName);
1823 if (wszParameters != parametersBuffer)
1824 heap_free(wszParameters);
1825 if (wszDir != dirBuffer)
1826 heap_free(wszDir);
1827 if (wcmd != wcmdBuffer)
1828 heap_free(wcmd);
1829 return TRUE;
1832 /* Else, try to find the executable */
1833 wcmd[0] = '\0';
1834 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1835 if (retval > 32) /* Found */
1837 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszKeyname,
1838 wszApplicationName, env, &sei_tmp,
1839 sei, execfunc );
1840 heap_free( env );
1842 else if (PathIsDirectoryW(lpFile))
1844 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r',0};
1845 static const WCHAR wQuote[] = {'"',0};
1846 WCHAR wExec[MAX_PATH];
1847 WCHAR * lpQuotedFile = heap_alloc( sizeof(WCHAR) * (strlenW(lpFile) + 3) );
1849 if (lpQuotedFile)
1851 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, wExplorer,
1852 wszOpen, wExec, MAX_PATH,
1853 NULL, &env, NULL, NULL );
1854 if (retval > 32)
1856 strcpyW(lpQuotedFile, wQuote);
1857 strcatW(lpQuotedFile, lpFile);
1858 strcatW(lpQuotedFile, wQuote);
1859 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1860 wszKeyname,
1861 wszApplicationName, env,
1862 &sei_tmp, sei, execfunc );
1863 heap_free( env );
1865 heap_free( lpQuotedFile );
1867 else
1868 retval = 0; /* Out of memory */
1870 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1872 retval = SHELL_execute_url( lpFile, wcmd, &sei_tmp, sei, execfunc );
1874 /* Check if file specified is in the form www.??????.*** */
1875 else if (!strncmpiW(lpFile, wWww, 3))
1877 /* if so, prefix lpFile with http:// and call ShellExecute */
1878 WCHAR lpstrTmpFile[256];
1879 strcpyW(lpstrTmpFile, wHttp);
1880 strcatW(lpstrTmpFile, lpFile);
1881 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1884 end:
1885 TRACE("retval %lu\n", retval);
1887 heap_free(wszApplicationName);
1888 if (wszParameters != parametersBuffer)
1889 heap_free(wszParameters);
1890 if (wszDir != dirBuffer)
1891 heap_free(wszDir);
1892 if (wcmd != wcmdBuffer)
1893 heap_free(wcmd);
1895 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1897 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1898 do_error_dialog(retval, sei_tmp.hwnd);
1899 return retval > 32;
1902 /*************************************************************************
1903 * ShellExecuteA [SHELL32.290]
1905 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
1906 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1908 SHELLEXECUTEINFOA sei;
1910 TRACE("%p,%s,%s,%s,%s,%d\n",
1911 hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
1912 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1914 sei.cbSize = sizeof(sei);
1915 sei.fMask = SEE_MASK_FLAG_NO_UI;
1916 sei.hwnd = hWnd;
1917 sei.lpVerb = lpVerb;
1918 sei.lpFile = lpFile;
1919 sei.lpParameters = lpParameters;
1920 sei.lpDirectory = lpDirectory;
1921 sei.nShow = iShowCmd;
1922 sei.lpIDList = 0;
1923 sei.lpClass = 0;
1924 sei.hkeyClass = 0;
1925 sei.dwHotKey = 0;
1926 sei.hProcess = 0;
1928 ShellExecuteExA (&sei);
1929 return sei.hInstApp;
1932 /*************************************************************************
1933 * ShellExecuteExA [SHELL32.292]
1936 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1938 SHELLEXECUTEINFOW seiW;
1939 BOOL ret;
1940 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1942 TRACE("%p\n", sei);
1944 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1946 if (sei->lpVerb)
1947 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1949 if (sei->lpFile)
1950 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1952 if (sei->lpParameters)
1953 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1955 if (sei->lpDirectory)
1956 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1958 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1959 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1960 else
1961 seiW.lpClass = NULL;
1963 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1965 sei->hInstApp = seiW.hInstApp;
1967 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1968 sei->hProcess = seiW.hProcess;
1970 SHFree(wVerb);
1971 SHFree(wFile);
1972 SHFree(wParameters);
1973 SHFree(wDirectory);
1974 SHFree(wClass);
1976 return ret;
1979 /*************************************************************************
1980 * ShellExecuteExW [SHELL32.293]
1983 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1985 return SHELL_execute( sei, SHELL_ExecuteW );
1988 /*************************************************************************
1989 * ShellExecuteW [SHELL32.294]
1990 * from shellapi.h
1991 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1992 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1994 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
1995 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1997 SHELLEXECUTEINFOW sei;
1999 TRACE("\n");
2000 sei.cbSize = sizeof(sei);
2001 sei.fMask = SEE_MASK_FLAG_NO_UI;
2002 sei.hwnd = hwnd;
2003 sei.lpVerb = lpVerb;
2004 sei.lpFile = lpFile;
2005 sei.lpParameters = lpParameters;
2006 sei.lpDirectory = lpDirectory;
2007 sei.nShow = nShowCmd;
2008 sei.lpIDList = 0;
2009 sei.lpClass = 0;
2010 sei.hkeyClass = 0;
2011 sei.dwHotKey = 0;
2012 sei.hProcess = 0;
2014 SHELL_execute( &sei, SHELL_ExecuteW );
2015 return sei.hInstApp;
2018 /*************************************************************************
2019 * WOWShellExecute [SHELL32.@]
2021 * FIXME: the callback function most likely doesn't work the same way on Windows.
2023 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb,LPCSTR lpFile,
2024 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
2026 SHELLEXECUTEINFOW seiW;
2027 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2028 HANDLE hProcess = 0;
2030 seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
2031 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2032 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2033 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2035 seiW.cbSize = sizeof(seiW);
2036 seiW.fMask = 0;
2037 seiW.hwnd = hWnd;
2038 seiW.nShow = iShowCmd;
2039 seiW.lpIDList = 0;
2040 seiW.lpClass = 0;
2041 seiW.hkeyClass = 0;
2042 seiW.dwHotKey = 0;
2043 seiW.hProcess = hProcess;
2045 SHELL_execute( &seiW, callback );
2047 SHFree(wVerb);
2048 SHFree(wFile);
2049 SHFree(wParameters);
2050 SHFree(wDirectory);
2051 return seiW.hInstApp;
2054 /*************************************************************************
2055 * ShellExec_RunDLLW [SHELL32.@]
2057 void WINAPI ShellExec_RunDLLW(HWND hwnd, HINSTANCE instance, WCHAR *cmdline, int cmdshow)
2059 BOOL in_single_quotes = FALSE, in_double_quotes = FALSE;
2060 WCHAR *args;
2062 TRACE("%p, %p, %s, %d\n", hwnd, instance, debugstr_w(cmdline), cmdshow);
2064 /* Replace the first whitespace character in the command line string with a
2065 null terminator to separate the program name from the program arguments */
2066 for (args = cmdline; *args; args++)
2068 switch (*args)
2070 case '\\':
2071 args++; /* skip the next character */
2072 break;
2073 case '\'':
2074 if (!in_double_quotes)
2075 in_single_quotes = !in_single_quotes;
2076 break;
2077 case '"':
2078 if (!in_single_quotes)
2079 in_double_quotes = !in_double_quotes;
2080 break;
2081 case ' ':
2082 case '\t':
2083 if (!in_single_quotes && !in_double_quotes)
2085 *args = 0;
2086 args++;
2087 goto execute;
2092 execute:
2093 ShellExecuteW(hwnd, NULL, cmdline, args, NULL, cmdshow);
2096 /*************************************************************************
2097 * ShellExec_RunDLLA [SHELL32.@]
2099 void WINAPI ShellExec_RunDLLA(HWND hwnd, HINSTANCE instance, CHAR *cmdline, int cmdshow)
2101 WCHAR *cmdlineW;
2102 ShellExec_RunDLLW(hwnd, instance, __SHCloneStrAtoW(&cmdlineW, cmdline), cmdshow);
2103 SHFree(cmdlineW);
2106 /*************************************************************************
2107 * OpenAs_RunDLLA [SHELL32.@]
2109 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2111 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2114 /*************************************************************************
2115 * OpenAs_RunDLLW [SHELL32.@]
2117 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2119 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2122 /*************************************************************************
2123 * RegenerateUserEnvironment [SHELL32.@]
2125 BOOL WINAPI RegenerateUserEnvironment(WCHAR *wunknown, BOOL bunknown)
2127 FIXME("stub: %p, %d\n", wunknown, bunknown);
2128 return FALSE;