uiautomationcore: Implement IUIAutomationElement::get_CachedName.
[wine.git] / dlls / shell32 / shlexec.c
blobb31f800e92a2489f209a85d53f12b8f87e40fdda
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 <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <assert.h>
29 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "winuser.h"
36 #include "shlwapi.h"
37 #include "ddeml.h"
39 #include "shell32_main.h"
40 #include "pidl.h"
41 #include "shresdef.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(exec);
47 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
49 typedef UINT_PTR (*SHELL_ExecuteW32)(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
50 const SHELLEXECUTEINFOW *sei, LPSHELLEXECUTEINFOW sei_out);
52 static inline BOOL isSpace(WCHAR c)
54 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
57 /***********************************************************************
58 * SHELL_ArgifyW [Internal]
60 * this function is supposed to expand the escape sequences found in the registry
61 * some diving reported that the following were used:
62 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
63 * %1 file
64 * %2 printer
65 * %3 driver
66 * %4 port
67 * %I address of a global item ID (explorer switch /idlist)
68 * %L seems to be %1 as long filename followed by the 8+3 variation
69 * %S ???
70 * %* all following parameters (see batfile)
73 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
75 WCHAR xlpFile[1024];
76 BOOL done = FALSE;
77 BOOL found_p1 = FALSE;
78 PWSTR res = out;
79 PCWSTR cmd;
80 DWORD used = 0;
82 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
83 debugstr_w(lpFile), pidl, args);
85 while (*fmt)
87 if (*fmt == '%')
89 switch (*++fmt)
91 case '\0':
92 case '%':
93 used++;
94 if (used < len)
95 *res++ = '%';
96 break;
98 case '2':
99 case '3':
100 case '4':
101 case '5':
102 case '6':
103 case '7':
104 case '8':
105 case '9':
106 case '0':
107 case '*':
108 if (args)
110 if (*fmt == '*')
112 used++;
113 while(*args)
115 used++;
116 if (used < len)
117 *res++ = *args++;
118 else
119 args++;
121 used++;
123 else
125 while(*args && !isSpace(*args))
127 used++;
128 if (used < len)
129 *res++ = *args++;
130 else
131 args++;
134 while(isSpace(*args))
135 ++args;
137 break;
139 /* else fall through */
140 case '1':
141 if (!done || (*fmt == '1'))
143 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
144 if (SearchPathW(NULL, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
145 cmd = xlpFile;
146 else
147 cmd = lpFile;
149 used += lstrlenW(cmd);
150 if (used < len)
152 lstrcpyW(res, cmd);
153 res += lstrlenW(cmd);
156 found_p1 = TRUE;
157 break;
160 * IE uses this a lot for activating things such as windows media
161 * player. This is not verified to be fully correct but it appears
162 * to work just fine.
164 case 'l':
165 case 'L':
166 if (lpFile) {
167 used += lstrlenW(lpFile);
168 if (used < len)
170 lstrcpyW(res, lpFile);
171 res += lstrlenW(lpFile);
174 found_p1 = TRUE;
175 break;
177 case 'i':
178 case 'I':
179 if (pidl) {
180 INT chars = 0;
181 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
182 * allowing a buffer of 100 should more than exceed all needs */
183 WCHAR buf[100];
184 LPVOID pv;
185 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
186 pv = SHLockShared(hmem, 0);
187 chars = swprintf(buf, ARRAY_SIZE(buf), L":%p", pv);
188 if (chars >= ARRAY_SIZE(buf))
189 ERR("pidl format buffer too small!\n");
190 used += chars;
191 if (used < len)
193 lstrcpyW(res,buf);
194 res += chars;
196 SHUnlockShared(pv);
198 found_p1 = TRUE;
199 break;
201 default:
203 * Check if this is an env-variable here...
206 /* Make sure that we have at least one more %.*/
207 if (wcschr(fmt, '%'))
209 WCHAR tmpBuffer[1024];
210 PWSTR tmpB = tmpBuffer;
211 WCHAR tmpEnvBuff[MAX_PATH];
212 DWORD envRet;
214 while (*fmt != '%')
215 *tmpB++ = *fmt++;
216 *tmpB++ = 0;
218 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
220 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
221 if (envRet == 0 || envRet > MAX_PATH)
223 used += lstrlenW(tmpBuffer);
224 if (used < len)
226 lstrcpyW( res, tmpBuffer );
227 res += lstrlenW(tmpBuffer);
230 else
232 used += lstrlenW(tmpEnvBuff);
233 if (used < len)
235 lstrcpyW( res, tmpEnvBuff );
236 res += lstrlenW(tmpEnvBuff);
240 done = TRUE;
241 break;
243 /* Don't skip past terminator (catch a single '%' at the end) */
244 if (*fmt != '\0')
246 fmt++;
249 else
251 used ++;
252 if (used < len)
253 *res++ = *fmt++;
254 else
255 fmt++;
259 used ++;
260 if (res - out < len)
261 *res = '\0';
262 else
263 out[len-1] = '\0';
265 TRACE("used %li of %i space\n",used,len);
266 if (out_len)
267 *out_len = used;
269 return found_p1;
272 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
274 STRRET strret;
275 IShellFolder* desktop;
277 HRESULT hr = SHGetDesktopFolder(&desktop);
279 if (SUCCEEDED(hr)) {
280 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
282 if (SUCCEEDED(hr))
283 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
285 IShellFolder_Release(desktop);
288 return hr;
291 /*************************************************************************
292 * SHELL_ExecuteW [Internal]
295 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
296 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
298 STARTUPINFOW startup;
299 PROCESS_INFORMATION info;
300 UINT_PTR retval = SE_ERR_NOASSOC;
301 UINT gcdret = 0;
302 WCHAR curdir[MAX_PATH];
303 DWORD dwCreationFlags;
305 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
307 /* make sure we don't fail the CreateProcess if the calling app passes in
308 * a bad working directory */
309 if (psei->lpDirectory && psei->lpDirectory[0])
311 /* ShellExecute specifies the command from psei->lpDirectory
312 * if present. Not from the current dir as CreateProcess does */
313 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir )))
314 if( !SetCurrentDirectoryW( psei->lpDirectory ))
315 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory ));
318 ZeroMemory(&startup,sizeof(STARTUPINFOW));
319 startup.cb = sizeof(STARTUPINFOW);
320 startup.dwFlags = STARTF_USESHOWWINDOW;
321 startup.wShowWindow = psei->nShow;
322 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
323 if (!(psei->fMask & SEE_MASK_NO_CONSOLE))
324 dwCreationFlags |= CREATE_NEW_CONSOLE;
325 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
326 NULL, &startup, &info))
328 /* Give 30 seconds to the app to come up, if desired. Probably only needed
329 when starting app immediately before making a DDE connection. */
330 if (shWait)
331 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
332 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
333 retval = 33;
334 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
335 psei_out->hProcess = info.hProcess;
336 else
337 CloseHandle( info.hProcess );
338 CloseHandle( info.hThread );
340 else if ((retval = GetLastError()) >= 32)
342 TRACE("CreateProcess returned error %Id\n", retval);
343 retval = ERROR_BAD_FORMAT;
346 TRACE("returning %Iu\n", retval);
348 psei_out->hInstApp = (HINSTANCE)retval;
349 if( gcdret )
350 if( !SetCurrentDirectoryW( curdir))
351 ERR("cannot return to directory %s\n", debugstr_w(curdir));
353 return retval;
357 /***********************************************************************
358 * SHELL_BuildEnvW [Internal]
360 * Build the environment for the new process, adding the specified
361 * path to the PATH variable. Returned pointer must be freed by caller.
363 static void *SHELL_BuildEnvW( const WCHAR *path )
365 WCHAR *strings, *new_env;
366 WCHAR *p, *p2;
367 int total = lstrlenW(path) + 1;
368 BOOL got_path = FALSE;
370 if (!(strings = GetEnvironmentStringsW())) return NULL;
371 p = strings;
372 while (*p)
374 int len = lstrlenW(p) + 1;
375 if (!wcsnicmp( p, L"PATH=", 5 )) got_path = TRUE;
376 total += len;
377 p += len;
379 if (!got_path) total += 5; /* we need to create PATH */
380 total++; /* terminating null */
382 if (!(new_env = malloc( total * sizeof(WCHAR) )))
384 FreeEnvironmentStringsW( strings );
385 return NULL;
387 p = strings;
388 p2 = new_env;
389 while (*p)
391 int len = lstrlenW(p) + 1;
392 memcpy( p2, p, len * sizeof(WCHAR) );
393 if (!wcsnicmp( p, L"PATH=", 5 ))
395 p2[len - 1] = ';';
396 lstrcpyW( p2 + len, path );
397 p2 += lstrlenW(path) + 1;
399 p += len;
400 p2 += len;
402 if (!got_path)
404 lstrcpyW( p2, L"PATH=" );
405 lstrcatW( p2, path );
406 p2 += lstrlenW(p2) + 1;
408 *p2 = 0;
409 FreeEnvironmentStringsW( strings );
410 return new_env;
414 /***********************************************************************
415 * SHELL_TryAppPathW [Internal]
417 * Helper function for SHELL_FindExecutable
418 * @param lpResult - pointer to a buffer of size MAX_PATH
419 * On entry: szName is a filename (probably without path separators).
420 * On exit: if szName found in "App Path", place full path in lpResult, and return true
422 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
424 HKEY hkApp = 0;
425 WCHAR buffer[1024];
426 LONG len;
427 LONG res;
428 BOOL found = FALSE;
430 if (env) *env = NULL;
431 wcscpy(buffer, L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
432 if (wcslen(buffer) + wcslen(szName) + 1 > ARRAY_SIZE(buffer))
434 WARN("Name is too big.\n");
435 return FALSE;
438 wcscat(buffer, szName);
439 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
440 if (res) goto end;
442 len = MAX_PATH*sizeof(WCHAR);
443 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
444 if (res) goto end;
445 found = TRUE;
447 if (env)
449 DWORD count = sizeof(buffer);
450 if (!RegQueryValueExW(hkApp, L"Path", NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
451 *env = SHELL_BuildEnvW( buffer );
454 end:
455 if (hkApp) RegCloseKey(hkApp);
456 return found;
459 /*************************************************************************
460 * SHELL_FindExecutableByVerb [Internal]
462 * called from SHELL_FindExecutable or SHELL_execute_class
463 * in/out:
464 * classname a buffer, big enough, to get the key name to do actually the
465 * command "WordPad.Document.1\\shell\\open\\command"
466 * passed as "WordPad.Document.1"
467 * in:
468 * lpVerb the operation on it (open)
469 * commandlen the size of command buffer (in bytes)
470 * out:
471 * command a buffer, to store the command to do the
472 * operation on the file
473 * key a buffer, big enough, to get the key name to do actually the
474 * command "WordPad.Document.1\\shell\\open\\command"
475 * Can be NULL
477 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
479 HKEY hkeyClass;
480 WCHAR verb[MAX_PATH];
482 if (*classname == '.')
484 /* Extension, default key value holds class name. Extension may also have
485 * empty class name and shell\verb\command subkey. */
486 WCHAR class[MAX_PATH];
487 LONG len;
489 len = sizeof(class);
490 if (!RegQueryValueW(HKEY_CLASSES_ROOT, classname, class, &len) && *class)
491 wcscpy(classname, class);
494 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
495 return SE_ERR_NOASSOC;
496 if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, ARRAY_SIZE(verb)))
497 return SE_ERR_NOASSOC;
498 RegCloseKey(hkeyClass);
500 /* Looking for ...buffer\shell\<verb>\command */
501 lstrcatW(classname, L"\\shell\\");
502 lstrcatW(classname, verb);
503 lstrcatW(classname, L"\\command");
505 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
506 &commandlen) == ERROR_SUCCESS)
508 commandlen /= sizeof(WCHAR);
509 if (key) lstrcpyW(key, classname);
510 #if 0
511 LPWSTR tmp;
512 WCHAR param[256];
513 LONG paramlen = sizeof(param);
515 /* FIXME: it seems all Windows version don't behave the same here.
516 * the doc states that this ddeexec information can be found after
517 * the exec names.
518 * on Win98, it doesn't appear, but I think it does on Win2k
520 /* Get the parameters needed by the application
521 from the associated ddeexec key */
522 tmp = wcsstr(classname, L"\\command");
523 tmp[0] = '\0';
524 lstrcatW(classname, wDdeexec);
525 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
526 &paramlen) == ERROR_SUCCESS)
528 paramlen /= sizeof(WCHAR);
529 lstrcatW(command, L" ");
530 lstrcatW(command, param);
531 commandlen += paramlen;
533 #endif
535 command[commandlen] = '\0';
537 return 33; /* FIXME see SHELL_FindExecutable() */
540 return SE_ERR_NOASSOC;
543 /*************************************************************************
544 * SHELL_FindExecutable [Internal]
546 * Utility for code sharing between FindExecutable and ShellExecute
547 * in:
548 * lpFile the name of a file
549 * lpVerb the operation on it (open)
550 * out:
551 * lpResult a buffer, big enough :-(, to store the command to do the
552 * operation on the file
553 * key a buffer, big enough, to get the key name to do actually the
554 * command (it'll be used afterwards for more information
555 * on the operation)
557 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
558 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
560 WCHAR *extension = NULL; /* pointer to file extension */
561 WCHAR classname[256]; /* registry name for this file type */
562 LONG classnamelen = sizeof(classname); /* length of above */
563 WCHAR command[1024]; /* command from registry */
564 WCHAR wBuffer[256]; /* Used to GetProfileString */
565 UINT retval = SE_ERR_NOASSOC;
566 WCHAR *tok; /* token pointer */
567 WCHAR xlpFile[256]; /* result of SearchPath */
568 DWORD attribs; /* file attributes */
570 TRACE("%s\n", debugstr_w(lpFile));
572 if (!lpResult)
573 return ERROR_INVALID_PARAMETER;
575 xlpFile[0] = '\0';
576 lpResult[0] = '\0'; /* Start off with an empty return string */
577 if (key) *key = '\0';
579 /* trap NULL parameters on entry */
580 if (!lpFile)
582 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
583 debugstr_w(lpFile), debugstr_w(lpResult));
584 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
587 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
589 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
590 return 33;
593 if (SearchPathW(lpPath, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
595 TRACE("SearchPathW returned non-zero\n");
596 lpFile = xlpFile;
597 /* The file was found in the application-supplied default directory (or the system search path) */
599 else if (lpPath && SearchPathW(NULL, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
601 TRACE("SearchPathW returned non-zero\n");
602 lpFile = xlpFile;
603 /* The file was found in one of the directories in the system-wide search path */
606 attribs = GetFileAttributesW(lpFile);
607 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
609 lstrcpyW(classname, L"Folder");
611 else
613 /* Did we get something? Anything? */
614 if (xlpFile[0]==0)
616 TRACE("Returning SE_ERR_FNF\n");
617 return SE_ERR_FNF;
619 /* First thing we need is the file's extension */
620 extension = wcsrchr(xlpFile, '.'); /* Assume last "." is the one; */
621 /* File->Run in progman uses */
622 /* .\FILE.EXE :( */
623 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
625 if (extension == NULL || extension[1]==0)
627 WARN("Returning SE_ERR_NOASSOC\n");
628 return SE_ERR_NOASSOC;
631 /* Three places to check: */
632 /* 1. win.ini, [windows], programs (NB no leading '.') */
633 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
634 /* 3. win.ini, [extensions], extension (NB no leading '.' */
635 /* All I know of the order is that registry is checked before */
636 /* extensions; however, it'd make sense to check the programs */
637 /* section first, so that's what happens here. */
639 /* See if it's a program - if GetProfileString fails, we skip this
640 * section. Actually, if GetProfileString fails, we've probably
641 * got a lot more to worry about than running a program... */
642 if (GetProfileStringW(L"windows", L"programs", L"exe pif bat cmd com", wBuffer, ARRAY_SIZE(wBuffer)) > 0)
644 CharLowerW(wBuffer);
645 tok = wBuffer;
646 while (*tok)
648 WCHAR *p = tok;
649 while (*p && *p != ' ' && *p != '\t') p++;
650 if (*p)
652 *p++ = 0;
653 while (*p == ' ' || *p == '\t') p++;
656 if (wcsicmp(tok, &extension[1]) == 0) /* have to skip the leading "." */
658 lstrcpyW(lpResult, xlpFile);
659 /* Need to perhaps check that the file has a path
660 * attached */
661 TRACE("found %s\n", debugstr_w(lpResult));
662 return 33;
663 /* Greater than 32 to indicate success */
665 tok = p;
669 /* Check registry */
670 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
671 &classnamelen) == ERROR_SUCCESS)
673 classnamelen /= sizeof(WCHAR);
674 if (classnamelen == ARRAY_SIZE(classname))
675 classnamelen--;
676 classname[classnamelen] = '\0';
677 TRACE("File type: %s\n", debugstr_w(classname));
679 else
681 *classname = '\0';
685 if (*classname)
687 /* pass the verb string to SHELL_FindExecutableByVerb() */
688 retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
690 if (retval > 32)
692 DWORD finishedLen;
693 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
694 if (finishedLen > resultLen)
695 ERR("Argify buffer not large enough.. truncated\n");
697 /* Remove double quotation marks and command line arguments */
698 if (*lpResult == '"')
700 WCHAR *p = lpResult;
701 while (*(p + 1) != '"')
703 *p = *(p + 1);
704 p++;
706 *p = '\0';
708 else
710 /* Truncate on first space */
711 WCHAR *p = lpResult;
712 while (*p != ' ' && *p != '\0')
713 p++;
714 *p='\0';
718 else /* Check win.ini */
720 /* Toss the leading dot */
721 extension++;
722 if (GetProfileStringW(L"extensions", extension, L"", command, ARRAY_SIZE(command)) > 0)
724 if (*command)
726 lstrcpyW(lpResult, command);
727 tok = wcschr(lpResult, '^'); /* should be ^.extension? */
728 if (tok != NULL)
730 tok[0] = '\0';
731 lstrcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
732 tok = wcschr(command, '^'); /* see above */
733 if ((tok != NULL) && (lstrlenW(tok)>5))
735 lstrcatW(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, %08Ix, %08Ix\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 WCHAR regkey[256];
776 WCHAR * endkey = regkey + lstrlenW(key);
777 WCHAR app[256], topic[256], ifexec[256], static_res[256];
778 WCHAR * dynamic_res=NULL;
779 WCHAR * res;
780 LONG applen, topiclen, ifexeclen;
781 WCHAR * exec;
782 DWORD ddeInst = 0;
783 DWORD tid;
784 DWORD resultLen, endkeyLen;
785 HSZ hszApp, hszTopic;
786 HCONV hConv;
787 HDDEDATA hDdeData;
788 unsigned ret = SE_ERR_NOASSOC;
789 BOOL unicode = !(GetVersion() & 0x80000000);
791 if (lstrlenW(key) + 1 > ARRAY_SIZE(regkey))
793 FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
794 return 2;
796 lstrcpyW(regkey, key);
797 endkeyLen = ARRAY_SIZE(regkey) - (endkey - regkey);
798 if (lstrlenW(L"\\application") + 1 > endkeyLen)
800 FIXME("endkey overruns buffer\n");
801 return 2;
803 lstrcpyW(endkey, L"\\application");
804 applen = sizeof(app);
805 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
807 WCHAR command[1024], fullpath[MAX_PATH];
808 LPWSTR ptr = NULL;
809 DWORD ret = 0;
811 /* Get application command from start string and find filename of application */
812 if (*start == '"')
814 if (lstrlenW(start + 1) + 1 > ARRAY_SIZE(command))
816 FIXME("size of input parameter %s larger than buffer\n",
817 debugstr_w(start + 1));
818 return 2;
820 lstrcpyW(command, start+1);
821 if ((ptr = wcschr(command, '"')))
822 *ptr = 0;
823 ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
825 else
827 LPCWSTR p;
828 LPWSTR space;
829 for (p=start; (space=wcschr(p, ' ')); p=space+1)
831 int idx = space-start;
832 memcpy(command, start, idx*sizeof(WCHAR));
833 command[idx] = '\0';
834 if ((ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr)))
835 break;
837 if (!ret)
838 ret = SearchPathW(NULL, start, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
841 if (!ret)
843 ERR("Unable to find application path for command %s\n", debugstr_w(start));
844 return ERROR_ACCESS_DENIED;
846 if (lstrlenW(ptr) + 1 > ARRAY_SIZE(app))
848 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
849 return 2;
851 lstrcpyW(app, ptr);
853 /* Remove extensions (including .so) */
854 ptr = app + lstrlenW(app) - 3;
855 if (ptr > app && !wcscmp(ptr, L".so"))
856 *ptr = 0;
858 ptr = wcsrchr(app, '.');
859 assert(ptr);
860 *ptr = 0;
863 if (lstrlenW(L"\\topic") + 1 > endkeyLen)
865 FIXME("endkey overruns buffer\n");
866 return 2;
868 lstrcpyW(endkey, L"\\topic");
869 topiclen = sizeof(topic);
870 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
871 lstrcpyW(topic, L"System");
873 if (unicode)
875 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
876 return 2;
878 else
880 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
881 return 2;
884 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
885 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
887 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
888 exec = ddeexec;
889 if (!hConv)
891 TRACE("Launching %s\n", debugstr_w(start));
892 ret = execfunc(start, env, TRUE, psei, psei_out);
893 if (ret <= 32)
895 TRACE("Couldn't launch\n");
896 goto error;
898 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
899 if (!hConv)
901 TRACE("Couldn't connect. ret=%d\n", ret);
902 DdeUninitialize(ddeInst);
903 SetLastError(ERROR_DDE_FAIL);
904 return 30; /* whatever */
906 if (lstrlenW(L"\\ifexec") + 1 > endkeyLen)
908 FIXME("endkey overruns buffer\n");
909 return 2;
911 lstrcpyW(endkey, L"\\ifexec");
912 ifexeclen = sizeof(ifexec);
913 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
915 exec = ifexec;
919 SHELL_ArgifyW(static_res, ARRAY_SIZE(static_res), exec, lpFile, pidl, szCommandline, &resultLen);
920 if (resultLen > ARRAY_SIZE(static_res))
922 res = dynamic_res = malloc(resultLen * sizeof(WCHAR));
923 SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL);
925 else
926 res = static_res;
927 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
929 /* It's documented in the KB 330337 that IE has a bug and returns
930 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
932 if (unicode)
933 hDdeData = DdeClientTransaction((LPBYTE)res, (lstrlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
934 XTYP_EXECUTE, 30000, &tid);
935 else
937 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
938 char *resA = malloc(lenA);
939 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
940 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
941 XTYP_EXECUTE, 10000, &tid );
942 free(resA);
944 if (hDdeData)
945 DdeFreeDataHandle(hDdeData);
946 else
947 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
948 ret = 33;
950 free(dynamic_res);
952 DdeDisconnect(hConv);
954 error:
955 DdeUninitialize(ddeInst);
957 return ret;
960 /*************************************************************************
961 * execute_from_key [Internal]
963 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
964 LPCWSTR executable_name,
965 SHELL_ExecuteW32 execfunc,
966 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
968 WCHAR cmd[256], parambuffer[1024], ddeexec[256], *param = parambuffer;
969 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
970 UINT_PTR retval = SE_ERR_NOASSOC;
971 DWORD resultLen;
972 LPWSTR tmp;
974 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
975 debugstr_w(szCommandline), debugstr_w(executable_name));
977 cmd[0] = '\0';
978 param[0] = '\0';
980 /* Get the application from the registry */
981 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
983 TRACE("got cmd: %s\n", debugstr_w(cmd));
985 /* Is there a replace() function anywhere? */
986 cmdlen /= sizeof(WCHAR);
987 if (cmdlen >= ARRAY_SIZE(cmd))
988 cmdlen = ARRAY_SIZE(cmd) - 1;
989 cmd[cmdlen] = '\0';
990 SHELL_ArgifyW(param, ARRAY_SIZE(parambuffer), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
991 if (resultLen > ARRAY_SIZE(parambuffer))
993 if ((param = malloc(resultLen * sizeof(*param))))
994 SHELL_ArgifyW(param, resultLen, cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
998 /* Get the parameters needed by the application
999 from the associated ddeexec key */
1000 tmp = wcsstr(key, L"command");
1001 assert(tmp);
1002 lstrcpyW(tmp, L"ddeexec");
1004 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
1006 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
1007 if (!param[0]) lstrcpyW(param, executable_name);
1008 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
1010 else if (param[0])
1012 TRACE("executing: %s\n", debugstr_w(param));
1013 retval = execfunc(param, env, FALSE, psei, psei_out);
1015 else
1016 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1018 if (param != parambuffer) free(param);
1019 return retval;
1022 /*************************************************************************
1023 * FindExecutableA [SHELL32.@]
1025 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1027 HINSTANCE retval;
1028 WCHAR *wFile = NULL, *wDirectory = NULL;
1029 WCHAR wResult[MAX_PATH];
1031 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1032 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1034 retval = FindExecutableW(wFile, wDirectory, wResult);
1035 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1036 SHFree( wFile );
1037 SHFree( wDirectory );
1039 TRACE("returning %s\n", lpResult);
1040 return retval;
1043 /*************************************************************************
1044 * FindExecutableW [SHELL32.@]
1046 * This function returns the executable associated with the specified file
1047 * for the default verb.
1049 * PARAMS
1050 * lpFile [I] The file to find the association for. This must refer to
1051 * an existing file otherwise FindExecutable fails and returns
1052 * SE_ERR_FNF.
1053 * lpResult [O] Points to a buffer into which the executable path is
1054 * copied. This parameter must not be NULL otherwise
1055 * FindExecutable() segfaults. The buffer must be of size at
1056 * least MAX_PATH characters.
1058 * RETURNS
1059 * A value greater than 32 on success, less than or equal to 32 otherwise.
1060 * See the SE_ERR_* constants.
1062 * NOTES
1063 * On Windows XP and 2003, FindExecutable() seems to first convert the
1064 * filename into 8.3 format, thus taking into account only the first three
1065 * characters of the extension, and expects to find an association for those.
1066 * However other Windows versions behave sanely.
1068 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1070 UINT_PTR retval = SE_ERR_NOASSOC;
1071 WCHAR old_dir[1024];
1072 WCHAR res[MAX_PATH];
1074 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1076 lpResult[0] = '\0'; /* Start off with an empty return string */
1077 if (lpFile == NULL)
1078 return (HINSTANCE)SE_ERR_FNF;
1080 if (lpDirectory)
1082 GetCurrentDirectoryW(ARRAY_SIZE(old_dir), old_dir);
1083 SetCurrentDirectoryW(lpDirectory);
1086 retval = SHELL_FindExecutable(lpDirectory, lpFile, L"open", res, MAX_PATH, NULL, NULL, NULL, NULL);
1088 if (retval > 32)
1089 lstrcpyW(lpResult, res);
1091 TRACE("returning %s\n", debugstr_w(lpResult));
1092 if (lpDirectory)
1093 SetCurrentDirectoryW(old_dir);
1094 return (HINSTANCE)retval;
1097 /* FIXME: is this already implemented somewhere else? */
1098 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1100 LPCWSTR ext = NULL, lpClass = NULL;
1101 LPWSTR cls = NULL;
1102 DWORD type = 0, sz = 0;
1103 HKEY hkey = 0;
1104 LONG r;
1106 if (sei->fMask & SEE_MASK_CLASSALL)
1107 return sei->hkeyClass;
1109 if (sei->fMask & SEE_MASK_CLASSNAME)
1110 lpClass = sei->lpClass;
1111 else
1113 ext = PathFindExtensionW( sei->lpFile );
1114 TRACE("ext = %s\n", debugstr_w( ext ) );
1115 if (!ext)
1116 return hkey;
1118 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1119 if (r != ERROR_SUCCESS )
1120 return hkey;
1122 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1123 if ( r == ERROR_SUCCESS && type == REG_SZ )
1125 sz += sizeof (WCHAR);
1126 cls = malloc( sz );
1127 cls[0] = 0;
1128 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1131 RegCloseKey( hkey );
1132 lpClass = cls;
1135 TRACE("class = %s\n", debugstr_w(lpClass) );
1137 hkey = 0;
1138 if ( lpClass )
1139 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1141 free( cls );
1143 return hkey;
1146 static HRESULT shellex_get_dataobj( LPSHELLEXECUTEINFOW sei, IDataObject **dataobj )
1148 LPCITEMIDLIST pidllast = NULL;
1149 IShellFolder *shf = NULL;
1150 LPITEMIDLIST pidl = NULL;
1151 HRESULT r = SE_ERR_DLLNOTFOUND;
1153 if (sei->fMask & SEE_MASK_CLASSALL)
1154 pidl = sei->lpIDList;
1155 else
1157 WCHAR fullpath[MAX_PATH];
1158 BOOL ret;
1160 fullpath[0] = 0;
1161 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1162 if (!ret)
1163 goto end;
1165 pidl = ILCreateFromPathW( fullpath );
1168 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1169 if ( FAILED( r ) )
1170 goto end;
1172 r = IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1173 &IID_IDataObject, NULL, (void**)dataobj );
1175 end:
1176 if ( pidl != sei->lpIDList )
1177 ILFree( pidl );
1178 if ( shf )
1179 IShellFolder_Release( shf );
1180 return r;
1183 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1184 LPSHELLEXECUTEINFOW sei )
1186 IContextMenu *cm = NULL;
1187 CMINVOKECOMMANDINFOEX ici;
1188 MENUITEMINFOW info;
1189 WCHAR string[0x80];
1190 INT i, n, def = -1;
1191 HMENU hmenu = 0;
1192 HRESULT r;
1194 TRACE("%p %p\n", obj, sei );
1196 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1197 if ( FAILED( r ) )
1198 return r;
1200 hmenu = CreateMenu();
1201 if ( !hmenu )
1202 goto end;
1204 /* the number of the last menu added is returned in r */
1205 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1206 if ( FAILED( r ) )
1207 goto end;
1209 n = GetMenuItemCount( hmenu );
1210 for ( i = 0; i < n; i++ )
1212 memset( &info, 0, sizeof info );
1213 info.cbSize = sizeof info;
1214 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1215 info.dwTypeData = string;
1216 info.cch = ARRAY_SIZE(string);
1217 string[0] = 0;
1218 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1220 TRACE("menu %d %s %08x %08Ix %08x %08x\n", i, debugstr_w(string),
1221 info.fState, info.dwItemData, info.fType, info.wID );
1222 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1223 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1225 def = i;
1226 break;
1230 r = E_FAIL;
1231 if ( def == -1 )
1232 goto end;
1234 memset( &ici, 0, sizeof ici );
1235 ici.cbSize = sizeof ici;
1236 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE|SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1237 ici.nShow = sei->nShow;
1238 ici.lpVerb = MAKEINTRESOURCEA( def );
1239 ici.hwnd = sei->hwnd;
1240 ici.lpParametersW = sei->lpParameters;
1242 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1244 TRACE("invoke command returned %08lx\n", r );
1246 end:
1247 if ( hmenu )
1248 DestroyMenu( hmenu );
1249 if ( cm )
1250 IContextMenu_Release( cm );
1251 return r;
1254 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1256 IDataObject *dataobj = NULL;
1257 IObjectWithSite *ows = NULL;
1258 IShellExtInit *obj = NULL;
1259 HRESULT r;
1261 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1263 r = CoInitialize( NULL );
1264 if ( FAILED( r ) )
1265 goto end;
1267 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1268 &IID_IShellExtInit, (LPVOID*)&obj );
1269 if ( FAILED( r ) )
1271 ERR("failed %08lx\n", r );
1272 goto end;
1275 r = shellex_get_dataobj( sei, &dataobj );
1276 if ( FAILED( r ) )
1278 ERR("failed to get data object\n");
1279 goto end;
1282 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1283 if ( FAILED( r ) )
1284 goto end;
1286 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1287 if ( FAILED( r ) )
1288 goto end;
1290 IObjectWithSite_SetSite( ows, NULL );
1292 r = shellex_run_context_menu_default( obj, sei );
1294 end:
1295 if ( ows )
1296 IObjectWithSite_Release( ows );
1297 if ( dataobj )
1298 IDataObject_Release( dataobj );
1299 if ( obj )
1300 IShellExtInit_Release( obj );
1301 CoUninitialize();
1302 return r;
1306 /*************************************************************************
1307 * ShellExecute_FromContextMenu [Internal]
1309 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1311 HKEY hkey, hkeycm = 0;
1312 WCHAR szguid[39];
1313 HRESULT hr;
1314 GUID guid;
1315 DWORD i;
1316 LONG r;
1318 TRACE("%s\n", debugstr_w(sei->lpFile) );
1320 hkey = ShellExecute_GetClassKey( sei );
1321 if ( !hkey )
1322 return ERROR_FUNCTION_FAILED;
1324 r = RegOpenKeyW( hkey, L"shellex\\ContextMenuHandlers", &hkeycm );
1325 if ( r == ERROR_SUCCESS )
1327 i = 0;
1328 while ( 1 )
1330 r = RegEnumKeyW( hkeycm, i++, szguid, ARRAY_SIZE(szguid));
1331 if ( r != ERROR_SUCCESS )
1332 break;
1334 hr = CLSIDFromString( szguid, &guid );
1335 if (SUCCEEDED(hr))
1337 /* stop at the first one that succeeds in running */
1338 hr = shellex_load_object_and_run( hkey, &guid, sei );
1339 if ( SUCCEEDED( hr ) )
1340 break;
1343 RegCloseKey( hkeycm );
1346 if ( hkey != sei->hkeyClass )
1347 RegCloseKey( hkey );
1348 return r;
1351 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 );
1353 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1355 WCHAR execCmd[1024], classname[1024];
1356 /* launch a document by fileclass like 'WordPad.Document.1' */
1357 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1358 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1359 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1360 DWORD resultLen;
1361 BOOL done;
1362 UINT_PTR rslt;
1364 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1365 if (cmask != SEE_MASK_CLASSNAME)
1367 WCHAR wcmd[1024];
1368 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1369 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1370 psei->lpVerb,
1371 execCmd, sizeof(execCmd));
1373 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1374 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1376 wcmd[0] = '\0';
1377 done = SHELL_ArgifyW(wcmd, ARRAY_SIZE(wcmd), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1378 if (!done && wszApplicationName[0])
1380 lstrcatW(wcmd, L" ");
1381 if (*wszApplicationName != '"')
1383 lstrcatW(wcmd, L"\"");
1384 lstrcatW(wcmd, wszApplicationName);
1385 lstrcatW(wcmd, L"\"");
1387 else
1388 lstrcatW(wcmd, wszApplicationName);
1390 if (resultLen > ARRAY_SIZE(wcmd))
1391 ERR("Argify buffer not large enough... truncating\n");
1392 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1395 lstrcpyW(classname, psei->lpClass);
1396 rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1398 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1399 if (33 > rslt)
1400 return rslt;
1401 rslt = SHELL_quote_and_execute( execCmd, L"", classname,
1402 wszApplicationName, NULL, psei,
1403 psei_out, execfunc );
1404 return rslt;
1407 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1409 WCHAR buffer[MAX_PATH];
1411 /* last chance to translate IDList: now also allow CLSID paths */
1412 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, ARRAY_SIZE(buffer)))) {
1413 if (buffer[0]==':' && buffer[1]==':') {
1414 /* open shell folder for the specified class GUID */
1415 if (lstrlenW(buffer) + 1 > parametersLen)
1416 ERR("parameters len exceeds buffer size (%i > %li), truncating\n",
1417 lstrlenW(buffer) + 1, parametersLen);
1418 lstrcpynW(wszParameters, buffer, parametersLen);
1419 if (lstrlenW(L"explorer.exe") > dwApplicationNameLen)
1420 ERR("application len exceeds buffer size (%li), truncating\n",
1421 dwApplicationNameLen);
1422 lstrcpynW(wszApplicationName, L"explorer.exe", dwApplicationNameLen);
1424 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1425 } else {
1426 WCHAR target[MAX_PATH];
1427 DWORD attribs;
1428 DWORD resultLen;
1429 /* Check if we're executing a directory and if so use the
1430 handler for the Folder class */
1431 lstrcpyW(target, buffer);
1432 attribs = GetFileAttributesW(buffer);
1433 if (attribs != INVALID_FILE_ATTRIBUTES &&
1434 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1435 HCR_GetExecuteCommandW(0, L"Folder",
1436 sei->lpVerb,
1437 buffer, sizeof(buffer))) {
1438 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1439 buffer, target, sei->lpIDList, NULL, &resultLen);
1440 if (resultLen > dwApplicationNameLen)
1441 ERR("Argify buffer not large enough... truncating\n");
1443 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1448 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 )
1450 UINT_PTR retval;
1451 DWORD len;
1452 WCHAR *wszQuotedCmd;
1454 /* Length of quotes plus length of command plus NULL terminator */
1455 len = 2 + lstrlenW(wcmd) + 1;
1456 if (wszParameters[0])
1458 /* Length of space plus length of parameters */
1459 len += 1 + lstrlenW(wszParameters);
1461 wszQuotedCmd = malloc(len * sizeof(WCHAR));
1462 /* Must quote to handle case where cmd contains spaces,
1463 * else security hole if malicious user creates executable file "C:\\Program"
1465 lstrcpyW(wszQuotedCmd, L"\"");
1466 lstrcatW(wszQuotedCmd, wcmd);
1467 lstrcatW(wszQuotedCmd, L"\"");
1468 if (wszParameters[0]) {
1469 lstrcatW(wszQuotedCmd, L" ");
1470 lstrcatW(wszQuotedCmd, wszParameters);
1472 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1473 if (*wszKeyname)
1474 retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1475 else
1476 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1477 free(wszQuotedCmd);
1478 return retval;
1481 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1483 UINT_PTR retval;
1484 WCHAR *lpstrProtocol;
1485 LPCWSTR lpstrRes;
1486 INT iSize;
1487 DWORD len;
1489 lpstrRes = wcschr(lpFile, ':');
1490 if (lpstrRes)
1491 iSize = lpstrRes - lpFile;
1492 else
1493 iSize = lstrlenW(lpFile);
1495 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1496 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1497 len = iSize + lstrlenW(L"\\shell\\") + lstrlenW(L"\\command") + 1;
1498 if (psei->lpVerb && *psei->lpVerb)
1499 len += lstrlenW(psei->lpVerb);
1500 else
1501 len += lstrlenW(L"open");
1502 lpstrProtocol = malloc(len * sizeof(WCHAR));
1503 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1504 lpstrProtocol[iSize] = '\0';
1505 lstrcatW(lpstrProtocol, L"\\shell\\");
1506 lstrcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb: L"open");
1507 lstrcatW(lpstrProtocol, L"\\command");
1509 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1510 wcmd, execfunc, psei, psei_out);
1511 free(lpstrProtocol);
1512 return retval;
1515 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1517 WCHAR msg[2048];
1518 int error_code=GetLastError();
1520 if (retval == SE_ERR_NOASSOC)
1521 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, ARRAY_SIZE(msg));
1522 else
1523 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, ARRAY_SIZE(msg), NULL);
1525 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1528 static WCHAR *expand_environment( const WCHAR *str )
1530 WCHAR *buf;
1531 DWORD len;
1533 len = ExpandEnvironmentStringsW(str, NULL, 0);
1534 if (!len) return NULL;
1536 buf = malloc(len * sizeof(WCHAR));
1537 if (!buf) return NULL;
1539 len = ExpandEnvironmentStringsW(str, buf, len);
1540 if (!len)
1542 free(buf);
1543 return NULL;
1545 return buf;
1548 /*************************************************************************
1549 * SHELL_execute [Internal]
1551 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1553 static const DWORD unsupportedFlags =
1554 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1555 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1556 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1558 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1559 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd = NULL;
1560 DWORD dwApplicationNameLen = MAX_PATH+2;
1561 DWORD parametersLen = ARRAY_SIZE(parametersBuffer);
1562 DWORD wcmdLen = ARRAY_SIZE(wcmdBuffer);
1563 DWORD len;
1564 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1565 WCHAR *env;
1566 WCHAR wszKeyname[256];
1567 LPCWSTR lpFile;
1568 UINT_PTR retval = SE_ERR_NOASSOC;
1570 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1571 sei_tmp = *sei;
1573 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1574 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1575 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1576 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1577 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1578 debugstr_w(sei_tmp.lpClass) : "not used");
1580 sei->hProcess = NULL;
1582 /* make copies of all path/command strings */
1583 if (!sei_tmp.lpFile)
1585 wszApplicationName = malloc(dwApplicationNameLen * sizeof(WCHAR));
1586 *wszApplicationName = '\0';
1588 else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = lstrlenW(sei_tmp.lpFile))-1] == '\"')
1590 if(len-1 >= dwApplicationNameLen) dwApplicationNameLen = len;
1591 wszApplicationName = malloc(dwApplicationNameLen * sizeof(WCHAR));
1592 memcpy(wszApplicationName, sei_tmp.lpFile+1, len*sizeof(WCHAR));
1593 if(len > 2)
1594 wszApplicationName[len-2] = '\0';
1595 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1596 } else {
1597 DWORD l = lstrlenW(sei_tmp.lpFile)+1;
1598 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1599 wszApplicationName = malloc(dwApplicationNameLen * sizeof(WCHAR));
1600 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1603 wszParameters = parametersBuffer;
1604 if (sei_tmp.lpParameters)
1606 len = lstrlenW(sei_tmp.lpParameters) + 1;
1607 if (len > parametersLen)
1609 wszParameters = malloc(len * sizeof(WCHAR));
1610 parametersLen = len;
1612 lstrcpyW(wszParameters, sei_tmp.lpParameters);
1614 else
1615 *wszParameters = '\0';
1617 wszDir = dirBuffer;
1618 if (sei_tmp.lpDirectory)
1620 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1621 if (len > ARRAY_SIZE(dirBuffer))
1622 wszDir = malloc(len * sizeof(WCHAR));
1623 lstrcpyW(wszDir, sei_tmp.lpDirectory);
1625 else
1626 *wszDir = '\0';
1628 /* adjust string pointers to point to the new buffers */
1629 sei_tmp.lpFile = wszApplicationName;
1630 sei_tmp.lpParameters = wszParameters;
1631 sei_tmp.lpDirectory = wszDir;
1633 if (sei_tmp.fMask & unsupportedFlags)
1635 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask & unsupportedFlags);
1638 /* process the IDList */
1639 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1641 IShellExecuteHookW* pSEH;
1643 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1645 if (SUCCEEDED(hr))
1647 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1649 IShellExecuteHookW_Release(pSEH);
1651 if (hr == S_OK) {
1652 free(wszApplicationName);
1653 if (wszParameters != parametersBuffer)
1654 free(wszParameters);
1655 if (wszDir != dirBuffer)
1656 free(wszDir);
1657 return TRUE;
1661 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1662 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1665 if (sei_tmp.fMask & SEE_MASK_DOENVSUBST)
1667 WCHAR *tmp;
1669 tmp = expand_environment(sei_tmp.lpFile);
1670 if (!tmp)
1672 retval = SE_ERR_OOM;
1673 goto end;
1675 free(wszApplicationName);
1676 sei_tmp.lpFile = wszApplicationName = tmp;
1678 tmp = expand_environment(sei_tmp.lpDirectory);
1679 if (!tmp)
1681 retval = SE_ERR_OOM;
1682 goto end;
1684 if (wszDir != dirBuffer)
1685 free(wszDir);
1686 sei_tmp.lpDirectory = wszDir = tmp;
1689 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1691 sei->hInstApp = (HINSTANCE) 33;
1692 free(wszApplicationName);
1693 if (wszParameters != parametersBuffer)
1694 free(wszParameters);
1695 if (wszDir != dirBuffer)
1696 free(wszDir);
1697 return TRUE;
1700 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1702 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1703 execfunc );
1704 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1705 do_error_dialog(retval, sei_tmp.hwnd);
1706 free(wszApplicationName);
1707 if (wszParameters != parametersBuffer)
1708 free(wszParameters);
1709 if (wszDir != dirBuffer)
1710 free(wszDir);
1711 return retval > 32;
1714 /* Has the IDList not yet been translated? */
1715 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1717 SHELL_translate_idlist( &sei_tmp, wszParameters,
1718 parametersLen,
1719 wszApplicationName,
1720 dwApplicationNameLen );
1723 /* convert file URLs */
1724 if (UrlIsFileUrlW(sei_tmp.lpFile))
1726 LPWSTR buf;
1727 DWORD size;
1729 size = MAX_PATH;
1730 buf = malloc(size * sizeof(WCHAR));
1731 if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0))) {
1732 free(buf);
1733 retval = SE_ERR_OOM;
1734 goto end;
1737 free(wszApplicationName);
1738 wszApplicationName = buf;
1739 sei_tmp.lpFile = wszApplicationName;
1741 else /* or expand environment strings (not both!) */
1743 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1744 if (len>0)
1746 LPWSTR buf;
1747 buf = malloc((len + 1) * sizeof(WCHAR));
1749 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1750 free(wszApplicationName);
1751 wszApplicationName = buf;
1753 sei_tmp.lpFile = wszApplicationName;
1757 if (*sei_tmp.lpDirectory)
1759 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1760 if (len > 0)
1762 LPWSTR buf;
1763 len++;
1764 buf = malloc(len * sizeof(WCHAR));
1765 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1766 if (wszDir != dirBuffer)
1767 free(wszDir);
1768 wszDir = buf;
1769 sei_tmp.lpDirectory = wszDir;
1773 /* Else, try to execute the filename */
1774 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1775 lpFile = sei_tmp.lpFile;
1776 wcmd = wcmdBuffer;
1777 len = lstrlenW(wszApplicationName) + 3;
1778 if (sei_tmp.lpParameters[0])
1779 len += 1 + lstrlenW(wszParameters);
1780 if (len > wcmdLen)
1782 wcmd = malloc(len * sizeof(WCHAR));
1783 wcmdLen = len;
1785 lstrcpyW(wcmd, wszApplicationName);
1786 if (sei_tmp.lpDirectory)
1788 LPCWSTR searchPath[] = {
1789 sei_tmp.lpDirectory,
1790 NULL
1792 PathFindOnPathW(wcmd, searchPath);
1794 retval = SHELL_quote_and_execute( wcmd, wszParameters, L"",
1795 wszApplicationName, NULL, &sei_tmp,
1796 sei, execfunc );
1797 if (retval > 32) {
1798 free(wszApplicationName);
1799 if (wszParameters != parametersBuffer)
1800 free(wszParameters);
1801 if (wszDir != dirBuffer)
1802 free(wszDir);
1803 if (wcmd != wcmdBuffer)
1804 free(wcmd);
1805 return TRUE;
1808 /* Else, try to find the executable */
1809 wcmd[0] = '\0';
1810 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1811 if (retval > 32) /* Found */
1813 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszKeyname,
1814 wszApplicationName, env, &sei_tmp,
1815 sei, execfunc );
1816 free( env );
1818 else if (PathIsDirectoryW(lpFile))
1820 WCHAR wExec[MAX_PATH];
1821 WCHAR * lpQuotedFile = malloc( sizeof(WCHAR) * (lstrlenW(lpFile) + 3) );
1823 if (lpQuotedFile)
1825 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, L"explorer",
1826 L"open", wExec, MAX_PATH,
1827 NULL, &env, NULL, NULL );
1828 if (retval > 32)
1830 lstrcpyW(lpQuotedFile, L"\"");
1831 lstrcatW(lpQuotedFile, lpFile);
1832 lstrcatW(lpQuotedFile, L"\"");
1833 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1834 wszKeyname,
1835 wszApplicationName, env,
1836 &sei_tmp, sei, execfunc );
1837 free( env );
1839 free( lpQuotedFile );
1841 else
1842 retval = 0; /* Out of memory */
1844 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1846 retval = SHELL_execute_url( lpFile, wcmd, &sei_tmp, sei, execfunc );
1848 /* Check if file specified is in the form www.??????.*** */
1849 else if (!wcsnicmp(lpFile, L"www", 3))
1851 /* if so, prefix lpFile with http:// and call ShellExecute */
1852 WCHAR lpstrTmpFile[256];
1853 lstrcpyW(lpstrTmpFile, L"http://");
1854 lstrcatW(lpstrTmpFile, lpFile);
1855 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1858 end:
1859 TRACE("retval %Iu\n", retval);
1861 free(wszApplicationName);
1862 if (wszParameters != parametersBuffer)
1863 free(wszParameters);
1864 if (wszDir != dirBuffer)
1865 free(wszDir);
1866 if (wcmd != wcmdBuffer)
1867 free(wcmd);
1869 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1871 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1872 do_error_dialog(retval, sei_tmp.hwnd);
1873 return retval > 32;
1876 /*************************************************************************
1877 * ShellExecuteA [SHELL32.290]
1879 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
1880 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1882 SHELLEXECUTEINFOA sei;
1884 TRACE("%p,%s,%s,%s,%s,%d\n",
1885 hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
1886 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1888 sei.cbSize = sizeof(sei);
1889 sei.fMask = SEE_MASK_FLAG_NO_UI;
1890 sei.hwnd = hWnd;
1891 sei.lpVerb = lpVerb;
1892 sei.lpFile = lpFile;
1893 sei.lpParameters = lpParameters;
1894 sei.lpDirectory = lpDirectory;
1895 sei.nShow = iShowCmd;
1896 sei.lpIDList = 0;
1897 sei.lpClass = 0;
1898 sei.hkeyClass = 0;
1899 sei.dwHotKey = 0;
1900 sei.hProcess = 0;
1902 ShellExecuteExA (&sei);
1903 return sei.hInstApp;
1906 /*************************************************************************
1907 * ShellExecuteExA [SHELL32.292]
1910 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1912 SHELLEXECUTEINFOW seiW;
1913 BOOL ret;
1914 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1916 TRACE("%p\n", sei);
1918 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1920 if (sei->lpVerb)
1921 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1923 if (sei->lpFile)
1924 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1926 if (sei->lpParameters)
1927 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1929 if (sei->lpDirectory)
1930 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1932 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1933 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1934 else
1935 seiW.lpClass = NULL;
1937 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1939 sei->hInstApp = seiW.hInstApp;
1941 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1942 sei->hProcess = seiW.hProcess;
1944 SHFree(wVerb);
1945 SHFree(wFile);
1946 SHFree(wParameters);
1947 SHFree(wDirectory);
1948 SHFree(wClass);
1950 return ret;
1953 /*************************************************************************
1954 * ShellExecuteExW [SHELL32.293]
1957 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1959 return SHELL_execute( sei, SHELL_ExecuteW );
1962 /*************************************************************************
1963 * ShellExecuteW [SHELL32.294]
1964 * from shellapi.h
1965 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1966 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1968 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
1969 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1971 SHELLEXECUTEINFOW sei;
1973 TRACE("\n");
1974 sei.cbSize = sizeof(sei);
1975 sei.fMask = SEE_MASK_FLAG_NO_UI;
1976 sei.hwnd = hwnd;
1977 sei.lpVerb = lpVerb;
1978 sei.lpFile = lpFile;
1979 sei.lpParameters = lpParameters;
1980 sei.lpDirectory = lpDirectory;
1981 sei.nShow = nShowCmd;
1982 sei.lpIDList = 0;
1983 sei.lpClass = 0;
1984 sei.hkeyClass = 0;
1985 sei.dwHotKey = 0;
1986 sei.hProcess = 0;
1988 SHELL_execute( &sei, SHELL_ExecuteW );
1989 return sei.hInstApp;
1992 /*************************************************************************
1993 * WOWShellExecute [SHELL32.@]
1995 * FIXME: the callback function most likely doesn't work the same way on Windows.
1997 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb,LPCSTR lpFile,
1998 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
2000 SHELLEXECUTEINFOW seiW;
2001 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2002 HANDLE hProcess = 0;
2004 seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
2005 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2006 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2007 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2009 seiW.cbSize = sizeof(seiW);
2010 seiW.fMask = 0;
2011 seiW.hwnd = hWnd;
2012 seiW.nShow = iShowCmd;
2013 seiW.lpIDList = 0;
2014 seiW.lpClass = 0;
2015 seiW.hkeyClass = 0;
2016 seiW.dwHotKey = 0;
2017 seiW.hProcess = hProcess;
2019 SHELL_execute( &seiW, callback );
2021 SHFree(wVerb);
2022 SHFree(wFile);
2023 SHFree(wParameters);
2024 SHFree(wDirectory);
2025 return seiW.hInstApp;
2028 /*************************************************************************
2029 * ShellExec_RunDLLW [SHELL32.@]
2031 void WINAPI ShellExec_RunDLLW(HWND hwnd, HINSTANCE instance, WCHAR *cmdline, int cmdshow)
2033 BOOL in_single_quotes = FALSE, in_double_quotes = FALSE;
2034 WCHAR *args;
2036 TRACE("%p, %p, %s, %d\n", hwnd, instance, debugstr_w(cmdline), cmdshow);
2038 /* Replace the first whitespace character in the command line string with a
2039 null terminator to separate the program name from the program arguments */
2040 for (args = cmdline; *args; args++)
2042 switch (*args)
2044 case '\\':
2045 args++; /* skip the next character */
2046 break;
2047 case '\'':
2048 if (!in_double_quotes)
2049 in_single_quotes = !in_single_quotes;
2050 break;
2051 case '"':
2052 if (!in_single_quotes)
2053 in_double_quotes = !in_double_quotes;
2054 break;
2055 case ' ':
2056 case '\t':
2057 if (!in_single_quotes && !in_double_quotes)
2059 *args = 0;
2060 args++;
2061 goto execute;
2066 execute:
2067 ShellExecuteW(hwnd, NULL, cmdline, args, NULL, cmdshow);
2070 /*************************************************************************
2071 * ShellExec_RunDLLA [SHELL32.@]
2073 void WINAPI ShellExec_RunDLLA(HWND hwnd, HINSTANCE instance, CHAR *cmdline, int cmdshow)
2075 WCHAR *cmdlineW;
2076 ShellExec_RunDLLW(hwnd, instance, __SHCloneStrAtoW(&cmdlineW, cmdline), cmdshow);
2077 SHFree(cmdlineW);
2080 /*************************************************************************
2081 * OpenAs_RunDLLA [SHELL32.@]
2083 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2085 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2088 /*************************************************************************
2089 * OpenAs_RunDLLW [SHELL32.@]
2091 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2093 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2096 /*************************************************************************
2097 * RegenerateUserEnvironment [SHELL32.@]
2099 BOOL WINAPI RegenerateUserEnvironment(WCHAR *wunknown, BOOL bunknown)
2101 FIXME("stub: %p, %d\n", wunknown, bunknown);
2102 return FALSE;