shell32: Avoid using isspace() for WCHARs.
[wine.git] / dlls / shell32 / shlexec.c
blobc0ef53ab8e7e0a86307fde6d6d3e5b76da72bd0d
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33 #include <assert.h>
35 #define COBJMACROS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winuser.h"
42 #include "shlwapi.h"
43 #include "ddeml.h"
45 #include "shell32_main.h"
46 #include "pidl.h"
47 #include "shresdef.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(exec);
53 static const WCHAR wszOpen[] = {'o','p','e','n',0};
54 static const WCHAR wszExe[] = {'.','e','x','e',0};
55 static const WCHAR wszILPtr[] = {':','%','p',0};
56 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
57 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
58 static const WCHAR wszEmpty[] = {0};
60 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
62 typedef UINT_PTR (*SHELL_ExecuteW32)(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
63 const SHELLEXECUTEINFOW *sei, LPSHELLEXECUTEINFOW sei_out);
65 static inline BOOL isSpace(WCHAR c)
67 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
70 /***********************************************************************
71 * SHELL_ArgifyW [Internal]
73 * this function is supposed to expand the escape sequences found in the registry
74 * some diving reported that the following were used:
75 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
76 * %1 file
77 * %2 printer
78 * %3 driver
79 * %4 port
80 * %I address of a global item ID (explorer switch /idlist)
81 * %L seems to be %1 as long filename followed by the 8+3 variation
82 * %S ???
83 * %* all following parameters (see batfile)
86 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
88 WCHAR xlpFile[1024];
89 BOOL done = FALSE;
90 BOOL found_p1 = FALSE;
91 PWSTR res = out;
92 PCWSTR cmd;
93 DWORD used = 0;
95 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
96 debugstr_w(lpFile), pidl, args);
98 while (*fmt)
100 if (*fmt == '%')
102 switch (*++fmt)
104 case '\0':
105 case '%':
106 used++;
107 if (used < len)
108 *res++ = '%';
109 break;
111 case '2':
112 case '3':
113 case '4':
114 case '5':
115 case '6':
116 case '7':
117 case '8':
118 case '9':
119 case '0':
120 case '*':
121 if (args)
123 if (*fmt == '*')
125 used++;
126 if (used < len)
127 *res++ = '"';
128 while(*args)
130 used++;
131 if (used < len)
132 *res++ = *args++;
133 else
134 args++;
136 used++;
137 if (used < len)
138 *res++ = '"';
140 else
142 while(*args && !isSpace(*args))
144 used++;
145 if (used < len)
146 *res++ = *args++;
147 else
148 args++;
151 while(isSpace(*args))
152 ++args;
154 break;
156 /* else fall through */
157 case '1':
158 if (!done || (*fmt == '1'))
160 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
161 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
162 cmd = xlpFile;
163 else
164 cmd = lpFile;
166 used += strlenW(cmd);
167 if (used < len)
169 strcpyW(res, cmd);
170 res += strlenW(cmd);
173 found_p1 = TRUE;
174 break;
177 * IE uses this a lot for activating things such as windows media
178 * player. This is not verified to be fully correct but it appears
179 * to work just fine.
181 case 'l':
182 case 'L':
183 if (lpFile) {
184 used += strlenW(lpFile);
185 if (used < len)
187 strcpyW(res, lpFile);
188 res += strlenW(lpFile);
191 found_p1 = TRUE;
192 break;
194 case 'i':
195 case 'I':
196 if (pidl) {
197 INT chars = 0;
198 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
199 * allowing a buffer of 100 should more than exceed all needs */
200 WCHAR buf[100];
201 LPVOID pv;
202 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
203 pv = SHLockShared(hmem, 0);
204 chars = sprintfW(buf, wszILPtr, pv);
205 if (chars >= sizeof(buf)/sizeof(WCHAR))
206 ERR("pidl format buffer too small!\n");
207 used += chars;
208 if (used < len)
210 strcpyW(res,buf);
211 res += chars;
213 SHUnlockShared(pv);
215 found_p1 = TRUE;
216 break;
218 default:
220 * Check if this is an env-variable here...
223 /* Make sure that we have at least one more %.*/
224 if (strchrW(fmt, '%'))
226 WCHAR tmpBuffer[1024];
227 PWSTR tmpB = tmpBuffer;
228 WCHAR tmpEnvBuff[MAX_PATH];
229 DWORD envRet;
231 while (*fmt != '%')
232 *tmpB++ = *fmt++;
233 *tmpB++ = 0;
235 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
237 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
238 if (envRet == 0 || envRet > MAX_PATH)
240 used += strlenW(tmpBuffer);
241 if (used < len)
243 strcpyW( res, tmpBuffer );
244 res += strlenW(tmpBuffer);
247 else
249 used += strlenW(tmpEnvBuff);
250 if (used < len)
252 strcpyW( res, tmpEnvBuff );
253 res += strlenW(tmpEnvBuff);
257 done = TRUE;
258 break;
260 /* Don't skip past terminator (catch a single '%' at the end) */
261 if (*fmt != '\0')
263 fmt++;
266 else
268 used ++;
269 if (used < len)
270 *res++ = *fmt++;
271 else
272 fmt++;
276 used ++;
277 if (res - out < len)
278 *res = '\0';
279 else
280 out[len-1] = '\0';
282 TRACE("used %i of %i space\n",used,len);
283 if (out_len)
284 *out_len = used;
286 return found_p1;
289 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
291 STRRET strret;
292 IShellFolder* desktop;
294 HRESULT hr = SHGetDesktopFolder(&desktop);
296 if (SUCCEEDED(hr)) {
297 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
299 if (SUCCEEDED(hr))
300 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
302 IShellFolder_Release(desktop);
305 return hr;
308 /*************************************************************************
309 * SHELL_ExecuteW [Internal]
312 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
313 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
315 STARTUPINFOW startup;
316 PROCESS_INFORMATION info;
317 UINT_PTR retval = SE_ERR_NOASSOC;
318 UINT gcdret = 0;
319 WCHAR curdir[MAX_PATH];
320 DWORD dwCreationFlags;
321 const WCHAR *lpDirectory = NULL;
323 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
325 /* make sure we don't fail the CreateProcess if the calling app passes in
326 * a bad working directory */
327 if (psei->lpDirectory && psei->lpDirectory[0])
329 DWORD attr = GetFileAttributesW(psei->lpDirectory);
330 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
331 lpDirectory = psei->lpDirectory;
334 /* ShellExecute specifies the command from psei->lpDirectory
335 * if present. Not from the current dir as CreateProcess does */
336 if( lpDirectory )
337 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
338 if( !SetCurrentDirectoryW( lpDirectory))
339 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
340 ZeroMemory(&startup,sizeof(STARTUPINFOW));
341 startup.cb = sizeof(STARTUPINFOW);
342 startup.dwFlags = STARTF_USESHOWWINDOW;
343 startup.wShowWindow = psei->nShow;
344 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
345 if (!(psei->fMask & SEE_MASK_NO_CONSOLE))
346 dwCreationFlags |= CREATE_NEW_CONSOLE;
347 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
348 lpDirectory, &startup, &info))
350 /* Give 30 seconds to the app to come up, if desired. Probably only needed
351 when starting app immediately before making a DDE connection. */
352 if (shWait)
353 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
354 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
355 retval = 33;
356 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
357 psei_out->hProcess = info.hProcess;
358 else
359 CloseHandle( info.hProcess );
360 CloseHandle( info.hThread );
362 else if ((retval = GetLastError()) >= 32)
364 TRACE("CreateProcess returned error %ld\n", retval);
365 retval = ERROR_BAD_FORMAT;
368 TRACE("returning %lu\n", retval);
370 psei_out->hInstApp = (HINSTANCE)retval;
371 if( gcdret )
372 if( !SetCurrentDirectoryW( curdir))
373 ERR("cannot return to directory %s\n", debugstr_w(curdir));
375 return retval;
379 /***********************************************************************
380 * SHELL_BuildEnvW [Internal]
382 * Build the environment for the new process, adding the specified
383 * path to the PATH variable. Returned pointer must be freed by caller.
385 static void *SHELL_BuildEnvW( const WCHAR *path )
387 static const WCHAR wPath[] = {'P','A','T','H','=',0};
388 WCHAR *strings, *new_env;
389 WCHAR *p, *p2;
390 int total = strlenW(path) + 1;
391 BOOL got_path = FALSE;
393 if (!(strings = GetEnvironmentStringsW())) return NULL;
394 p = strings;
395 while (*p)
397 int len = strlenW(p) + 1;
398 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
399 total += len;
400 p += len;
402 if (!got_path) total += 5; /* we need to create PATH */
403 total++; /* terminating null */
405 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
407 FreeEnvironmentStringsW( strings );
408 return NULL;
410 p = strings;
411 p2 = new_env;
412 while (*p)
414 int len = strlenW(p) + 1;
415 memcpy( p2, p, len * sizeof(WCHAR) );
416 if (!strncmpiW( p, wPath, 5 ))
418 p2[len - 1] = ';';
419 strcpyW( p2 + len, path );
420 p2 += strlenW(path) + 1;
422 p += len;
423 p2 += len;
425 if (!got_path)
427 strcpyW( p2, wPath );
428 strcatW( p2, path );
429 p2 += strlenW(p2) + 1;
431 *p2 = 0;
432 FreeEnvironmentStringsW( strings );
433 return new_env;
437 /***********************************************************************
438 * SHELL_TryAppPathW [Internal]
440 * Helper function for SHELL_FindExecutable
441 * @param lpResult - pointer to a buffer of size MAX_PATH
442 * On entry: szName is a filename (probably without path separators).
443 * On exit: if szName found in "App Path", place full path in lpResult, and return true
445 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
447 static const WCHAR wszKeyAppPaths[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',
448 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
449 static const WCHAR wPath[] = {'P','a','t','h',0};
450 HKEY hkApp = 0;
451 WCHAR buffer[1024];
452 LONG len;
453 LONG res;
454 BOOL found = FALSE;
456 if (env) *env = NULL;
457 strcpyW(buffer, wszKeyAppPaths);
458 strcatW(buffer, szName);
459 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
460 if (res) goto end;
462 len = MAX_PATH*sizeof(WCHAR);
463 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
464 if (res) goto end;
465 found = TRUE;
467 if (env)
469 DWORD count = sizeof(buffer);
470 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
471 *env = SHELL_BuildEnvW( buffer );
474 end:
475 if (hkApp) RegCloseKey(hkApp);
476 return found;
479 /*************************************************************************
480 * SHELL_FindExecutableByVerb [Internal]
482 * called from SHELL_FindExecutable or SHELL_execute_class
483 * in/out:
484 * classname a buffer, big enough, to get the key name to do actually the
485 * command "WordPad.Document.1\\shell\\open\\command"
486 * passed as "WordPad.Document.1"
487 * in:
488 * lpVerb the operation on it (open)
489 * commandlen the size of command buffer (in bytes)
490 * out:
491 * command a buffer, to store the command to do the
492 * operation on the file
493 * key a buffer, big enough, to get the key name to do actually the
494 * command "WordPad.Document.1\\shell\\open\\command"
495 * Can be NULL
497 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
499 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
500 HKEY hkeyClass;
501 WCHAR verb[MAX_PATH];
503 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
504 return SE_ERR_NOASSOC;
505 if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, sizeof(verb)/sizeof(verb[0])))
506 return SE_ERR_NOASSOC;
507 RegCloseKey(hkeyClass);
509 /* Looking for ...buffer\shell\<verb>\command */
510 strcatW(classname, wszShell);
511 strcatW(classname, verb);
512 strcatW(classname, wCommand);
514 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
515 &commandlen) == ERROR_SUCCESS)
517 commandlen /= sizeof(WCHAR);
518 if (key) strcpyW(key, classname);
519 #if 0
520 LPWSTR tmp;
521 WCHAR param[256];
522 LONG paramlen = sizeof(param);
523 static const WCHAR wSpace[] = {' ',0};
525 /* FIXME: it seems all Windows version don't behave the same here.
526 * the doc states that this ddeexec information can be found after
527 * the exec names.
528 * on Win98, it doesn't appear, but I think it does on Win2k
530 /* Get the parameters needed by the application
531 from the associated ddeexec key */
532 tmp = strstrW(classname, wCommand);
533 tmp[0] = '\0';
534 strcatW(classname, wDdeexec);
535 if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
536 &paramlen) == ERROR_SUCCESS)
538 paramlen /= sizeof(WCHAR);
539 strcatW(command, wSpace);
540 strcatW(command, param);
541 commandlen += paramlen;
543 #endif
545 command[commandlen] = '\0';
547 return 33; /* FIXME see SHELL_FindExecutable() */
550 return SE_ERR_NOASSOC;
553 /*************************************************************************
554 * SHELL_FindExecutable [Internal]
556 * Utility for code sharing between FindExecutable and ShellExecute
557 * in:
558 * lpFile the name of a file
559 * lpVerb the operation on it (open)
560 * out:
561 * lpResult a buffer, big enough :-(, to store the command to do the
562 * operation on the file
563 * key a buffer, big enough, to get the key name to do actually the
564 * command (it'll be used afterwards for more information
565 * on the operation)
567 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
568 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
570 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
571 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
572 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
573 WCHAR *extension = NULL; /* pointer to file extension */
574 WCHAR classname[256]; /* registry name for this file type */
575 LONG classnamelen = sizeof(classname); /* length of above */
576 WCHAR command[1024]; /* command from registry */
577 WCHAR wBuffer[256]; /* Used to GetProfileString */
578 UINT retval = SE_ERR_NOASSOC;
579 WCHAR *tok; /* token pointer */
580 WCHAR xlpFile[256]; /* result of SearchPath */
581 DWORD attribs; /* file attributes */
583 TRACE("%s\n", debugstr_w(lpFile));
585 if (!lpResult)
586 return ERROR_INVALID_PARAMETER;
588 xlpFile[0] = '\0';
589 lpResult[0] = '\0'; /* Start off with an empty return string */
590 if (key) *key = '\0';
592 /* trap NULL parameters on entry */
593 if (!lpFile)
595 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
596 debugstr_w(lpFile), debugstr_w(lpResult));
597 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
600 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
602 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
603 return 33;
606 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
608 TRACE("SearchPathW returned non-zero\n");
609 lpFile = xlpFile;
610 /* The file was found in the application-supplied default directory (or the system search path) */
612 else if (lpPath && SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
614 TRACE("SearchPathW returned non-zero\n");
615 lpFile = xlpFile;
616 /* The file was found in one of the directories in the system-wide search path */
619 attribs = GetFileAttributesW(lpFile);
620 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
622 strcpyW(classname, wszFolder);
624 else
626 /* Did we get something? Anything? */
627 if (xlpFile[0]==0)
629 TRACE("Returning SE_ERR_FNF\n");
630 return SE_ERR_FNF;
632 /* First thing we need is the file's extension */
633 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
634 /* File->Run in progman uses */
635 /* .\FILE.EXE :( */
636 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
638 if (extension == NULL || extension[1]==0)
640 WARN("Returning SE_ERR_NOASSOC\n");
641 return SE_ERR_NOASSOC;
644 /* Three places to check: */
645 /* 1. win.ini, [windows], programs (NB no leading '.') */
646 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
647 /* 3. win.ini, [extensions], extension (NB no leading '.' */
648 /* All I know of the order is that registry is checked before */
649 /* extensions; however, it'd make sense to check the programs */
650 /* section first, so that's what happens here. */
652 /* See if it's a program - if GetProfileString fails, we skip this
653 * section. Actually, if GetProfileString fails, we've probably
654 * got a lot more to worry about than running a program... */
655 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
657 CharLowerW(wBuffer);
658 tok = wBuffer;
659 while (*tok)
661 WCHAR *p = tok;
662 while (*p && *p != ' ' && *p != '\t') p++;
663 if (*p)
665 *p++ = 0;
666 while (*p == ' ' || *p == '\t') p++;
669 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
671 strcpyW(lpResult, xlpFile);
672 /* Need to perhaps check that the file has a path
673 * attached */
674 TRACE("found %s\n", debugstr_w(lpResult));
675 return 33;
676 /* Greater than 32 to indicate success */
678 tok = p;
682 /* Check registry */
683 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
684 &classnamelen) == ERROR_SUCCESS)
686 classnamelen /= sizeof(WCHAR);
687 if (classnamelen == sizeof(classname)/sizeof(WCHAR))
688 classnamelen--;
689 classname[classnamelen] = '\0';
690 TRACE("File type: %s\n", debugstr_w(classname));
692 else
694 *classname = '\0';
698 if (*classname)
700 /* pass the verb string to SHELL_FindExecutableByVerb() */
701 retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
703 if (retval > 32)
705 DWORD finishedLen;
706 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
707 if (finishedLen > resultLen)
708 ERR("Argify buffer not large enough.. truncated\n");
710 /* Remove double quotation marks and command line arguments */
711 if (*lpResult == '"')
713 WCHAR *p = lpResult;
714 while (*(p + 1) != '"')
716 *p = *(p + 1);
717 p++;
719 *p = '\0';
721 else
723 /* Truncate on first space */
724 WCHAR *p = lpResult;
725 while (*p != ' ' && *p != '\0')
726 p++;
727 *p='\0';
731 else /* Check win.ini */
733 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
735 /* Toss the leading dot */
736 extension++;
737 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
739 if (strlenW(command) != 0)
741 strcpyW(lpResult, command);
742 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
743 if (tok != NULL)
745 tok[0] = '\0';
746 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
747 tok = strchrW(command, '^'); /* see above */
748 if ((tok != NULL) && (strlenW(tok)>5))
750 strcatW(lpResult, &tok[5]);
753 retval = 33; /* FIXME - see above */
758 TRACE("returning %s\n", debugstr_w(lpResult));
759 return retval;
762 /******************************************************************
763 * dde_cb
765 * callback for the DDE connection. not really useful
767 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
768 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
769 ULONG_PTR dwData1, ULONG_PTR dwData2)
771 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
772 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
773 return NULL;
776 /******************************************************************
777 * dde_connect
779 * ShellExecute helper. Used to do an operation with a DDE connection
781 * Handles both the direct connection (try #1), and if it fails,
782 * launching an application and trying (#2) to connect to it
785 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
786 const WCHAR* lpFile, WCHAR *env,
787 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
788 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
790 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
791 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
792 WCHAR regkey[256];
793 WCHAR * endkey = regkey + strlenW(key);
794 WCHAR app[256], topic[256], ifexec[256], static_res[256];
795 WCHAR * dynamic_res=NULL;
796 WCHAR * res;
797 LONG applen, topiclen, ifexeclen;
798 WCHAR * exec;
799 DWORD ddeInst = 0;
800 DWORD tid;
801 DWORD resultLen, endkeyLen;
802 HSZ hszApp, hszTopic;
803 HCONV hConv;
804 HDDEDATA hDdeData;
805 unsigned ret = SE_ERR_NOASSOC;
806 BOOL unicode = !(GetVersion() & 0x80000000);
808 if (strlenW(key) + 1 > sizeof(regkey) / sizeof(regkey[0]))
810 FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
811 return 2;
813 strcpyW(regkey, key);
814 endkeyLen = sizeof(regkey) / sizeof(regkey[0]) - (endkey - regkey);
815 if (strlenW(wApplication) + 1 > endkeyLen)
817 FIXME("endkey %s overruns buffer\n", debugstr_w(wApplication));
818 return 2;
820 strcpyW(endkey, wApplication);
821 applen = sizeof(app);
822 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
824 WCHAR command[1024], fullpath[MAX_PATH];
825 static const WCHAR wSo[] = { '.','s','o',0 };
826 int sizeSo = sizeof(wSo)/sizeof(WCHAR);
827 LPWSTR ptr = NULL;
828 DWORD ret = 0;
830 /* Get application command from start string and find filename of application */
831 if (*start == '"')
833 if (strlenW(start + 1) + 1 > sizeof(command) / sizeof(command[0]))
835 FIXME("size of input parameter %s larger than buffer\n",
836 debugstr_w(start + 1));
837 return 2;
839 strcpyW(command, start+1);
840 if ((ptr = strchrW(command, '"')))
841 *ptr = 0;
842 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
844 else
846 LPCWSTR p;
847 LPWSTR space;
848 for (p=start; (space=strchrW(p, ' ')); p=space+1)
850 int idx = space-start;
851 memcpy(command, start, idx*sizeof(WCHAR));
852 command[idx] = '\0';
853 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr)))
854 break;
856 if (!ret)
857 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
860 if (!ret)
862 ERR("Unable to find application path for command %s\n", debugstr_w(start));
863 return ERROR_ACCESS_DENIED;
865 if (strlenW(ptr) + 1 > sizeof(app) / sizeof(app[0]))
867 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
868 return 2;
870 strcpyW(app, ptr);
872 /* Remove extensions (including .so) */
873 ptr = app + strlenW(app) - (sizeSo-1);
874 if (strlenW(app) >= sizeSo &&
875 !strcmpW(ptr, wSo))
876 *ptr = 0;
878 ptr = strrchrW(app, '.');
879 assert(ptr);
880 *ptr = 0;
883 if (strlenW(wTopic) + 1 > endkeyLen)
885 FIXME("endkey %s overruns buffer\n", debugstr_w(wTopic));
886 return 2;
888 strcpyW(endkey, wTopic);
889 topiclen = sizeof(topic);
890 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
892 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
893 strcpyW(topic, wSystem);
896 if (unicode)
898 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
899 return 2;
901 else
903 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
904 return 2;
907 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
908 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
910 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
911 exec = ddeexec;
912 if (!hConv)
914 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
915 TRACE("Launching %s\n", debugstr_w(start));
916 ret = execfunc(start, env, TRUE, psei, psei_out);
917 if (ret <= 32)
919 TRACE("Couldn't launch\n");
920 goto error;
922 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
923 if (!hConv)
925 TRACE("Couldn't connect. ret=%d\n", ret);
926 DdeUninitialize(ddeInst);
927 SetLastError(ERROR_DDE_FAIL);
928 return 30; /* whatever */
930 if (strlenW(wIfexec) + 1 > endkeyLen)
932 FIXME("endkey %s overruns buffer\n", debugstr_w(wIfexec));
933 return 2;
935 strcpyW(endkey, wIfexec);
936 ifexeclen = sizeof(ifexec);
937 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
939 exec = ifexec;
943 SHELL_ArgifyW(static_res, sizeof(static_res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
944 if (resultLen > sizeof(static_res)/sizeof(WCHAR))
946 res = dynamic_res = HeapAlloc(GetProcessHeap(), 0, resultLen * sizeof(WCHAR));
947 SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL);
949 else
950 res = static_res;
951 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
953 /* It's documented in the KB 330337 that IE has a bug and returns
954 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
956 if (unicode)
957 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
958 XTYP_EXECUTE, 30000, &tid);
959 else
961 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
962 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
963 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
964 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
965 XTYP_EXECUTE, 10000, &tid );
966 HeapFree(GetProcessHeap(), 0, resA);
968 if (hDdeData)
969 DdeFreeDataHandle(hDdeData);
970 else
971 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
972 ret = 33;
974 HeapFree(GetProcessHeap(), 0, dynamic_res);
976 DdeDisconnect(hConv);
978 error:
979 DdeUninitialize(ddeInst);
981 return ret;
984 /*************************************************************************
985 * execute_from_key [Internal]
987 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
988 LPCWSTR executable_name,
989 SHELL_ExecuteW32 execfunc,
990 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
992 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
993 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
994 WCHAR cmd[256], param[1024], ddeexec[256];
995 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
996 UINT_PTR retval = SE_ERR_NOASSOC;
997 DWORD resultLen;
998 LPWSTR tmp;
1000 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
1001 debugstr_w(szCommandline), debugstr_w(executable_name));
1003 cmd[0] = '\0';
1004 param[0] = '\0';
1006 /* Get the application from the registry */
1007 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
1009 TRACE("got cmd: %s\n", debugstr_w(cmd));
1011 /* Is there a replace() function anywhere? */
1012 cmdlen /= sizeof(WCHAR);
1013 if (cmdlen >= sizeof(cmd)/sizeof(WCHAR))
1014 cmdlen = sizeof(cmd)/sizeof(WCHAR)-1;
1015 cmd[cmdlen] = '\0';
1016 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
1017 if (resultLen > sizeof(param)/sizeof(WCHAR))
1018 ERR("Argify buffer not large enough, truncating\n");
1021 /* Get the parameters needed by the application
1022 from the associated ddeexec key */
1023 tmp = strstrW(key, wCommand);
1024 assert(tmp);
1025 strcpyW(tmp, wDdeexec);
1027 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
1029 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
1030 if (!param[0]) strcpyW(param, executable_name);
1031 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
1033 else if (param[0])
1035 TRACE("executing: %s\n", debugstr_w(param));
1036 retval = execfunc(param, env, FALSE, psei, psei_out);
1038 else
1039 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1041 return retval;
1044 /*************************************************************************
1045 * FindExecutableA [SHELL32.@]
1047 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1049 HINSTANCE retval;
1050 WCHAR *wFile = NULL, *wDirectory = NULL;
1051 WCHAR wResult[MAX_PATH];
1053 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1054 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1056 retval = FindExecutableW(wFile, wDirectory, wResult);
1057 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1058 SHFree( wFile );
1059 SHFree( wDirectory );
1061 TRACE("returning %s\n", lpResult);
1062 return retval;
1065 /*************************************************************************
1066 * FindExecutableW [SHELL32.@]
1068 * This function returns the executable associated with the specified file
1069 * for the default verb.
1071 * PARAMS
1072 * lpFile [I] The file to find the association for. This must refer to
1073 * an existing file otherwise FindExecutable fails and returns
1074 * SE_ERR_FNF.
1075 * lpResult [O] Points to a buffer into which the executable path is
1076 * copied. This parameter must not be NULL otherwise
1077 * FindExecutable() segfaults. The buffer must be of size at
1078 * least MAX_PATH characters.
1080 * RETURNS
1081 * A value greater than 32 on success, less than or equal to 32 otherwise.
1082 * See the SE_ERR_* constants.
1084 * NOTES
1085 * On Windows XP and 2003, FindExecutable() seems to first convert the
1086 * filename into 8.3 format, thus taking into account only the first three
1087 * characters of the extension, and expects to find an association for those.
1088 * However other Windows versions behave sanely.
1090 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1092 UINT_PTR retval = SE_ERR_NOASSOC;
1093 WCHAR old_dir[1024];
1094 WCHAR res[MAX_PATH];
1096 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1098 lpResult[0] = '\0'; /* Start off with an empty return string */
1099 if (lpFile == NULL)
1100 return (HINSTANCE)SE_ERR_FNF;
1102 if (lpDirectory)
1104 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
1105 SetCurrentDirectoryW(lpDirectory);
1108 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, res, MAX_PATH, NULL, NULL, NULL, NULL);
1110 if (retval > 32)
1111 strcpyW(lpResult, res);
1113 TRACE("returning %s\n", debugstr_w(lpResult));
1114 if (lpDirectory)
1115 SetCurrentDirectoryW(old_dir);
1116 return (HINSTANCE)retval;
1119 /* FIXME: is this already implemented somewhere else? */
1120 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1122 LPCWSTR ext = NULL, lpClass = NULL;
1123 LPWSTR cls = NULL;
1124 DWORD type = 0, sz = 0;
1125 HKEY hkey = 0;
1126 LONG r;
1128 if (sei->fMask & SEE_MASK_CLASSALL)
1129 return sei->hkeyClass;
1131 if (sei->fMask & SEE_MASK_CLASSNAME)
1132 lpClass = sei->lpClass;
1133 else
1135 ext = PathFindExtensionW( sei->lpFile );
1136 TRACE("ext = %s\n", debugstr_w( ext ) );
1137 if (!ext)
1138 return hkey;
1140 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1141 if (r != ERROR_SUCCESS )
1142 return hkey;
1144 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1145 if ( r == ERROR_SUCCESS && type == REG_SZ )
1147 sz += sizeof (WCHAR);
1148 cls = HeapAlloc( GetProcessHeap(), 0, sz );
1149 cls[0] = 0;
1150 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1153 RegCloseKey( hkey );
1154 lpClass = cls;
1157 TRACE("class = %s\n", debugstr_w(lpClass) );
1159 hkey = 0;
1160 if ( lpClass )
1161 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1163 HeapFree( GetProcessHeap(), 0, cls );
1165 return hkey;
1168 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1170 LPCITEMIDLIST pidllast = NULL;
1171 IDataObject *dataobj = NULL;
1172 IShellFolder *shf = NULL;
1173 LPITEMIDLIST pidl = NULL;
1174 HRESULT r;
1176 if (sei->fMask & SEE_MASK_CLASSALL)
1177 pidl = sei->lpIDList;
1178 else
1180 WCHAR fullpath[MAX_PATH];
1181 BOOL ret;
1183 fullpath[0] = 0;
1184 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1185 if (!ret)
1186 goto end;
1188 pidl = ILCreateFromPathW( fullpath );
1191 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1192 if ( FAILED( r ) )
1193 goto end;
1195 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1196 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1198 end:
1199 if ( pidl != sei->lpIDList )
1200 ILFree( pidl );
1201 if ( shf )
1202 IShellFolder_Release( shf );
1203 return dataobj;
1206 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1207 LPSHELLEXECUTEINFOW sei )
1209 IContextMenu *cm = NULL;
1210 CMINVOKECOMMANDINFOEX ici;
1211 MENUITEMINFOW info;
1212 WCHAR string[0x80];
1213 INT i, n, def = -1;
1214 HMENU hmenu = 0;
1215 HRESULT r;
1217 TRACE("%p %p\n", obj, sei );
1219 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1220 if ( FAILED( r ) )
1221 return r;
1223 hmenu = CreateMenu();
1224 if ( !hmenu )
1225 goto end;
1227 /* the number of the last menu added is returned in r */
1228 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1229 if ( FAILED( r ) )
1230 goto end;
1232 n = GetMenuItemCount( hmenu );
1233 for ( i = 0; i < n; i++ )
1235 memset( &info, 0, sizeof info );
1236 info.cbSize = sizeof info;
1237 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1238 info.dwTypeData = string;
1239 info.cch = sizeof string;
1240 string[0] = 0;
1241 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1243 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1244 info.fState, info.dwItemData, info.fType, info.wID );
1245 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1246 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1248 def = i;
1249 break;
1253 r = E_FAIL;
1254 if ( def == -1 )
1255 goto end;
1257 memset( &ici, 0, sizeof ici );
1258 ici.cbSize = sizeof ici;
1259 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE|SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1260 ici.nShow = sei->nShow;
1261 ici.lpVerb = MAKEINTRESOURCEA( def );
1262 ici.hwnd = sei->hwnd;
1263 ici.lpParametersW = sei->lpParameters;
1265 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1267 TRACE("invoke command returned %08x\n", r );
1269 end:
1270 if ( hmenu )
1271 DestroyMenu( hmenu );
1272 if ( cm )
1273 IContextMenu_Release( cm );
1274 return r;
1277 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1279 IDataObject *dataobj = NULL;
1280 IObjectWithSite *ows = NULL;
1281 IShellExtInit *obj = NULL;
1282 HRESULT r;
1284 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1286 r = CoInitialize( NULL );
1287 if ( FAILED( r ) )
1288 goto end;
1290 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1291 &IID_IShellExtInit, (LPVOID*)&obj );
1292 if ( FAILED( r ) )
1294 ERR("failed %08x\n", r );
1295 goto end;
1298 dataobj = shellex_get_dataobj( sei );
1299 if ( !dataobj )
1301 ERR("failed to get data object\n");
1302 goto end;
1305 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1306 if ( FAILED( r ) )
1307 goto end;
1309 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1310 if ( FAILED( r ) )
1311 goto end;
1313 IObjectWithSite_SetSite( ows, NULL );
1315 r = shellex_run_context_menu_default( obj, sei );
1317 end:
1318 if ( ows )
1319 IObjectWithSite_Release( ows );
1320 if ( dataobj )
1321 IDataObject_Release( dataobj );
1322 if ( obj )
1323 IShellExtInit_Release( obj );
1324 CoUninitialize();
1325 return r;
1329 /*************************************************************************
1330 * ShellExecute_FromContextMenu [Internal]
1332 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1334 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1335 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1336 HKEY hkey, hkeycm = 0;
1337 WCHAR szguid[39];
1338 HRESULT hr;
1339 GUID guid;
1340 DWORD i;
1341 LONG r;
1343 TRACE("%s\n", debugstr_w(sei->lpFile) );
1345 hkey = ShellExecute_GetClassKey( sei );
1346 if ( !hkey )
1347 return ERROR_FUNCTION_FAILED;
1349 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1350 if ( r == ERROR_SUCCESS )
1352 i = 0;
1353 while ( 1 )
1355 r = RegEnumKeyW( hkeycm, i++, szguid, sizeof(szguid)/sizeof(szguid[0]) );
1356 if ( r != ERROR_SUCCESS )
1357 break;
1359 hr = CLSIDFromString( szguid, &guid );
1360 if (SUCCEEDED(hr))
1362 /* stop at the first one that succeeds in running */
1363 hr = shellex_load_object_and_run( hkey, &guid, sei );
1364 if ( SUCCEEDED( hr ) )
1365 break;
1368 RegCloseKey( hkeycm );
1371 if ( hkey != sei->hkeyClass )
1372 RegCloseKey( hkey );
1373 return r;
1376 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 );
1378 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1380 static const WCHAR wQuote[] = {'"',0};
1381 static const WCHAR wSpace[] = {' ',0};
1382 WCHAR execCmd[1024], classname[1024];
1383 /* launch a document by fileclass like 'WordPad.Document.1' */
1384 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1385 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1386 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1387 DWORD resultLen;
1388 BOOL done;
1389 UINT_PTR rslt;
1391 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1392 if (cmask != SEE_MASK_CLASSNAME)
1394 WCHAR wcmd[1024];
1395 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1396 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1397 psei->lpVerb,
1398 execCmd, sizeof(execCmd));
1400 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1401 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1403 wcmd[0] = '\0';
1404 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1405 if (!done && wszApplicationName[0])
1407 strcatW(wcmd, wSpace);
1408 if (*wszApplicationName != '"')
1410 strcatW(wcmd, wQuote);
1411 strcatW(wcmd, wszApplicationName);
1412 strcatW(wcmd, wQuote);
1414 else
1415 strcatW(wcmd, wszApplicationName);
1417 if (resultLen > sizeof(wcmd)/sizeof(WCHAR))
1418 ERR("Argify buffer not large enough... truncating\n");
1419 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1422 strcpyW(classname, psei->lpClass);
1423 rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1425 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1426 if (33 > rslt)
1427 return rslt;
1428 rslt = SHELL_quote_and_execute( execCmd, wszEmpty, classname,
1429 wszApplicationName, NULL, psei,
1430 psei_out, execfunc );
1431 return rslt;
1434 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1436 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1437 WCHAR buffer[MAX_PATH];
1439 /* last chance to translate IDList: now also allow CLSID paths */
1440 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, sizeof(buffer)/sizeof(WCHAR)))) {
1441 if (buffer[0]==':' && buffer[1]==':') {
1442 /* open shell folder for the specified class GUID */
1443 if (strlenW(buffer) + 1 > parametersLen)
1444 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1445 lstrlenW(buffer) + 1, parametersLen);
1446 lstrcpynW(wszParameters, buffer, parametersLen);
1447 if (strlenW(wExplorer) > dwApplicationNameLen)
1448 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1449 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1450 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1452 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1453 } else {
1454 WCHAR target[MAX_PATH];
1455 DWORD attribs;
1456 DWORD resultLen;
1457 /* Check if we're executing a directory and if so use the
1458 handler for the Folder class */
1459 strcpyW(target, buffer);
1460 attribs = GetFileAttributesW(buffer);
1461 if (attribs != INVALID_FILE_ATTRIBUTES &&
1462 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1463 HCR_GetExecuteCommandW(0, wszFolder,
1464 sei->lpVerb,
1465 buffer, sizeof(buffer))) {
1466 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1467 buffer, target, sei->lpIDList, NULL, &resultLen);
1468 if (resultLen > dwApplicationNameLen)
1469 ERR("Argify buffer not large enough... truncating\n");
1471 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1476 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 )
1478 static const WCHAR wQuote[] = {'"',0};
1479 static const WCHAR wSpace[] = {' ',0};
1480 UINT_PTR retval;
1481 DWORD len;
1482 WCHAR *wszQuotedCmd;
1484 /* Length of quotes plus length of command plus NULL terminator */
1485 len = 2 + lstrlenW(wcmd) + 1;
1486 if (wszParameters[0])
1488 /* Length of space plus length of parameters */
1489 len += 1 + lstrlenW(wszParameters);
1491 wszQuotedCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1492 /* Must quote to handle case where cmd contains spaces,
1493 * else security hole if malicious user creates executable file "C:\\Program"
1495 strcpyW(wszQuotedCmd, wQuote);
1496 strcatW(wszQuotedCmd, wcmd);
1497 strcatW(wszQuotedCmd, wQuote);
1498 if (wszParameters[0]) {
1499 strcatW(wszQuotedCmd, wSpace);
1500 strcatW(wszQuotedCmd, wszParameters);
1502 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1503 if (*wszKeyname)
1504 retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1505 else
1506 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1507 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1508 return retval;
1511 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1513 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1514 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1515 UINT_PTR retval;
1516 WCHAR *lpstrProtocol;
1517 LPCWSTR lpstrRes;
1518 INT iSize;
1519 DWORD len;
1521 lpstrRes = strchrW(lpFile, ':');
1522 if (lpstrRes)
1523 iSize = lpstrRes - lpFile;
1524 else
1525 iSize = strlenW(lpFile);
1527 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1528 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1529 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1530 if (psei->lpVerb && *psei->lpVerb)
1531 len += lstrlenW(psei->lpVerb);
1532 else
1533 len += lstrlenW(wszOpen);
1534 lpstrProtocol = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1535 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1536 lpstrProtocol[iSize] = '\0';
1537 strcatW(lpstrProtocol, wShell);
1538 strcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb: wszOpen);
1539 strcatW(lpstrProtocol, wCommand);
1541 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1542 wcmd, execfunc, psei, psei_out);
1543 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1544 return retval;
1547 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1549 WCHAR msg[2048];
1550 int error_code=GetLastError();
1552 if (retval == SE_ERR_NOASSOC)
1553 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, sizeof(msg)/sizeof(WCHAR));
1554 else
1555 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, sizeof(msg)/sizeof(WCHAR), NULL);
1557 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1560 /*************************************************************************
1561 * SHELL_execute [Internal]
1563 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1565 static const WCHAR wWww[] = {'w','w','w',0};
1566 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1567 static const DWORD unsupportedFlags =
1568 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1569 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1570 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1572 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1573 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1574 DWORD dwApplicationNameLen = MAX_PATH+2;
1575 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1576 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1577 DWORD len;
1578 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1579 WCHAR *env;
1580 WCHAR wszKeyname[256];
1581 LPCWSTR lpFile;
1582 UINT_PTR retval = SE_ERR_NOASSOC;
1584 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1585 sei_tmp = *sei;
1587 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1588 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1589 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1590 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1591 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1592 debugstr_w(sei_tmp.lpClass) : "not used");
1594 sei->hProcess = NULL;
1596 /* make copies of all path/command strings */
1597 if (!sei_tmp.lpFile)
1599 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1600 *wszApplicationName = '\0';
1602 else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = strlenW(sei_tmp.lpFile))-1] == '\"')
1604 if(len-1 >= dwApplicationNameLen) dwApplicationNameLen = len;
1605 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1606 memcpy(wszApplicationName, sei_tmp.lpFile+1, len*sizeof(WCHAR));
1607 if(len > 2)
1608 wszApplicationName[len-2] = '\0';
1609 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1610 } else {
1611 DWORD l = strlenW(sei_tmp.lpFile)+1;
1612 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1613 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1614 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1617 wszParameters = parametersBuffer;
1618 if (sei_tmp.lpParameters)
1620 len = lstrlenW(sei_tmp.lpParameters) + 1;
1621 if (len > parametersLen)
1623 wszParameters = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1624 parametersLen = len;
1626 strcpyW(wszParameters, sei_tmp.lpParameters);
1628 else
1629 *wszParameters = '\0';
1631 wszDir = dirBuffer;
1632 if (sei_tmp.lpDirectory)
1634 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1635 if (len > sizeof(dirBuffer) / sizeof(WCHAR))
1636 wszDir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1637 strcpyW(wszDir, sei_tmp.lpDirectory);
1639 else
1640 *wszDir = '\0';
1642 /* adjust string pointers to point to the new buffers */
1643 sei_tmp.lpFile = wszApplicationName;
1644 sei_tmp.lpParameters = wszParameters;
1645 sei_tmp.lpDirectory = wszDir;
1647 if (sei_tmp.fMask & unsupportedFlags)
1649 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1652 /* process the IDList */
1653 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1655 IShellExecuteHookW* pSEH;
1657 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1659 if (SUCCEEDED(hr))
1661 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1663 IShellExecuteHookW_Release(pSEH);
1665 if (hr == S_OK) {
1666 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1667 if (wszParameters != parametersBuffer)
1668 HeapFree(GetProcessHeap(), 0, wszParameters);
1669 if (wszDir != dirBuffer)
1670 HeapFree(GetProcessHeap(), 0, wszDir);
1671 return TRUE;
1675 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1676 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1679 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1681 sei->hInstApp = (HINSTANCE) 33;
1682 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1683 if (wszParameters != parametersBuffer)
1684 HeapFree(GetProcessHeap(), 0, wszParameters);
1685 if (wszDir != dirBuffer)
1686 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1697 if (wszParameters != parametersBuffer)
1698 HeapFree(GetProcessHeap(), 0, wszParameters);
1699 if (wszDir != dirBuffer)
1700 HeapFree(GetProcessHeap(), 0, 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 = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1721 if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0))) {
1722 HeapFree(GetProcessHeap(), 0, buf);
1723 return SE_ERR_OOM;
1726 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1727 wszApplicationName = buf;
1728 sei_tmp.lpFile = wszApplicationName;
1730 else /* or expand environment strings (not both!) */
1732 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1733 if (len>0)
1735 LPWSTR buf;
1736 buf = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1738 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1739 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1740 wszApplicationName = buf;
1742 sei_tmp.lpFile = wszApplicationName;
1746 if (*sei_tmp.lpDirectory)
1748 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1749 if (len > 0)
1751 LPWSTR buf;
1752 len++;
1753 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1754 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1755 if (wszDir != dirBuffer)
1756 HeapFree(GetProcessHeap(), 0, wszDir);
1757 wszDir = buf;
1758 sei_tmp.lpDirectory = wszDir;
1762 /* Else, try to execute the filename */
1763 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1764 lpFile = sei_tmp.lpFile;
1765 wcmd = wcmdBuffer;
1766 len = lstrlenW(wszApplicationName) + 3;
1767 if (sei_tmp.lpParameters[0])
1768 len += 1 + lstrlenW(wszParameters);
1769 if (len > wcmdLen)
1771 wcmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1772 wcmdLen = len;
1774 wcmd[0] = '\"';
1775 len = lstrlenW(wszApplicationName);
1776 memcpy(wcmd+1, wszApplicationName, len * sizeof(WCHAR));
1777 len++;
1778 wcmd[len++] = '\"';
1779 wcmd[len] = 0;
1780 if (sei_tmp.lpParameters[0]) {
1781 wcmd[len++] = ' ';
1782 strcpyW(wcmd+len, wszParameters);
1785 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1786 if (retval > 32) {
1787 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1788 if (wszParameters != parametersBuffer)
1789 HeapFree(GetProcessHeap(), 0, wszParameters);
1790 if (wszDir != dirBuffer)
1791 HeapFree(GetProcessHeap(), 0, wszDir);
1792 if (wcmd != wcmdBuffer)
1793 HeapFree(GetProcessHeap(), 0, wcmd);
1794 return TRUE;
1797 /* Else, try to find the executable */
1798 wcmd[0] = '\0';
1799 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1800 if (retval > 32) /* Found */
1802 retval = SHELL_quote_and_execute( wcmd, wszParameters, wszKeyname,
1803 wszApplicationName, env, &sei_tmp,
1804 sei, execfunc );
1805 HeapFree( GetProcessHeap(), 0, env );
1807 else if (PathIsDirectoryW(lpFile))
1809 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r',0};
1810 static const WCHAR wQuote[] = {'"',0};
1811 WCHAR wExec[MAX_PATH];
1812 WCHAR * lpQuotedFile = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3) );
1814 if (lpQuotedFile)
1816 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, wExplorer,
1817 wszOpen, wExec, MAX_PATH,
1818 NULL, &env, NULL, NULL );
1819 if (retval > 32)
1821 strcpyW(lpQuotedFile, wQuote);
1822 strcatW(lpQuotedFile, lpFile);
1823 strcatW(lpQuotedFile, wQuote);
1824 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1825 wszKeyname,
1826 wszApplicationName, env,
1827 &sei_tmp, sei, execfunc );
1828 HeapFree( GetProcessHeap(), 0, env );
1830 HeapFree( GetProcessHeap(), 0, lpQuotedFile );
1832 else
1833 retval = 0; /* Out of memory */
1835 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1837 retval = SHELL_execute_url( lpFile, wcmd, &sei_tmp, sei, execfunc );
1839 /* Check if file specified is in the form www.??????.*** */
1840 else if (!strncmpiW(lpFile, wWww, 3))
1842 /* if so, prefix lpFile with http:// and call ShellExecute */
1843 WCHAR lpstrTmpFile[256];
1844 strcpyW(lpstrTmpFile, wHttp);
1845 strcatW(lpstrTmpFile, lpFile);
1846 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1849 TRACE("retval %lu\n", retval);
1851 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1852 if (wszParameters != parametersBuffer)
1853 HeapFree(GetProcessHeap(), 0, wszParameters);
1854 if (wszDir != dirBuffer)
1855 HeapFree(GetProcessHeap(), 0, wszDir);
1856 if (wcmd != wcmdBuffer)
1857 HeapFree(GetProcessHeap(), 0, wcmd);
1859 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1861 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1862 do_error_dialog(retval, sei_tmp.hwnd);
1863 return retval > 32;
1866 /*************************************************************************
1867 * ShellExecuteA [SHELL32.290]
1869 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
1870 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1872 SHELLEXECUTEINFOA sei;
1874 TRACE("%p,%s,%s,%s,%s,%d\n",
1875 hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
1876 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1878 sei.cbSize = sizeof(sei);
1879 sei.fMask = SEE_MASK_FLAG_NO_UI;
1880 sei.hwnd = hWnd;
1881 sei.lpVerb = lpVerb;
1882 sei.lpFile = lpFile;
1883 sei.lpParameters = lpParameters;
1884 sei.lpDirectory = lpDirectory;
1885 sei.nShow = iShowCmd;
1886 sei.lpIDList = 0;
1887 sei.lpClass = 0;
1888 sei.hkeyClass = 0;
1889 sei.dwHotKey = 0;
1890 sei.hProcess = 0;
1892 ShellExecuteExA (&sei);
1893 return sei.hInstApp;
1896 /*************************************************************************
1897 * ShellExecuteExA [SHELL32.292]
1900 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1902 SHELLEXECUTEINFOW seiW;
1903 BOOL ret;
1904 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1906 TRACE("%p\n", sei);
1908 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1910 if (sei->lpVerb)
1911 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1913 if (sei->lpFile)
1914 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1916 if (sei->lpParameters)
1917 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1919 if (sei->lpDirectory)
1920 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1922 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1923 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1924 else
1925 seiW.lpClass = NULL;
1927 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1929 sei->hInstApp = seiW.hInstApp;
1931 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1932 sei->hProcess = seiW.hProcess;
1934 SHFree(wVerb);
1935 SHFree(wFile);
1936 SHFree(wParameters);
1937 SHFree(wDirectory);
1938 SHFree(wClass);
1940 return ret;
1943 /*************************************************************************
1944 * ShellExecuteExW [SHELL32.293]
1947 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1949 return SHELL_execute( sei, SHELL_ExecuteW );
1952 /*************************************************************************
1953 * ShellExecuteW [SHELL32.294]
1954 * from shellapi.h
1955 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1956 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1958 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
1959 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1961 SHELLEXECUTEINFOW sei;
1963 TRACE("\n");
1964 sei.cbSize = sizeof(sei);
1965 sei.fMask = SEE_MASK_FLAG_NO_UI;
1966 sei.hwnd = hwnd;
1967 sei.lpVerb = lpVerb;
1968 sei.lpFile = lpFile;
1969 sei.lpParameters = lpParameters;
1970 sei.lpDirectory = lpDirectory;
1971 sei.nShow = nShowCmd;
1972 sei.lpIDList = 0;
1973 sei.lpClass = 0;
1974 sei.hkeyClass = 0;
1975 sei.dwHotKey = 0;
1976 sei.hProcess = 0;
1978 SHELL_execute( &sei, SHELL_ExecuteW );
1979 return sei.hInstApp;
1982 /*************************************************************************
1983 * WOWShellExecute [SHELL32.@]
1985 * FIXME: the callback function most likely doesn't work the same way on Windows.
1987 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb,LPCSTR lpFile,
1988 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
1990 SHELLEXECUTEINFOW seiW;
1991 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
1992 HANDLE hProcess = 0;
1994 seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
1995 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
1996 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
1997 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
1999 seiW.cbSize = sizeof(seiW);
2000 seiW.fMask = 0;
2001 seiW.hwnd = hWnd;
2002 seiW.nShow = iShowCmd;
2003 seiW.lpIDList = 0;
2004 seiW.lpClass = 0;
2005 seiW.hkeyClass = 0;
2006 seiW.dwHotKey = 0;
2007 seiW.hProcess = hProcess;
2009 SHELL_execute( &seiW, callback );
2011 SHFree(wVerb);
2012 SHFree(wFile);
2013 SHFree(wParameters);
2014 SHFree(wDirectory);
2015 return seiW.hInstApp;
2018 /*************************************************************************
2019 * OpenAs_RunDLLA [SHELL32.@]
2021 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2023 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2026 /*************************************************************************
2027 * OpenAs_RunDLLW [SHELL32.@]
2029 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2031 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2034 /*************************************************************************
2035 * RegenerateUserEnvironment [SHELL32.@]
2037 BOOL WINAPI RegenerateUserEnvironment(WCHAR *wunknown, BOOL bunknown)
2039 FIXME("stub: %p, %d\n", wunknown, bunknown);
2040 return FALSE;