webservices: Add a stub implementation of WS_TYPE_ATTRIBUTE_FIELD_MAPPING in the...
[wine.git] / dlls / shell32 / shlexec.c
blob6aa3eecf86181f70f85880935e5d285b185ef19d
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33 #include <assert.h>
35 #define COBJMACROS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winuser.h"
42 #include "shlwapi.h"
43 #include "ddeml.h"
45 #include "shell32_main.h"
46 #include "pidl.h"
47 #include "shresdef.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(exec);
53 static const WCHAR wszOpen[] = {'o','p','e','n',0};
54 static const WCHAR wszExe[] = {'.','e','x','e',0};
55 static const WCHAR wszILPtr[] = {':','%','p',0};
56 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
57 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
58 static const WCHAR wszEmpty[] = {0};
60 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
62 typedef UINT_PTR (*SHELL_ExecuteW32)(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
63 const SHELLEXECUTEINFOW *sei, LPSHELLEXECUTEINFOW sei_out);
66 /***********************************************************************
67 * SHELL_ArgifyW [Internal]
69 * this function is supposed to expand the escape sequences found in the registry
70 * some diving reported that the following were used:
71 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
72 * %1 file
73 * %2 printer
74 * %3 driver
75 * %4 port
76 * %I address of a global item ID (explorer switch /idlist)
77 * %L seems to be %1 as long filename followed by the 8+3 variation
78 * %S ???
79 * %* all following parameters (see batfile)
82 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
84 WCHAR xlpFile[1024];
85 BOOL done = FALSE;
86 BOOL found_p1 = FALSE;
87 PWSTR res = out;
88 PCWSTR cmd;
89 DWORD used = 0;
91 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
92 debugstr_w(lpFile), pidl, args);
94 while (*fmt)
96 if (*fmt == '%')
98 switch (*++fmt)
100 case '\0':
101 case '%':
102 used++;
103 if (used < len)
104 *res++ = '%';
105 break;
107 case '2':
108 case '3':
109 case '4':
110 case '5':
111 case '6':
112 case '7':
113 case '8':
114 case '9':
115 case '0':
116 case '*':
117 if (args)
119 if (*fmt == '*')
121 used++;
122 if (used < len)
123 *res++ = '"';
124 while(*args)
126 used++;
127 if (used < len)
128 *res++ = *args++;
129 else
130 args++;
132 used++;
133 if (used < len)
134 *res++ = '"';
136 else
138 while(*args && !isspace(*args))
140 used++;
141 if (used < len)
142 *res++ = *args++;
143 else
144 args++;
147 while(isspace(*args))
148 ++args;
150 break;
152 /* else fall through */
153 case '1':
154 if (!done || (*fmt == '1'))
156 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
157 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
158 cmd = xlpFile;
159 else
160 cmd = lpFile;
162 used += strlenW(cmd);
163 if (used < len)
165 strcpyW(res, cmd);
166 res += strlenW(cmd);
169 found_p1 = TRUE;
170 break;
173 * IE uses this a lot for activating things such as windows media
174 * player. This is not verified to be fully correct but it appears
175 * to work just fine.
177 case 'l':
178 case 'L':
179 if (lpFile) {
180 used += strlenW(lpFile);
181 if (used < len)
183 strcpyW(res, lpFile);
184 res += strlenW(lpFile);
187 found_p1 = TRUE;
188 break;
190 case 'i':
191 case 'I':
192 if (pidl) {
193 INT chars = 0;
194 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
195 * allowing a buffer of 100 should more than exceed all needs */
196 WCHAR buf[100];
197 LPVOID pv;
198 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
199 pv = SHLockShared(hmem, 0);
200 chars = sprintfW(buf, wszILPtr, pv);
201 if (chars >= sizeof(buf)/sizeof(WCHAR))
202 ERR("pidl format buffer too small!\n");
203 used += chars;
204 if (used < len)
206 strcpyW(res,buf);
207 res += chars;
209 SHUnlockShared(pv);
211 found_p1 = TRUE;
212 break;
214 default:
216 * Check if this is an env-variable here...
219 /* Make sure that we have at least one more %.*/
220 if (strchrW(fmt, '%'))
222 WCHAR tmpBuffer[1024];
223 PWSTR tmpB = tmpBuffer;
224 WCHAR tmpEnvBuff[MAX_PATH];
225 DWORD envRet;
227 while (*fmt != '%')
228 *tmpB++ = *fmt++;
229 *tmpB++ = 0;
231 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
233 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
234 if (envRet == 0 || envRet > MAX_PATH)
236 used += strlenW(tmpBuffer);
237 if (used < len)
239 strcpyW( res, tmpBuffer );
240 res += strlenW(tmpBuffer);
243 else
245 used += strlenW(tmpEnvBuff);
246 if (used < len)
248 strcpyW( res, tmpEnvBuff );
249 res += strlenW(tmpEnvBuff);
253 done = TRUE;
254 break;
256 /* Don't skip past terminator (catch a single '%' at the end) */
257 if (*fmt != '\0')
259 fmt++;
262 else
264 used ++;
265 if (used < len)
266 *res++ = *fmt++;
267 else
268 fmt++;
272 used ++;
273 if (res - out < len)
274 *res = '\0';
275 else
276 out[len-1] = '\0';
278 TRACE("used %i of %i space\n",used,len);
279 if (out_len)
280 *out_len = used;
282 return found_p1;
285 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
287 STRRET strret;
288 IShellFolder* desktop;
290 HRESULT hr = SHGetDesktopFolder(&desktop);
292 if (SUCCEEDED(hr)) {
293 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
295 if (SUCCEEDED(hr))
296 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
298 IShellFolder_Release(desktop);
301 return hr;
304 /*************************************************************************
305 * SHELL_ExecuteW [Internal]
308 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
309 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
311 STARTUPINFOW startup;
312 PROCESS_INFORMATION info;
313 UINT_PTR retval = SE_ERR_NOASSOC;
314 UINT gcdret = 0;
315 WCHAR curdir[MAX_PATH];
316 DWORD dwCreationFlags;
317 const WCHAR *lpDirectory = NULL;
319 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
321 /* make sure we don't fail the CreateProcess if the calling app passes in
322 * a bad working directory */
323 if (psei->lpDirectory && psei->lpDirectory[0])
325 DWORD attr = GetFileAttributesW(psei->lpDirectory);
326 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
327 lpDirectory = psei->lpDirectory;
330 /* ShellExecute specifies the command from psei->lpDirectory
331 * if present. Not from the current dir as CreateProcess does */
332 if( lpDirectory )
333 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
334 if( !SetCurrentDirectoryW( lpDirectory))
335 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
336 ZeroMemory(&startup,sizeof(STARTUPINFOW));
337 startup.cb = sizeof(STARTUPINFOW);
338 startup.dwFlags = STARTF_USESHOWWINDOW;
339 startup.wShowWindow = psei->nShow;
340 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
341 if (!(psei->fMask & SEE_MASK_NO_CONSOLE))
342 dwCreationFlags |= CREATE_NEW_CONSOLE;
343 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
344 lpDirectory, &startup, &info))
346 /* Give 30 seconds to the app to come up, if desired. Probably only needed
347 when starting app immediately before making a DDE connection. */
348 if (shWait)
349 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
350 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
351 retval = 33;
352 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
353 psei_out->hProcess = info.hProcess;
354 else
355 CloseHandle( info.hProcess );
356 CloseHandle( info.hThread );
358 else if ((retval = GetLastError()) >= 32)
360 TRACE("CreateProcess returned error %ld\n", retval);
361 retval = ERROR_BAD_FORMAT;
364 TRACE("returning %lu\n", retval);
366 psei_out->hInstApp = (HINSTANCE)retval;
367 if( gcdret )
368 if( !SetCurrentDirectoryW( curdir))
369 ERR("cannot return to directory %s\n", debugstr_w(curdir));
371 return retval;
375 /***********************************************************************
376 * SHELL_BuildEnvW [Internal]
378 * Build the environment for the new process, adding the specified
379 * path to the PATH variable. Returned pointer must be freed by caller.
381 static void *SHELL_BuildEnvW( const WCHAR *path )
383 static const WCHAR wPath[] = {'P','A','T','H','=',0};
384 WCHAR *strings, *new_env;
385 WCHAR *p, *p2;
386 int total = strlenW(path) + 1;
387 BOOL got_path = FALSE;
389 if (!(strings = GetEnvironmentStringsW())) return NULL;
390 p = strings;
391 while (*p)
393 int len = strlenW(p) + 1;
394 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
395 total += len;
396 p += len;
398 if (!got_path) total += 5; /* we need to create PATH */
399 total++; /* terminating null */
401 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
403 FreeEnvironmentStringsW( strings );
404 return NULL;
406 p = strings;
407 p2 = new_env;
408 while (*p)
410 int len = strlenW(p) + 1;
411 memcpy( p2, p, len * sizeof(WCHAR) );
412 if (!strncmpiW( p, wPath, 5 ))
414 p2[len - 1] = ';';
415 strcpyW( p2 + len, path );
416 p2 += strlenW(path) + 1;
418 p += len;
419 p2 += len;
421 if (!got_path)
423 strcpyW( p2, wPath );
424 strcatW( p2, path );
425 p2 += strlenW(p2) + 1;
427 *p2 = 0;
428 FreeEnvironmentStringsW( strings );
429 return new_env;
433 /***********************************************************************
434 * SHELL_TryAppPathW [Internal]
436 * Helper function for SHELL_FindExecutable
437 * @param lpResult - pointer to a buffer of size MAX_PATH
438 * On entry: szName is a filename (probably without path separators).
439 * On exit: if szName found in "App Path", place full path in lpResult, and return true
441 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
443 static const WCHAR wszKeyAppPaths[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',
444 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
445 static const WCHAR wPath[] = {'P','a','t','h',0};
446 HKEY hkApp = 0;
447 WCHAR buffer[1024];
448 LONG len;
449 LONG res;
450 BOOL found = FALSE;
452 if (env) *env = NULL;
453 strcpyW(buffer, wszKeyAppPaths);
454 strcatW(buffer, szName);
455 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
456 if (res) goto end;
458 len = MAX_PATH*sizeof(WCHAR);
459 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
460 if (res) goto end;
461 found = TRUE;
463 if (env)
465 DWORD count = sizeof(buffer);
466 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
467 *env = SHELL_BuildEnvW( buffer );
470 end:
471 if (hkApp) RegCloseKey(hkApp);
472 return found;
475 /*************************************************************************
476 * SHELL_FindExecutableByVerb [Internal]
478 * called from SHELL_FindExecutable or SHELL_execute_class
479 * in/out:
480 * classname a buffer, big enough, to get the key name to do actually the
481 * command "WordPad.Document.1\\shell\\open\\command"
482 * passed as "WordPad.Document.1"
483 * in:
484 * lpVerb the operation on it (open)
485 * commandlen the size of command buffer (in bytes)
486 * out:
487 * command a buffer, to store the command to do the
488 * operation on the file
489 * key a buffer, big enough, to get the key name to do actually the
490 * command "WordPad.Document.1\\shell\\open\\command"
491 * Can be NULL
493 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
495 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
496 HKEY hkeyClass;
497 WCHAR verb[MAX_PATH];
499 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
500 return SE_ERR_NOASSOC;
501 if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, sizeof(verb)/sizeof(verb[0])))
502 return SE_ERR_NOASSOC;
503 RegCloseKey(hkeyClass);
505 /* Looking for ...buffer\shell\<verb>\command */
506 strcatW(classname, wszShell);
507 strcatW(classname, verb);
508 strcatW(classname, wCommand);
510 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
511 &commandlen) == ERROR_SUCCESS)
513 commandlen /= sizeof(WCHAR);
514 if (key) strcpyW(key, classname);
515 #if 0
516 LPWSTR tmp;
517 WCHAR param[256];
518 LONG paramlen = sizeof(param);
519 static const WCHAR wSpace[] = {' ',0};
521 /* FIXME: it seems all Windows version don't behave the same here.
522 * the doc states that this ddeexec information can be found after
523 * the exec names.
524 * on Win98, it doesn't appear, but I think it does on Win2k
526 /* Get the parameters needed by the application
527 from the associated ddeexec key */
528 tmp = strstrW(classname, wCommand);
529 tmp[0] = '\0';
530 strcatW(classname, wDdeexec);
531 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
532 &paramlen) == ERROR_SUCCESS)
534 paramlen /= sizeof(WCHAR);
535 strcatW(command, wSpace);
536 strcatW(command, param);
537 commandlen += paramlen;
539 #endif
541 command[commandlen] = '\0';
543 return 33; /* FIXME see SHELL_FindExecutable() */
546 return SE_ERR_NOASSOC;
549 /*************************************************************************
550 * SHELL_FindExecutable [Internal]
552 * Utility for code sharing between FindExecutable and ShellExecute
553 * in:
554 * lpFile the name of a file
555 * lpVerb the operation on it (open)
556 * out:
557 * lpResult a buffer, big enough :-(, to store the command to do the
558 * operation on the file
559 * key a buffer, big enough, to get the key name to do actually the
560 * command (it'll be used afterwards for more information
561 * on the operation)
563 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
564 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
566 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
567 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
568 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
569 WCHAR *extension = NULL; /* pointer to file extension */
570 WCHAR classname[256]; /* registry name for this file type */
571 LONG classnamelen = sizeof(classname); /* length of above */
572 WCHAR command[1024]; /* command from registry */
573 WCHAR wBuffer[256]; /* Used to GetProfileString */
574 UINT retval = SE_ERR_NOASSOC;
575 WCHAR *tok; /* token pointer */
576 WCHAR xlpFile[256]; /* result of SearchPath */
577 DWORD attribs; /* file attributes */
579 TRACE("%s\n", debugstr_w(lpFile));
581 if (!lpResult)
582 return ERROR_INVALID_PARAMETER;
584 xlpFile[0] = '\0';
585 lpResult[0] = '\0'; /* Start off with an empty return string */
586 if (key) *key = '\0';
588 /* trap NULL parameters on entry */
589 if (!lpFile)
591 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
592 debugstr_w(lpFile), debugstr_w(lpResult));
593 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
596 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
598 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
599 return 33;
602 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
604 TRACE("SearchPathW returned non-zero\n");
605 lpFile = xlpFile;
606 /* The file was found in the application-supplied default directory (or the system search path) */
608 else if (lpPath && SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
610 TRACE("SearchPathW returned non-zero\n");
611 lpFile = xlpFile;
612 /* The file was found in one of the directories in the system-wide search path */
615 attribs = GetFileAttributesW(lpFile);
616 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
618 strcpyW(classname, wszFolder);
620 else
622 /* Did we get something? Anything? */
623 if (xlpFile[0]==0)
625 TRACE("Returning SE_ERR_FNF\n");
626 return SE_ERR_FNF;
628 /* First thing we need is the file's extension */
629 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
630 /* File->Run in progman uses */
631 /* .\FILE.EXE :( */
632 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
634 if (extension == NULL || extension[1]==0)
636 WARN("Returning SE_ERR_NOASSOC\n");
637 return SE_ERR_NOASSOC;
640 /* Three places to check: */
641 /* 1. win.ini, [windows], programs (NB no leading '.') */
642 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
643 /* 3. win.ini, [extensions], extension (NB no leading '.' */
644 /* All I know of the order is that registry is checked before */
645 /* extensions; however, it'd make sense to check the programs */
646 /* section first, so that's what happens here. */
648 /* See if it's a program - if GetProfileString fails, we skip this
649 * section. Actually, if GetProfileString fails, we've probably
650 * got a lot more to worry about than running a program... */
651 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
653 CharLowerW(wBuffer);
654 tok = wBuffer;
655 while (*tok)
657 WCHAR *p = tok;
658 while (*p && *p != ' ' && *p != '\t') p++;
659 if (*p)
661 *p++ = 0;
662 while (*p == ' ' || *p == '\t') p++;
665 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
667 strcpyW(lpResult, xlpFile);
668 /* Need to perhaps check that the file has a path
669 * attached */
670 TRACE("found %s\n", debugstr_w(lpResult));
671 return 33;
672 /* Greater than 32 to indicate success */
674 tok = p;
678 /* Check registry */
679 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
680 &classnamelen) == ERROR_SUCCESS)
682 classnamelen /= sizeof(WCHAR);
683 if (classnamelen == sizeof(classname)/sizeof(WCHAR))
684 classnamelen--;
685 classname[classnamelen] = '\0';
686 TRACE("File type: %s\n", debugstr_w(classname));
688 else
690 *classname = '\0';
694 if (*classname)
696 /* pass the verb string to SHELL_FindExecutableByVerb() */
697 retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
699 if (retval > 32)
701 DWORD finishedLen;
702 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
703 if (finishedLen > resultLen)
704 ERR("Argify buffer not large enough.. truncated\n");
706 /* Remove double quotation marks and command line arguments */
707 if (*lpResult == '"')
709 WCHAR *p = lpResult;
710 while (*(p + 1) != '"')
712 *p = *(p + 1);
713 p++;
715 *p = '\0';
717 else
719 /* Truncate on first space */
720 WCHAR *p = lpResult;
721 while (*p != ' ' && *p != '\0')
722 p++;
723 *p='\0';
727 else /* Check win.ini */
729 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
731 /* Toss the leading dot */
732 extension++;
733 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
735 if (strlenW(command) != 0)
737 strcpyW(lpResult, command);
738 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
739 if (tok != NULL)
741 tok[0] = '\0';
742 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
743 tok = strchrW(command, '^'); /* see above */
744 if ((tok != NULL) && (strlenW(tok)>5))
746 strcatW(lpResult, &tok[5]);
749 retval = 33; /* FIXME - see above */
754 TRACE("returning %s\n", debugstr_w(lpResult));
755 return retval;
758 /******************************************************************
759 * dde_cb
761 * callback for the DDE connection. not really useful
763 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
764 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
765 ULONG_PTR dwData1, ULONG_PTR dwData2)
767 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
768 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
769 return NULL;
772 /******************************************************************
773 * dde_connect
775 * ShellExecute helper. Used to do an operation with a DDE connection
777 * Handles both the direct connection (try #1), and if it fails,
778 * launching an application and trying (#2) to connect to it
781 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
782 const WCHAR* lpFile, WCHAR *env,
783 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
784 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
786 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
787 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
788 WCHAR regkey[256];
789 WCHAR * endkey = regkey + strlenW(key);
790 WCHAR app[256], topic[256], ifexec[256], static_res[256];
791 WCHAR * dynamic_res=NULL;
792 WCHAR * res;
793 LONG applen, topiclen, ifexeclen;
794 WCHAR * exec;
795 DWORD ddeInst = 0;
796 DWORD tid;
797 DWORD resultLen, endkeyLen;
798 HSZ hszApp, hszTopic;
799 HCONV hConv;
800 HDDEDATA hDdeData;
801 unsigned ret = SE_ERR_NOASSOC;
802 BOOL unicode = !(GetVersion() & 0x80000000);
804 if (strlenW(key) + 1 > sizeof(regkey) / sizeof(regkey[0]))
806 FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
807 return 2;
809 strcpyW(regkey, key);
810 endkeyLen = sizeof(regkey) / sizeof(regkey[0]) - (endkey - regkey);
811 if (strlenW(wApplication) + 1 > endkeyLen)
813 FIXME("endkey %s overruns buffer\n", debugstr_w(wApplication));
814 return 2;
816 strcpyW(endkey, wApplication);
817 applen = sizeof(app);
818 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
820 WCHAR command[1024], fullpath[MAX_PATH];
821 static const WCHAR wSo[] = { '.','s','o',0 };
822 int sizeSo = sizeof(wSo)/sizeof(WCHAR);
823 LPWSTR ptr = NULL;
824 DWORD ret = 0;
826 /* Get application command from start string and find filename of application */
827 if (*start == '"')
829 if (strlenW(start + 1) + 1 > sizeof(command) / sizeof(command[0]))
831 FIXME("size of input parameter %s larger than buffer\n",
832 debugstr_w(start + 1));
833 return 2;
835 strcpyW(command, start+1);
836 if ((ptr = strchrW(command, '"')))
837 *ptr = 0;
838 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
840 else
842 LPCWSTR p;
843 LPWSTR space;
844 for (p=start; (space=strchrW(p, ' ')); p=space+1)
846 int idx = space-start;
847 memcpy(command, start, idx*sizeof(WCHAR));
848 command[idx] = '\0';
849 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr)))
850 break;
852 if (!ret)
853 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
856 if (!ret)
858 ERR("Unable to find application path for command %s\n", debugstr_w(start));
859 return ERROR_ACCESS_DENIED;
861 if (strlenW(ptr) + 1 > sizeof(app) / sizeof(app[0]))
863 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
864 return 2;
866 strcpyW(app, ptr);
868 /* Remove extensions (including .so) */
869 ptr = app + strlenW(app) - (sizeSo-1);
870 if (strlenW(app) >= sizeSo &&
871 !strcmpW(ptr, wSo))
872 *ptr = 0;
874 ptr = strrchrW(app, '.');
875 assert(ptr);
876 *ptr = 0;
879 if (strlenW(wTopic) + 1 > endkeyLen)
881 FIXME("endkey %s overruns buffer\n", debugstr_w(wTopic));
882 return 2;
884 strcpyW(endkey, wTopic);
885 topiclen = sizeof(topic);
886 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
888 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
889 strcpyW(topic, wSystem);
892 if (unicode)
894 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
895 return 2;
897 else
899 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
900 return 2;
903 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
904 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
906 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
907 exec = ddeexec;
908 if (!hConv)
910 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
911 TRACE("Launching %s\n", debugstr_w(start));
912 ret = execfunc(start, env, TRUE, psei, psei_out);
913 if (ret <= 32)
915 TRACE("Couldn't launch\n");
916 goto error;
918 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
919 if (!hConv)
921 TRACE("Couldn't connect. ret=%d\n", ret);
922 DdeUninitialize(ddeInst);
923 SetLastError(ERROR_DDE_FAIL);
924 return 30; /* whatever */
926 if (strlenW(wIfexec) + 1 > endkeyLen)
928 FIXME("endkey %s overruns buffer\n", debugstr_w(wIfexec));
929 return 2;
931 strcpyW(endkey, wIfexec);
932 ifexeclen = sizeof(ifexec);
933 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
935 exec = ifexec;
939 SHELL_ArgifyW(static_res, sizeof(static_res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
940 if (resultLen > sizeof(static_res)/sizeof(WCHAR))
942 res = dynamic_res = HeapAlloc(GetProcessHeap(), 0, resultLen * sizeof(WCHAR));
943 SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL);
945 else
946 res = static_res;
947 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
949 /* It's documented in the KB 330337 that IE has a bug and returns
950 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
952 if (unicode)
953 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
954 XTYP_EXECUTE, 30000, &tid);
955 else
957 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
958 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
959 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
960 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
961 XTYP_EXECUTE, 10000, &tid );
962 HeapFree(GetProcessHeap(), 0, resA);
964 if (hDdeData)
965 DdeFreeDataHandle(hDdeData);
966 else
967 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
968 ret = 33;
970 HeapFree(GetProcessHeap(), 0, dynamic_res);
972 DdeDisconnect(hConv);
974 error:
975 DdeUninitialize(ddeInst);
977 return ret;
980 /*************************************************************************
981 * execute_from_key [Internal]
983 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
984 LPCWSTR executable_name,
985 SHELL_ExecuteW32 execfunc,
986 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
988 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
989 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
990 WCHAR cmd[256], param[1024], ddeexec[256];
991 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
992 UINT_PTR retval = SE_ERR_NOASSOC;
993 DWORD resultLen;
994 LPWSTR tmp;
996 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
997 debugstr_w(szCommandline), debugstr_w(executable_name));
999 cmd[0] = '\0';
1000 param[0] = '\0';
1002 /* Get the application from the registry */
1003 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
1005 TRACE("got cmd: %s\n", debugstr_w(cmd));
1007 /* Is there a replace() function anywhere? */
1008 cmdlen /= sizeof(WCHAR);
1009 if (cmdlen >= sizeof(cmd)/sizeof(WCHAR))
1010 cmdlen = sizeof(cmd)/sizeof(WCHAR)-1;
1011 cmd[cmdlen] = '\0';
1012 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
1013 if (resultLen > sizeof(param)/sizeof(WCHAR))
1014 ERR("Argify buffer not large enough, truncating\n");
1017 /* Get the parameters needed by the application
1018 from the associated ddeexec key */
1019 tmp = strstrW(key, wCommand);
1020 assert(tmp);
1021 strcpyW(tmp, wDdeexec);
1023 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
1025 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
1026 if (!param[0]) strcpyW(param, executable_name);
1027 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
1029 else if (param[0])
1031 TRACE("executing: %s\n", debugstr_w(param));
1032 retval = execfunc(param, env, FALSE, psei, psei_out);
1034 else
1035 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1037 return retval;
1040 /*************************************************************************
1041 * FindExecutableA [SHELL32.@]
1043 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1045 HINSTANCE retval;
1046 WCHAR *wFile = NULL, *wDirectory = NULL;
1047 WCHAR wResult[MAX_PATH];
1049 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1050 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1052 retval = FindExecutableW(wFile, wDirectory, wResult);
1053 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1054 SHFree( wFile );
1055 SHFree( wDirectory );
1057 TRACE("returning %s\n", lpResult);
1058 return retval;
1061 /*************************************************************************
1062 * FindExecutableW [SHELL32.@]
1064 * This function returns the executable associated with the specified file
1065 * for the default verb.
1067 * PARAMS
1068 * lpFile [I] The file to find the association for. This must refer to
1069 * an existing file otherwise FindExecutable fails and returns
1070 * SE_ERR_FNF.
1071 * lpResult [O] Points to a buffer into which the executable path is
1072 * copied. This parameter must not be NULL otherwise
1073 * FindExecutable() segfaults. The buffer must be of size at
1074 * least MAX_PATH characters.
1076 * RETURNS
1077 * A value greater than 32 on success, less than or equal to 32 otherwise.
1078 * See the SE_ERR_* constants.
1080 * NOTES
1081 * On Windows XP and 2003, FindExecutable() seems to first convert the
1082 * filename into 8.3 format, thus taking into account only the first three
1083 * characters of the extension, and expects to find an association for those.
1084 * However other Windows versions behave sanely.
1086 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1088 UINT_PTR retval = SE_ERR_NOASSOC;
1089 WCHAR old_dir[1024];
1090 WCHAR res[MAX_PATH];
1092 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1094 lpResult[0] = '\0'; /* Start off with an empty return string */
1095 if (lpFile == NULL)
1096 return (HINSTANCE)SE_ERR_FNF;
1098 if (lpDirectory)
1100 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
1101 SetCurrentDirectoryW(lpDirectory);
1104 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, res, MAX_PATH, NULL, NULL, NULL, NULL);
1106 if (retval > 32)
1107 strcpyW(lpResult, res);
1109 TRACE("returning %s\n", debugstr_w(lpResult));
1110 if (lpDirectory)
1111 SetCurrentDirectoryW(old_dir);
1112 return (HINSTANCE)retval;
1115 /* FIXME: is this already implemented somewhere else? */
1116 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1118 LPCWSTR ext = NULL, lpClass = NULL;
1119 LPWSTR cls = NULL;
1120 DWORD type = 0, sz = 0;
1121 HKEY hkey = 0;
1122 LONG r;
1124 if (sei->fMask & SEE_MASK_CLASSALL)
1125 return sei->hkeyClass;
1127 if (sei->fMask & SEE_MASK_CLASSNAME)
1128 lpClass = sei->lpClass;
1129 else
1131 ext = PathFindExtensionW( sei->lpFile );
1132 TRACE("ext = %s\n", debugstr_w( ext ) );
1133 if (!ext)
1134 return hkey;
1136 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1137 if (r != ERROR_SUCCESS )
1138 return hkey;
1140 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1141 if ( r == ERROR_SUCCESS && type == REG_SZ )
1143 sz += sizeof (WCHAR);
1144 cls = HeapAlloc( GetProcessHeap(), 0, sz );
1145 cls[0] = 0;
1146 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1149 RegCloseKey( hkey );
1150 lpClass = cls;
1153 TRACE("class = %s\n", debugstr_w(lpClass) );
1155 hkey = 0;
1156 if ( lpClass )
1157 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1159 HeapFree( GetProcessHeap(), 0, cls );
1161 return hkey;
1164 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1166 LPCITEMIDLIST pidllast = NULL;
1167 IDataObject *dataobj = NULL;
1168 IShellFolder *shf = NULL;
1169 LPITEMIDLIST pidl = NULL;
1170 HRESULT r;
1172 if (sei->fMask & SEE_MASK_CLASSALL)
1173 pidl = sei->lpIDList;
1174 else
1176 WCHAR fullpath[MAX_PATH];
1177 BOOL ret;
1179 fullpath[0] = 0;
1180 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1181 if (!ret)
1182 goto end;
1184 pidl = ILCreateFromPathW( fullpath );
1187 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1188 if ( FAILED( r ) )
1189 goto end;
1191 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1192 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1194 end:
1195 if ( pidl != sei->lpIDList )
1196 ILFree( pidl );
1197 if ( shf )
1198 IShellFolder_Release( shf );
1199 return dataobj;
1202 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1203 LPSHELLEXECUTEINFOW sei )
1205 IContextMenu *cm = NULL;
1206 CMINVOKECOMMANDINFOEX ici;
1207 MENUITEMINFOW info;
1208 WCHAR string[0x80];
1209 INT i, n, def = -1;
1210 HMENU hmenu = 0;
1211 HRESULT r;
1213 TRACE("%p %p\n", obj, sei );
1215 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1216 if ( FAILED( r ) )
1217 return r;
1219 hmenu = CreateMenu();
1220 if ( !hmenu )
1221 goto end;
1223 /* the number of the last menu added is returned in r */
1224 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1225 if ( FAILED( r ) )
1226 goto end;
1228 n = GetMenuItemCount( hmenu );
1229 for ( i = 0; i < n; i++ )
1231 memset( &info, 0, sizeof info );
1232 info.cbSize = sizeof info;
1233 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1234 info.dwTypeData = string;
1235 info.cch = sizeof string;
1236 string[0] = 0;
1237 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1239 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1240 info.fState, info.dwItemData, info.fType, info.wID );
1241 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1242 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1244 def = i;
1245 break;
1249 r = E_FAIL;
1250 if ( def == -1 )
1251 goto end;
1253 memset( &ici, 0, sizeof ici );
1254 ici.cbSize = sizeof ici;
1255 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE|SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1256 ici.nShow = sei->nShow;
1257 ici.lpVerb = MAKEINTRESOURCEA( def );
1258 ici.hwnd = sei->hwnd;
1259 ici.lpParametersW = sei->lpParameters;
1261 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1263 TRACE("invoke command returned %08x\n", r );
1265 end:
1266 if ( hmenu )
1267 DestroyMenu( hmenu );
1268 if ( cm )
1269 IContextMenu_Release( cm );
1270 return r;
1273 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1275 IDataObject *dataobj = NULL;
1276 IObjectWithSite *ows = NULL;
1277 IShellExtInit *obj = NULL;
1278 HRESULT r;
1280 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1282 r = CoInitialize( NULL );
1283 if ( FAILED( r ) )
1284 goto end;
1286 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1287 &IID_IShellExtInit, (LPVOID*)&obj );
1288 if ( FAILED( r ) )
1290 ERR("failed %08x\n", r );
1291 goto end;
1294 dataobj = shellex_get_dataobj( sei );
1295 if ( !dataobj )
1297 ERR("failed to get data object\n");
1298 goto end;
1301 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1302 if ( FAILED( r ) )
1303 goto end;
1305 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1306 if ( FAILED( r ) )
1307 goto end;
1309 IObjectWithSite_SetSite( ows, NULL );
1311 r = shellex_run_context_menu_default( obj, sei );
1313 end:
1314 if ( ows )
1315 IObjectWithSite_Release( ows );
1316 if ( dataobj )
1317 IDataObject_Release( dataobj );
1318 if ( obj )
1319 IShellExtInit_Release( obj );
1320 CoUninitialize();
1321 return r;
1325 /*************************************************************************
1326 * ShellExecute_FromContextMenu [Internal]
1328 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1330 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1331 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1332 HKEY hkey, hkeycm = 0;
1333 WCHAR szguid[39];
1334 HRESULT hr;
1335 GUID guid;
1336 DWORD i;
1337 LONG r;
1339 TRACE("%s\n", debugstr_w(sei->lpFile) );
1341 hkey = ShellExecute_GetClassKey( sei );
1342 if ( !hkey )
1343 return ERROR_FUNCTION_FAILED;
1345 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1346 if ( r == ERROR_SUCCESS )
1348 i = 0;
1349 while ( 1 )
1351 r = RegEnumKeyW( hkeycm, i++, szguid, sizeof(szguid)/sizeof(szguid[0]) );
1352 if ( r != ERROR_SUCCESS )
1353 break;
1355 hr = CLSIDFromString( szguid, &guid );
1356 if (SUCCEEDED(hr))
1358 /* stop at the first one that succeeds in running */
1359 hr = shellex_load_object_and_run( hkey, &guid, sei );
1360 if ( SUCCEEDED( hr ) )
1361 break;
1364 RegCloseKey( hkeycm );
1367 if ( hkey != sei->hkeyClass )
1368 RegCloseKey( hkey );
1369 return r;
1372 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 );
1374 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1376 static const WCHAR wQuote[] = {'"',0};
1377 static const WCHAR wSpace[] = {' ',0};
1378 WCHAR execCmd[1024], classname[1024];
1379 /* launch a document by fileclass like 'WordPad.Document.1' */
1380 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1381 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1382 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1383 DWORD resultLen;
1384 BOOL done;
1385 UINT_PTR rslt;
1387 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1388 if (cmask != SEE_MASK_CLASSNAME)
1390 WCHAR wcmd[1024];
1391 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1392 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1393 psei->lpVerb,
1394 execCmd, sizeof(execCmd));
1396 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1397 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1399 wcmd[0] = '\0';
1400 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1401 if (!done && wszApplicationName[0])
1403 strcatW(wcmd, wSpace);
1404 if (*wszApplicationName != '"')
1406 strcatW(wcmd, wQuote);
1407 strcatW(wcmd, wszApplicationName);
1408 strcatW(wcmd, wQuote);
1410 else
1411 strcatW(wcmd, wszApplicationName);
1413 if (resultLen > sizeof(wcmd)/sizeof(WCHAR))
1414 ERR("Argify buffer not large enough... truncating\n");
1415 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1418 strcpyW(classname, psei->lpClass);
1419 rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1421 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1422 if (33 > rslt)
1423 return rslt;
1424 rslt = SHELL_quote_and_execute( execCmd, wszEmpty, classname,
1425 wszApplicationName, NULL, psei,
1426 psei_out, execfunc );
1427 return rslt;
1430 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1432 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1433 WCHAR buffer[MAX_PATH];
1435 /* last chance to translate IDList: now also allow CLSID paths */
1436 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, sizeof(buffer)/sizeof(WCHAR)))) {
1437 if (buffer[0]==':' && buffer[1]==':') {
1438 /* open shell folder for the specified class GUID */
1439 if (strlenW(buffer) + 1 > parametersLen)
1440 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1441 lstrlenW(buffer) + 1, parametersLen);
1442 lstrcpynW(wszParameters, buffer, parametersLen);
1443 if (strlenW(wExplorer) > dwApplicationNameLen)
1444 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1445 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1446 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1448 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1449 } else {
1450 WCHAR target[MAX_PATH];
1451 DWORD attribs;
1452 DWORD resultLen;
1453 /* Check if we're executing a directory and if so use the
1454 handler for the Folder class */
1455 strcpyW(target, buffer);
1456 attribs = GetFileAttributesW(buffer);
1457 if (attribs != INVALID_FILE_ATTRIBUTES &&
1458 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1459 HCR_GetExecuteCommandW(0, wszFolder,
1460 sei->lpVerb,
1461 buffer, sizeof(buffer))) {
1462 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1463 buffer, target, sei->lpIDList, NULL, &resultLen);
1464 if (resultLen > dwApplicationNameLen)
1465 ERR("Argify buffer not large enough... truncating\n");
1467 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1472 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 )
1474 static const WCHAR wQuote[] = {'"',0};
1475 static const WCHAR wSpace[] = {' ',0};
1476 UINT_PTR retval;
1477 DWORD len;
1478 WCHAR *wszQuotedCmd;
1480 /* Length of quotes plus length of command plus NULL terminator */
1481 len = 2 + lstrlenW(wcmd) + 1;
1482 if (wszParameters[0])
1484 /* Length of space plus length of parameters */
1485 len += 1 + lstrlenW(wszParameters);
1487 wszQuotedCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1488 /* Must quote to handle case where cmd contains spaces,
1489 * else security hole if malicious user creates executable file "C:\\Program"
1491 strcpyW(wszQuotedCmd, wQuote);
1492 strcatW(wszQuotedCmd, wcmd);
1493 strcatW(wszQuotedCmd, wQuote);
1494 if (wszParameters[0]) {
1495 strcatW(wszQuotedCmd, wSpace);
1496 strcatW(wszQuotedCmd, wszParameters);
1498 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1499 if (*wszKeyname)
1500 retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1501 else
1502 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1503 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1504 return retval;
1507 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1509 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1510 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1511 UINT_PTR retval;
1512 WCHAR *lpstrProtocol;
1513 LPCWSTR lpstrRes;
1514 INT iSize;
1515 DWORD len;
1517 lpstrRes = strchrW(lpFile, ':');
1518 if (lpstrRes)
1519 iSize = lpstrRes - lpFile;
1520 else
1521 iSize = strlenW(lpFile);
1523 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1524 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1525 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1526 if (psei->lpVerb && *psei->lpVerb)
1527 len += lstrlenW(psei->lpVerb);
1528 else
1529 len += lstrlenW(wszOpen);
1530 lpstrProtocol = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1531 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1532 lpstrProtocol[iSize] = '\0';
1533 strcatW(lpstrProtocol, wShell);
1534 strcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb: wszOpen);
1535 strcatW(lpstrProtocol, wCommand);
1537 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1538 wcmd, execfunc, psei, psei_out);
1539 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1540 return retval;
1543 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1545 WCHAR msg[2048];
1546 int error_code=GetLastError();
1548 if (retval == SE_ERR_NOASSOC)
1549 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, sizeof(msg)/sizeof(WCHAR));
1550 else
1551 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, sizeof(msg)/sizeof(WCHAR), NULL);
1553 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1556 /*************************************************************************
1557 * SHELL_execute [Internal]
1559 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1561 static const WCHAR wWww[] = {'w','w','w',0};
1562 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1563 static const DWORD unsupportedFlags =
1564 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1565 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1566 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1568 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1569 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1570 DWORD dwApplicationNameLen = MAX_PATH+2;
1571 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1572 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1573 DWORD len;
1574 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1575 WCHAR *env;
1576 WCHAR wszKeyname[256];
1577 LPCWSTR lpFile;
1578 UINT_PTR retval = SE_ERR_NOASSOC;
1580 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1581 sei_tmp = *sei;
1583 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1584 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1585 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1586 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1587 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1588 debugstr_w(sei_tmp.lpClass) : "not used");
1590 sei->hProcess = NULL;
1592 /* make copies of all path/command strings */
1593 if (!sei_tmp.lpFile)
1595 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1596 *wszApplicationName = '\0';
1598 else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = strlenW(sei_tmp.lpFile))-1] == '\"')
1600 if(len-1 >= dwApplicationNameLen) dwApplicationNameLen = len;
1601 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1602 memcpy(wszApplicationName, sei_tmp.lpFile+1, len*sizeof(WCHAR));
1603 if(len > 2)
1604 wszApplicationName[len-2] = '\0';
1605 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1606 } else {
1607 DWORD l = strlenW(sei_tmp.lpFile)+1;
1608 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1609 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1610 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1613 wszParameters = parametersBuffer;
1614 if (sei_tmp.lpParameters)
1616 len = lstrlenW(sei_tmp.lpParameters) + 1;
1617 if (len > parametersLen)
1619 wszParameters = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1620 parametersLen = len;
1622 strcpyW(wszParameters, sei_tmp.lpParameters);
1624 else
1625 *wszParameters = '\0';
1627 wszDir = dirBuffer;
1628 if (sei_tmp.lpDirectory)
1630 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1631 if (len > sizeof(dirBuffer) / sizeof(WCHAR))
1632 wszDir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1633 strcpyW(wszDir, sei_tmp.lpDirectory);
1635 else
1636 *wszDir = '\0';
1638 /* adjust string pointers to point to the new buffers */
1639 sei_tmp.lpFile = wszApplicationName;
1640 sei_tmp.lpParameters = wszParameters;
1641 sei_tmp.lpDirectory = wszDir;
1643 if (sei_tmp.fMask & unsupportedFlags)
1645 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1648 /* process the IDList */
1649 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1651 IShellExecuteHookW* pSEH;
1653 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1655 if (SUCCEEDED(hr))
1657 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1659 IShellExecuteHookW_Release(pSEH);
1661 if (hr == S_OK) {
1662 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1663 if (wszParameters != parametersBuffer)
1664 HeapFree(GetProcessHeap(), 0, wszParameters);
1665 if (wszDir != dirBuffer)
1666 HeapFree(GetProcessHeap(), 0, wszDir);
1667 return TRUE;
1671 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1672 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1675 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1677 sei->hInstApp = (HINSTANCE) 33;
1678 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1679 if (wszParameters != parametersBuffer)
1680 HeapFree(GetProcessHeap(), 0, wszParameters);
1681 if (wszDir != dirBuffer)
1682 HeapFree(GetProcessHeap(), 0, wszDir);
1683 return TRUE;
1686 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1688 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1689 execfunc );
1690 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1691 do_error_dialog(retval, sei_tmp.hwnd);
1692 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1693 if (wszParameters != parametersBuffer)
1694 HeapFree(GetProcessHeap(), 0, wszParameters);
1695 if (wszDir != dirBuffer)
1696 HeapFree(GetProcessHeap(), 0, wszDir);
1697 return retval > 32;
1700 /* Has the IDList not yet been translated? */
1701 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1703 SHELL_translate_idlist( &sei_tmp, wszParameters,
1704 parametersLen,
1705 wszApplicationName,
1706 dwApplicationNameLen );
1709 /* convert file URLs */
1710 if (UrlIsFileUrlW(sei_tmp.lpFile))
1712 LPWSTR buf;
1713 DWORD size;
1715 size = MAX_PATH;
1716 buf = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1717 if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0))) {
1718 HeapFree(GetProcessHeap(), 0, buf);
1719 return SE_ERR_OOM;
1722 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1723 dwApplicationNameLen = lstrlenW(buf) + 1;
1724 wszApplicationName = buf;
1725 sei_tmp.lpFile = wszApplicationName;
1727 else /* or expand environment strings (not both!) */
1729 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1730 if (len>0)
1732 LPWSTR buf;
1733 buf = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1735 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1736 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1737 dwApplicationNameLen = len + 1;
1738 wszApplicationName = buf;
1740 sei_tmp.lpFile = wszApplicationName;
1744 if (*sei_tmp.lpDirectory)
1746 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1747 if (len > 0)
1749 LPWSTR buf;
1750 len++;
1751 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1752 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1753 if (wszDir != dirBuffer)
1754 HeapFree(GetProcessHeap(), 0, wszDir);
1755 wszDir = buf;
1756 sei_tmp.lpDirectory = wszDir;
1760 /* Else, try to execute the filename */
1761 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1762 lpFile = sei_tmp.lpFile;
1763 wcmd = wcmdBuffer;
1764 len = lstrlenW(wszApplicationName) + 3;
1765 if (sei_tmp.lpParameters[0])
1766 len += 1 + lstrlenW(wszParameters);
1767 if (len > wcmdLen)
1769 wcmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1770 wcmdLen = len;
1772 wcmd[0] = '\"';
1773 len = lstrlenW(wszApplicationName);
1774 memcpy(wcmd+1, wszApplicationName, len * sizeof(WCHAR));
1775 len++;
1776 wcmd[len++] = '\"';
1777 wcmd[len] = 0;
1778 if (sei_tmp.lpParameters[0]) {
1779 wcmd[len++] = ' ';
1780 strcpyW(wcmd+len, wszParameters);
1783 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1784 if (retval > 32) {
1785 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1786 if (wszParameters != parametersBuffer)
1787 HeapFree(GetProcessHeap(), 0, wszParameters);
1788 if (wszDir != dirBuffer)
1789 HeapFree(GetProcessHeap(), 0, wszDir);
1790 if (wcmd != wcmdBuffer)
1791 HeapFree(GetProcessHeap(), 0, wcmd);
1792 return TRUE;
1795 /* Else, try to find the executable */
1796 wcmd[0] = '\0';
1797 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1798 if (retval > 32) /* Found */
1800 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszKeyname,
1801 wszApplicationName, env, &sei_tmp,
1802 sei, execfunc );
1803 HeapFree( GetProcessHeap(), 0, env );
1805 else if (PathIsDirectoryW(lpFile))
1807 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r',0};
1808 static const WCHAR wQuote[] = {'"',0};
1809 WCHAR wExec[MAX_PATH];
1810 WCHAR * lpQuotedFile = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3) );
1812 if (lpQuotedFile)
1814 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, wExplorer,
1815 wszOpen, wExec, MAX_PATH,
1816 NULL, &env, NULL, NULL );
1817 if (retval > 32)
1819 strcpyW(lpQuotedFile, wQuote);
1820 strcatW(lpQuotedFile, lpFile);
1821 strcatW(lpQuotedFile, wQuote);
1822 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1823 wszKeyname,
1824 wszApplicationName, env,
1825 &sei_tmp, sei, execfunc );
1826 HeapFree( GetProcessHeap(), 0, env );
1828 HeapFree( GetProcessHeap(), 0, lpQuotedFile );
1830 else
1831 retval = 0; /* Out of memory */
1833 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1835 retval = SHELL_execute_url( lpFile, wcmd, &sei_tmp, sei, execfunc );
1837 /* Check if file specified is in the form www.??????.*** */
1838 else if (!strncmpiW(lpFile, wWww, 3))
1840 /* if so, prefix lpFile with http:// and call ShellExecute */
1841 WCHAR lpstrTmpFile[256];
1842 strcpyW(lpstrTmpFile, wHttp);
1843 strcatW(lpstrTmpFile, lpFile);
1844 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1847 TRACE("retval %lu\n", retval);
1849 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1850 if (wszParameters != parametersBuffer)
1851 HeapFree(GetProcessHeap(), 0, wszParameters);
1852 if (wszDir != dirBuffer)
1853 HeapFree(GetProcessHeap(), 0, wszDir);
1854 if (wcmd != wcmdBuffer)
1855 HeapFree(GetProcessHeap(), 0, wcmd);
1857 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1859 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1860 do_error_dialog(retval, sei_tmp.hwnd);
1861 return retval > 32;
1864 /*************************************************************************
1865 * ShellExecuteA [SHELL32.290]
1867 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
1868 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1870 SHELLEXECUTEINFOA sei;
1872 TRACE("%p,%s,%s,%s,%s,%d\n",
1873 hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
1874 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1876 sei.cbSize = sizeof(sei);
1877 sei.fMask = SEE_MASK_FLAG_NO_UI;
1878 sei.hwnd = hWnd;
1879 sei.lpVerb = lpVerb;
1880 sei.lpFile = lpFile;
1881 sei.lpParameters = lpParameters;
1882 sei.lpDirectory = lpDirectory;
1883 sei.nShow = iShowCmd;
1884 sei.lpIDList = 0;
1885 sei.lpClass = 0;
1886 sei.hkeyClass = 0;
1887 sei.dwHotKey = 0;
1888 sei.hProcess = 0;
1890 ShellExecuteExA (&sei);
1891 return sei.hInstApp;
1894 /*************************************************************************
1895 * ShellExecuteExA [SHELL32.292]
1898 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1900 SHELLEXECUTEINFOW seiW;
1901 BOOL ret;
1902 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1904 TRACE("%p\n", sei);
1906 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1908 if (sei->lpVerb)
1909 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1911 if (sei->lpFile)
1912 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1914 if (sei->lpParameters)
1915 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1917 if (sei->lpDirectory)
1918 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1920 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1921 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1922 else
1923 seiW.lpClass = NULL;
1925 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1927 sei->hInstApp = seiW.hInstApp;
1929 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1930 sei->hProcess = seiW.hProcess;
1932 SHFree(wVerb);
1933 SHFree(wFile);
1934 SHFree(wParameters);
1935 SHFree(wDirectory);
1936 SHFree(wClass);
1938 return ret;
1941 /*************************************************************************
1942 * ShellExecuteExW [SHELL32.293]
1945 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1947 return SHELL_execute( sei, SHELL_ExecuteW );
1950 /*************************************************************************
1951 * ShellExecuteW [SHELL32.294]
1952 * from shellapi.h
1953 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1954 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1956 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
1957 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1959 SHELLEXECUTEINFOW sei;
1961 TRACE("\n");
1962 sei.cbSize = sizeof(sei);
1963 sei.fMask = SEE_MASK_FLAG_NO_UI;
1964 sei.hwnd = hwnd;
1965 sei.lpVerb = lpVerb;
1966 sei.lpFile = lpFile;
1967 sei.lpParameters = lpParameters;
1968 sei.lpDirectory = lpDirectory;
1969 sei.nShow = nShowCmd;
1970 sei.lpIDList = 0;
1971 sei.lpClass = 0;
1972 sei.hkeyClass = 0;
1973 sei.dwHotKey = 0;
1974 sei.hProcess = 0;
1976 SHELL_execute( &sei, SHELL_ExecuteW );
1977 return sei.hInstApp;
1980 /*************************************************************************
1981 * WOWShellExecute [SHELL32.@]
1983 * FIXME: the callback function most likely doesn't work the same way on Windows.
1985 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb,LPCSTR lpFile,
1986 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
1988 SHELLEXECUTEINFOW seiW;
1989 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
1990 HANDLE hProcess = 0;
1992 seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
1993 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
1994 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
1995 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
1997 seiW.cbSize = sizeof(seiW);
1998 seiW.fMask = 0;
1999 seiW.hwnd = hWnd;
2000 seiW.nShow = iShowCmd;
2001 seiW.lpIDList = 0;
2002 seiW.lpClass = 0;
2003 seiW.hkeyClass = 0;
2004 seiW.dwHotKey = 0;
2005 seiW.hProcess = hProcess;
2007 SHELL_execute( &seiW, callback );
2009 SHFree(wVerb);
2010 SHFree(wFile);
2011 SHFree(wParameters);
2012 SHFree(wDirectory);
2013 return seiW.hInstApp;
2016 /*************************************************************************
2017 * OpenAs_RunDLLA [SHELL32.@]
2019 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2021 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2024 /*************************************************************************
2025 * OpenAs_RunDLLW [SHELL32.@]
2027 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2029 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2032 /*************************************************************************
2033 * RegenerateUserEnvironment [SHELL32.@]
2035 BOOL WINAPI RegenerateUserEnvironment(WCHAR *wunknown, BOOL bunknown)
2037 FIXME("stub: %p, %d\n", wunknown, bunknown);
2038 return FALSE;