wow32: Use spec file imports.
[wine.git] / dlls / shell32 / shlexec.c
blobda2b5fdc2b0cbdc98c865900a7a1260211540f4e
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 = heap_alloc( 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 lstrcpyW(buffer, L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
432 lstrcatW(buffer, szName);
433 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
434 if (res) goto end;
436 len = MAX_PATH*sizeof(WCHAR);
437 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
438 if (res) goto end;
439 found = TRUE;
441 if (env)
443 DWORD count = sizeof(buffer);
444 if (!RegQueryValueExW(hkApp, L"Path", NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
445 *env = SHELL_BuildEnvW( buffer );
448 end:
449 if (hkApp) RegCloseKey(hkApp);
450 return found;
453 /*************************************************************************
454 * SHELL_FindExecutableByVerb [Internal]
456 * called from SHELL_FindExecutable or SHELL_execute_class
457 * in/out:
458 * classname a buffer, big enough, to get the key name to do actually the
459 * command "WordPad.Document.1\\shell\\open\\command"
460 * passed as "WordPad.Document.1"
461 * in:
462 * lpVerb the operation on it (open)
463 * commandlen the size of command buffer (in bytes)
464 * out:
465 * command a buffer, to store the command to do the
466 * operation on the file
467 * key a buffer, big enough, to get the key name to do actually the
468 * command "WordPad.Document.1\\shell\\open\\command"
469 * Can be NULL
471 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
473 HKEY hkeyClass;
474 WCHAR verb[MAX_PATH];
476 if (*classname == '.')
478 /* Extension, default key value holds class name. Extension may also have
479 * empty class name and shell\verb\command subkey. */
480 WCHAR class[MAX_PATH];
481 LONG len;
483 len = sizeof(class);
484 if (!RegQueryValueW(HKEY_CLASSES_ROOT, classname, class, &len) && *class)
485 wcscpy(classname, class);
488 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
489 return SE_ERR_NOASSOC;
490 if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, ARRAY_SIZE(verb)))
491 return SE_ERR_NOASSOC;
492 RegCloseKey(hkeyClass);
494 /* Looking for ...buffer\shell\<verb>\command */
495 lstrcatW(classname, L"\\shell\\");
496 lstrcatW(classname, verb);
497 lstrcatW(classname, L"\\command");
499 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
500 &commandlen) == ERROR_SUCCESS)
502 commandlen /= sizeof(WCHAR);
503 if (key) lstrcpyW(key, classname);
504 #if 0
505 LPWSTR tmp;
506 WCHAR param[256];
507 LONG paramlen = sizeof(param);
509 /* FIXME: it seems all Windows version don't behave the same here.
510 * the doc states that this ddeexec information can be found after
511 * the exec names.
512 * on Win98, it doesn't appear, but I think it does on Win2k
514 /* Get the parameters needed by the application
515 from the associated ddeexec key */
516 tmp = wcsstr(classname, L"\\command");
517 tmp[0] = '\0';
518 lstrcatW(classname, wDdeexec);
519 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
520 &paramlen) == ERROR_SUCCESS)
522 paramlen /= sizeof(WCHAR);
523 lstrcatW(command, L" ");
524 lstrcatW(command, param);
525 commandlen += paramlen;
527 #endif
529 command[commandlen] = '\0';
531 return 33; /* FIXME see SHELL_FindExecutable() */
534 return SE_ERR_NOASSOC;
537 /*************************************************************************
538 * SHELL_FindExecutable [Internal]
540 * Utility for code sharing between FindExecutable and ShellExecute
541 * in:
542 * lpFile the name of a file
543 * lpVerb the operation on it (open)
544 * out:
545 * lpResult a buffer, big enough :-(, to store the command to do the
546 * operation on the file
547 * key a buffer, big enough, to get the key name to do actually the
548 * command (it'll be used afterwards for more information
549 * on the operation)
551 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
552 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
554 WCHAR *extension = NULL; /* pointer to file extension */
555 WCHAR classname[256]; /* registry name for this file type */
556 LONG classnamelen = sizeof(classname); /* length of above */
557 WCHAR command[1024]; /* command from registry */
558 WCHAR wBuffer[256]; /* Used to GetProfileString */
559 UINT retval = SE_ERR_NOASSOC;
560 WCHAR *tok; /* token pointer */
561 WCHAR xlpFile[256]; /* result of SearchPath */
562 DWORD attribs; /* file attributes */
564 TRACE("%s\n", debugstr_w(lpFile));
566 if (!lpResult)
567 return ERROR_INVALID_PARAMETER;
569 xlpFile[0] = '\0';
570 lpResult[0] = '\0'; /* Start off with an empty return string */
571 if (key) *key = '\0';
573 /* trap NULL parameters on entry */
574 if (!lpFile)
576 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
577 debugstr_w(lpFile), debugstr_w(lpResult));
578 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
581 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
583 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
584 return 33;
587 if (SearchPathW(lpPath, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
589 TRACE("SearchPathW returned non-zero\n");
590 lpFile = xlpFile;
591 /* The file was found in the application-supplied default directory (or the system search path) */
593 else if (lpPath && SearchPathW(NULL, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
595 TRACE("SearchPathW returned non-zero\n");
596 lpFile = xlpFile;
597 /* The file was found in one of the directories in the system-wide search path */
600 attribs = GetFileAttributesW(lpFile);
601 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
603 lstrcpyW(classname, L"Folder");
605 else
607 /* Did we get something? Anything? */
608 if (xlpFile[0]==0)
610 TRACE("Returning SE_ERR_FNF\n");
611 return SE_ERR_FNF;
613 /* First thing we need is the file's extension */
614 extension = wcsrchr(xlpFile, '.'); /* Assume last "." is the one; */
615 /* File->Run in progman uses */
616 /* .\FILE.EXE :( */
617 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
619 if (extension == NULL || extension[1]==0)
621 WARN("Returning SE_ERR_NOASSOC\n");
622 return SE_ERR_NOASSOC;
625 /* Three places to check: */
626 /* 1. win.ini, [windows], programs (NB no leading '.') */
627 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
628 /* 3. win.ini, [extensions], extension (NB no leading '.' */
629 /* All I know of the order is that registry is checked before */
630 /* extensions; however, it'd make sense to check the programs */
631 /* section first, so that's what happens here. */
633 /* See if it's a program - if GetProfileString fails, we skip this
634 * section. Actually, if GetProfileString fails, we've probably
635 * got a lot more to worry about than running a program... */
636 if (GetProfileStringW(L"windows", L"programs", L"exe pif bat cmd com", wBuffer, ARRAY_SIZE(wBuffer)) > 0)
638 CharLowerW(wBuffer);
639 tok = wBuffer;
640 while (*tok)
642 WCHAR *p = tok;
643 while (*p && *p != ' ' && *p != '\t') p++;
644 if (*p)
646 *p++ = 0;
647 while (*p == ' ' || *p == '\t') p++;
650 if (wcsicmp(tok, &extension[1]) == 0) /* have to skip the leading "." */
652 lstrcpyW(lpResult, xlpFile);
653 /* Need to perhaps check that the file has a path
654 * attached */
655 TRACE("found %s\n", debugstr_w(lpResult));
656 return 33;
657 /* Greater than 32 to indicate success */
659 tok = p;
663 /* Check registry */
664 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
665 &classnamelen) == ERROR_SUCCESS)
667 classnamelen /= sizeof(WCHAR);
668 if (classnamelen == ARRAY_SIZE(classname))
669 classnamelen--;
670 classname[classnamelen] = '\0';
671 TRACE("File type: %s\n", debugstr_w(classname));
673 else
675 *classname = '\0';
679 if (*classname)
681 /* pass the verb string to SHELL_FindExecutableByVerb() */
682 retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
684 if (retval > 32)
686 DWORD finishedLen;
687 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
688 if (finishedLen > resultLen)
689 ERR("Argify buffer not large enough.. truncated\n");
691 /* Remove double quotation marks and command line arguments */
692 if (*lpResult == '"')
694 WCHAR *p = lpResult;
695 while (*(p + 1) != '"')
697 *p = *(p + 1);
698 p++;
700 *p = '\0';
702 else
704 /* Truncate on first space */
705 WCHAR *p = lpResult;
706 while (*p != ' ' && *p != '\0')
707 p++;
708 *p='\0';
712 else /* Check win.ini */
714 /* Toss the leading dot */
715 extension++;
716 if (GetProfileStringW(L"extensions", extension, L"", command, ARRAY_SIZE(command)) > 0)
718 if (*command)
720 lstrcpyW(lpResult, command);
721 tok = wcschr(lpResult, '^'); /* should be ^.extension? */
722 if (tok != NULL)
724 tok[0] = '\0';
725 lstrcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
726 tok = wcschr(command, '^'); /* see above */
727 if ((tok != NULL) && (lstrlenW(tok)>5))
729 lstrcatW(lpResult, &tok[5]);
732 retval = 33; /* FIXME - see above */
737 TRACE("returning %s\n", debugstr_w(lpResult));
738 return retval;
741 /******************************************************************
742 * dde_cb
744 * callback for the DDE connection. not really useful
746 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
747 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
748 ULONG_PTR dwData1, ULONG_PTR dwData2)
750 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08Ix, %08Ix\n",
751 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
752 return NULL;
755 /******************************************************************
756 * dde_connect
758 * ShellExecute helper. Used to do an operation with a DDE connection
760 * Handles both the direct connection (try #1), and if it fails,
761 * launching an application and trying (#2) to connect to it
764 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
765 const WCHAR* lpFile, WCHAR *env,
766 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
767 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
769 WCHAR regkey[256];
770 WCHAR * endkey = regkey + lstrlenW(key);
771 WCHAR app[256], topic[256], ifexec[256], static_res[256];
772 WCHAR * dynamic_res=NULL;
773 WCHAR * res;
774 LONG applen, topiclen, ifexeclen;
775 WCHAR * exec;
776 DWORD ddeInst = 0;
777 DWORD tid;
778 DWORD resultLen, endkeyLen;
779 HSZ hszApp, hszTopic;
780 HCONV hConv;
781 HDDEDATA hDdeData;
782 unsigned ret = SE_ERR_NOASSOC;
783 BOOL unicode = !(GetVersion() & 0x80000000);
785 if (lstrlenW(key) + 1 > ARRAY_SIZE(regkey))
787 FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
788 return 2;
790 lstrcpyW(regkey, key);
791 endkeyLen = ARRAY_SIZE(regkey) - (endkey - regkey);
792 if (lstrlenW(L"\\application") + 1 > endkeyLen)
794 FIXME("endkey overruns buffer\n");
795 return 2;
797 lstrcpyW(endkey, L"\\application");
798 applen = sizeof(app);
799 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
801 WCHAR command[1024], fullpath[MAX_PATH];
802 LPWSTR ptr = NULL;
803 DWORD ret = 0;
805 /* Get application command from start string and find filename of application */
806 if (*start == '"')
808 if (lstrlenW(start + 1) + 1 > ARRAY_SIZE(command))
810 FIXME("size of input parameter %s larger than buffer\n",
811 debugstr_w(start + 1));
812 return 2;
814 lstrcpyW(command, start+1);
815 if ((ptr = wcschr(command, '"')))
816 *ptr = 0;
817 ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
819 else
821 LPCWSTR p;
822 LPWSTR space;
823 for (p=start; (space=wcschr(p, ' ')); p=space+1)
825 int idx = space-start;
826 memcpy(command, start, idx*sizeof(WCHAR));
827 command[idx] = '\0';
828 if ((ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr)))
829 break;
831 if (!ret)
832 ret = SearchPathW(NULL, start, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
835 if (!ret)
837 ERR("Unable to find application path for command %s\n", debugstr_w(start));
838 return ERROR_ACCESS_DENIED;
840 if (lstrlenW(ptr) + 1 > ARRAY_SIZE(app))
842 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
843 return 2;
845 lstrcpyW(app, ptr);
847 /* Remove extensions (including .so) */
848 ptr = app + lstrlenW(app) - 3;
849 if (ptr > app && !wcscmp(ptr, L".so"))
850 *ptr = 0;
852 ptr = wcsrchr(app, '.');
853 assert(ptr);
854 *ptr = 0;
857 if (lstrlenW(L"\\topic") + 1 > endkeyLen)
859 FIXME("endkey overruns buffer\n");
860 return 2;
862 lstrcpyW(endkey, L"\\topic");
863 topiclen = sizeof(topic);
864 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
865 lstrcpyW(topic, L"System");
867 if (unicode)
869 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
870 return 2;
872 else
874 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
875 return 2;
878 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
879 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
881 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
882 exec = ddeexec;
883 if (!hConv)
885 TRACE("Launching %s\n", debugstr_w(start));
886 ret = execfunc(start, env, TRUE, psei, psei_out);
887 if (ret <= 32)
889 TRACE("Couldn't launch\n");
890 goto error;
892 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
893 if (!hConv)
895 TRACE("Couldn't connect. ret=%d\n", ret);
896 DdeUninitialize(ddeInst);
897 SetLastError(ERROR_DDE_FAIL);
898 return 30; /* whatever */
900 if (lstrlenW(L"\\ifexec") + 1 > endkeyLen)
902 FIXME("endkey overruns buffer\n");
903 return 2;
905 lstrcpyW(endkey, L"\\ifexec");
906 ifexeclen = sizeof(ifexec);
907 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
909 exec = ifexec;
913 SHELL_ArgifyW(static_res, ARRAY_SIZE(static_res), exec, lpFile, pidl, szCommandline, &resultLen);
914 if (resultLen > ARRAY_SIZE(static_res))
916 res = dynamic_res = heap_alloc(resultLen * sizeof(WCHAR));
917 SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL);
919 else
920 res = static_res;
921 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
923 /* It's documented in the KB 330337 that IE has a bug and returns
924 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
926 if (unicode)
927 hDdeData = DdeClientTransaction((LPBYTE)res, (lstrlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
928 XTYP_EXECUTE, 30000, &tid);
929 else
931 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
932 char *resA = heap_alloc(lenA);
933 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
934 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
935 XTYP_EXECUTE, 10000, &tid );
936 heap_free(resA);
938 if (hDdeData)
939 DdeFreeDataHandle(hDdeData);
940 else
941 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
942 ret = 33;
944 heap_free(dynamic_res);
946 DdeDisconnect(hConv);
948 error:
949 DdeUninitialize(ddeInst);
951 return ret;
954 /*************************************************************************
955 * execute_from_key [Internal]
957 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
958 LPCWSTR executable_name,
959 SHELL_ExecuteW32 execfunc,
960 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
962 WCHAR cmd[256], param[1024], ddeexec[256];
963 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
964 UINT_PTR retval = SE_ERR_NOASSOC;
965 DWORD resultLen;
966 LPWSTR tmp;
968 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
969 debugstr_w(szCommandline), debugstr_w(executable_name));
971 cmd[0] = '\0';
972 param[0] = '\0';
974 /* Get the application from the registry */
975 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
977 TRACE("got cmd: %s\n", debugstr_w(cmd));
979 /* Is there a replace() function anywhere? */
980 cmdlen /= sizeof(WCHAR);
981 if (cmdlen >= ARRAY_SIZE(cmd))
982 cmdlen = ARRAY_SIZE(cmd) - 1;
983 cmd[cmdlen] = '\0';
984 SHELL_ArgifyW(param, ARRAY_SIZE(param), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
985 if (resultLen > ARRAY_SIZE(param))
986 ERR("Argify buffer not large enough, truncating\n");
989 /* Get the parameters needed by the application
990 from the associated ddeexec key */
991 tmp = wcsstr(key, L"command");
992 assert(tmp);
993 lstrcpyW(tmp, L"ddeexec");
995 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
997 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
998 if (!param[0]) lstrcpyW(param, executable_name);
999 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
1001 else if (param[0])
1003 TRACE("executing: %s\n", debugstr_w(param));
1004 retval = execfunc(param, env, FALSE, psei, psei_out);
1006 else
1007 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1009 return retval;
1012 /*************************************************************************
1013 * FindExecutableA [SHELL32.@]
1015 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1017 HINSTANCE retval;
1018 WCHAR *wFile = NULL, *wDirectory = NULL;
1019 WCHAR wResult[MAX_PATH];
1021 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1022 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1024 retval = FindExecutableW(wFile, wDirectory, wResult);
1025 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1026 SHFree( wFile );
1027 SHFree( wDirectory );
1029 TRACE("returning %s\n", lpResult);
1030 return retval;
1033 /*************************************************************************
1034 * FindExecutableW [SHELL32.@]
1036 * This function returns the executable associated with the specified file
1037 * for the default verb.
1039 * PARAMS
1040 * lpFile [I] The file to find the association for. This must refer to
1041 * an existing file otherwise FindExecutable fails and returns
1042 * SE_ERR_FNF.
1043 * lpResult [O] Points to a buffer into which the executable path is
1044 * copied. This parameter must not be NULL otherwise
1045 * FindExecutable() segfaults. The buffer must be of size at
1046 * least MAX_PATH characters.
1048 * RETURNS
1049 * A value greater than 32 on success, less than or equal to 32 otherwise.
1050 * See the SE_ERR_* constants.
1052 * NOTES
1053 * On Windows XP and 2003, FindExecutable() seems to first convert the
1054 * filename into 8.3 format, thus taking into account only the first three
1055 * characters of the extension, and expects to find an association for those.
1056 * However other Windows versions behave sanely.
1058 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1060 UINT_PTR retval = SE_ERR_NOASSOC;
1061 WCHAR old_dir[1024];
1062 WCHAR res[MAX_PATH];
1064 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1066 lpResult[0] = '\0'; /* Start off with an empty return string */
1067 if (lpFile == NULL)
1068 return (HINSTANCE)SE_ERR_FNF;
1070 if (lpDirectory)
1072 GetCurrentDirectoryW(ARRAY_SIZE(old_dir), old_dir);
1073 SetCurrentDirectoryW(lpDirectory);
1076 retval = SHELL_FindExecutable(lpDirectory, lpFile, L"open", res, MAX_PATH, NULL, NULL, NULL, NULL);
1078 if (retval > 32)
1079 lstrcpyW(lpResult, res);
1081 TRACE("returning %s\n", debugstr_w(lpResult));
1082 if (lpDirectory)
1083 SetCurrentDirectoryW(old_dir);
1084 return (HINSTANCE)retval;
1087 /* FIXME: is this already implemented somewhere else? */
1088 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1090 LPCWSTR ext = NULL, lpClass = NULL;
1091 LPWSTR cls = NULL;
1092 DWORD type = 0, sz = 0;
1093 HKEY hkey = 0;
1094 LONG r;
1096 if (sei->fMask & SEE_MASK_CLASSALL)
1097 return sei->hkeyClass;
1099 if (sei->fMask & SEE_MASK_CLASSNAME)
1100 lpClass = sei->lpClass;
1101 else
1103 ext = PathFindExtensionW( sei->lpFile );
1104 TRACE("ext = %s\n", debugstr_w( ext ) );
1105 if (!ext)
1106 return hkey;
1108 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1109 if (r != ERROR_SUCCESS )
1110 return hkey;
1112 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1113 if ( r == ERROR_SUCCESS && type == REG_SZ )
1115 sz += sizeof (WCHAR);
1116 cls = heap_alloc( sz );
1117 cls[0] = 0;
1118 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1121 RegCloseKey( hkey );
1122 lpClass = cls;
1125 TRACE("class = %s\n", debugstr_w(lpClass) );
1127 hkey = 0;
1128 if ( lpClass )
1129 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1131 heap_free( cls );
1133 return hkey;
1136 static HRESULT shellex_get_dataobj( LPSHELLEXECUTEINFOW sei, IDataObject **dataobj )
1138 LPCITEMIDLIST pidllast = NULL;
1139 IShellFolder *shf = NULL;
1140 LPITEMIDLIST pidl = NULL;
1141 HRESULT r = SE_ERR_DLLNOTFOUND;
1143 if (sei->fMask & SEE_MASK_CLASSALL)
1144 pidl = sei->lpIDList;
1145 else
1147 WCHAR fullpath[MAX_PATH];
1148 BOOL ret;
1150 fullpath[0] = 0;
1151 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1152 if (!ret)
1153 goto end;
1155 pidl = ILCreateFromPathW( fullpath );
1158 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1159 if ( FAILED( r ) )
1160 goto end;
1162 r = IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1163 &IID_IDataObject, NULL, (void**)dataobj );
1165 end:
1166 if ( pidl != sei->lpIDList )
1167 ILFree( pidl );
1168 if ( shf )
1169 IShellFolder_Release( shf );
1170 return r;
1173 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1174 LPSHELLEXECUTEINFOW sei )
1176 IContextMenu *cm = NULL;
1177 CMINVOKECOMMANDINFOEX ici;
1178 MENUITEMINFOW info;
1179 WCHAR string[0x80];
1180 INT i, n, def = -1;
1181 HMENU hmenu = 0;
1182 HRESULT r;
1184 TRACE("%p %p\n", obj, sei );
1186 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1187 if ( FAILED( r ) )
1188 return r;
1190 hmenu = CreateMenu();
1191 if ( !hmenu )
1192 goto end;
1194 /* the number of the last menu added is returned in r */
1195 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1196 if ( FAILED( r ) )
1197 goto end;
1199 n = GetMenuItemCount( hmenu );
1200 for ( i = 0; i < n; i++ )
1202 memset( &info, 0, sizeof info );
1203 info.cbSize = sizeof info;
1204 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1205 info.dwTypeData = string;
1206 info.cch = ARRAY_SIZE(string);
1207 string[0] = 0;
1208 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1210 TRACE("menu %d %s %08x %08Ix %08x %08x\n", i, debugstr_w(string),
1211 info.fState, info.dwItemData, info.fType, info.wID );
1212 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1213 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1215 def = i;
1216 break;
1220 r = E_FAIL;
1221 if ( def == -1 )
1222 goto end;
1224 memset( &ici, 0, sizeof ici );
1225 ici.cbSize = sizeof ici;
1226 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE|SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1227 ici.nShow = sei->nShow;
1228 ici.lpVerb = MAKEINTRESOURCEA( def );
1229 ici.hwnd = sei->hwnd;
1230 ici.lpParametersW = sei->lpParameters;
1232 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1234 TRACE("invoke command returned %08lx\n", r );
1236 end:
1237 if ( hmenu )
1238 DestroyMenu( hmenu );
1239 if ( cm )
1240 IContextMenu_Release( cm );
1241 return r;
1244 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1246 IDataObject *dataobj = NULL;
1247 IObjectWithSite *ows = NULL;
1248 IShellExtInit *obj = NULL;
1249 HRESULT r;
1251 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1253 r = CoInitialize( NULL );
1254 if ( FAILED( r ) )
1255 goto end;
1257 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1258 &IID_IShellExtInit, (LPVOID*)&obj );
1259 if ( FAILED( r ) )
1261 ERR("failed %08lx\n", r );
1262 goto end;
1265 r = shellex_get_dataobj( sei, &dataobj );
1266 if ( FAILED( r ) )
1268 ERR("failed to get data object\n");
1269 goto end;
1272 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1273 if ( FAILED( r ) )
1274 goto end;
1276 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1277 if ( FAILED( r ) )
1278 goto end;
1280 IObjectWithSite_SetSite( ows, NULL );
1282 r = shellex_run_context_menu_default( obj, sei );
1284 end:
1285 if ( ows )
1286 IObjectWithSite_Release( ows );
1287 if ( dataobj )
1288 IDataObject_Release( dataobj );
1289 if ( obj )
1290 IShellExtInit_Release( obj );
1291 CoUninitialize();
1292 return r;
1296 /*************************************************************************
1297 * ShellExecute_FromContextMenu [Internal]
1299 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1301 HKEY hkey, hkeycm = 0;
1302 WCHAR szguid[39];
1303 HRESULT hr;
1304 GUID guid;
1305 DWORD i;
1306 LONG r;
1308 TRACE("%s\n", debugstr_w(sei->lpFile) );
1310 hkey = ShellExecute_GetClassKey( sei );
1311 if ( !hkey )
1312 return ERROR_FUNCTION_FAILED;
1314 r = RegOpenKeyW( hkey, L"shellex\\ContextMenuHandlers", &hkeycm );
1315 if ( r == ERROR_SUCCESS )
1317 i = 0;
1318 while ( 1 )
1320 r = RegEnumKeyW( hkeycm, i++, szguid, ARRAY_SIZE(szguid));
1321 if ( r != ERROR_SUCCESS )
1322 break;
1324 hr = CLSIDFromString( szguid, &guid );
1325 if (SUCCEEDED(hr))
1327 /* stop at the first one that succeeds in running */
1328 hr = shellex_load_object_and_run( hkey, &guid, sei );
1329 if ( SUCCEEDED( hr ) )
1330 break;
1333 RegCloseKey( hkeycm );
1336 if ( hkey != sei->hkeyClass )
1337 RegCloseKey( hkey );
1338 return r;
1341 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 );
1343 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1345 WCHAR execCmd[1024], classname[1024];
1346 /* launch a document by fileclass like 'WordPad.Document.1' */
1347 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1348 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1349 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1350 DWORD resultLen;
1351 BOOL done;
1352 UINT_PTR rslt;
1354 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1355 if (cmask != SEE_MASK_CLASSNAME)
1357 WCHAR wcmd[1024];
1358 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1359 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1360 psei->lpVerb,
1361 execCmd, sizeof(execCmd));
1363 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1364 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1366 wcmd[0] = '\0';
1367 done = SHELL_ArgifyW(wcmd, ARRAY_SIZE(wcmd), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1368 if (!done && wszApplicationName[0])
1370 lstrcatW(wcmd, L" ");
1371 if (*wszApplicationName != '"')
1373 lstrcatW(wcmd, L"\"");
1374 lstrcatW(wcmd, wszApplicationName);
1375 lstrcatW(wcmd, L"\"");
1377 else
1378 lstrcatW(wcmd, wszApplicationName);
1380 if (resultLen > ARRAY_SIZE(wcmd))
1381 ERR("Argify buffer not large enough... truncating\n");
1382 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1385 lstrcpyW(classname, psei->lpClass);
1386 rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1388 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1389 if (33 > rslt)
1390 return rslt;
1391 rslt = SHELL_quote_and_execute( execCmd, L"", classname,
1392 wszApplicationName, NULL, psei,
1393 psei_out, execfunc );
1394 return rslt;
1397 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1399 WCHAR buffer[MAX_PATH];
1401 /* last chance to translate IDList: now also allow CLSID paths */
1402 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, ARRAY_SIZE(buffer)))) {
1403 if (buffer[0]==':' && buffer[1]==':') {
1404 /* open shell folder for the specified class GUID */
1405 if (lstrlenW(buffer) + 1 > parametersLen)
1406 ERR("parameters len exceeds buffer size (%i > %li), truncating\n",
1407 lstrlenW(buffer) + 1, parametersLen);
1408 lstrcpynW(wszParameters, buffer, parametersLen);
1409 if (lstrlenW(L"explorer.exe") > dwApplicationNameLen)
1410 ERR("application len exceeds buffer size (%li), truncating\n",
1411 dwApplicationNameLen);
1412 lstrcpynW(wszApplicationName, L"explorer.exe", dwApplicationNameLen);
1414 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1415 } else {
1416 WCHAR target[MAX_PATH];
1417 DWORD attribs;
1418 DWORD resultLen;
1419 /* Check if we're executing a directory and if so use the
1420 handler for the Folder class */
1421 lstrcpyW(target, buffer);
1422 attribs = GetFileAttributesW(buffer);
1423 if (attribs != INVALID_FILE_ATTRIBUTES &&
1424 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1425 HCR_GetExecuteCommandW(0, L"Folder",
1426 sei->lpVerb,
1427 buffer, sizeof(buffer))) {
1428 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1429 buffer, target, sei->lpIDList, NULL, &resultLen);
1430 if (resultLen > dwApplicationNameLen)
1431 ERR("Argify buffer not large enough... truncating\n");
1433 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1438 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 )
1440 UINT_PTR retval;
1441 DWORD len;
1442 WCHAR *wszQuotedCmd;
1444 /* Length of quotes plus length of command plus NULL terminator */
1445 len = 2 + lstrlenW(wcmd) + 1;
1446 if (wszParameters[0])
1448 /* Length of space plus length of parameters */
1449 len += 1 + lstrlenW(wszParameters);
1451 wszQuotedCmd = heap_alloc(len * sizeof(WCHAR));
1452 /* Must quote to handle case where cmd contains spaces,
1453 * else security hole if malicious user creates executable file "C:\\Program"
1455 lstrcpyW(wszQuotedCmd, L"\"");
1456 lstrcatW(wszQuotedCmd, wcmd);
1457 lstrcatW(wszQuotedCmd, L"\"");
1458 if (wszParameters[0]) {
1459 lstrcatW(wszQuotedCmd, L" ");
1460 lstrcatW(wszQuotedCmd, wszParameters);
1462 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1463 if (*wszKeyname)
1464 retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1465 else
1466 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1467 heap_free(wszQuotedCmd);
1468 return retval;
1471 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1473 UINT_PTR retval;
1474 WCHAR *lpstrProtocol;
1475 LPCWSTR lpstrRes;
1476 INT iSize;
1477 DWORD len;
1479 lpstrRes = wcschr(lpFile, ':');
1480 if (lpstrRes)
1481 iSize = lpstrRes - lpFile;
1482 else
1483 iSize = lstrlenW(lpFile);
1485 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1486 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1487 len = iSize + lstrlenW(L"\\shell\\") + lstrlenW(L"\\command") + 1;
1488 if (psei->lpVerb && *psei->lpVerb)
1489 len += lstrlenW(psei->lpVerb);
1490 else
1491 len += lstrlenW(L"open");
1492 lpstrProtocol = heap_alloc(len * sizeof(WCHAR));
1493 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1494 lpstrProtocol[iSize] = '\0';
1495 lstrcatW(lpstrProtocol, L"\\shell\\");
1496 lstrcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb: L"open");
1497 lstrcatW(lpstrProtocol, L"\\command");
1499 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1500 wcmd, execfunc, psei, psei_out);
1501 heap_free(lpstrProtocol);
1502 return retval;
1505 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1507 WCHAR msg[2048];
1508 int error_code=GetLastError();
1510 if (retval == SE_ERR_NOASSOC)
1511 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, ARRAY_SIZE(msg));
1512 else
1513 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, ARRAY_SIZE(msg), NULL);
1515 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1518 static WCHAR *expand_environment( const WCHAR *str )
1520 WCHAR *buf;
1521 DWORD len;
1523 len = ExpandEnvironmentStringsW(str, NULL, 0);
1524 if (!len) return NULL;
1526 buf = heap_alloc(len * sizeof(WCHAR));
1527 if (!buf) return NULL;
1529 len = ExpandEnvironmentStringsW(str, buf, len);
1530 if (!len)
1532 heap_free(buf);
1533 return NULL;
1535 return buf;
1538 /*************************************************************************
1539 * SHELL_execute [Internal]
1541 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1543 static const DWORD unsupportedFlags =
1544 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1545 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1546 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1548 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1549 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd = NULL;
1550 DWORD dwApplicationNameLen = MAX_PATH+2;
1551 DWORD parametersLen = ARRAY_SIZE(parametersBuffer);
1552 DWORD wcmdLen = ARRAY_SIZE(wcmdBuffer);
1553 DWORD len;
1554 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1555 WCHAR *env;
1556 WCHAR wszKeyname[256];
1557 LPCWSTR lpFile;
1558 UINT_PTR retval = SE_ERR_NOASSOC;
1560 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1561 sei_tmp = *sei;
1563 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1564 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1565 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1566 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1567 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1568 debugstr_w(sei_tmp.lpClass) : "not used");
1570 sei->hProcess = NULL;
1572 /* make copies of all path/command strings */
1573 if (!sei_tmp.lpFile)
1575 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1576 *wszApplicationName = '\0';
1578 else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = lstrlenW(sei_tmp.lpFile))-1] == '\"')
1580 if(len-1 >= dwApplicationNameLen) dwApplicationNameLen = len;
1581 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1582 memcpy(wszApplicationName, sei_tmp.lpFile+1, len*sizeof(WCHAR));
1583 if(len > 2)
1584 wszApplicationName[len-2] = '\0';
1585 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1586 } else {
1587 DWORD l = lstrlenW(sei_tmp.lpFile)+1;
1588 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1589 wszApplicationName = heap_alloc(dwApplicationNameLen*sizeof(WCHAR));
1590 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1593 wszParameters = parametersBuffer;
1594 if (sei_tmp.lpParameters)
1596 len = lstrlenW(sei_tmp.lpParameters) + 1;
1597 if (len > parametersLen)
1599 wszParameters = heap_alloc(len * sizeof(WCHAR));
1600 parametersLen = len;
1602 lstrcpyW(wszParameters, sei_tmp.lpParameters);
1604 else
1605 *wszParameters = '\0';
1607 wszDir = dirBuffer;
1608 if (sei_tmp.lpDirectory)
1610 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1611 if (len > ARRAY_SIZE(dirBuffer))
1612 wszDir = heap_alloc(len * sizeof(WCHAR));
1613 lstrcpyW(wszDir, sei_tmp.lpDirectory);
1615 else
1616 *wszDir = '\0';
1618 /* adjust string pointers to point to the new buffers */
1619 sei_tmp.lpFile = wszApplicationName;
1620 sei_tmp.lpParameters = wszParameters;
1621 sei_tmp.lpDirectory = wszDir;
1623 if (sei_tmp.fMask & unsupportedFlags)
1625 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask & unsupportedFlags);
1628 /* process the IDList */
1629 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1631 IShellExecuteHookW* pSEH;
1633 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1635 if (SUCCEEDED(hr))
1637 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1639 IShellExecuteHookW_Release(pSEH);
1641 if (hr == S_OK) {
1642 heap_free(wszApplicationName);
1643 if (wszParameters != parametersBuffer)
1644 heap_free(wszParameters);
1645 if (wszDir != dirBuffer)
1646 heap_free(wszDir);
1647 return TRUE;
1651 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1652 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1655 if (sei_tmp.fMask & SEE_MASK_DOENVSUBST)
1657 WCHAR *tmp;
1659 tmp = expand_environment(sei_tmp.lpFile);
1660 if (!tmp)
1662 retval = SE_ERR_OOM;
1663 goto end;
1665 heap_free(wszApplicationName);
1666 sei_tmp.lpFile = wszApplicationName = tmp;
1668 tmp = expand_environment(sei_tmp.lpDirectory);
1669 if (!tmp)
1671 retval = SE_ERR_OOM;
1672 goto end;
1674 if (wszDir != dirBuffer)
1675 heap_free(wszDir);
1676 sei_tmp.lpDirectory = wszDir = tmp;
1679 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1681 sei->hInstApp = (HINSTANCE) 33;
1682 heap_free(wszApplicationName);
1683 if (wszParameters != parametersBuffer)
1684 heap_free(wszParameters);
1685 if (wszDir != dirBuffer)
1686 heap_free(wszDir);
1687 return TRUE;
1690 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1692 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1693 execfunc );
1694 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1695 do_error_dialog(retval, sei_tmp.hwnd);
1696 heap_free(wszApplicationName);
1697 if (wszParameters != parametersBuffer)
1698 heap_free(wszParameters);
1699 if (wszDir != dirBuffer)
1700 heap_free(wszDir);
1701 return retval > 32;
1704 /* Has the IDList not yet been translated? */
1705 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1707 SHELL_translate_idlist( &sei_tmp, wszParameters,
1708 parametersLen,
1709 wszApplicationName,
1710 dwApplicationNameLen );
1713 /* convert file URLs */
1714 if (UrlIsFileUrlW(sei_tmp.lpFile))
1716 LPWSTR buf;
1717 DWORD size;
1719 size = MAX_PATH;
1720 buf = heap_alloc(size * sizeof(WCHAR));
1721 if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0))) {
1722 heap_free(buf);
1723 retval = SE_ERR_OOM;
1724 goto end;
1727 heap_free(wszApplicationName);
1728 wszApplicationName = buf;
1729 sei_tmp.lpFile = wszApplicationName;
1731 else /* or expand environment strings (not both!) */
1733 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1734 if (len>0)
1736 LPWSTR buf;
1737 buf = heap_alloc((len + 1) * sizeof(WCHAR));
1739 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1740 heap_free(wszApplicationName);
1741 wszApplicationName = buf;
1743 sei_tmp.lpFile = wszApplicationName;
1747 if (*sei_tmp.lpDirectory)
1749 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1750 if (len > 0)
1752 LPWSTR buf;
1753 len++;
1754 buf = heap_alloc(len * sizeof(WCHAR));
1755 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1756 if (wszDir != dirBuffer)
1757 heap_free(wszDir);
1758 wszDir = buf;
1759 sei_tmp.lpDirectory = wszDir;
1763 /* Else, try to execute the filename */
1764 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1765 lpFile = sei_tmp.lpFile;
1766 wcmd = wcmdBuffer;
1767 lstrcpyW(wcmd, wszApplicationName);
1768 if (sei_tmp.lpDirectory)
1770 LPCWSTR searchPath[] = {
1771 sei_tmp.lpDirectory,
1772 NULL
1774 PathFindOnPathW(wcmd, searchPath);
1776 retval = SHELL_quote_and_execute( wcmd, wszParameters, L"",
1777 wszApplicationName, NULL, &sei_tmp,
1778 sei, execfunc );
1779 if (retval > 32) {
1780 heap_free(wszApplicationName);
1781 if (wszParameters != parametersBuffer)
1782 heap_free(wszParameters);
1783 if (wszDir != dirBuffer)
1784 heap_free(wszDir);
1785 if (wcmd != wcmdBuffer)
1786 heap_free(wcmd);
1787 return TRUE;
1790 /* Else, try to find the executable */
1791 wcmd[0] = '\0';
1792 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1793 if (retval > 32) /* Found */
1795 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszKeyname,
1796 wszApplicationName, env, &sei_tmp,
1797 sei, execfunc );
1798 heap_free( env );
1800 else if (PathIsDirectoryW(lpFile))
1802 WCHAR wExec[MAX_PATH];
1803 WCHAR * lpQuotedFile = heap_alloc( sizeof(WCHAR) * (lstrlenW(lpFile) + 3) );
1805 if (lpQuotedFile)
1807 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, L"explorer",
1808 L"open", wExec, MAX_PATH,
1809 NULL, &env, NULL, NULL );
1810 if (retval > 32)
1812 lstrcpyW(lpQuotedFile, L"\"");
1813 lstrcatW(lpQuotedFile, lpFile);
1814 lstrcatW(lpQuotedFile, L"\"");
1815 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1816 wszKeyname,
1817 wszApplicationName, env,
1818 &sei_tmp, sei, execfunc );
1819 heap_free( env );
1821 heap_free( lpQuotedFile );
1823 else
1824 retval = 0; /* Out of memory */
1826 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1828 retval = SHELL_execute_url( lpFile, wcmd, &sei_tmp, sei, execfunc );
1830 /* Check if file specified is in the form www.??????.*** */
1831 else if (!wcsnicmp(lpFile, L"www", 3))
1833 /* if so, prefix lpFile with http:// and call ShellExecute */
1834 WCHAR lpstrTmpFile[256];
1835 lstrcpyW(lpstrTmpFile, L"http://");
1836 lstrcatW(lpstrTmpFile, lpFile);
1837 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1840 end:
1841 TRACE("retval %Iu\n", retval);
1843 heap_free(wszApplicationName);
1844 if (wszParameters != parametersBuffer)
1845 heap_free(wszParameters);
1846 if (wszDir != dirBuffer)
1847 heap_free(wszDir);
1848 if (wcmd != wcmdBuffer)
1849 heap_free(wcmd);
1851 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1853 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1854 do_error_dialog(retval, sei_tmp.hwnd);
1855 return retval > 32;
1858 /*************************************************************************
1859 * ShellExecuteA [SHELL32.290]
1861 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
1862 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1864 SHELLEXECUTEINFOA sei;
1866 TRACE("%p,%s,%s,%s,%s,%d\n",
1867 hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
1868 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1870 sei.cbSize = sizeof(sei);
1871 sei.fMask = SEE_MASK_FLAG_NO_UI;
1872 sei.hwnd = hWnd;
1873 sei.lpVerb = lpVerb;
1874 sei.lpFile = lpFile;
1875 sei.lpParameters = lpParameters;
1876 sei.lpDirectory = lpDirectory;
1877 sei.nShow = iShowCmd;
1878 sei.lpIDList = 0;
1879 sei.lpClass = 0;
1880 sei.hkeyClass = 0;
1881 sei.dwHotKey = 0;
1882 sei.hProcess = 0;
1884 ShellExecuteExA (&sei);
1885 return sei.hInstApp;
1888 /*************************************************************************
1889 * ShellExecuteExA [SHELL32.292]
1892 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1894 SHELLEXECUTEINFOW seiW;
1895 BOOL ret;
1896 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1898 TRACE("%p\n", sei);
1900 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1902 if (sei->lpVerb)
1903 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1905 if (sei->lpFile)
1906 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1908 if (sei->lpParameters)
1909 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1911 if (sei->lpDirectory)
1912 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1914 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1915 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1916 else
1917 seiW.lpClass = NULL;
1919 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1921 sei->hInstApp = seiW.hInstApp;
1923 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1924 sei->hProcess = seiW.hProcess;
1926 SHFree(wVerb);
1927 SHFree(wFile);
1928 SHFree(wParameters);
1929 SHFree(wDirectory);
1930 SHFree(wClass);
1932 return ret;
1935 /*************************************************************************
1936 * ShellExecuteExW [SHELL32.293]
1939 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1941 return SHELL_execute( sei, SHELL_ExecuteW );
1944 /*************************************************************************
1945 * ShellExecuteW [SHELL32.294]
1946 * from shellapi.h
1947 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1948 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1950 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
1951 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1953 SHELLEXECUTEINFOW sei;
1955 TRACE("\n");
1956 sei.cbSize = sizeof(sei);
1957 sei.fMask = SEE_MASK_FLAG_NO_UI;
1958 sei.hwnd = hwnd;
1959 sei.lpVerb = lpVerb;
1960 sei.lpFile = lpFile;
1961 sei.lpParameters = lpParameters;
1962 sei.lpDirectory = lpDirectory;
1963 sei.nShow = nShowCmd;
1964 sei.lpIDList = 0;
1965 sei.lpClass = 0;
1966 sei.hkeyClass = 0;
1967 sei.dwHotKey = 0;
1968 sei.hProcess = 0;
1970 SHELL_execute( &sei, SHELL_ExecuteW );
1971 return sei.hInstApp;
1974 /*************************************************************************
1975 * WOWShellExecute [SHELL32.@]
1977 * FIXME: the callback function most likely doesn't work the same way on Windows.
1979 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb,LPCSTR lpFile,
1980 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
1982 SHELLEXECUTEINFOW seiW;
1983 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
1984 HANDLE hProcess = 0;
1986 seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
1987 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
1988 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
1989 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
1991 seiW.cbSize = sizeof(seiW);
1992 seiW.fMask = 0;
1993 seiW.hwnd = hWnd;
1994 seiW.nShow = iShowCmd;
1995 seiW.lpIDList = 0;
1996 seiW.lpClass = 0;
1997 seiW.hkeyClass = 0;
1998 seiW.dwHotKey = 0;
1999 seiW.hProcess = hProcess;
2001 SHELL_execute( &seiW, callback );
2003 SHFree(wVerb);
2004 SHFree(wFile);
2005 SHFree(wParameters);
2006 SHFree(wDirectory);
2007 return seiW.hInstApp;
2010 /*************************************************************************
2011 * ShellExec_RunDLLW [SHELL32.@]
2013 void WINAPI ShellExec_RunDLLW(HWND hwnd, HINSTANCE instance, WCHAR *cmdline, int cmdshow)
2015 BOOL in_single_quotes = FALSE, in_double_quotes = FALSE;
2016 WCHAR *args;
2018 TRACE("%p, %p, %s, %d\n", hwnd, instance, debugstr_w(cmdline), cmdshow);
2020 /* Replace the first whitespace character in the command line string with a
2021 null terminator to separate the program name from the program arguments */
2022 for (args = cmdline; *args; args++)
2024 switch (*args)
2026 case '\\':
2027 args++; /* skip the next character */
2028 break;
2029 case '\'':
2030 if (!in_double_quotes)
2031 in_single_quotes = !in_single_quotes;
2032 break;
2033 case '"':
2034 if (!in_single_quotes)
2035 in_double_quotes = !in_double_quotes;
2036 break;
2037 case ' ':
2038 case '\t':
2039 if (!in_single_quotes && !in_double_quotes)
2041 *args = 0;
2042 args++;
2043 goto execute;
2048 execute:
2049 ShellExecuteW(hwnd, NULL, cmdline, args, NULL, cmdshow);
2052 /*************************************************************************
2053 * ShellExec_RunDLLA [SHELL32.@]
2055 void WINAPI ShellExec_RunDLLA(HWND hwnd, HINSTANCE instance, CHAR *cmdline, int cmdshow)
2057 WCHAR *cmdlineW;
2058 ShellExec_RunDLLW(hwnd, instance, __SHCloneStrAtoW(&cmdlineW, cmdline), cmdshow);
2059 SHFree(cmdlineW);
2062 /*************************************************************************
2063 * OpenAs_RunDLLA [SHELL32.@]
2065 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2067 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2070 /*************************************************************************
2071 * OpenAs_RunDLLW [SHELL32.@]
2073 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2075 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2078 /*************************************************************************
2079 * RegenerateUserEnvironment [SHELL32.@]
2081 BOOL WINAPI RegenerateUserEnvironment(WCHAR *wunknown, BOOL bunknown)
2083 FIXME("stub: %p, %d\n", wunknown, bunknown);
2084 return FALSE;