shlwapi/tests: Link directly to Url*() functions.
[wine.git] / dlls / shell32 / shlexec.c
blobc385c26e1e68b7ed353f11e0a2c01d5bd598a9de
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 if (used < len)
114 *res++ = '"';
115 while(*args)
117 used++;
118 if (used < len)
119 *res++ = *args++;
120 else
121 args++;
123 used++;
124 if (used < len)
125 *res++ = '"';
127 else
129 while(*args && !isSpace(*args))
131 used++;
132 if (used < len)
133 *res++ = *args++;
134 else
135 args++;
138 while(isSpace(*args))
139 ++args;
141 break;
143 /* else fall through */
144 case '1':
145 if (!done || (*fmt == '1'))
147 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
148 if (SearchPathW(NULL, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
149 cmd = xlpFile;
150 else
151 cmd = lpFile;
153 used += lstrlenW(cmd);
154 if (used < len)
156 lstrcpyW(res, cmd);
157 res += lstrlenW(cmd);
160 found_p1 = TRUE;
161 break;
164 * IE uses this a lot for activating things such as windows media
165 * player. This is not verified to be fully correct but it appears
166 * to work just fine.
168 case 'l':
169 case 'L':
170 if (lpFile) {
171 used += lstrlenW(lpFile);
172 if (used < len)
174 lstrcpyW(res, lpFile);
175 res += lstrlenW(lpFile);
178 found_p1 = TRUE;
179 break;
181 case 'i':
182 case 'I':
183 if (pidl) {
184 INT chars = 0;
185 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
186 * allowing a buffer of 100 should more than exceed all needs */
187 WCHAR buf[100];
188 LPVOID pv;
189 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
190 pv = SHLockShared(hmem, 0);
191 chars = swprintf(buf, ARRAY_SIZE(buf), L":%p", pv);
192 if (chars >= ARRAY_SIZE(buf))
193 ERR("pidl format buffer too small!\n");
194 used += chars;
195 if (used < len)
197 lstrcpyW(res,buf);
198 res += chars;
200 SHUnlockShared(pv);
202 found_p1 = TRUE;
203 break;
205 default:
207 * Check if this is an env-variable here...
210 /* Make sure that we have at least one more %.*/
211 if (wcschr(fmt, '%'))
213 WCHAR tmpBuffer[1024];
214 PWSTR tmpB = tmpBuffer;
215 WCHAR tmpEnvBuff[MAX_PATH];
216 DWORD envRet;
218 while (*fmt != '%')
219 *tmpB++ = *fmt++;
220 *tmpB++ = 0;
222 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
224 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
225 if (envRet == 0 || envRet > MAX_PATH)
227 used += lstrlenW(tmpBuffer);
228 if (used < len)
230 lstrcpyW( res, tmpBuffer );
231 res += lstrlenW(tmpBuffer);
234 else
236 used += lstrlenW(tmpEnvBuff);
237 if (used < len)
239 lstrcpyW( res, tmpEnvBuff );
240 res += lstrlenW(tmpEnvBuff);
244 done = TRUE;
245 break;
247 /* Don't skip past terminator (catch a single '%' at the end) */
248 if (*fmt != '\0')
250 fmt++;
253 else
255 used ++;
256 if (used < len)
257 *res++ = *fmt++;
258 else
259 fmt++;
263 used ++;
264 if (res - out < len)
265 *res = '\0';
266 else
267 out[len-1] = '\0';
269 TRACE("used %i of %i space\n",used,len);
270 if (out_len)
271 *out_len = used;
273 return found_p1;
276 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
278 STRRET strret;
279 IShellFolder* desktop;
281 HRESULT hr = SHGetDesktopFolder(&desktop);
283 if (SUCCEEDED(hr)) {
284 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
286 if (SUCCEEDED(hr))
287 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
289 IShellFolder_Release(desktop);
292 return hr;
295 /*************************************************************************
296 * SHELL_ExecuteW [Internal]
299 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
300 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
302 STARTUPINFOW startup;
303 PROCESS_INFORMATION info;
304 UINT_PTR retval = SE_ERR_NOASSOC;
305 UINT gcdret = 0;
306 WCHAR curdir[MAX_PATH];
307 DWORD dwCreationFlags;
309 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
311 /* make sure we don't fail the CreateProcess if the calling app passes in
312 * a bad working directory */
313 if (psei->lpDirectory && psei->lpDirectory[0])
315 /* ShellExecute specifies the command from psei->lpDirectory
316 * if present. Not from the current dir as CreateProcess does */
317 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir )))
318 if( !SetCurrentDirectoryW( psei->lpDirectory ))
319 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory ));
322 ZeroMemory(&startup,sizeof(STARTUPINFOW));
323 startup.cb = sizeof(STARTUPINFOW);
324 startup.dwFlags = STARTF_USESHOWWINDOW;
325 startup.wShowWindow = psei->nShow;
326 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
327 if (!(psei->fMask & SEE_MASK_NO_CONSOLE))
328 dwCreationFlags |= CREATE_NEW_CONSOLE;
329 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
330 NULL, &startup, &info))
332 /* Give 30 seconds to the app to come up, if desired. Probably only needed
333 when starting app immediately before making a DDE connection. */
334 if (shWait)
335 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
336 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
337 retval = 33;
338 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
339 psei_out->hProcess = info.hProcess;
340 else
341 CloseHandle( info.hProcess );
342 CloseHandle( info.hThread );
344 else if ((retval = GetLastError()) >= 32)
346 TRACE("CreateProcess returned error %ld\n", retval);
347 retval = ERROR_BAD_FORMAT;
350 TRACE("returning %lu\n", retval);
352 psei_out->hInstApp = (HINSTANCE)retval;
353 if( gcdret )
354 if( !SetCurrentDirectoryW( curdir))
355 ERR("cannot return to directory %s\n", debugstr_w(curdir));
357 return retval;
361 /***********************************************************************
362 * SHELL_BuildEnvW [Internal]
364 * Build the environment for the new process, adding the specified
365 * path to the PATH variable. Returned pointer must be freed by caller.
367 static void *SHELL_BuildEnvW( const WCHAR *path )
369 WCHAR *strings, *new_env;
370 WCHAR *p, *p2;
371 int total = lstrlenW(path) + 1;
372 BOOL got_path = FALSE;
374 if (!(strings = GetEnvironmentStringsW())) return NULL;
375 p = strings;
376 while (*p)
378 int len = lstrlenW(p) + 1;
379 if (!wcsnicmp( p, L"PATH=", 5 )) got_path = TRUE;
380 total += len;
381 p += len;
383 if (!got_path) total += 5; /* we need to create PATH */
384 total++; /* terminating null */
386 if (!(new_env = heap_alloc( total * sizeof(WCHAR) )))
388 FreeEnvironmentStringsW( strings );
389 return NULL;
391 p = strings;
392 p2 = new_env;
393 while (*p)
395 int len = lstrlenW(p) + 1;
396 memcpy( p2, p, len * sizeof(WCHAR) );
397 if (!wcsnicmp( p, L"PATH=", 5 ))
399 p2[len - 1] = ';';
400 lstrcpyW( p2 + len, path );
401 p2 += lstrlenW(path) + 1;
403 p += len;
404 p2 += len;
406 if (!got_path)
408 lstrcpyW( p2, L"PATH=" );
409 lstrcatW( p2, path );
410 p2 += lstrlenW(p2) + 1;
412 *p2 = 0;
413 FreeEnvironmentStringsW( strings );
414 return new_env;
418 /***********************************************************************
419 * SHELL_TryAppPathW [Internal]
421 * Helper function for SHELL_FindExecutable
422 * @param lpResult - pointer to a buffer of size MAX_PATH
423 * On entry: szName is a filename (probably without path separators).
424 * On exit: if szName found in "App Path", place full path in lpResult, and return true
426 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
428 HKEY hkApp = 0;
429 WCHAR buffer[1024];
430 LONG len;
431 LONG res;
432 BOOL found = FALSE;
434 if (env) *env = NULL;
435 lstrcpyW(buffer, L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
436 lstrcatW(buffer, szName);
437 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
438 if (res) goto end;
440 len = MAX_PATH*sizeof(WCHAR);
441 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
442 if (res) goto end;
443 found = TRUE;
445 if (env)
447 DWORD count = sizeof(buffer);
448 if (!RegQueryValueExW(hkApp, L"Path", NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
449 *env = SHELL_BuildEnvW( buffer );
452 end:
453 if (hkApp) RegCloseKey(hkApp);
454 return found;
457 /*************************************************************************
458 * SHELL_FindExecutableByVerb [Internal]
460 * called from SHELL_FindExecutable or SHELL_execute_class
461 * in/out:
462 * classname a buffer, big enough, to get the key name to do actually the
463 * command "WordPad.Document.1\\shell\\open\\command"
464 * passed as "WordPad.Document.1"
465 * in:
466 * lpVerb the operation on it (open)
467 * commandlen the size of command buffer (in bytes)
468 * out:
469 * command a buffer, to store the command to do the
470 * operation on the file
471 * key a buffer, big enough, to get the key name to do actually the
472 * command "WordPad.Document.1\\shell\\open\\command"
473 * Can be NULL
475 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
477 HKEY hkeyClass;
478 WCHAR verb[MAX_PATH];
480 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
481 return SE_ERR_NOASSOC;
482 if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, ARRAY_SIZE(verb)))
483 return SE_ERR_NOASSOC;
484 RegCloseKey(hkeyClass);
486 /* Looking for ...buffer\shell\<verb>\command */
487 lstrcatW(classname, L"\\shell\\");
488 lstrcatW(classname, verb);
489 lstrcatW(classname, L"\\command");
491 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
492 &commandlen) == ERROR_SUCCESS)
494 commandlen /= sizeof(WCHAR);
495 if (key) lstrcpyW(key, classname);
496 #if 0
497 LPWSTR tmp;
498 WCHAR param[256];
499 LONG paramlen = sizeof(param);
501 /* FIXME: it seems all Windows version don't behave the same here.
502 * the doc states that this ddeexec information can be found after
503 * the exec names.
504 * on Win98, it doesn't appear, but I think it does on Win2k
506 /* Get the parameters needed by the application
507 from the associated ddeexec key */
508 tmp = wcsstr(classname, L"\\command");
509 tmp[0] = '\0';
510 lstrcatW(classname, wDdeexec);
511 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
512 &paramlen) == ERROR_SUCCESS)
514 paramlen /= sizeof(WCHAR);
515 lstrcatW(command, L" ");
516 lstrcatW(command, param);
517 commandlen += paramlen;
519 #endif
521 command[commandlen] = '\0';
523 return 33; /* FIXME see SHELL_FindExecutable() */
526 return SE_ERR_NOASSOC;
529 /*************************************************************************
530 * SHELL_FindExecutable [Internal]
532 * Utility for code sharing between FindExecutable and ShellExecute
533 * in:
534 * lpFile the name of a file
535 * lpVerb the operation on it (open)
536 * out:
537 * lpResult a buffer, big enough :-(, to store the command to do the
538 * operation on the file
539 * key a buffer, big enough, to get the key name to do actually the
540 * command (it'll be used afterwards for more information
541 * on the operation)
543 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
544 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
546 WCHAR *extension = NULL; /* pointer to file extension */
547 WCHAR classname[256]; /* registry name for this file type */
548 LONG classnamelen = sizeof(classname); /* length of above */
549 WCHAR command[1024]; /* command from registry */
550 WCHAR wBuffer[256]; /* Used to GetProfileString */
551 UINT retval = SE_ERR_NOASSOC;
552 WCHAR *tok; /* token pointer */
553 WCHAR xlpFile[256]; /* result of SearchPath */
554 DWORD attribs; /* file attributes */
556 TRACE("%s\n", debugstr_w(lpFile));
558 if (!lpResult)
559 return ERROR_INVALID_PARAMETER;
561 xlpFile[0] = '\0';
562 lpResult[0] = '\0'; /* Start off with an empty return string */
563 if (key) *key = '\0';
565 /* trap NULL parameters on entry */
566 if (!lpFile)
568 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
569 debugstr_w(lpFile), debugstr_w(lpResult));
570 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
573 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
575 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
576 return 33;
579 if (SearchPathW(lpPath, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
581 TRACE("SearchPathW returned non-zero\n");
582 lpFile = xlpFile;
583 /* The file was found in the application-supplied default directory (or the system search path) */
585 else if (lpPath && SearchPathW(NULL, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
587 TRACE("SearchPathW returned non-zero\n");
588 lpFile = xlpFile;
589 /* The file was found in one of the directories in the system-wide search path */
592 attribs = GetFileAttributesW(lpFile);
593 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
595 lstrcpyW(classname, L"Folder");
597 else
599 /* Did we get something? Anything? */
600 if (xlpFile[0]==0)
602 TRACE("Returning SE_ERR_FNF\n");
603 return SE_ERR_FNF;
605 /* First thing we need is the file's extension */
606 extension = wcsrchr(xlpFile, '.'); /* Assume last "." is the one; */
607 /* File->Run in progman uses */
608 /* .\FILE.EXE :( */
609 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
611 if (extension == NULL || extension[1]==0)
613 WARN("Returning SE_ERR_NOASSOC\n");
614 return SE_ERR_NOASSOC;
617 /* Three places to check: */
618 /* 1. win.ini, [windows], programs (NB no leading '.') */
619 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
620 /* 3. win.ini, [extensions], extension (NB no leading '.' */
621 /* All I know of the order is that registry is checked before */
622 /* extensions; however, it'd make sense to check the programs */
623 /* section first, so that's what happens here. */
625 /* See if it's a program - if GetProfileString fails, we skip this
626 * section. Actually, if GetProfileString fails, we've probably
627 * got a lot more to worry about than running a program... */
628 if (GetProfileStringW(L"windows", L"programs", L"exe pif bat cmd com", wBuffer, ARRAY_SIZE(wBuffer)) > 0)
630 CharLowerW(wBuffer);
631 tok = wBuffer;
632 while (*tok)
634 WCHAR *p = tok;
635 while (*p && *p != ' ' && *p != '\t') p++;
636 if (*p)
638 *p++ = 0;
639 while (*p == ' ' || *p == '\t') p++;
642 if (wcsicmp(tok, &extension[1]) == 0) /* have to skip the leading "." */
644 lstrcpyW(lpResult, xlpFile);
645 /* Need to perhaps check that the file has a path
646 * attached */
647 TRACE("found %s\n", debugstr_w(lpResult));
648 return 33;
649 /* Greater than 32 to indicate success */
651 tok = p;
655 /* Check registry */
656 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
657 &classnamelen) == ERROR_SUCCESS)
659 classnamelen /= sizeof(WCHAR);
660 if (classnamelen == ARRAY_SIZE(classname))
661 classnamelen--;
662 classname[classnamelen] = '\0';
663 TRACE("File type: %s\n", debugstr_w(classname));
665 else
667 *classname = '\0';
671 if (*classname)
673 /* pass the verb string to SHELL_FindExecutableByVerb() */
674 retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
676 if (retval > 32)
678 DWORD finishedLen;
679 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
680 if (finishedLen > resultLen)
681 ERR("Argify buffer not large enough.. truncated\n");
683 /* Remove double quotation marks and command line arguments */
684 if (*lpResult == '"')
686 WCHAR *p = lpResult;
687 while (*(p + 1) != '"')
689 *p = *(p + 1);
690 p++;
692 *p = '\0';
694 else
696 /* Truncate on first space */
697 WCHAR *p = lpResult;
698 while (*p != ' ' && *p != '\0')
699 p++;
700 *p='\0';
704 else /* Check win.ini */
706 /* Toss the leading dot */
707 extension++;
708 if (GetProfileStringW(L"extensions", extension, L"", command, ARRAY_SIZE(command)) > 0)
710 if (*command)
712 lstrcpyW(lpResult, command);
713 tok = wcschr(lpResult, '^'); /* should be ^.extension? */
714 if (tok != NULL)
716 tok[0] = '\0';
717 lstrcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
718 tok = wcschr(command, '^'); /* see above */
719 if ((tok != NULL) && (lstrlenW(tok)>5))
721 lstrcatW(lpResult, &tok[5]);
724 retval = 33; /* FIXME - see above */
729 TRACE("returning %s\n", debugstr_w(lpResult));
730 return retval;
733 /******************************************************************
734 * dde_cb
736 * callback for the DDE connection. not really useful
738 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
739 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
740 ULONG_PTR dwData1, ULONG_PTR dwData2)
742 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
743 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
744 return NULL;
747 /******************************************************************
748 * dde_connect
750 * ShellExecute helper. Used to do an operation with a DDE connection
752 * Handles both the direct connection (try #1), and if it fails,
753 * launching an application and trying (#2) to connect to it
756 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
757 const WCHAR* lpFile, WCHAR *env,
758 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
759 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
761 WCHAR regkey[256];
762 WCHAR * endkey = regkey + lstrlenW(key);
763 WCHAR app[256], topic[256], ifexec[256], static_res[256];
764 WCHAR * dynamic_res=NULL;
765 WCHAR * res;
766 LONG applen, topiclen, ifexeclen;
767 WCHAR * exec;
768 DWORD ddeInst = 0;
769 DWORD tid;
770 DWORD resultLen, endkeyLen;
771 HSZ hszApp, hszTopic;
772 HCONV hConv;
773 HDDEDATA hDdeData;
774 unsigned ret = SE_ERR_NOASSOC;
775 BOOL unicode = !(GetVersion() & 0x80000000);
777 if (lstrlenW(key) + 1 > ARRAY_SIZE(regkey))
779 FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
780 return 2;
782 lstrcpyW(regkey, key);
783 endkeyLen = ARRAY_SIZE(regkey) - (endkey - regkey);
784 if (lstrlenW(L"\\application") + 1 > endkeyLen)
786 FIXME("endkey overruns buffer\n");
787 return 2;
789 lstrcpyW(endkey, L"\\application");
790 applen = sizeof(app);
791 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
793 WCHAR command[1024], fullpath[MAX_PATH];
794 LPWSTR ptr = NULL;
795 DWORD ret = 0;
797 /* Get application command from start string and find filename of application */
798 if (*start == '"')
800 if (lstrlenW(start + 1) + 1 > ARRAY_SIZE(command))
802 FIXME("size of input parameter %s larger than buffer\n",
803 debugstr_w(start + 1));
804 return 2;
806 lstrcpyW(command, start+1);
807 if ((ptr = wcschr(command, '"')))
808 *ptr = 0;
809 ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
811 else
813 LPCWSTR p;
814 LPWSTR space;
815 for (p=start; (space=wcschr(p, ' ')); p=space+1)
817 int idx = space-start;
818 memcpy(command, start, idx*sizeof(WCHAR));
819 command[idx] = '\0';
820 if ((ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr)))
821 break;
823 if (!ret)
824 ret = SearchPathW(NULL, start, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
827 if (!ret)
829 ERR("Unable to find application path for command %s\n", debugstr_w(start));
830 return ERROR_ACCESS_DENIED;
832 if (lstrlenW(ptr) + 1 > ARRAY_SIZE(app))
834 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
835 return 2;
837 lstrcpyW(app, ptr);
839 /* Remove extensions (including .so) */
840 ptr = app + lstrlenW(app) - 3;
841 if (ptr > app && !wcscmp(ptr, L".so"))
842 *ptr = 0;
844 ptr = wcsrchr(app, '.');
845 assert(ptr);
846 *ptr = 0;
849 if (lstrlenW(L"\\topic") + 1 > endkeyLen)
851 FIXME("endkey overruns buffer\n");
852 return 2;
854 lstrcpyW(endkey, L"\\topic");
855 topiclen = sizeof(topic);
856 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
857 lstrcpyW(topic, L"System");
859 if (unicode)
861 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
862 return 2;
864 else
866 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
867 return 2;
870 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
871 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
873 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
874 exec = ddeexec;
875 if (!hConv)
877 TRACE("Launching %s\n", debugstr_w(start));
878 ret = execfunc(start, env, TRUE, psei, psei_out);
879 if (ret <= 32)
881 TRACE("Couldn't launch\n");
882 goto error;
884 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
885 if (!hConv)
887 TRACE("Couldn't connect. ret=%d\n", ret);
888 DdeUninitialize(ddeInst);
889 SetLastError(ERROR_DDE_FAIL);
890 return 30; /* whatever */
892 if (lstrlenW(L"\\ifexec") + 1 > endkeyLen)
894 FIXME("endkey overruns buffer\n");
895 return 2;
897 lstrcpyW(endkey, L"\\ifexec");
898 ifexeclen = sizeof(ifexec);
899 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
901 exec = ifexec;
905 SHELL_ArgifyW(static_res, ARRAY_SIZE(static_res), exec, lpFile, pidl, szCommandline, &resultLen);
906 if (resultLen > ARRAY_SIZE(static_res))
908 res = dynamic_res = heap_alloc(resultLen * sizeof(WCHAR));
909 SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL);
911 else
912 res = static_res;
913 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
915 /* It's documented in the KB 330337 that IE has a bug and returns
916 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
918 if (unicode)
919 hDdeData = DdeClientTransaction((LPBYTE)res, (lstrlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
920 XTYP_EXECUTE, 30000, &tid);
921 else
923 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
924 char *resA = heap_alloc(lenA);
925 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
926 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
927 XTYP_EXECUTE, 10000, &tid );
928 heap_free(resA);
930 if (hDdeData)
931 DdeFreeDataHandle(hDdeData);
932 else
933 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
934 ret = 33;
936 heap_free(dynamic_res);
938 DdeDisconnect(hConv);
940 error:
941 DdeUninitialize(ddeInst);
943 return ret;
946 /*************************************************************************
947 * execute_from_key [Internal]
949 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
950 LPCWSTR executable_name,
951 SHELL_ExecuteW32 execfunc,
952 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
954 WCHAR cmd[256], param[1024], ddeexec[256];
955 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
956 UINT_PTR retval = SE_ERR_NOASSOC;
957 DWORD resultLen;
958 LPWSTR tmp;
960 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
961 debugstr_w(szCommandline), debugstr_w(executable_name));
963 cmd[0] = '\0';
964 param[0] = '\0';
966 /* Get the application from the registry */
967 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
969 TRACE("got cmd: %s\n", debugstr_w(cmd));
971 /* Is there a replace() function anywhere? */
972 cmdlen /= sizeof(WCHAR);
973 if (cmdlen >= ARRAY_SIZE(cmd))
974 cmdlen = ARRAY_SIZE(cmd) - 1;
975 cmd[cmdlen] = '\0';
976 SHELL_ArgifyW(param, ARRAY_SIZE(param), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
977 if (resultLen > ARRAY_SIZE(param))
978 ERR("Argify buffer not large enough, truncating\n");
981 /* Get the parameters needed by the application
982 from the associated ddeexec key */
983 tmp = wcsstr(key, L"command");
984 assert(tmp);
985 lstrcpyW(tmp, L"ddeexec");
987 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
989 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
990 if (!param[0]) lstrcpyW(param, executable_name);
991 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
993 else if (param[0])
995 TRACE("executing: %s\n", debugstr_w(param));
996 retval = execfunc(param, env, FALSE, psei, psei_out);
998 else
999 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1001 return retval;
1004 /*************************************************************************
1005 * FindExecutableA [SHELL32.@]
1007 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1009 HINSTANCE retval;
1010 WCHAR *wFile = NULL, *wDirectory = NULL;
1011 WCHAR wResult[MAX_PATH];
1013 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1014 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1016 retval = FindExecutableW(wFile, wDirectory, wResult);
1017 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1018 SHFree( wFile );
1019 SHFree( wDirectory );
1021 TRACE("returning %s\n", lpResult);
1022 return retval;
1025 /*************************************************************************
1026 * FindExecutableW [SHELL32.@]
1028 * This function returns the executable associated with the specified file
1029 * for the default verb.
1031 * PARAMS
1032 * lpFile [I] The file to find the association for. This must refer to
1033 * an existing file otherwise FindExecutable fails and returns
1034 * SE_ERR_FNF.
1035 * lpResult [O] Points to a buffer into which the executable path is
1036 * copied. This parameter must not be NULL otherwise
1037 * FindExecutable() segfaults. The buffer must be of size at
1038 * least MAX_PATH characters.
1040 * RETURNS
1041 * A value greater than 32 on success, less than or equal to 32 otherwise.
1042 * See the SE_ERR_* constants.
1044 * NOTES
1045 * On Windows XP and 2003, FindExecutable() seems to first convert the
1046 * filename into 8.3 format, thus taking into account only the first three
1047 * characters of the extension, and expects to find an association for those.
1048 * However other Windows versions behave sanely.
1050 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1052 UINT_PTR retval = SE_ERR_NOASSOC;
1053 WCHAR old_dir[1024];
1054 WCHAR res[MAX_PATH];
1056 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1058 lpResult[0] = '\0'; /* Start off with an empty return string */
1059 if (lpFile == NULL)
1060 return (HINSTANCE)SE_ERR_FNF;
1062 if (lpDirectory)
1064 GetCurrentDirectoryW(ARRAY_SIZE(old_dir), old_dir);
1065 SetCurrentDirectoryW(lpDirectory);
1068 retval = SHELL_FindExecutable(lpDirectory, lpFile, L"open", res, MAX_PATH, NULL, NULL, NULL, NULL);
1070 if (retval > 32)
1071 lstrcpyW(lpResult, res);
1073 TRACE("returning %s\n", debugstr_w(lpResult));
1074 if (lpDirectory)
1075 SetCurrentDirectoryW(old_dir);
1076 return (HINSTANCE)retval;
1079 /* FIXME: is this already implemented somewhere else? */
1080 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1082 LPCWSTR ext = NULL, lpClass = NULL;
1083 LPWSTR cls = NULL;
1084 DWORD type = 0, sz = 0;
1085 HKEY hkey = 0;
1086 LONG r;
1088 if (sei->fMask & SEE_MASK_CLASSALL)
1089 return sei->hkeyClass;
1091 if (sei->fMask & SEE_MASK_CLASSNAME)
1092 lpClass = sei->lpClass;
1093 else
1095 ext = PathFindExtensionW( sei->lpFile );
1096 TRACE("ext = %s\n", debugstr_w( ext ) );
1097 if (!ext)
1098 return hkey;
1100 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1101 if (r != ERROR_SUCCESS )
1102 return hkey;
1104 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1105 if ( r == ERROR_SUCCESS && type == REG_SZ )
1107 sz += sizeof (WCHAR);
1108 cls = heap_alloc( sz );
1109 cls[0] = 0;
1110 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1113 RegCloseKey( hkey );
1114 lpClass = cls;
1117 TRACE("class = %s\n", debugstr_w(lpClass) );
1119 hkey = 0;
1120 if ( lpClass )
1121 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1123 heap_free( cls );
1125 return hkey;
1128 static HRESULT shellex_get_dataobj( LPSHELLEXECUTEINFOW sei, IDataObject **dataobj )
1130 LPCITEMIDLIST pidllast = NULL;
1131 IShellFolder *shf = NULL;
1132 LPITEMIDLIST pidl = NULL;
1133 HRESULT r = SE_ERR_DLLNOTFOUND;
1135 if (sei->fMask & SEE_MASK_CLASSALL)
1136 pidl = sei->lpIDList;
1137 else
1139 WCHAR fullpath[MAX_PATH];
1140 BOOL ret;
1142 fullpath[0] = 0;
1143 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1144 if (!ret)
1145 goto end;
1147 pidl = ILCreateFromPathW( fullpath );
1150 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1151 if ( FAILED( r ) )
1152 goto end;
1154 r = IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1155 &IID_IDataObject, NULL, (void**)dataobj );
1157 end:
1158 if ( pidl != sei->lpIDList )
1159 ILFree( pidl );
1160 if ( shf )
1161 IShellFolder_Release( shf );
1162 return r;
1165 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1166 LPSHELLEXECUTEINFOW sei )
1168 IContextMenu *cm = NULL;
1169 CMINVOKECOMMANDINFOEX ici;
1170 MENUITEMINFOW info;
1171 WCHAR string[0x80];
1172 INT i, n, def = -1;
1173 HMENU hmenu = 0;
1174 HRESULT r;
1176 TRACE("%p %p\n", obj, sei );
1178 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1179 if ( FAILED( r ) )
1180 return r;
1182 hmenu = CreateMenu();
1183 if ( !hmenu )
1184 goto end;
1186 /* the number of the last menu added is returned in r */
1187 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1188 if ( FAILED( r ) )
1189 goto end;
1191 n = GetMenuItemCount( hmenu );
1192 for ( i = 0; i < n; i++ )
1194 memset( &info, 0, sizeof info );
1195 info.cbSize = sizeof info;
1196 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1197 info.dwTypeData = string;
1198 info.cch = ARRAY_SIZE(string);
1199 string[0] = 0;
1200 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1202 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1203 info.fState, info.dwItemData, info.fType, info.wID );
1204 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1205 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1207 def = i;
1208 break;
1212 r = E_FAIL;
1213 if ( def == -1 )
1214 goto end;
1216 memset( &ici, 0, sizeof ici );
1217 ici.cbSize = sizeof ici;
1218 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE|SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1219 ici.nShow = sei->nShow;
1220 ici.lpVerb = MAKEINTRESOURCEA( def );
1221 ici.hwnd = sei->hwnd;
1222 ici.lpParametersW = sei->lpParameters;
1224 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1226 TRACE("invoke command returned %08x\n", r );
1228 end:
1229 if ( hmenu )
1230 DestroyMenu( hmenu );
1231 if ( cm )
1232 IContextMenu_Release( cm );
1233 return r;
1236 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1238 IDataObject *dataobj = NULL;
1239 IObjectWithSite *ows = NULL;
1240 IShellExtInit *obj = NULL;
1241 HRESULT r;
1243 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1245 r = CoInitialize( NULL );
1246 if ( FAILED( r ) )
1247 goto end;
1249 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1250 &IID_IShellExtInit, (LPVOID*)&obj );
1251 if ( FAILED( r ) )
1253 ERR("failed %08x\n", r );
1254 goto end;
1257 r = shellex_get_dataobj( sei, &dataobj );
1258 if ( FAILED( r ) )
1260 ERR("failed to get data object\n");
1261 goto end;
1264 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1265 if ( FAILED( r ) )
1266 goto end;
1268 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1269 if ( FAILED( r ) )
1270 goto end;
1272 IObjectWithSite_SetSite( ows, NULL );
1274 r = shellex_run_context_menu_default( obj, sei );
1276 end:
1277 if ( ows )
1278 IObjectWithSite_Release( ows );
1279 if ( dataobj )
1280 IDataObject_Release( dataobj );
1281 if ( obj )
1282 IShellExtInit_Release( obj );
1283 CoUninitialize();
1284 return r;
1288 /*************************************************************************
1289 * ShellExecute_FromContextMenu [Internal]
1291 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1293 HKEY hkey, hkeycm = 0;
1294 WCHAR szguid[39];
1295 HRESULT hr;
1296 GUID guid;
1297 DWORD i;
1298 LONG r;
1300 TRACE("%s\n", debugstr_w(sei->lpFile) );
1302 hkey = ShellExecute_GetClassKey( sei );
1303 if ( !hkey )
1304 return ERROR_FUNCTION_FAILED;
1306 r = RegOpenKeyW( hkey, L"shellex\\ContextMenuHandlers", &hkeycm );
1307 if ( r == ERROR_SUCCESS )
1309 i = 0;
1310 while ( 1 )
1312 r = RegEnumKeyW( hkeycm, i++, szguid, ARRAY_SIZE(szguid));
1313 if ( r != ERROR_SUCCESS )
1314 break;
1316 hr = CLSIDFromString( szguid, &guid );
1317 if (SUCCEEDED(hr))
1319 /* stop at the first one that succeeds in running */
1320 hr = shellex_load_object_and_run( hkey, &guid, sei );
1321 if ( SUCCEEDED( hr ) )
1322 break;
1325 RegCloseKey( hkeycm );
1328 if ( hkey != sei->hkeyClass )
1329 RegCloseKey( hkey );
1330 return r;
1333 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 );
1335 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1337 WCHAR execCmd[1024], classname[1024];
1338 /* launch a document by fileclass like 'WordPad.Document.1' */
1339 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1340 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1341 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1342 DWORD resultLen;
1343 BOOL done;
1344 UINT_PTR rslt;
1346 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1347 if (cmask != SEE_MASK_CLASSNAME)
1349 WCHAR wcmd[1024];
1350 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1351 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1352 psei->lpVerb,
1353 execCmd, sizeof(execCmd));
1355 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1356 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1358 wcmd[0] = '\0';
1359 done = SHELL_ArgifyW(wcmd, ARRAY_SIZE(wcmd), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1360 if (!done && wszApplicationName[0])
1362 lstrcatW(wcmd, L" ");
1363 if (*wszApplicationName != '"')
1365 lstrcatW(wcmd, L"\"");
1366 lstrcatW(wcmd, wszApplicationName);
1367 lstrcatW(wcmd, L"\"");
1369 else
1370 lstrcatW(wcmd, wszApplicationName);
1372 if (resultLen > ARRAY_SIZE(wcmd))
1373 ERR("Argify buffer not large enough... truncating\n");
1374 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1377 lstrcpyW(classname, psei->lpClass);
1378 rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1380 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1381 if (33 > rslt)
1382 return rslt;
1383 rslt = SHELL_quote_and_execute( execCmd, L"", classname,
1384 wszApplicationName, NULL, psei,
1385 psei_out, execfunc );
1386 return rslt;
1389 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1391 WCHAR buffer[MAX_PATH];
1393 /* last chance to translate IDList: now also allow CLSID paths */
1394 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, ARRAY_SIZE(buffer)))) {
1395 if (buffer[0]==':' && buffer[1]==':') {
1396 /* open shell folder for the specified class GUID */
1397 if (lstrlenW(buffer) + 1 > parametersLen)
1398 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1399 lstrlenW(buffer) + 1, parametersLen);
1400 lstrcpynW(wszParameters, buffer, parametersLen);
1401 if (lstrlenW(L"explorer.exe") > dwApplicationNameLen)
1402 ERR("application len exceeds buffer size (%i), truncating\n",
1403 dwApplicationNameLen);
1404 lstrcpynW(wszApplicationName, L"explorer.exe", dwApplicationNameLen);
1406 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1407 } else {
1408 WCHAR target[MAX_PATH];
1409 DWORD attribs;
1410 DWORD resultLen;
1411 /* Check if we're executing a directory and if so use the
1412 handler for the Folder class */
1413 lstrcpyW(target, buffer);
1414 attribs = GetFileAttributesW(buffer);
1415 if (attribs != INVALID_FILE_ATTRIBUTES &&
1416 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1417 HCR_GetExecuteCommandW(0, L"Folder",
1418 sei->lpVerb,
1419 buffer, sizeof(buffer))) {
1420 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1421 buffer, target, sei->lpIDList, NULL, &resultLen);
1422 if (resultLen > dwApplicationNameLen)
1423 ERR("Argify buffer not large enough... truncating\n");
1425 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1430 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 )
1432 UINT_PTR retval;
1433 DWORD len;
1434 WCHAR *wszQuotedCmd;
1436 /* Length of quotes plus length of command plus NULL terminator */
1437 len = 2 + lstrlenW(wcmd) + 1;
1438 if (wszParameters[0])
1440 /* Length of space plus length of parameters */
1441 len += 1 + lstrlenW(wszParameters);
1443 wszQuotedCmd = heap_alloc(len * sizeof(WCHAR));
1444 /* Must quote to handle case where cmd contains spaces,
1445 * else security hole if malicious user creates executable file "C:\\Program"
1447 lstrcpyW(wszQuotedCmd, L"\"");
1448 lstrcatW(wszQuotedCmd, wcmd);
1449 lstrcatW(wszQuotedCmd, L"\"");
1450 if (wszParameters[0]) {
1451 lstrcatW(wszQuotedCmd, L" ");
1452 lstrcatW(wszQuotedCmd, wszParameters);
1454 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1455 if (*wszKeyname)
1456 retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1457 else
1458 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1459 heap_free(wszQuotedCmd);
1460 return retval;
1463 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1465 UINT_PTR retval;
1466 WCHAR *lpstrProtocol;
1467 LPCWSTR lpstrRes;
1468 INT iSize;
1469 DWORD len;
1471 lpstrRes = wcschr(lpFile, ':');
1472 if (lpstrRes)
1473 iSize = lpstrRes - lpFile;
1474 else
1475 iSize = lstrlenW(lpFile);
1477 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1478 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1479 len = iSize + lstrlenW(L"\\shell\\") + lstrlenW(L"\\command") + 1;
1480 if (psei->lpVerb && *psei->lpVerb)
1481 len += lstrlenW(psei->lpVerb);
1482 else
1483 len += lstrlenW(L"open");
1484 lpstrProtocol = heap_alloc(len * sizeof(WCHAR));
1485 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1486 lpstrProtocol[iSize] = '\0';
1487 lstrcatW(lpstrProtocol, L"\\shell\\");
1488 lstrcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb: L"open");
1489 lstrcatW(lpstrProtocol, L"\\command");
1491 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1492 wcmd, execfunc, psei, psei_out);
1493 heap_free(lpstrProtocol);
1494 return retval;
1497 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1499 WCHAR msg[2048];
1500 int error_code=GetLastError();
1502 if (retval == SE_ERR_NOASSOC)
1503 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, ARRAY_SIZE(msg));
1504 else
1505 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, ARRAY_SIZE(msg), NULL);
1507 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1510 static WCHAR *expand_environment( const WCHAR *str )
1512 WCHAR *buf;
1513 DWORD len;
1515 len = ExpandEnvironmentStringsW(str, NULL, 0);
1516 if (!len) return NULL;
1518 buf = heap_alloc(len * sizeof(WCHAR));
1519 if (!buf) return NULL;
1521 len = ExpandEnvironmentStringsW(str, buf, len);
1522 if (!len)
1524 heap_free(buf);
1525 return NULL;
1527 return buf;
1530 /*************************************************************************
1531 * SHELL_execute [Internal]
1533 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1535 static const DWORD unsupportedFlags =
1536 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1537 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1538 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1540 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1541 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd = NULL;
1542 DWORD dwApplicationNameLen = MAX_PATH+2;
1543 DWORD parametersLen = ARRAY_SIZE(parametersBuffer);
1544 DWORD wcmdLen = ARRAY_SIZE(wcmdBuffer);
1545 DWORD len;
1546 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1547 WCHAR *env;
1548 WCHAR wszKeyname[256];
1549 LPCWSTR lpFile;
1550 UINT_PTR retval = SE_ERR_NOASSOC;
1552 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1553 sei_tmp = *sei;
1555 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1556 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1557 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1558 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1559 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1560 debugstr_w(sei_tmp.lpClass) : "not used");
1562 sei->hProcess = NULL;
1564 /* make copies of all path/command strings */
1565 if (!sei_tmp.lpFile)
1567 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1568 *wszApplicationName = '\0';
1570 else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = lstrlenW(sei_tmp.lpFile))-1] == '\"')
1572 if(len-1 >= dwApplicationNameLen) dwApplicationNameLen = len;
1573 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1574 memcpy(wszApplicationName, sei_tmp.lpFile+1, len*sizeof(WCHAR));
1575 if(len > 2)
1576 wszApplicationName[len-2] = '\0';
1577 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1578 } else {
1579 DWORD l = lstrlenW(sei_tmp.lpFile)+1;
1580 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1581 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1582 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1585 wszParameters = parametersBuffer;
1586 if (sei_tmp.lpParameters)
1588 len = lstrlenW(sei_tmp.lpParameters) + 1;
1589 if (len > parametersLen)
1591 wszParameters = heap_alloc(len * sizeof(WCHAR));
1592 parametersLen = len;
1594 lstrcpyW(wszParameters, sei_tmp.lpParameters);
1596 else
1597 *wszParameters = '\0';
1599 wszDir = dirBuffer;
1600 if (sei_tmp.lpDirectory)
1602 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1603 if (len > ARRAY_SIZE(dirBuffer))
1604 wszDir = heap_alloc(len * sizeof(WCHAR));
1605 lstrcpyW(wszDir, sei_tmp.lpDirectory);
1607 else
1608 *wszDir = '\0';
1610 /* adjust string pointers to point to the new buffers */
1611 sei_tmp.lpFile = wszApplicationName;
1612 sei_tmp.lpParameters = wszParameters;
1613 sei_tmp.lpDirectory = wszDir;
1615 if (sei_tmp.fMask & unsupportedFlags)
1617 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1620 /* process the IDList */
1621 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1623 IShellExecuteHookW* pSEH;
1625 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1627 if (SUCCEEDED(hr))
1629 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1631 IShellExecuteHookW_Release(pSEH);
1633 if (hr == S_OK) {
1634 heap_free(wszApplicationName);
1635 if (wszParameters != parametersBuffer)
1636 heap_free(wszParameters);
1637 if (wszDir != dirBuffer)
1638 heap_free(wszDir);
1639 return TRUE;
1643 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1644 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1647 if (sei_tmp.fMask & SEE_MASK_DOENVSUBST)
1649 WCHAR *tmp;
1651 tmp = expand_environment(sei_tmp.lpFile);
1652 if (!tmp)
1654 retval = SE_ERR_OOM;
1655 goto end;
1657 heap_free(wszApplicationName);
1658 sei_tmp.lpFile = wszApplicationName = tmp;
1660 tmp = expand_environment(sei_tmp.lpDirectory);
1661 if (!tmp)
1663 retval = SE_ERR_OOM;
1664 goto end;
1666 if (wszDir != dirBuffer)
1667 heap_free(wszDir);
1668 sei_tmp.lpDirectory = wszDir = tmp;
1671 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1673 sei->hInstApp = (HINSTANCE) 33;
1674 heap_free(wszApplicationName);
1675 if (wszParameters != parametersBuffer)
1676 heap_free(wszParameters);
1677 if (wszDir != dirBuffer)
1678 heap_free(wszDir);
1679 return TRUE;
1682 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1684 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1685 execfunc );
1686 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1687 do_error_dialog(retval, sei_tmp.hwnd);
1688 heap_free(wszApplicationName);
1689 if (wszParameters != parametersBuffer)
1690 heap_free(wszParameters);
1691 if (wszDir != dirBuffer)
1692 heap_free(wszDir);
1693 return retval > 32;
1696 /* Has the IDList not yet been translated? */
1697 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1699 SHELL_translate_idlist( &sei_tmp, wszParameters,
1700 parametersLen,
1701 wszApplicationName,
1702 dwApplicationNameLen );
1705 /* convert file URLs */
1706 if (UrlIsFileUrlW(sei_tmp.lpFile))
1708 LPWSTR buf;
1709 DWORD size;
1711 size = MAX_PATH;
1712 buf = heap_alloc(size * sizeof(WCHAR));
1713 if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0))) {
1714 heap_free(buf);
1715 retval = SE_ERR_OOM;
1716 goto end;
1719 heap_free(wszApplicationName);
1720 wszApplicationName = buf;
1721 sei_tmp.lpFile = wszApplicationName;
1723 else /* or expand environment strings (not both!) */
1725 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1726 if (len>0)
1728 LPWSTR buf;
1729 buf = heap_alloc((len + 1) * sizeof(WCHAR));
1731 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1732 heap_free(wszApplicationName);
1733 wszApplicationName = buf;
1735 sei_tmp.lpFile = wszApplicationName;
1739 if (*sei_tmp.lpDirectory)
1741 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1742 if (len > 0)
1744 LPWSTR buf;
1745 len++;
1746 buf = heap_alloc(len * sizeof(WCHAR));
1747 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1748 if (wszDir != dirBuffer)
1749 heap_free(wszDir);
1750 wszDir = buf;
1751 sei_tmp.lpDirectory = wszDir;
1755 /* Else, try to execute the filename */
1756 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1757 lpFile = sei_tmp.lpFile;
1758 wcmd = wcmdBuffer;
1759 lstrcpyW(wcmd, wszApplicationName);
1760 if (sei_tmp.lpDirectory)
1762 LPCWSTR searchPath[] = {
1763 sei_tmp.lpDirectory,
1764 NULL
1766 PathFindOnPathW(wcmd, searchPath);
1768 retval = SHELL_quote_and_execute( wcmd, wszParameters, L"",
1769 wszApplicationName, NULL, &sei_tmp,
1770 sei, execfunc );
1771 if (retval > 32) {
1772 heap_free(wszApplicationName);
1773 if (wszParameters != parametersBuffer)
1774 heap_free(wszParameters);
1775 if (wszDir != dirBuffer)
1776 heap_free(wszDir);
1777 if (wcmd != wcmdBuffer)
1778 heap_free(wcmd);
1779 return TRUE;
1782 /* Else, try to find the executable */
1783 wcmd[0] = '\0';
1784 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1785 if (retval > 32) /* Found */
1787 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszKeyname,
1788 wszApplicationName, env, &sei_tmp,
1789 sei, execfunc );
1790 heap_free( env );
1792 else if (PathIsDirectoryW(lpFile))
1794 WCHAR wExec[MAX_PATH];
1795 WCHAR * lpQuotedFile = heap_alloc( sizeof(WCHAR) * (lstrlenW(lpFile) + 3) );
1797 if (lpQuotedFile)
1799 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, L"explorer",
1800 L"open", wExec, MAX_PATH,
1801 NULL, &env, NULL, NULL );
1802 if (retval > 32)
1804 lstrcpyW(lpQuotedFile, L"\"");
1805 lstrcatW(lpQuotedFile, lpFile);
1806 lstrcatW(lpQuotedFile, L"\"");
1807 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1808 wszKeyname,
1809 wszApplicationName, env,
1810 &sei_tmp, sei, execfunc );
1811 heap_free( env );
1813 heap_free( lpQuotedFile );
1815 else
1816 retval = 0; /* Out of memory */
1818 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1820 retval = SHELL_execute_url( lpFile, wcmd, &sei_tmp, sei, execfunc );
1822 /* Check if file specified is in the form www.??????.*** */
1823 else if (!wcsnicmp(lpFile, L"www", 3))
1825 /* if so, prefix lpFile with http:// and call ShellExecute */
1826 WCHAR lpstrTmpFile[256];
1827 lstrcpyW(lpstrTmpFile, L"http://");
1828 lstrcatW(lpstrTmpFile, lpFile);
1829 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1832 end:
1833 TRACE("retval %lu\n", retval);
1835 heap_free(wszApplicationName);
1836 if (wszParameters != parametersBuffer)
1837 heap_free(wszParameters);
1838 if (wszDir != dirBuffer)
1839 heap_free(wszDir);
1840 if (wcmd != wcmdBuffer)
1841 heap_free(wcmd);
1843 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1845 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1846 do_error_dialog(retval, sei_tmp.hwnd);
1847 return retval > 32;
1850 /*************************************************************************
1851 * ShellExecuteA [SHELL32.290]
1853 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
1854 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1856 SHELLEXECUTEINFOA sei;
1858 TRACE("%p,%s,%s,%s,%s,%d\n",
1859 hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
1860 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1862 sei.cbSize = sizeof(sei);
1863 sei.fMask = SEE_MASK_FLAG_NO_UI;
1864 sei.hwnd = hWnd;
1865 sei.lpVerb = lpVerb;
1866 sei.lpFile = lpFile;
1867 sei.lpParameters = lpParameters;
1868 sei.lpDirectory = lpDirectory;
1869 sei.nShow = iShowCmd;
1870 sei.lpIDList = 0;
1871 sei.lpClass = 0;
1872 sei.hkeyClass = 0;
1873 sei.dwHotKey = 0;
1874 sei.hProcess = 0;
1876 ShellExecuteExA (&sei);
1877 return sei.hInstApp;
1880 /*************************************************************************
1881 * ShellExecuteExA [SHELL32.292]
1884 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1886 SHELLEXECUTEINFOW seiW;
1887 BOOL ret;
1888 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1890 TRACE("%p\n", sei);
1892 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1894 if (sei->lpVerb)
1895 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1897 if (sei->lpFile)
1898 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1900 if (sei->lpParameters)
1901 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1903 if (sei->lpDirectory)
1904 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1906 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1907 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1908 else
1909 seiW.lpClass = NULL;
1911 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1913 sei->hInstApp = seiW.hInstApp;
1915 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1916 sei->hProcess = seiW.hProcess;
1918 SHFree(wVerb);
1919 SHFree(wFile);
1920 SHFree(wParameters);
1921 SHFree(wDirectory);
1922 SHFree(wClass);
1924 return ret;
1927 /*************************************************************************
1928 * ShellExecuteExW [SHELL32.293]
1931 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1933 return SHELL_execute( sei, SHELL_ExecuteW );
1936 /*************************************************************************
1937 * ShellExecuteW [SHELL32.294]
1938 * from shellapi.h
1939 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1940 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1942 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
1943 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1945 SHELLEXECUTEINFOW sei;
1947 TRACE("\n");
1948 sei.cbSize = sizeof(sei);
1949 sei.fMask = SEE_MASK_FLAG_NO_UI;
1950 sei.hwnd = hwnd;
1951 sei.lpVerb = lpVerb;
1952 sei.lpFile = lpFile;
1953 sei.lpParameters = lpParameters;
1954 sei.lpDirectory = lpDirectory;
1955 sei.nShow = nShowCmd;
1956 sei.lpIDList = 0;
1957 sei.lpClass = 0;
1958 sei.hkeyClass = 0;
1959 sei.dwHotKey = 0;
1960 sei.hProcess = 0;
1962 SHELL_execute( &sei, SHELL_ExecuteW );
1963 return sei.hInstApp;
1966 /*************************************************************************
1967 * WOWShellExecute [SHELL32.@]
1969 * FIXME: the callback function most likely doesn't work the same way on Windows.
1971 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb,LPCSTR lpFile,
1972 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
1974 SHELLEXECUTEINFOW seiW;
1975 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
1976 HANDLE hProcess = 0;
1978 seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
1979 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
1980 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
1981 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
1983 seiW.cbSize = sizeof(seiW);
1984 seiW.fMask = 0;
1985 seiW.hwnd = hWnd;
1986 seiW.nShow = iShowCmd;
1987 seiW.lpIDList = 0;
1988 seiW.lpClass = 0;
1989 seiW.hkeyClass = 0;
1990 seiW.dwHotKey = 0;
1991 seiW.hProcess = hProcess;
1993 SHELL_execute( &seiW, callback );
1995 SHFree(wVerb);
1996 SHFree(wFile);
1997 SHFree(wParameters);
1998 SHFree(wDirectory);
1999 return seiW.hInstApp;
2002 /*************************************************************************
2003 * ShellExec_RunDLLW [SHELL32.@]
2005 void WINAPI ShellExec_RunDLLW(HWND hwnd, HINSTANCE instance, WCHAR *cmdline, int cmdshow)
2007 BOOL in_single_quotes = FALSE, in_double_quotes = FALSE;
2008 WCHAR *args;
2010 TRACE("%p, %p, %s, %d\n", hwnd, instance, debugstr_w(cmdline), cmdshow);
2012 /* Replace the first whitespace character in the command line string with a
2013 null terminator to separate the program name from the program arguments */
2014 for (args = cmdline; *args; args++)
2016 switch (*args)
2018 case '\\':
2019 args++; /* skip the next character */
2020 break;
2021 case '\'':
2022 if (!in_double_quotes)
2023 in_single_quotes = !in_single_quotes;
2024 break;
2025 case '"':
2026 if (!in_single_quotes)
2027 in_double_quotes = !in_double_quotes;
2028 break;
2029 case ' ':
2030 case '\t':
2031 if (!in_single_quotes && !in_double_quotes)
2033 *args = 0;
2034 args++;
2035 goto execute;
2040 execute:
2041 ShellExecuteW(hwnd, NULL, cmdline, args, NULL, cmdshow);
2044 /*************************************************************************
2045 * ShellExec_RunDLLA [SHELL32.@]
2047 void WINAPI ShellExec_RunDLLA(HWND hwnd, HINSTANCE instance, CHAR *cmdline, int cmdshow)
2049 WCHAR *cmdlineW;
2050 ShellExec_RunDLLW(hwnd, instance, __SHCloneStrAtoW(&cmdlineW, cmdline), cmdshow);
2051 SHFree(cmdlineW);
2054 /*************************************************************************
2055 * OpenAs_RunDLLA [SHELL32.@]
2057 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2059 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2062 /*************************************************************************
2063 * OpenAs_RunDLLW [SHELL32.@]
2065 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2067 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2070 /*************************************************************************
2071 * RegenerateUserEnvironment [SHELL32.@]
2073 BOOL WINAPI RegenerateUserEnvironment(WCHAR *wunknown, BOOL bunknown)
2075 FIXME("stub: %p, %d\n", wunknown, bunknown);
2076 return FALSE;