Sort files/folders before testing.
[wine/multimedia.git] / dlls / shell32 / shlexec.c
blob81a31b0361b17dc3e3bfa6800e971b24fcde92ad
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "wownt32.h"
42 #include "shellapi.h"
43 #include "wingdi.h"
44 #include "winuser.h"
45 #include "shlobj.h"
46 #include "shlwapi.h"
47 #include "ddeml.h"
49 #include "wine/winbase16.h"
50 #include "shell32_main.h"
51 #include "undocshell.h"
52 #include "pidl.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(exec);
58 static const WCHAR wszOpen[] = {'o','p','e','n',0};
59 static const WCHAR wszExe[] = {'.','e','x','e',0};
60 static const WCHAR wszILPtr[] = {':','%','p',0};
61 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
62 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
63 static const WCHAR wszEmpty[] = {0};
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)
81 * FIXME: use 'len'
82 * FIXME: Careful of going over string boundaries. No checking is done to 'res'...
84 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
86 WCHAR xlpFile[1024];
87 BOOL done = FALSE;
88 PWSTR res = out;
89 PCWSTR cmd;
90 LPVOID pv;
92 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
93 debugstr_w(lpFile), pidl, args);
95 while (*fmt)
97 if (*fmt == '%')
99 switch (*++fmt)
101 case '\0':
102 case '%':
103 *res++ = '%';
104 break;
106 case '2':
107 case '3':
108 case '4':
109 case '5':
110 case '6':
111 case '7':
112 case '8':
113 case '9':
114 case '0':
115 case '*':
116 if (args)
118 if (*fmt == '*')
120 *res++ = '"';
121 while(*args)
122 *res++ = *args++;
123 *res++ = '"';
125 else
127 while(*args && !isspace(*args))
128 *res++ = *args++;
130 while(isspace(*args))
131 ++args;
133 break;
135 /* else fall through */
136 case '1':
137 if (!done || (*fmt == '1'))
139 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
140 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
141 cmd = xlpFile;
142 else
143 cmd = lpFile;
145 /* Add double quotation marks unless we already have them (e.g.: "file://%1" %* for exefile) */
146 if (res == out || *(fmt + 1) != '"')
148 *res++ = '"';
149 strcpyW(res, cmd);
150 res += strlenW(cmd);
151 *res++ = '"';
153 else
155 strcpyW(res, cmd);
156 res += strlenW(cmd);
159 break;
162 * IE uses this a lot for activating things such as windows media
163 * player. This is not verified to be fully correct but it appears
164 * to work just fine.
166 case 'l':
167 case 'L':
168 if (lpFile) {
169 strcpyW(res, lpFile);
170 res += strlenW(lpFile);
172 break;
174 case 'i':
175 case 'I':
176 if (pidl) {
177 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
178 pv = SHLockShared(hmem, 0);
179 res += sprintfW(res, wszILPtr, pv);
180 SHUnlockShared(pv);
182 break;
184 default:
186 * Check if this is a env-variable here...
189 /* Make sure that we have at least one more %.*/
190 if (strchrW(fmt, '%'))
192 WCHAR tmpBuffer[1024];
193 PWSTR tmpB = tmpBuffer;
194 WCHAR tmpEnvBuff[MAX_PATH];
195 DWORD envRet;
197 while (*fmt != '%')
198 *tmpB++ = *fmt++;
199 *tmpB++ = 0;
201 TRACE("Checking %s to be a env-var\n", debugstr_w(tmpBuffer));
203 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
204 if (envRet == 0 || envRet > MAX_PATH)
205 strcpyW( res, tmpBuffer );
206 else
207 strcpyW( res, tmpEnvBuff );
208 res += strlenW(res);
210 fmt++;
211 done = TRUE;
212 break;
215 else
216 *res++ = *fmt++;
219 *res = '\0';
221 return done;
224 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
226 STRRET strret;
227 IShellFolder* desktop;
229 HRESULT hr = SHGetDesktopFolder(&desktop);
231 if (SUCCEEDED(hr)) {
232 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
234 if (SUCCEEDED(hr))
235 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
237 IShellFolder_Release(desktop);
240 return hr;
243 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
245 STRRET strret;
246 IShellFolder* desktop;
248 HRESULT hr = SHGetDesktopFolder(&desktop);
250 if (SUCCEEDED(hr)) {
251 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
253 if (SUCCEEDED(hr))
254 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
256 IShellFolder_Release(desktop);
259 return hr;
262 /*************************************************************************
263 * SHELL_ResolveShortCutW [Internal]
264 * read shortcut file at 'wcmd'
266 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
268 IShellFolder* psf;
270 HRESULT hr = SHGetDesktopFolder(&psf);
272 *ppidl = NULL;
274 if (SUCCEEDED(hr)) {
275 LPITEMIDLIST pidl;
276 ULONG l;
278 hr = IShellFolder_ParseDisplayName(psf, 0, 0, wcmd, &l, &pidl, 0);
280 if (SUCCEEDED(hr)) {
281 IShellLinkW* psl;
283 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
285 if (SUCCEEDED(hr)) {
286 hr = IShellLinkW_Resolve(psl, hwnd, 0);
288 if (SUCCEEDED(hr)) {
289 hr = IShellLinkW_GetPath(psl, wcmd, MAX_PATH, NULL, SLGP_UNCPRIORITY);
291 if (SUCCEEDED(hr)) {
292 if (!*wcmd) {
293 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
294 hr = IShellLinkW_GetIDList(psl, ppidl);
296 if (SUCCEEDED(hr) && *ppidl) {
297 /* We got a PIDL instead of a file system path - try to translate it. */
298 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl, wcmd, MAX_PATH))) {
299 SHFree(*ppidl);
300 *ppidl = NULL;
305 if (SUCCEEDED(hr)) {
306 /* get command line arguments, working directory and display mode if available */
307 IShellLinkW_GetWorkingDirectory(psl, wdir, MAX_PATH);
308 IShellLinkW_GetArguments(psl, wargs, MAX_PATH);
309 IShellLinkW_GetShowCmd(psl, pshowcmd);
314 IShellLinkW_Release(psl);
317 SHFree(pidl);
320 IShellFolder_Release(psf);
323 return hr;
326 /*************************************************************************
327 * SHELL_ExecuteW [Internal]
330 static UINT SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
331 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
333 STARTUPINFOW startup;
334 PROCESS_INFORMATION info;
335 UINT retval = 31;
336 UINT gcdret = 0;
337 WCHAR curdir[MAX_PATH];
339 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
340 /* ShellExecute specifies the command from psei->lpDirectory
341 * if present. Not from the current dir as CreateProcess does */
342 if( psei->lpDirectory && psei->lpDirectory[0] )
343 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
344 if( !SetCurrentDirectoryW( psei->lpDirectory))
345 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
346 ZeroMemory(&startup,sizeof(STARTUPINFOW));
347 startup.cb = sizeof(STARTUPINFOW);
348 startup.dwFlags = STARTF_USESHOWWINDOW;
349 startup.wShowWindow = psei->nShow;
350 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT,
351 env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
353 /* Give 30 seconds to the app to come up, if desired. Probably only needed
354 when starting app immediately before making a DDE connection. */
355 if (shWait)
356 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
357 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
358 retval = 33;
359 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
360 psei_out->hProcess = info.hProcess;
361 else
362 CloseHandle( info.hProcess );
363 CloseHandle( info.hThread );
365 else if ((retval = GetLastError()) >= 32)
367 FIXME("Strange error set by CreateProcess: %d\n", retval);
368 retval = ERROR_BAD_FORMAT;
371 TRACE("returning %u\n", retval);
373 psei_out->hInstApp = (HINSTANCE)retval;
374 if( gcdret )
375 if( !SetCurrentDirectoryW( curdir))
376 ERR("cannot return to directory %s\n", debugstr_w(curdir));
378 return retval;
382 /***********************************************************************
383 * SHELL_BuildEnvW [Internal]
385 * Build the environment for the new process, adding the specified
386 * path to the PATH variable. Returned pointer must be freed by caller.
388 static void *SHELL_BuildEnvW( const WCHAR *path )
390 static const WCHAR wPath[] = {'P','A','T','H','=',0};
391 WCHAR *strings, *new_env;
392 WCHAR *p, *p2;
393 int total = strlenW(path) + 1;
394 BOOL got_path = FALSE;
396 if (!(strings = GetEnvironmentStringsW())) return NULL;
397 p = strings;
398 while (*p)
400 int len = strlenW(p) + 1;
401 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
402 total += len;
403 p += len;
405 if (!got_path) total += 5; /* we need to create PATH */
406 total++; /* terminating null */
408 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
410 FreeEnvironmentStringsW( strings );
411 return NULL;
413 p = strings;
414 p2 = new_env;
415 while (*p)
417 int len = strlenW(p) + 1;
418 memcpy( p2, p, len * sizeof(WCHAR) );
419 if (!strncmpiW( p, wPath, 5 ))
421 p2[len - 1] = ';';
422 strcpyW( p2 + len, path );
423 p2 += strlenW(path) + 1;
425 p += len;
426 p2 += len;
428 if (!got_path)
430 strcpyW( p2, wPath );
431 strcatW( p2, path );
432 p2 += strlenW(p2) + 1;
434 *p2 = 0;
435 FreeEnvironmentStringsW( strings );
436 return new_env;
440 /***********************************************************************
441 * SHELL_TryAppPathW [Internal]
443 * Helper function for SHELL_FindExecutable
444 * @param lpResult - pointer to a buffer of size MAX_PATH
445 * On entry: szName is a filename (probably without path separators).
446 * On exit: if szName found in "App Path", place full path in lpResult, and return true
448 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
450 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',
451 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
452 static const WCHAR wPath[] = {'P','a','t','h',0};
453 HKEY hkApp = 0;
454 WCHAR buffer[1024];
455 LONG len;
456 LONG res;
457 BOOL found = FALSE;
459 if (env) *env = NULL;
460 strcpyW(buffer, wszKeyAppPaths);
461 strcatW(buffer, szName);
462 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
463 if (res) goto end;
465 len = MAX_PATH*sizeof(WCHAR);
466 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
467 if (res) goto end;
468 found = TRUE;
470 if (env)
472 DWORD count = sizeof(buffer);
473 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
474 *env = SHELL_BuildEnvW( buffer );
477 end:
478 if (hkApp) RegCloseKey(hkApp);
479 return found;
482 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
484 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
486 /* Looking for ...buffer\shell\<verb>\command */
487 strcatW(filetype, wszShell);
488 strcatW(filetype, lpOperation);
489 strcatW(filetype, wCommand);
491 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
492 &commandlen) == ERROR_SUCCESS)
494 commandlen /= sizeof(WCHAR);
495 if (key) strcpyW(key, filetype);
496 #if 0
497 LPWSTR tmp;
498 WCHAR param[256];
499 LONG paramlen = sizeof(param);
500 static const WCHAR wSpace[] = {' ',0};
502 /* FIXME: it seems all Windows version don't behave the same here.
503 * the doc states that this ddeexec information can be found after
504 * the exec names.
505 * on Win98, it doesn't appear, but I think it does on Win2k
507 /* Get the parameters needed by the application
508 from the associated ddeexec key */
509 tmp = strstrW(filetype, wCommand);
510 tmp[0] = '\0';
511 strcatW(filetype, wDdeexec);
512 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
513 &paramlen) == ERROR_SUCCESS)
515 paramlen /= sizeof(WCHAR);
516 strcatW(command, wSpace);
517 strcatW(command, param);
518 commandlen += paramlen;
520 #endif
522 command[commandlen] = '\0';
524 return 33; /* FIXME see SHELL_FindExecutable() */
527 return 31; /* default - 'No association was found' */
530 /*************************************************************************
531 * SHELL_FindExecutable [Internal]
533 * Utility for code sharing between FindExecutable and ShellExecute
534 * in:
535 * lpFile the name of a file
536 * lpOperation the operation on it (open)
537 * out:
538 * lpResult a buffer, big enough :-(, to store the command to do the
539 * operation on the file
540 * key a buffer, big enough, to get the key name to do actually the
541 * command (it'll be used afterwards for more information
542 * on the operation)
544 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
545 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
547 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
548 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
549 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
550 WCHAR *extension = NULL; /* pointer to file extension */
551 WCHAR filetype[256]; /* registry name for this filetype */
552 LONG filetypelen = sizeof(filetype); /* length of above */
553 WCHAR command[1024]; /* command from registry */
554 WCHAR wBuffer[256]; /* Used to GetProfileString */
555 UINT retval = 31; /* default - 'No association was found' */
556 WCHAR *tok; /* token pointer */
557 WCHAR xlpFile[256]; /* result of SearchPath */
558 DWORD attribs; /* file attributes */
560 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
562 xlpFile[0] = '\0';
563 lpResult[0] = '\0'; /* Start off with an empty return string */
564 if (key) *key = '\0';
566 /* trap NULL parameters on entry */
567 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
569 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
570 debugstr_w(lpFile), debugstr_w(lpOperation), debugstr_w(lpResult));
571 return 2; /* File not found. Close enough, I guess. */
574 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
576 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
577 return 33;
580 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
582 TRACE("SearchPathW returned non-zero\n");
583 lpFile = xlpFile;
584 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
587 attribs = GetFileAttributesW(lpFile);
588 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
590 strcpyW(filetype, wszFolder);
591 filetypelen = 6; /* strlen("Folder") */
593 else
595 /* First thing we need is the file's extension */
596 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
597 /* File->Run in progman uses */
598 /* .\FILE.EXE :( */
599 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
601 if (extension == NULL || extension[1]==0)
603 WARN("Returning 31 - No association\n");
604 return 31; /* no association */
607 /* Three places to check: */
608 /* 1. win.ini, [windows], programs (NB no leading '.') */
609 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
610 /* 3. win.ini, [extensions], extension (NB no leading '.' */
611 /* All I know of the order is that registry is checked before */
612 /* extensions; however, it'd make sense to check the programs */
613 /* section first, so that's what happens here. */
615 /* See if it's a program - if GetProfileString fails, we skip this
616 * section. Actually, if GetProfileString fails, we've probably
617 * got a lot more to worry about than running a program... */
618 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
620 CharLowerW(wBuffer);
621 tok = wBuffer;
622 while (*tok)
624 WCHAR *p = tok;
625 while (*p && *p != ' ' && *p != '\t') p++;
626 if (*p)
628 *p++ = 0;
629 while (*p == ' ' || *p == '\t') p++;
632 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
634 strcpyW(lpResult, xlpFile);
635 /* Need to perhaps check that the file has a path
636 * attached */
637 TRACE("found %s\n", debugstr_w(lpResult));
638 return 33;
640 /* Greater than 32 to indicate success FIXME According to the
641 * docs, I should be returning a handle for the
642 * executable. Does this mean I'm supposed to open the
643 * executable file or something? More RTFM, I guess... */
645 tok = p;
649 /* Check registry */
650 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
651 &filetypelen) == ERROR_SUCCESS)
653 filetypelen /= sizeof(WCHAR);
654 filetype[filetypelen] = '\0';
655 TRACE("File type: %s\n", debugstr_w(filetype));
659 if (*filetype)
661 if (lpOperation)
663 /* pass the operation string to SHELL_FindExecutableByOperation() */
664 filetype[filetypelen] = '\0';
665 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
667 else
669 WCHAR operation[MAX_PATH];
670 HKEY hkey;
672 /* Looking for ...buffer\shell\<operation>\command */
673 strcatW(filetype, wszShell);
675 /* enumerate the operation subkeys in the registry and search for one with an associated command */
676 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
678 int idx = 0;
679 for(;; ++idx)
681 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
682 break;
684 filetype[filetypelen] = '\0';
685 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
687 if (retval > 32)
688 break;
690 RegCloseKey(hkey);
694 if (retval > 32)
696 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
698 /* Remove double quotation marks and command line arguments */
699 if (*lpResult == '"')
701 WCHAR *p = lpResult;
702 while (*(p + 1) != '"')
704 *p = *(p + 1);
705 p++;
707 *p = '\0';
711 else /* Check win.ini */
713 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
715 /* Toss the leading dot */
716 extension++;
717 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
719 if (strlenW(command) != 0)
721 strcpyW(lpResult, command);
722 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
723 if (tok != NULL)
725 tok[0] = '\0';
726 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
727 tok = strchrW(command, '^'); /* see above */
728 if ((tok != NULL) && (strlenW(tok)>5))
730 strcatW(lpResult, &tok[5]);
733 retval = 33; /* FIXME - see above */
738 TRACE("returning %s\n", debugstr_w(lpResult));
739 return retval;
742 /******************************************************************
743 * dde_cb
745 * callback for the DDE connection. not really usefull
747 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
748 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
749 ULONG_PTR dwData1, ULONG_PTR dwData2)
751 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
752 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
753 return NULL;
756 /******************************************************************
757 * dde_connect
759 * ShellExecute helper. Used to do an operation with a DDE connection
761 * Handles both the direct connection (try #1), and if it fails,
762 * launching an application and trying (#2) to connect to it
765 static unsigned dde_connect(WCHAR* key, WCHAR* start, WCHAR* ddeexec,
766 const WCHAR* lpFile, WCHAR *env,
767 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
768 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
770 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
771 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
772 WCHAR * endkey = key + strlenW(key);
773 WCHAR app[256], topic[256], ifexec[256], res[256];
774 LONG applen, topiclen, ifexeclen;
775 WCHAR * exec;
776 DWORD ddeInst = 0;
777 DWORD tid;
778 HSZ hszApp, hszTopic;
779 HCONV hConv;
780 HDDEDATA hDdeData;
781 unsigned ret = 31;
783 strcpyW(endkey, wApplication);
784 applen = sizeof(app);
785 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
787 FIXME("default app name NIY %s\n", debugstr_w(key));
788 return 2;
791 strcpyW(endkey, wTopic);
792 topiclen = sizeof(topic);
793 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
795 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
796 strcpyW(topic, wSystem);
799 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
801 return 2;
804 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
805 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
807 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
808 exec = ddeexec;
809 if (!hConv)
811 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
812 TRACE("Launching '%s'\n", debugstr_w(start));
813 ret = execfunc(start, env, TRUE, psei, psei_out);
814 if (ret < 32)
816 TRACE("Couldn't launch\n");
817 goto error;
819 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
820 if (!hConv)
822 TRACE("Couldn't connect. ret=%d\n", ret);
823 DdeUninitialize(ddeInst);
824 SetLastError(ERROR_DDE_FAIL);
825 return 30; /* whatever */
827 strcpyW(endkey, wIfexec);
828 ifexeclen = sizeof(ifexec);
829 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
831 exec = ifexec;
835 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
836 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
838 /* It's documented in the KB 330337 that IE has a bug and returns
839 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
841 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
842 XTYP_EXECUTE, 10000, &tid);
843 if (hDdeData)
844 DdeFreeDataHandle(hDdeData);
845 else
846 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
847 ret = 33;
849 DdeDisconnect(hConv);
851 error:
852 DdeUninitialize(ddeInst);
854 return ret;
857 /*************************************************************************
858 * execute_from_key [Internal]
860 static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
861 SHELL_ExecuteW32 execfunc,
862 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
864 WCHAR cmd[1024];
865 LONG cmdlen = sizeof(cmd);
866 UINT retval = 31;
868 cmd[0] = '\0';
870 /* Get the application for the registry */
871 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
873 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
874 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
875 LPWSTR tmp;
876 WCHAR param[256];
877 LONG paramlen = sizeof(param);
879 param[0] = '\0';
881 /* Get the parameters needed by the application
882 from the associated ddeexec key */
883 tmp = strstrW(key, wCommand);
884 assert(tmp);
885 strcpyW(tmp, wDdeexec);
887 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
889 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
890 retval = dde_connect(key, cmd, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
892 else
894 /* Is there a replace() function anywhere? */
895 cmdlen /= sizeof(WCHAR);
896 cmd[cmdlen] = '\0';
897 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
898 retval = execfunc(param, env, FALSE, psei, psei_out);
901 else TRACE("ooch\n");
903 return retval;
906 /*************************************************************************
907 * FindExecutableA [SHELL32.@]
909 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
911 HINSTANCE retval;
912 WCHAR *wFile = NULL, *wDirectory = NULL;
913 WCHAR wResult[MAX_PATH];
915 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
916 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
918 retval = FindExecutableW(wFile, wDirectory, wResult);
919 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
920 if (wFile) SHFree( wFile );
921 if (wDirectory) SHFree( wDirectory );
923 TRACE("returning %s\n", lpResult);
924 return (HINSTANCE)retval;
927 /*************************************************************************
928 * FindExecutableW [SHELL32.@]
930 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
932 UINT retval = 31; /* default - 'No association was found' */
933 WCHAR old_dir[1024];
935 TRACE("File %s, Dir %s\n",
936 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
938 lpResult[0] = '\0'; /* Start off with an empty return string */
940 /* trap NULL parameters on entry */
941 if ((lpFile == NULL) || (lpResult == NULL))
943 /* FIXME - should throw a warning, perhaps! */
944 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
947 if (lpDirectory)
949 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
950 SetCurrentDirectoryW(lpDirectory);
953 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
955 TRACE("returning %s\n", debugstr_w(lpResult));
956 if (lpDirectory)
957 SetCurrentDirectoryW(old_dir);
958 return (HINSTANCE)retval;
961 /*************************************************************************
962 * ShellExecuteExW32 [Internal]
964 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
966 static const WCHAR wQuote[] = {'"',0};
967 static const WCHAR wSpace[] = {' ',0};
968 static const WCHAR wWww[] = {'w','w','w',0};
969 static const WCHAR wFile[] = {'f','i','l','e',0};
970 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
971 static const WCHAR wExtLnk[] = {'.','l','n','k',0};
972 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
974 WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
975 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
976 WCHAR wfileName[MAX_PATH];
977 WCHAR *env;
978 WCHAR lpstrProtocol[256];
979 LPCWSTR lpFile;
980 UINT retval = 31;
981 WCHAR wcmd[1024];
982 WCHAR buffer[MAX_PATH];
983 const WCHAR* ext;
984 BOOL done;
986 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
987 memcpy(&sei_tmp, sei, sizeof(sei_tmp));
989 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
990 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
991 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
992 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
993 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei_tmp.lpClass) : "not used");
995 sei->hProcess = NULL;
997 /* make copies of all path/command strings */
998 if (sei_tmp.lpFile)
999 strcpyW(wszApplicationName, sei_tmp.lpFile);
1000 else
1001 *wszApplicationName = '\0';
1003 if (sei_tmp.lpParameters)
1004 strcpyW(wszParameters, sei_tmp.lpParameters);
1005 else
1006 *wszParameters = '\0';
1008 if (sei_tmp.lpDirectory)
1009 strcpyW(wszDir, sei_tmp.lpDirectory);
1010 else
1011 *wszDir = '\0';
1013 /* adjust string pointers to point to the new buffers */
1014 sei_tmp.lpFile = wszApplicationName;
1015 sei_tmp.lpParameters = wszParameters;
1016 sei_tmp.lpDirectory = wszDir;
1018 if (sei_tmp.fMask & (SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1019 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1020 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
1021 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
1023 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask);
1026 /* process the IDList */
1027 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1029 IShellExecuteHookW* pSEH;
1031 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1033 if (SUCCEEDED(hr))
1035 hr = IShellExecuteHookW_Execute(pSEH, sei);
1037 IShellExecuteHookW_Release(pSEH);
1039 if (hr == S_OK)
1040 return TRUE;
1043 wszApplicationName[0] = '"';
1044 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName+1);
1045 strcatW(wszApplicationName, wQuote);
1046 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1049 if (sei_tmp.fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
1051 /* launch a document by fileclass like 'WordPad.Document.1' */
1052 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1053 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1054 HCR_GetExecuteCommandW((sei_tmp.fMask & SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
1055 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
1056 (sei_tmp.lpVerb) ? sei_tmp.lpVerb : wszOpen,
1057 wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
1059 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1060 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
1062 wcmd[0] = '\0';
1063 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL);
1064 if (!done && wszApplicationName[0])
1066 strcatW(wcmd, wSpace);
1067 strcatW(wcmd, wszApplicationName);
1069 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1070 if (retval > 32)
1071 return TRUE;
1072 else
1073 return FALSE;
1077 /* resolve shell shortcuts */
1078 ext = PathFindExtensionW(sei_tmp.lpFile);
1080 if (ext && !strncmpiW(ext, wExtLnk, sizeof(wExtLnk) / sizeof(WCHAR) - 1) &&
1081 (ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '\0' ||
1082 (sei_tmp.lpFile[0] == '"' && ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '"'))) /* or check for: shell_attribs & SFGAO_LINK */
1084 HRESULT hr;
1085 BOOL Quoted;
1087 if (wszApplicationName[0] == '"')
1089 if (wszApplicationName[strlenW(wszApplicationName) - 1] == '"')
1091 wszApplicationName[strlenW(wszApplicationName) - 1] = '\0';
1092 Quoted = TRUE;
1094 else
1096 Quoted = FALSE;
1099 else
1101 Quoted = FALSE;
1103 /* expand paths before reading shell link */
1104 if (ExpandEnvironmentStringsW(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile, buffer, MAX_PATH))
1105 lstrcpyW(Quoted ? wszApplicationName + 1 : wszApplicationName/*sei_tmp.lpFile*/, buffer);
1107 if (*sei_tmp.lpParameters)
1108 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1109 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1111 hr = SHELL_ResolveShortCutW((LPWSTR)(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile),
1112 (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
1113 sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
1114 if (Quoted)
1116 wszApplicationName[strlenW(wszApplicationName) + 1] = '\0';
1117 wszApplicationName[strlenW(wszApplicationName)] = '"';
1120 if (sei->lpIDList)
1121 sei->fMask |= SEE_MASK_IDLIST;
1123 if (SUCCEEDED(hr))
1125 /* repeat IDList processing if needed */
1126 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1128 IShellExecuteHookW* pSEH;
1130 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1132 if (SUCCEEDED(hr))
1134 hr = IShellExecuteHookW_Execute(pSEH, sei);
1136 IShellExecuteHookW_Release(pSEH);
1138 if (hr == S_OK)
1139 return TRUE;
1142 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp.lpIDList), debugstr_w(sei_tmp.lpFile));
1148 /* Has the IDList not yet been translated? */
1149 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1151 /* last chance to translate IDList: now also allow CLSID paths */
1152 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1153 if (buffer[0]==':' && buffer[1]==':') {
1154 /* open shell folder for the specified class GUID */
1155 strcpyW(wszParameters, buffer);
1156 strcpyW(wszApplicationName, wExplorer);
1158 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1159 } else if (HCR_GetExecuteCommandW(0, wszFolder, sei_tmp.lpVerb?sei_tmp.lpVerb:wszOpen, buffer, sizeof(buffer))) {
1160 SHELL_ArgifyW(wszApplicationName, sizeof(wszApplicationName)/sizeof(WCHAR), buffer, NULL, sei_tmp.lpIDList, NULL);
1162 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1167 /* expand environment strings */
1168 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1169 lstrcpyW(wszApplicationName, buffer);
1171 if (*sei_tmp.lpParameters)
1172 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1173 lstrcpyW(wszParameters, buffer);
1175 if (*sei_tmp.lpDirectory)
1176 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1177 lstrcpyW(wszDir, buffer);
1179 /* Else, try to execute the filename */
1180 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1182 /* separate out command line arguments from executable file name */
1183 if (!*sei_tmp.lpParameters) {
1184 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1185 if (sei_tmp.lpFile[0] == '"') {
1186 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1187 LPWSTR dst = wfileName;
1188 LPWSTR end;
1190 /* copy the unquoted executable path to 'wfileName' */
1191 while(*src && *src!='"')
1192 *dst++ = *src++;
1194 *dst = '\0';
1196 if (*src == '"') {
1197 end = ++src;
1199 while(isspace(*src))
1200 ++src;
1201 } else
1202 end = src;
1204 /* copy the parameter string to 'wszParameters' */
1205 strcpyW(wszParameters, src);
1207 /* terminate previous command string after the quote character */
1208 *end = '\0';
1210 else
1212 /* If the executable name is not quoted, we have to use this search loop here,
1213 that in CreateProcess() is not sufficient because it does not handle shell links. */
1214 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1215 LPWSTR space, s;
1217 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1218 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1219 int idx = space-sei_tmp.lpFile;
1220 strncpyW(buffer, sei_tmp.lpFile, idx);
1221 buffer[idx] = '\0';
1223 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1224 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1226 /* separate out command from parameter string */
1227 LPCWSTR p = space + 1;
1229 while(isspaceW(*p))
1230 ++p;
1232 strcpyW(wszParameters, p);
1233 *space = '\0';
1235 break;
1239 strcpyW(wfileName, sei_tmp.lpFile);
1241 } else
1242 strcpyW(wfileName, sei_tmp.lpFile);
1244 lpFile = wfileName;
1246 if (sei_tmp.lpParameters[0]) {
1247 strcatW(wszApplicationName, wSpace);
1248 strcatW(wszApplicationName, wszParameters);
1251 /* We set the default to open, and that should generally work.
1252 But that is not really the way the MS docs say to do it. */
1253 if (!sei_tmp.lpVerb)
1254 sei_tmp.lpVerb = wszOpen;
1256 retval = execfunc(wszApplicationName, NULL, FALSE, &sei_tmp, sei);
1257 if (retval > 32)
1258 return TRUE;
1260 /* Else, try to find the executable */
1261 wcmd[0] = '\0';
1262 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1263 if (retval > 32) /* Found */
1265 WCHAR wszQuotedCmd[MAX_PATH+2];
1266 /* Must quote to handle case where cmd contains spaces,
1267 * else security hole if malicious user creates executable file "C:\\Program"
1269 strcpyW(wszQuotedCmd, wQuote);
1270 strcatW(wszQuotedCmd, wcmd);
1271 strcatW(wszQuotedCmd, wQuote);
1272 if (wszParameters[0]) {
1273 strcatW(wszQuotedCmd, wSpace);
1274 strcatW(wszQuotedCmd, wszParameters);
1276 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1277 if (*lpstrProtocol)
1278 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1279 else
1280 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
1281 if (env) HeapFree( GetProcessHeap(), 0, env );
1283 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
1285 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1286 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1287 LPWSTR lpstrRes;
1288 INT iSize;
1290 lpstrRes = strchrW(lpFile, ':');
1291 if (lpstrRes)
1292 iSize = lpstrRes - lpFile;
1293 else
1294 iSize = strlenW(lpFile);
1296 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1297 /* Looking for ...protocol\shell\lpOperation\command */
1298 strncpyW(lpstrProtocol, lpFile, iSize);
1299 lpstrProtocol[iSize] = '\0';
1300 strcatW(lpstrProtocol, wShell);
1301 strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1302 strcatW(lpstrProtocol, wCommand);
1304 /* Remove File Protocol from lpFile */
1305 /* In the case file://path/file */
1306 if (!strncmpiW(lpFile, wFile, iSize))
1308 lpFile += iSize;
1309 while (*lpFile == ':') lpFile++;
1311 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1313 /* Check if file specified is in the form www.??????.*** */
1314 else if (!strncmpiW(lpFile, wWww, 3))
1316 /* if so, append lpFile http:// and call ShellExecute */
1317 WCHAR lpstrTmpFile[256];
1318 strcpyW(lpstrTmpFile, wHttp);
1319 strcatW(lpstrTmpFile, lpFile);
1320 retval = (UINT)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1323 TRACE("retval %u\n", retval);
1325 if (retval <= 32)
1327 sei->hInstApp = (HINSTANCE)retval;
1328 return FALSE;
1331 sei->hInstApp = (HINSTANCE)33;
1332 return TRUE;
1335 /*************************************************************************
1336 * ShellExecuteA [SHELL32.290]
1338 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1339 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1341 SHELLEXECUTEINFOA sei;
1342 HANDLE hProcess = 0;
1344 TRACE("%p,%s,%s,%s,%s,%d\n",
1345 hWnd, lpOperation, lpFile, lpParameters, lpDirectory, iShowCmd);
1347 sei.cbSize = sizeof(sei);
1348 sei.fMask = 0;
1349 sei.hwnd = hWnd;
1350 sei.lpVerb = lpOperation;
1351 sei.lpFile = lpFile;
1352 sei.lpParameters = lpParameters;
1353 sei.lpDirectory = lpDirectory;
1354 sei.nShow = iShowCmd;
1355 sei.lpIDList = 0;
1356 sei.lpClass = 0;
1357 sei.hkeyClass = 0;
1358 sei.dwHotKey = 0;
1359 sei.hProcess = hProcess;
1361 ShellExecuteExA (&sei);
1362 return sei.hInstApp;
1365 /*************************************************************************
1366 * ShellExecuteEx [SHELL32.291]
1369 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
1371 if (SHELL_OsIsUnicode())
1372 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1373 return ShellExecuteExA (sei);
1376 /*************************************************************************
1377 * ShellExecuteExA [SHELL32.292]
1380 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1382 SHELLEXECUTEINFOW seiW;
1383 BOOL ret;
1384 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1386 TRACE("%p\n", sei);
1388 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1390 if (sei->lpVerb)
1391 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1393 if (sei->lpFile)
1394 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1396 if (sei->lpParameters)
1397 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1399 if (sei->lpDirectory)
1400 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1402 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
1403 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1404 else
1405 seiW.lpClass = NULL;
1407 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
1409 sei->hInstApp = seiW.hInstApp;
1411 if (wVerb) SHFree(wVerb);
1412 if (wFile) SHFree(wFile);
1413 if (wParameters) SHFree(wParameters);
1414 if (wDirectory) SHFree(wDirectory);
1415 if (wClass) SHFree(wClass);
1417 return ret;
1420 /*************************************************************************
1421 * ShellExecuteExW [SHELL32.293]
1424 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1426 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1429 /*************************************************************************
1430 * ShellExecuteW [SHELL32.294]
1431 * from shellapi.h
1432 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1433 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1435 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1436 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1438 SHELLEXECUTEINFOW sei;
1439 HANDLE hProcess = 0;
1441 TRACE("\n");
1442 sei.cbSize = sizeof(sei);
1443 sei.fMask = 0;
1444 sei.hwnd = hwnd;
1445 sei.lpVerb = lpOperation;
1446 sei.lpFile = lpFile;
1447 sei.lpParameters = lpParameters;
1448 sei.lpDirectory = lpDirectory;
1449 sei.nShow = nShowCmd;
1450 sei.lpIDList = 0;
1451 sei.lpClass = 0;
1452 sei.hkeyClass = 0;
1453 sei.dwHotKey = 0;
1454 sei.hProcess = hProcess;
1456 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1457 return sei.hInstApp;