shlwapi/tests: Make the string.c tests compile in Visual C++.
[wine/dibdrv.git] / dlls / shell32 / shlexec.c
blobf57cb6ec8fa327eb277f5e888ea2765c80ff777a
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 "wine/winbase16.h"
46 #include "shell32_main.h"
47 #include "pidl.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)
63 /***********************************************************************
64 * SHELL_ArgifyW [Internal]
66 * this function is supposed to expand the escape sequences found in the registry
67 * some diving reported that the following were used:
68 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
69 * %1 file
70 * %2 printer
71 * %3 driver
72 * %4 port
73 * %I address of a global item ID (explorer switch /idlist)
74 * %L seems to be %1 as long filename followed by the 8+3 variation
75 * %S ???
76 * %* all following parameters (see batfile)
78 * FIXME: use 'len'
79 * FIXME: Careful of going over string boundaries. No checking is done to 'res'...
81 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
83 WCHAR xlpFile[1024];
84 BOOL done = FALSE;
85 BOOL found_p1 = FALSE;
86 PWSTR res = out;
87 PCWSTR cmd;
88 LPVOID pv;
90 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
91 debugstr_w(lpFile), pidl, args);
93 while (*fmt)
95 if (*fmt == '%')
97 switch (*++fmt)
99 case '\0':
100 case '%':
101 *res++ = '%';
102 break;
104 case '2':
105 case '3':
106 case '4':
107 case '5':
108 case '6':
109 case '7':
110 case '8':
111 case '9':
112 case '0':
113 case '*':
114 if (args)
116 if (*fmt == '*')
118 *res++ = '"';
119 while(*args)
120 *res++ = *args++;
121 *res++ = '"';
123 else
125 while(*args && !isspace(*args))
126 *res++ = *args++;
128 while(isspace(*args))
129 ++args;
131 break;
133 /* else fall through */
134 case '1':
135 if (!done || (*fmt == '1'))
137 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
138 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
139 cmd = xlpFile;
140 else
141 cmd = lpFile;
143 /* Add double quotation marks unless we already have them
144 (e.g.: "file://%1" %* for exefile) or unless the arg is already
145 enclosed in double quotation marks */
146 if ((res == out || *(fmt + 1) != '"') && *cmd != '"')
148 *res++ = '"';
149 strcpyW(res, cmd);
150 res += strlenW(cmd);
151 *res++ = '"';
153 else
155 strcpyW(res, cmd);
156 res += strlenW(cmd);
159 found_p1 = TRUE;
160 break;
163 * IE uses this a lot for activating things such as windows media
164 * player. This is not verified to be fully correct but it appears
165 * to work just fine.
167 case 'l':
168 case 'L':
169 if (lpFile) {
170 strcpyW(res, lpFile);
171 res += strlenW(lpFile);
173 found_p1 = TRUE;
174 break;
176 case 'i':
177 case 'I':
178 if (pidl) {
179 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
180 pv = SHLockShared(hmem, 0);
181 res += sprintfW(res, wszILPtr, pv);
182 SHUnlockShared(pv);
184 found_p1 = TRUE;
185 break;
187 default:
189 * Check if this is an env-variable here...
192 /* Make sure that we have at least one more %.*/
193 if (strchrW(fmt, '%'))
195 WCHAR tmpBuffer[1024];
196 PWSTR tmpB = tmpBuffer;
197 WCHAR tmpEnvBuff[MAX_PATH];
198 DWORD envRet;
200 while (*fmt != '%')
201 *tmpB++ = *fmt++;
202 *tmpB++ = 0;
204 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
206 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
207 if (envRet == 0 || envRet > MAX_PATH)
208 strcpyW( res, tmpBuffer );
209 else
210 strcpyW( res, tmpEnvBuff );
211 res += strlenW(res);
213 done = TRUE;
214 break;
216 /* Don't skip past terminator (catch a single '%' at the end) */
217 if (*fmt != '\0')
219 fmt++;
222 else
223 *res++ = *fmt++;
226 *res = '\0';
228 return found_p1;
231 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
233 STRRET strret;
234 IShellFolder* desktop;
236 HRESULT hr = SHGetDesktopFolder(&desktop);
238 if (SUCCEEDED(hr)) {
239 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
241 if (SUCCEEDED(hr))
242 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
244 IShellFolder_Release(desktop);
247 return hr;
250 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
252 STRRET strret;
253 IShellFolder* desktop;
255 HRESULT hr = SHGetDesktopFolder(&desktop);
257 if (SUCCEEDED(hr)) {
258 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
260 if (SUCCEEDED(hr))
261 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
263 IShellFolder_Release(desktop);
266 return hr;
269 /*************************************************************************
270 * SHELL_ExecuteW [Internal]
273 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
274 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
276 STARTUPINFOW startup;
277 PROCESS_INFORMATION info;
278 UINT_PTR retval = 31;
279 UINT gcdret = 0;
280 WCHAR curdir[MAX_PATH];
282 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
283 /* ShellExecute specifies the command from psei->lpDirectory
284 * if present. Not from the current dir as CreateProcess does */
285 if( psei->lpDirectory && psei->lpDirectory[0] )
286 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
287 if( !SetCurrentDirectoryW( psei->lpDirectory))
288 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
289 ZeroMemory(&startup,sizeof(STARTUPINFOW));
290 startup.cb = sizeof(STARTUPINFOW);
291 startup.dwFlags = STARTF_USESHOWWINDOW;
292 startup.wShowWindow = psei->nShow;
293 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, env,
294 psei->lpDirectory && *psei->lpDirectory ? psei->lpDirectory : NULL,
295 &startup, &info))
297 /* Give 30 seconds to the app to come up, if desired. Probably only needed
298 when starting app immediately before making a DDE connection. */
299 if (shWait)
300 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
301 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
302 retval = 33;
303 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
304 psei_out->hProcess = info.hProcess;
305 else
306 CloseHandle( info.hProcess );
307 CloseHandle( info.hThread );
309 else if ((retval = GetLastError()) >= 32)
311 TRACE("CreateProcess returned error %d\n", retval);
312 retval = ERROR_BAD_FORMAT;
315 TRACE("returning %u\n", retval);
317 psei_out->hInstApp = (HINSTANCE)retval;
318 if( gcdret )
319 if( !SetCurrentDirectoryW( curdir))
320 ERR("cannot return to directory %s\n", debugstr_w(curdir));
322 return retval;
326 /***********************************************************************
327 * SHELL_BuildEnvW [Internal]
329 * Build the environment for the new process, adding the specified
330 * path to the PATH variable. Returned pointer must be freed by caller.
332 static void *SHELL_BuildEnvW( const WCHAR *path )
334 static const WCHAR wPath[] = {'P','A','T','H','=',0};
335 WCHAR *strings, *new_env;
336 WCHAR *p, *p2;
337 int total = strlenW(path) + 1;
338 BOOL got_path = FALSE;
340 if (!(strings = GetEnvironmentStringsW())) return NULL;
341 p = strings;
342 while (*p)
344 int len = strlenW(p) + 1;
345 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
346 total += len;
347 p += len;
349 if (!got_path) total += 5; /* we need to create PATH */
350 total++; /* terminating null */
352 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
354 FreeEnvironmentStringsW( strings );
355 return NULL;
357 p = strings;
358 p2 = new_env;
359 while (*p)
361 int len = strlenW(p) + 1;
362 memcpy( p2, p, len * sizeof(WCHAR) );
363 if (!strncmpiW( p, wPath, 5 ))
365 p2[len - 1] = ';';
366 strcpyW( p2 + len, path );
367 p2 += strlenW(path) + 1;
369 p += len;
370 p2 += len;
372 if (!got_path)
374 strcpyW( p2, wPath );
375 strcatW( p2, path );
376 p2 += strlenW(p2) + 1;
378 *p2 = 0;
379 FreeEnvironmentStringsW( strings );
380 return new_env;
384 /***********************************************************************
385 * SHELL_TryAppPathW [Internal]
387 * Helper function for SHELL_FindExecutable
388 * @param lpResult - pointer to a buffer of size MAX_PATH
389 * On entry: szName is a filename (probably without path separators).
390 * On exit: if szName found in "App Path", place full path in lpResult, and return true
392 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
394 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',
395 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
396 static const WCHAR wPath[] = {'P','a','t','h',0};
397 HKEY hkApp = 0;
398 WCHAR buffer[1024];
399 LONG len;
400 LONG res;
401 BOOL found = FALSE;
403 if (env) *env = NULL;
404 strcpyW(buffer, wszKeyAppPaths);
405 strcatW(buffer, szName);
406 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
407 if (res) goto end;
409 len = MAX_PATH*sizeof(WCHAR);
410 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
411 if (res) goto end;
412 found = TRUE;
414 if (env)
416 DWORD count = sizeof(buffer);
417 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
418 *env = SHELL_BuildEnvW( buffer );
421 end:
422 if (hkApp) RegCloseKey(hkApp);
423 return found;
426 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
428 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
429 HKEY hkeyClass;
430 WCHAR verb[MAX_PATH];
432 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, 0x02000000, &hkeyClass))
433 return 31; /* default - 'No association was found' */
434 if (!HCR_GetDefaultVerbW(hkeyClass, lpOperation, verb, sizeof(verb)))
435 return 31; /* default - 'No association was found' */
436 RegCloseKey(hkeyClass);
438 /* Looking for ...buffer\shell\<verb>\command */
439 strcatW(filetype, wszShell);
440 strcatW(filetype, verb);
441 strcatW(filetype, wCommand);
443 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
444 &commandlen) == ERROR_SUCCESS)
446 commandlen /= sizeof(WCHAR);
447 if (key) strcpyW(key, filetype);
448 #if 0
449 LPWSTR tmp;
450 WCHAR param[256];
451 LONG paramlen = sizeof(param);
452 static const WCHAR wSpace[] = {' ',0};
454 /* FIXME: it seems all Windows version don't behave the same here.
455 * the doc states that this ddeexec information can be found after
456 * the exec names.
457 * on Win98, it doesn't appear, but I think it does on Win2k
459 /* Get the parameters needed by the application
460 from the associated ddeexec key */
461 tmp = strstrW(filetype, wCommand);
462 tmp[0] = '\0';
463 strcatW(filetype, wDdeexec);
464 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
465 &paramlen) == ERROR_SUCCESS)
467 paramlen /= sizeof(WCHAR);
468 strcatW(command, wSpace);
469 strcatW(command, param);
470 commandlen += paramlen;
472 #endif
474 command[commandlen] = '\0';
476 return 33; /* FIXME see SHELL_FindExecutable() */
479 return 31; /* default - 'No association was found' */
482 /*************************************************************************
483 * SHELL_FindExecutable [Internal]
485 * Utility for code sharing between FindExecutable and ShellExecute
486 * in:
487 * lpFile the name of a file
488 * lpOperation the operation on it (open)
489 * out:
490 * lpResult a buffer, big enough :-(, to store the command to do the
491 * operation on the file
492 * key a buffer, big enough, to get the key name to do actually the
493 * command (it'll be used afterwards for more information
494 * on the operation)
496 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
497 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
499 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
500 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
501 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
502 WCHAR *extension = NULL; /* pointer to file extension */
503 WCHAR filetype[256]; /* registry name for this filetype */
504 LONG filetypelen = sizeof(filetype); /* length of above */
505 WCHAR command[1024]; /* command from registry */
506 WCHAR wBuffer[256]; /* Used to GetProfileString */
507 UINT retval = 31; /* default - 'No association was found' */
508 WCHAR *tok; /* token pointer */
509 WCHAR xlpFile[256]; /* result of SearchPath */
510 DWORD attribs; /* file attributes */
512 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
514 xlpFile[0] = '\0';
515 lpResult[0] = '\0'; /* Start off with an empty return string */
516 if (key) *key = '\0';
518 /* trap NULL parameters on entry */
519 if ((lpFile == NULL) || (lpResult == NULL))
521 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
522 debugstr_w(lpFile), debugstr_w(lpResult));
523 return 2; /* File not found. Close enough, I guess. */
526 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
528 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
529 return 33;
532 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
534 TRACE("SearchPathW returned non-zero\n");
535 lpFile = xlpFile;
536 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
539 attribs = GetFileAttributesW(lpFile);
540 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
542 strcpyW(filetype, wszFolder);
543 filetypelen = 6; /* strlen("Folder") */
545 else
547 /* First thing we need is the file's extension */
548 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
549 /* File->Run in progman uses */
550 /* .\FILE.EXE :( */
551 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
553 if (extension == NULL || extension[1]==0)
555 WARN("Returning 31 - No association\n");
556 return 31; /* no association */
559 /* Three places to check: */
560 /* 1. win.ini, [windows], programs (NB no leading '.') */
561 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
562 /* 3. win.ini, [extensions], extension (NB no leading '.' */
563 /* All I know of the order is that registry is checked before */
564 /* extensions; however, it'd make sense to check the programs */
565 /* section first, so that's what happens here. */
567 /* See if it's a program - if GetProfileString fails, we skip this
568 * section. Actually, if GetProfileString fails, we've probably
569 * got a lot more to worry about than running a program... */
570 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
572 CharLowerW(wBuffer);
573 tok = wBuffer;
574 while (*tok)
576 WCHAR *p = tok;
577 while (*p && *p != ' ' && *p != '\t') p++;
578 if (*p)
580 *p++ = 0;
581 while (*p == ' ' || *p == '\t') p++;
584 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
586 strcpyW(lpResult, xlpFile);
587 /* Need to perhaps check that the file has a path
588 * attached */
589 TRACE("found %s\n", debugstr_w(lpResult));
590 return 33;
592 /* Greater than 32 to indicate success FIXME According to the
593 * docs, I should be returning a handle for the
594 * executable. Does this mean I'm supposed to open the
595 * executable file or something? More RTFM, I guess... */
597 tok = p;
601 /* Check registry */
602 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
603 &filetypelen) == ERROR_SUCCESS)
605 filetypelen /= sizeof(WCHAR);
606 filetype[filetypelen] = '\0';
607 TRACE("File type: %s\n", debugstr_w(filetype));
609 else
611 *filetype = '\0';
612 filetypelen = 0;
616 if (*filetype)
618 /* pass the operation string to SHELL_FindExecutableByOperation() */
619 filetype[filetypelen] = '\0';
620 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
622 if (retval > 32)
624 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
626 /* Remove double quotation marks and command line arguments */
627 if (*lpResult == '"')
629 WCHAR *p = lpResult;
630 while (*(p + 1) != '"')
632 *p = *(p + 1);
633 p++;
635 *p = '\0';
639 else /* Check win.ini */
641 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
643 /* Toss the leading dot */
644 extension++;
645 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
647 if (strlenW(command) != 0)
649 strcpyW(lpResult, command);
650 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
651 if (tok != NULL)
653 tok[0] = '\0';
654 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
655 tok = strchrW(command, '^'); /* see above */
656 if ((tok != NULL) && (strlenW(tok)>5))
658 strcatW(lpResult, &tok[5]);
661 retval = 33; /* FIXME - see above */
666 TRACE("returning %s\n", debugstr_w(lpResult));
667 return retval;
670 /******************************************************************
671 * dde_cb
673 * callback for the DDE connection. not really useful
675 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
676 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
677 ULONG_PTR dwData1, ULONG_PTR dwData2)
679 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
680 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
681 return NULL;
684 /******************************************************************
685 * dde_connect
687 * ShellExecute helper. Used to do an operation with a DDE connection
689 * Handles both the direct connection (try #1), and if it fails,
690 * launching an application and trying (#2) to connect to it
693 static unsigned dde_connect(WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
694 const WCHAR* lpFile, WCHAR *env,
695 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
696 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
698 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
699 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
700 WCHAR * endkey = key + strlenW(key);
701 WCHAR app[256], topic[256], ifexec[256], res[256];
702 LONG applen, topiclen, ifexeclen;
703 WCHAR * exec;
704 DWORD ddeInst = 0;
705 DWORD tid;
706 HSZ hszApp, hszTopic;
707 HCONV hConv;
708 HDDEDATA hDdeData;
709 unsigned ret = 31;
710 BOOL unicode = !(GetVersion() & 0x80000000);
712 strcpyW(endkey, wApplication);
713 applen = sizeof(app);
714 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
716 FIXME("default app name NIY %s\n", debugstr_w(key));
717 return 2;
720 strcpyW(endkey, wTopic);
721 topiclen = sizeof(topic);
722 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
724 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
725 strcpyW(topic, wSystem);
728 if (unicode)
730 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
731 return 2;
733 else
735 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
736 return 2;
739 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
740 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
742 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
743 exec = ddeexec;
744 if (!hConv)
746 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
747 TRACE("Launching '%s'\n", debugstr_w(start));
748 ret = execfunc(start, env, TRUE, psei, psei_out);
749 if (ret < 32)
751 TRACE("Couldn't launch\n");
752 goto error;
754 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
755 if (!hConv)
757 TRACE("Couldn't connect. ret=%d\n", ret);
758 DdeUninitialize(ddeInst);
759 SetLastError(ERROR_DDE_FAIL);
760 return 30; /* whatever */
762 strcpyW(endkey, wIfexec);
763 ifexeclen = sizeof(ifexec);
764 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
766 exec = ifexec;
770 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
771 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
773 /* It's documented in the KB 330337 that IE has a bug and returns
774 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
776 if (unicode)
777 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
778 XTYP_EXECUTE, 30000, &tid);
779 else
781 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
782 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
783 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
784 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
785 XTYP_EXECUTE, 10000, &tid );
786 HeapFree(GetProcessHeap(), 0, resA);
788 if (hDdeData)
789 DdeFreeDataHandle(hDdeData);
790 else
791 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
792 ret = 33;
794 DdeDisconnect(hConv);
796 error:
797 DdeUninitialize(ddeInst);
799 return ret;
802 /*************************************************************************
803 * execute_from_key [Internal]
805 static UINT_PTR execute_from_key(LPWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
806 LPCWSTR executable_name,
807 SHELL_ExecuteW32 execfunc,
808 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
810 WCHAR cmd[256];
811 LONG cmdlen = sizeof(cmd);
812 UINT_PTR retval = 31;
814 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
815 debugstr_w(szCommandline), debugstr_w(executable_name));
817 cmd[0] = '\0';
819 /* Get the application from the registry */
820 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
822 WCHAR param[1024];
824 TRACE("got cmd: %s\n", debugstr_w(cmd));
826 param[0] = '\0';
828 /* Is there a replace() function anywhere? */
829 cmdlen /= sizeof(WCHAR);
830 cmd[cmdlen] = '\0';
831 if (!SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline))
833 /* looks like there is no %1 param in the cmd, add one */
834 static const WCHAR oneW[] = { ' ','\"','%','1','\"',0 };
835 strcatW(cmd, oneW);
836 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
838 TRACE("executing: %s\n", debugstr_w(param));
839 retval = execfunc(param, env, FALSE, psei, psei_out);
841 else
843 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
844 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
845 LPWSTR tmp;
846 WCHAR param[256];
847 LONG paramlen = sizeof(param);
849 param[0] = '\0';
851 /* Get the parameters needed by the application
852 from the associated ddeexec key */
853 tmp = strstrW(key, wCommand);
854 assert(tmp);
855 strcpyW(tmp, wDdeexec);
857 TRACE("trying ddeexec cmd: %s\n", debugstr_w(key));
859 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
861 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
862 retval = dde_connect(key, executable_name, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
864 else
865 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
868 return retval;
871 /*************************************************************************
872 * FindExecutableA [SHELL32.@]
874 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
876 HINSTANCE retval;
877 WCHAR *wFile = NULL, *wDirectory = NULL;
878 WCHAR wResult[MAX_PATH];
880 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
881 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
883 retval = FindExecutableW(wFile, wDirectory, wResult);
884 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
885 SHFree( wFile );
886 SHFree( wDirectory );
888 TRACE("returning %s\n", lpResult);
889 return retval;
892 /*************************************************************************
893 * FindExecutableW [SHELL32.@]
895 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
897 UINT_PTR retval = 31; /* default - 'No association was found' */
898 WCHAR old_dir[1024];
900 TRACE("File %s, Dir %s\n",
901 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
903 lpResult[0] = '\0'; /* Start off with an empty return string */
905 /* trap NULL parameters on entry */
906 if ((lpFile == NULL) || (lpResult == NULL))
908 /* FIXME - should throw a warning, perhaps! */
909 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
912 if (lpDirectory)
914 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
915 SetCurrentDirectoryW(lpDirectory);
918 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
920 TRACE("returning %s\n", debugstr_w(lpResult));
921 if (lpDirectory)
922 SetCurrentDirectoryW(old_dir);
923 return (HINSTANCE)retval;
926 /* FIXME: is this already implemented somewhere else? */
927 static HKEY ShellExecute_GetClassKey( LPSHELLEXECUTEINFOW sei )
929 LPCWSTR ext = NULL, lpClass = NULL;
930 LPWSTR cls = NULL;
931 DWORD type = 0, sz = 0;
932 HKEY hkey = 0;
933 LONG r;
935 if (sei->fMask & SEE_MASK_CLASSALL)
936 return sei->hkeyClass;
938 if (sei->fMask & SEE_MASK_CLASSNAME)
939 lpClass = sei->lpClass;
940 else
942 ext = PathFindExtensionW( sei->lpFile );
943 TRACE("ext = %s\n", debugstr_w( ext ) );
944 if (!ext)
945 return hkey;
947 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
948 if (r != ERROR_SUCCESS )
949 return hkey;
951 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
952 if ( r == ERROR_SUCCESS && type == REG_SZ )
954 sz += sizeof (WCHAR);
955 cls = HeapAlloc( GetProcessHeap(), 0, sz );
956 cls[0] = 0;
957 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
960 RegCloseKey( hkey );
961 lpClass = cls;
964 TRACE("class = %s\n", debugstr_w(lpClass) );
966 hkey = 0;
967 if ( lpClass )
968 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
970 HeapFree( GetProcessHeap(), 0, cls );
972 return hkey;
975 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
977 LPCITEMIDLIST pidllast = NULL;
978 IDataObject *dataobj = NULL;
979 IShellFolder *shf = NULL;
980 LPITEMIDLIST pidl = NULL;
981 HRESULT r;
983 if (sei->fMask & SEE_MASK_CLASSALL)
984 pidl = sei->lpIDList;
985 else
987 WCHAR fullpath[MAX_PATH];
989 fullpath[0] = 0;
990 r = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
991 if (!r)
992 goto end;
994 pidl = ILCreateFromPathW( fullpath );
997 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
998 if ( FAILED( r ) )
999 goto end;
1001 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1002 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1004 end:
1005 if ( pidl != sei->lpIDList )
1006 ILFree( pidl );
1007 if ( shf )
1008 IShellFolder_Release( shf );
1009 return dataobj;
1012 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1013 LPSHELLEXECUTEINFOW sei )
1015 IContextMenu *cm = NULL;
1016 CMINVOKECOMMANDINFOEX ici;
1017 MENUITEMINFOW info;
1018 WCHAR string[0x80];
1019 INT i, n, def = -1;
1020 HMENU hmenu = 0;
1021 HRESULT r;
1023 TRACE("%p %p\n", obj, sei );
1025 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1026 if ( FAILED( r ) )
1027 return r;
1029 hmenu = CreateMenu();
1030 if ( !hmenu )
1031 goto end;
1033 /* the number of the last menu added is returned in r */
1034 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1035 if ( FAILED( r ) )
1036 goto end;
1038 n = GetMenuItemCount( hmenu );
1039 for ( i = 0; i < n; i++ )
1041 memset( &info, 0, sizeof info );
1042 info.cbSize = sizeof info;
1043 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1044 info.dwTypeData = string;
1045 info.cch = sizeof string;
1046 string[0] = 0;
1047 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1049 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1050 info.fState, info.dwItemData, info.fType, info.wID );
1051 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1052 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1054 def = i;
1055 break;
1059 r = E_FAIL;
1060 if ( def == -1 )
1061 goto end;
1063 memset( &ici, 0, sizeof ici );
1064 ici.cbSize = sizeof ici;
1065 ici.fMask = CMIC_MASK_UNICODE;
1066 ici.nShow = sei->nShow;
1067 ici.lpVerb = MAKEINTRESOURCEA( def );
1068 ici.hwnd = sei->hwnd;
1069 ici.lpParametersW = sei->lpParameters;
1071 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1073 TRACE("invoke command returned %08x\n", r );
1075 end:
1076 if ( hmenu )
1077 DestroyMenu( hmenu );
1078 if ( cm )
1079 IContextMenu_Release( cm );
1080 return r;
1083 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1085 IDataObject *dataobj = NULL;
1086 IObjectWithSite *ows = NULL;
1087 IShellExtInit *obj = NULL;
1088 HRESULT r;
1090 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1092 r = CoInitialize( NULL );
1093 if ( FAILED( r ) )
1094 goto end;
1096 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1097 &IID_IShellExtInit, (LPVOID*)&obj );
1098 if ( FAILED( r ) )
1100 ERR("failed %08x\n", r );
1101 goto end;
1104 dataobj = shellex_get_dataobj( sei );
1105 if ( !dataobj )
1107 ERR("failed to get data object\n");
1108 goto end;
1111 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1112 if ( FAILED( r ) )
1113 goto end;
1115 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1116 if ( FAILED( r ) )
1117 goto end;
1119 IObjectWithSite_SetSite( ows, NULL );
1121 r = shellex_run_context_menu_default( obj, sei );
1123 end:
1124 if ( ows )
1125 IObjectWithSite_Release( ows );
1126 if ( dataobj )
1127 IDataObject_Release( dataobj );
1128 if ( obj )
1129 IShellExtInit_Release( obj );
1130 CoUninitialize();
1131 return r;
1135 /*************************************************************************
1136 * ShellExecute_FromContextMenu [Internal]
1138 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1140 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1141 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1142 HKEY hkey, hkeycm = 0;
1143 WCHAR szguid[39];
1144 HRESULT hr;
1145 GUID guid;
1146 DWORD i;
1147 LONG r;
1149 TRACE("%s\n", debugstr_w(sei->lpFile) );
1151 hkey = ShellExecute_GetClassKey( sei );
1152 if ( !hkey )
1153 return ERROR_FUNCTION_FAILED;
1155 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1156 if ( r == ERROR_SUCCESS )
1158 i = 0;
1159 while ( 1 )
1161 r = RegEnumKeyW( hkeycm, i++, szguid, 39 );
1162 if ( r != ERROR_SUCCESS )
1163 break;
1165 hr = CLSIDFromString( szguid, &guid );
1166 if (SUCCEEDED(hr))
1168 /* stop at the first one that succeeds in running */
1169 hr = shellex_load_object_and_run( hkey, &guid, sei );
1170 if ( SUCCEEDED( hr ) )
1171 break;
1174 RegCloseKey( hkeycm );
1177 if ( hkey != sei->hkeyClass )
1178 RegCloseKey( hkey );
1179 return r;
1182 /*************************************************************************
1183 * SHELL_execute [Internal]
1185 BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1187 static const WCHAR wQuote[] = {'"',0};
1188 static const WCHAR wSpace[] = {' ',0};
1189 static const WCHAR wWww[] = {'w','w','w',0};
1190 static const WCHAR wFile[] = {'f','i','l','e',0};
1191 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1192 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1193 static const DWORD unsupportedFlags =
1194 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1195 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI |
1196 SEE_MASK_UNICODE | SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK |
1197 SEE_MASK_HMONITOR;
1199 WCHAR *wszApplicationName, wszParameters[1024], wszDir[MAX_PATH];
1200 DWORD dwApplicationNameLen = MAX_PATH+2;
1201 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1202 WCHAR wfileName[MAX_PATH];
1203 WCHAR *env;
1204 WCHAR lpstrProtocol[256];
1205 LPCWSTR lpFile;
1206 UINT_PTR retval = 31;
1207 WCHAR wcmd[1024];
1208 WCHAR buffer[MAX_PATH];
1209 BOOL done;
1211 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1212 memcpy(&sei_tmp, sei, sizeof(sei_tmp));
1214 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1215 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1216 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1217 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1218 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1219 debugstr_w(sei_tmp.lpClass) : "not used");
1221 sei->hProcess = NULL;
1223 /* make copies of all path/command strings */
1224 if (!sei_tmp.lpFile)
1226 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1227 *wszApplicationName = '\0';
1229 else if (*sei_tmp.lpFile == '\"')
1231 DWORD l = strlenW(sei_tmp.lpFile+1);
1232 if(l >= dwApplicationNameLen) dwApplicationNameLen = l+1;
1233 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1234 memcpy(wszApplicationName, sei_tmp.lpFile+1, (l+1)*sizeof(WCHAR));
1235 if (wszApplicationName[l-1] == '\"')
1236 wszApplicationName[l-1] = '\0';
1237 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1238 } else {
1239 DWORD l = strlenW(sei_tmp.lpFile)+1;
1240 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1241 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1242 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1245 if (sei_tmp.lpParameters)
1246 strcpyW(wszParameters, sei_tmp.lpParameters);
1247 else
1248 *wszParameters = '\0';
1250 if (sei_tmp.lpDirectory)
1251 strcpyW(wszDir, sei_tmp.lpDirectory);
1252 else
1253 *wszDir = '\0';
1255 /* adjust string pointers to point to the new buffers */
1256 sei_tmp.lpFile = wszApplicationName;
1257 sei_tmp.lpParameters = wszParameters;
1258 sei_tmp.lpDirectory = wszDir;
1260 if (sei_tmp.fMask & unsupportedFlags)
1262 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1265 /* process the IDList */
1266 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1268 IShellExecuteHookW* pSEH;
1270 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1272 if (SUCCEEDED(hr))
1274 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1276 IShellExecuteHookW_Release(pSEH);
1278 if (hr == S_OK) {
1279 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1280 return TRUE;
1284 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1285 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1288 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1290 sei->hInstApp = (HINSTANCE) 33;
1291 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1292 return TRUE;
1295 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1297 /* launch a document by fileclass like 'WordPad.Document.1' */
1298 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1299 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1300 ULONG cmask=(sei_tmp.fMask & SEE_MASK_CLASSALL);
1301 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
1302 (cmask == SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
1303 sei_tmp.lpVerb,
1304 wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
1306 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1307 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
1309 wcmd[0] = '\0';
1310 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL);
1311 if (!done && wszApplicationName[0])
1313 strcatW(wcmd, wSpace);
1314 strcatW(wcmd, wszApplicationName);
1316 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1318 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1319 return retval > 32;
1322 /* Has the IDList not yet been translated? */
1323 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1325 /* last chance to translate IDList: now also allow CLSID paths */
1326 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1327 if (buffer[0]==':' && buffer[1]==':') {
1328 /* open shell folder for the specified class GUID */
1329 strcpyW(wszParameters, buffer);
1330 strcpyW(wszApplicationName, wExplorer);
1332 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1333 } else {
1334 WCHAR target[MAX_PATH];
1335 DWORD attribs;
1336 /* Check if we're executing a directory and if so use the
1337 handler for the Folder class */
1338 strcpyW(target, buffer);
1339 attribs = GetFileAttributesW(buffer);
1340 if (attribs != INVALID_FILE_ATTRIBUTES &&
1341 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1342 HCR_GetExecuteCommandW(0, wszFolder,
1343 sei_tmp.lpVerb,
1344 buffer, sizeof(buffer))) {
1345 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1346 buffer, target, sei_tmp.lpIDList, NULL);
1348 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1353 /* expand environment strings */
1354 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1355 lstrcpyW(wszApplicationName, buffer);
1357 if (*sei_tmp.lpParameters)
1358 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1359 lstrcpyW(wszParameters, buffer);
1361 if (*sei_tmp.lpDirectory)
1362 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1363 lstrcpyW(wszDir, buffer);
1365 /* Else, try to execute the filename */
1366 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1368 /* separate out command line arguments from executable file name */
1369 if (!*sei_tmp.lpParameters) {
1370 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1371 if (sei_tmp.lpFile[0] == '"') {
1372 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1373 LPWSTR dst = wfileName;
1374 LPWSTR end;
1376 /* copy the unquoted executable path to 'wfileName' */
1377 while(*src && *src!='"')
1378 *dst++ = *src++;
1380 *dst = '\0';
1382 if (*src == '"') {
1383 end = ++src;
1385 while(isspace(*src))
1386 ++src;
1387 } else
1388 end = src;
1390 /* copy the parameter string to 'wszParameters' */
1391 strcpyW(wszParameters, src);
1393 /* terminate previous command string after the quote character */
1394 *end = '\0';
1396 else
1398 /* If the executable name is not quoted, we have to use this search loop here,
1399 that in CreateProcess() is not sufficient because it does not handle shell links. */
1400 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1401 LPWSTR space, s;
1403 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1404 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1405 int idx = space-sei_tmp.lpFile;
1406 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1407 buffer[idx] = '\0';
1409 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1410 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1412 /* separate out command from parameter string */
1413 LPCWSTR p = space + 1;
1415 while(isspaceW(*p))
1416 ++p;
1418 strcpyW(wszParameters, p);
1419 *space = '\0';
1421 break;
1425 strcpyW(wfileName, sei_tmp.lpFile);
1427 } else
1428 strcpyW(wfileName, sei_tmp.lpFile);
1430 lpFile = wfileName;
1432 strcpyW(wcmd, wszApplicationName);
1433 if (sei_tmp.lpParameters[0]) {
1434 strcatW(wcmd, wSpace);
1435 strcatW(wcmd, wszParameters);
1438 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1439 if (retval > 32) {
1440 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1441 return TRUE;
1444 /* Else, try to find the executable */
1445 wcmd[0] = '\0';
1446 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1447 if (retval > 32) /* Found */
1449 WCHAR wszQuotedCmd[MAX_PATH+2];
1450 /* Must quote to handle case where cmd contains spaces,
1451 * else security hole if malicious user creates executable file "C:\\Program"
1453 strcpyW(wszQuotedCmd, wQuote);
1454 strcatW(wszQuotedCmd, wcmd);
1455 strcatW(wszQuotedCmd, wQuote);
1456 if (wszParameters[0]) {
1457 strcatW(wszQuotedCmd, wSpace);
1458 strcatW(wszQuotedCmd, wszParameters);
1460 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1461 if (*lpstrProtocol)
1462 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, wcmd, execfunc, &sei_tmp, sei);
1463 else
1464 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
1465 HeapFree( GetProcessHeap(), 0, env );
1467 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1469 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1470 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1471 LPWSTR lpstrRes;
1472 INT iSize;
1474 lpstrRes = strchrW(lpFile, ':');
1475 if (lpstrRes)
1476 iSize = lpstrRes - lpFile;
1477 else
1478 iSize = strlenW(lpFile);
1480 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1481 /* Looking for ...protocol\shell\lpOperation\command */
1482 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1483 lpstrProtocol[iSize] = '\0';
1484 strcatW(lpstrProtocol, wShell);
1485 strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1486 strcatW(lpstrProtocol, wCommand);
1488 /* Remove File Protocol from lpFile */
1489 /* In the case file://path/file */
1490 if (!strncmpiW(lpFile, wFile, iSize))
1492 lpFile += iSize;
1493 while (*lpFile == ':') lpFile++;
1495 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, wcmd, execfunc, &sei_tmp, sei);
1497 /* Check if file specified is in the form www.??????.*** */
1498 else if (!strncmpiW(lpFile, wWww, 3))
1500 /* if so, append lpFile http:// and call ShellExecute */
1501 WCHAR lpstrTmpFile[256];
1502 strcpyW(lpstrTmpFile, wHttp);
1503 strcatW(lpstrTmpFile, lpFile);
1504 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1507 TRACE("retval %u\n", retval);
1509 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1511 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1512 return retval > 32;
1515 /*************************************************************************
1516 * ShellExecuteA [SHELL32.290]
1518 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1519 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1521 SHELLEXECUTEINFOA sei;
1523 TRACE("%p,%s,%s,%s,%s,%d\n",
1524 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
1525 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1527 sei.cbSize = sizeof(sei);
1528 sei.fMask = 0;
1529 sei.hwnd = hWnd;
1530 sei.lpVerb = lpOperation;
1531 sei.lpFile = lpFile;
1532 sei.lpParameters = lpParameters;
1533 sei.lpDirectory = lpDirectory;
1534 sei.nShow = iShowCmd;
1535 sei.lpIDList = 0;
1536 sei.lpClass = 0;
1537 sei.hkeyClass = 0;
1538 sei.dwHotKey = 0;
1539 sei.hProcess = 0;
1541 ShellExecuteExA (&sei);
1542 return sei.hInstApp;
1545 /*************************************************************************
1546 * ShellExecuteExA [SHELL32.292]
1549 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1551 SHELLEXECUTEINFOW seiW;
1552 BOOL ret;
1553 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1555 TRACE("%p\n", sei);
1557 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1559 if (sei->lpVerb)
1560 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1562 if (sei->lpFile)
1563 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1565 if (sei->lpParameters)
1566 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1568 if (sei->lpDirectory)
1569 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1571 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1572 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1573 else
1574 seiW.lpClass = NULL;
1576 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1578 sei->hInstApp = seiW.hInstApp;
1580 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1581 sei->hProcess = seiW.hProcess;
1583 SHFree(wVerb);
1584 SHFree(wFile);
1585 SHFree(wParameters);
1586 SHFree(wDirectory);
1587 SHFree(wClass);
1589 return ret;
1592 /*************************************************************************
1593 * ShellExecuteExW [SHELL32.293]
1596 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1598 return SHELL_execute( sei, SHELL_ExecuteW );
1601 /*************************************************************************
1602 * ShellExecuteW [SHELL32.294]
1603 * from shellapi.h
1604 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1605 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1607 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1608 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1610 SHELLEXECUTEINFOW sei;
1612 TRACE("\n");
1613 sei.cbSize = sizeof(sei);
1614 sei.fMask = 0;
1615 sei.hwnd = hwnd;
1616 sei.lpVerb = lpOperation;
1617 sei.lpFile = lpFile;
1618 sei.lpParameters = lpParameters;
1619 sei.lpDirectory = lpDirectory;
1620 sei.nShow = nShowCmd;
1621 sei.lpIDList = 0;
1622 sei.lpClass = 0;
1623 sei.hkeyClass = 0;
1624 sei.dwHotKey = 0;
1625 sei.hProcess = 0;
1627 SHELL_execute( &sei, SHELL_ExecuteW );
1628 return sei.hInstApp;