Refactoring of Wine DLL import
[reactos.git] / reactos / lib / shell32 / shlexec.c
1 /*
2 * Shell Library Functions
3 *
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "heap.h"
41 #include "shellapi.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "shlobj.h"
45 #include "shlwapi.h"
46 #include "ddeml.h"
47
48 #include "wine/winbase16.h"
49 #include "shell32_main.h"
50 #include "undocshell.h"
51
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(exec);
55
56 /***********************************************************************
57 * this function is supposed to expand the escape sequences found in the registry
58 * some diving reported that the following were used:
59 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
60 * %1 file
61 * %2 printer
62 * %3 driver
63 * %4 port
64 * %I address of a global item ID (explorer switch /idlist)
65 * %L seems to be %1 as long filename followed by the 8+3 variation
66 * %S ???
67 * %* all following parameters (see batfile)
68 */
69 static BOOL argify(char* res, int len, const char* fmt, const char* lpFile, LPITEMIDLIST pidl, LPCSTR args)
70 {
71 char xlpFile[1024];
72 BOOL done = FALSE;
73 LPVOID pv;
74
75 while (*fmt)
76 {
77 if (*fmt == '%')
78 {
79 switch (*++fmt)
80 {
81 case '\0':
82 case '%':
83 *res++ = '%';
84 break;
85
86 case '2':
87 case '3':
88 case '4':
89 case '5':
90 case '6':
91 case '7':
92 case '8':
93 case '9':
94 case '0':
95 case '*':
96 if (args)
97 {
98 if (*fmt == '*')
99 {
100 while(*args)
101 *res++ = *args++;
102 }
103 else
104 {
105 while(*args && !isspace(*args))
106 *res++ = *args++;
107
108 while(isspace(*args))
109 ++args;
110 }
111 }
112 else
113 {
114 case '1':
115 if (!done || (*fmt == '1'))
116 {
117 /*FIXME Is SearchPath() really needed? We already have separated out the parameter string in args. */
118 if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
119 {
120 *res++ = '"';
121 strcpy(res, xlpFile);
122 res += strlen(xlpFile);
123 *res++ = '"';
124 }
125 else
126 {
127 *res++ = '"';
128 strcpy(res, lpFile);
129 res += strlen(lpFile);
130 *res++ = '"';
131 }
132 }
133 }
134 break;
135
136 /*
137 * IE uses this alot for activating things such as windows media
138 * player. This is not verified to be fully correct but it appears
139 * to work just fine.
140 */
141 case 'l':
142 case 'L':
143 if (lpFile) {
144 strcpy(res, lpFile);
145 res += strlen(lpFile);
146 }
147 break;
148
149 case 'i':
150 case 'I':
151 if (pidl) {
152 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
153 pv = SHLockShared(hmem, 0);
154 res += sprintf(res, ":%p", pv);
155 SHUnlockShared(pv);
156 }
157 break;
158
159 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
160 }
161 fmt++;
162 done = TRUE;
163 }
164 else
165 *res++ = *fmt++;
166 }
167 *res = '\0';
168 return done;
169 }
170
171 /*************************************************************************
172 * _ResolveShortCut [Internal]
173 *
174 */
175 static HRESULT _ResolveShortCut(LPWSTR path, LPWSTR args, HWND hwnd, int* pshowcmd, LPITEMIDLIST* ppidl)
176 {
177 IShellFolder* psf;
178
179 HRESULT hr = SHGetDesktopFolder(&psf);
180
181 *ppidl = NULL;
182
183 if (SUCCEEDED(hr)) {
184 LPITEMIDLIST pidl;
185 ULONG l;
186
187 hr = IShellFolder_ParseDisplayName(psf, 0, 0, path, &l, &pidl, 0);
188
189 if (SUCCEEDED(hr)) {
190 IShellLinkW* psl;
191
192 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
193
194 if (SUCCEEDED(hr)) {
195 hr = IShellLinkW_Resolve(psl, hwnd, SLR_NO_UI);
196
197 if (SUCCEEDED(hr)) {
198 hr = IShellLinkW_GetPath(psl, path, MAX_PATH, NULL, SLGP_UNCPRIORITY);
199
200 if (SUCCEEDED(hr)) {
201 if (!*path)
202 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
203 hr = IShellLinkW_GetIDList(psl, ppidl);
204
205 if (SUCCEEDED(hr)) {
206 /* get command line arguments and display mode if available */
207 IShellLinkW_GetArguments(psl, args, MAX_PATH);
208 IShellLinkW_GetShowCmd(psl, pshowcmd);
209 }
210 }
211 }
212
213 IShellLinkW_Release(psl);
214 }
215
216 SHFree(pidl);
217 }
218
219 IShellFolder_Release(psf);
220 }
221
222 return hr;
223 }
224
225 /*************************************************************************
226 * SHELL_ExecuteA [Internal]
227 *
228 */
229 static UINT SHELL_ExecuteA(char *lpCmd, void *env, LPSHELLEXECUTEINFOA sei, BOOL shWait)
230 {
231 STARTUPINFOA startup;
232 PROCESS_INFORMATION info;
233 UINT retval = 31;
234
235 TRACE("Execute %s from directory %s\n", lpCmd, sei->lpDirectory);
236 ZeroMemory(&startup,sizeof(STARTUPINFOA));
237 startup.cb = sizeof(STARTUPINFOA);
238 startup.dwFlags = STARTF_USESHOWWINDOW;
239 startup.wShowWindow = sei->nShow;
240 if (CreateProcessA(NULL, lpCmd, NULL, NULL, FALSE, 0,
241 env, sei->lpDirectory, &startup, &info))
242 {
243 /* Give 30 seconds to the app to come up, if desired. Probably only needed
244 when starting app immediately before making a DDE connection. */
245 if (shWait)
246 if (WaitForInputIdle( info.hProcess, 30000 ) == -1)
247 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
248 retval = 33;
249 if(sei->fMask & SEE_MASK_NOCLOSEPROCESS)
250 sei->hProcess = info.hProcess;
251 else
252 CloseHandle( info.hProcess );
253 CloseHandle( info.hThread );
254 }
255 else if ((retval = GetLastError()) >= 32)
256 {
257 FIXME("Strange error set by CreateProcess: %d\n", retval);
258 retval = ERROR_BAD_FORMAT;
259 }
260
261 sei->hInstApp = (HINSTANCE)retval;
262 return retval;
263 }
264
265
266 /***********************************************************************
267 * build_env
268 *
269 * Build the environment for the new process, adding the specified
270 * path to the PATH variable. Returned pointer must be freed by caller.
271 */
272 static void *build_env( const char *path )
273 {
274 char *strings, *new_env;
275 char *p, *p2;
276 int total = strlen(path) + 1;
277 BOOL got_path = FALSE;
278
279 if (!(strings = GetEnvironmentStringsA())) return NULL;
280 p = strings;
281 while (*p)
282 {
283 int len = strlen(p) + 1;
284 if (!strncasecmp( p, "PATH=", 5 )) got_path = TRUE;
285 total += len;
286 p += len;
287 }
288 if (!got_path) total += 5; /* we need to create PATH */
289 total++; /* terminating null */
290
291 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total )))
292 {
293 FreeEnvironmentStringsA( strings );
294 return NULL;
295 }
296 p = strings;
297 p2 = new_env;
298 while (*p)
299 {
300 int len = strlen(p) + 1;
301 memcpy( p2, p, len );
302 if (!strncasecmp( p, "PATH=", 5 ))
303 {
304 p2[len - 1] = ';';
305 strcpy( p2 + len, path );
306 p2 += strlen(path) + 1;
307 }
308 p += len;
309 p2 += len;
310 }
311 if (!got_path)
312 {
313 strcpy( p2, "PATH=" );
314 strcat( p2, path );
315 p2 += strlen(p2) + 1;
316 }
317 *p2 = 0;
318 FreeEnvironmentStringsA( strings );
319 return new_env;
320 }
321
322
323 /***********************************************************************
324 * SHELL_TryAppPath
325 *
326 * Helper function for SHELL_FindExecutable
327 * @param lpResult - pointer to a buffer of size MAX_PATH
328 * On entry: szName is a filename (probably without path separators).
329 * On exit: if szName found in "App Path", place full path in lpResult, and return true
330 */
331 static BOOL SHELL_TryAppPath( LPCSTR szName, LPSTR lpResult, void**env)
332 {
333 HKEY hkApp = 0;
334 char buffer[256];
335 LONG len;
336 LONG res;
337 BOOL found = FALSE;
338
339 if (env) *env = NULL;
340 sprintf(buffer, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s", szName);
341 res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
342 if (res) goto end;
343
344 len = MAX_PATH;
345 res = RegQueryValueA(hkApp, NULL, lpResult, &len);
346 if (res) goto end;
347 found = TRUE;
348
349 if (env)
350 {
351 DWORD count = sizeof(buffer);
352 if (!RegQueryValueExA(hkApp, "Path", NULL, NULL, buffer, &count) && buffer[0])
353 *env = build_env( buffer );
354 }
355
356 end:
357 if (hkApp) RegCloseKey(hkApp);
358 return found;
359 }
360
361 static UINT _FindExecutableByOperation(LPCSTR lpPath, LPCSTR lpFile, LPCSTR lpOperation, LPSTR key, LPSTR filetype, LPSTR command)
362 {
363 LONG commandlen = 256; /* This is the most DOS can handle :) */
364
365 /* Looking for ...buffer\shell\<verb>\command */
366 strcat(filetype, "\\shell\\");
367 strcat(filetype, lpOperation);
368 strcat(filetype, "\\command");
369
370 if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, command, &commandlen) == ERROR_SUCCESS)
371 {
372 if (key) strcpy(key, filetype);
373 #if 0
374 LPSTR tmp;
375 char param[256];
376 LONG paramlen = 256;
377
378 /* FIXME: it seems all Windows version don't behave the same here.
379 * the doc states that this ddeexec information can be found after
380 * the exec names.
381 * on Win98, it doesn't appear, but I think it does on Win2k
382 */
383 /* Get the parameters needed by the application
384 from the associated ddeexec key */
385 tmp = strstr(filetype, "command");
386 tmp[0] = '\0';
387 strcat(filetype, "ddeexec");
388
389 if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, param, &paramlen) == ERROR_SUCCESS)
390 {
391 strcat(command, " ");
392 strcat(command, param);
393 commandlen += paramlen;
394 }
395 #endif
396
397 command[commandlen] = '\0';
398
399 return 33; /* FIXME see SHELL_FindExecutable() */
400 }
401
402 return 31; /* default - 'No association was found' */
403 }
404
405 /*************************************************************************
406 * SHELL_FindExecutable [Internal]
407 *
408 * Utility for code sharing between FindExecutable and ShellExecute
409 * in:
410 * lpFile the name of a file
411 * lpOperation the operation on it (open)
412 * out:
413 * lpResult a buffer, big enough :-(, to store the command to do the
414 * operation on the file
415 * key a buffer, big enough, to get the key name to do actually the
416 * command (it'll be used afterwards for more information
417 * on the operation)
418 */
419 UINT SHELL_FindExecutable(LPCSTR lpPath, LPCSTR lpFile, LPCSTR lpOperation,
420 LPSTR lpResult, LPSTR key, void **env, LPITEMIDLIST pidl, LPCSTR args)
421 {
422 char *extension = NULL; /* pointer to file extension */
423 char tmpext[5]; /* local copy to mung as we please */
424 char filetype[256]; /* registry name for this filetype */
425 LONG filetypelen = 256; /* length of above */
426 char command[256]; /* command from registry */
427 char buffer[256]; /* Used to GetProfileString */
428 UINT retval = 31; /* default - 'No association was found' */
429 char *tok; /* token pointer */
430 char xlpFile[256] = ""; /* result of SearchPath */
431 DWORD attribs; /* file attributes */
432
433 TRACE("%s\n", (lpFile != NULL) ? lpFile : "-");
434
435 lpResult[0] = '\0'; /* Start off with an empty return string */
436 if (key) *key = '\0';
437
438 /* trap NULL parameters on entry */
439 if ((lpFile == NULL) || (lpResult == NULL))
440 {
441 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", lpFile, lpResult);
442 return 2; /* File not found. Close enough, I guess. */
443 }
444
445 if (SHELL_TryAppPath( lpFile, lpResult, env ))
446 {
447 TRACE("found %s via App Paths\n", lpResult);
448 return 33;
449 }
450
451 if (SearchPathA(lpPath, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
452 {
453 TRACE("SearchPathA returned non-zero\n");
454 lpFile = xlpFile;
455 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
456 }
457
458 attribs = GetFileAttributesA(lpFile);
459
460 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
461 {
462 strcpy(filetype, "Folder");
463 filetypelen = 6; /* strlen("Folder") */
464 }
465 else
466 {
467 /* First thing we need is the file's extension */
468 extension = PathFindExtensionA(xlpFile); /* Assume last "." is the one; */
469 /* File->Run in progman uses */
470 /* .\FILE.EXE :( */
471 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
472
473 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
474 {
475 WARN("Returning 31 - No association\n");
476 return 31; /* no association */
477 }
478
479 /* Make local copy & lowercase it for reg & 'programs=' lookup */
480 lstrcpynA(tmpext, extension, 5);
481 CharLowerA(tmpext);
482 TRACE("%s file\n", tmpext);
483
484 /* Three places to check: */
485 /* 1. win.ini, [windows], programs (NB no leading '.') */
486 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
487 /* 3. win.ini, [extensions], extension (NB no leading '.' */
488 /* All I know of the order is that registry is checked before */
489 /* extensions; however, it'd make sense to check the programs */
490 /* section first, so that's what happens here. */
491
492 /* See if it's a program - if GetProfileString fails, we skip this
493 * section. Actually, if GetProfileString fails, we've probably
494 * got a lot more to worry about than running a program... */
495 if (GetProfileStringA("windows", "programs", "exe pif bat cmd com",
496 buffer, sizeof(buffer)) > 0)
497 {
498 UINT i;
499
500 for (i = 0;i<strlen(buffer); i++) buffer[i] = tolower(buffer[i]);
501
502 tok = strtok(buffer, " \t"); /* ? */
503 while (tok!= NULL)
504 {
505 if (strcmp(tok, &tmpext[1]) == 0) /* have to skip the leading "." */
506 {
507 strcpy(lpResult, xlpFile);
508 /* Need to perhaps check that the file has a path
509 * attached */
510 TRACE("found %s\n", lpResult);
511 return 33;
512
513 /* Greater than 32 to indicate success FIXME According to the
514 * docs, I should be returning a handle for the
515 * executable. Does this mean I'm supposed to open the
516 * executable file or something? More RTFM, I guess... */
517 }
518 tok = strtok(NULL, " \t");
519 }
520 }
521
522 /* Check registry */
523 if (RegQueryValueA(HKEY_CLASSES_ROOT, tmpext, filetype, &filetypelen) != ERROR_SUCCESS)
524 *filetype = '\0';
525 }
526
527 if (*filetype)
528 {
529 if (lpOperation)
530 {
531 /* pass the operation string to _FindExecutableByOperation() */
532 filetype[filetypelen] = '\0';
533 retval = _FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command);
534 }
535 else
536 {
537 char operation[MAX_PATH];
538 HKEY hkey;
539
540 strcat(filetype, "\\shell");
541
542 /* enumerate the operation subkeys in the registry and search for one with an associated command */
543 if (RegOpenKeyA(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
544 {
545 int idx = 0;
546 for(;; ++idx)
547 {
548 if (RegEnumKeyA(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
549 break;
550
551 filetype[filetypelen] = '\0';
552 retval = _FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command);
553
554 if (retval > 32)
555 break;
556 }
557
558 RegCloseKey(hkey);
559 }
560 }
561
562 if (retval > 32)
563 {
564 argify(lpResult, sizeof(lpResult), command, xlpFile, pidl, args);
565 /* Remove double quotation marks */
566 if (*lpResult == '"')
567 {
568 char *p = lpResult;
569 while (*(p + 1) != '"' && *(p + 1) != ' ')
570 {
571 *p = *(p + 1);
572 p++;
573 }
574 *p = '\0';
575 }
576 }
577 }
578 else if (extension) /* Check win.ini */
579 {
580 /* Toss the leading dot */
581 extension++;
582 if (GetProfileStringA("extensions", extension, "", command,
583 sizeof(command)) > 0)
584 {
585 if (strlen(command) != 0)
586 {
587 strcpy(lpResult, command);
588 tok = strstr(lpResult, "^"); /* should be ^.extension? */
589 if (tok != NULL)
590 {
591 tok[0] = '\0';
592 strcat(lpResult, xlpFile); /* what if no dir in xlpFile? */
593 tok = strstr(command, "^"); /* see above */
594 if ((tok != NULL) && (strlen(tok)>5))
595 {
596 strcat(lpResult, &tok[5]);
597 }
598 }
599 retval = 33; /* FIXME - see above */
600 }
601 }
602 }
603
604 TRACE("returning %s\n", lpResult);
605 return retval;
606 }
607
608 /******************************************************************
609 * dde_cb
610 *
611 * callback for the DDE connection. not really usefull
612 */
613 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
614 HSZ hsz1, HSZ hsz2,
615 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
616 {
617 return NULL;
618 }
619
620 /******************************************************************
621 * dde_connect
622 *
623 * ShellExecute helper. Used to do an operation with a DDE connection
624 *
625 * Handles both the direct connection (try #1), and if it fails,
626 * launching an application and trying (#2) to connect to it
627 *
628 */
629 static unsigned dde_connect(char* key, char* start, char* ddeexec,
630 const char* lpFile, void *env,
631 LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc,
632 LPCSTR szCommandline, LPITEMIDLIST pidl)
633 {
634 char* endkey = key + strlen(key);
635 char app[256], topic[256], ifexec[256], res[256];
636 LONG applen, topiclen, ifexeclen;
637 char* exec;
638 DWORD ddeInst = 0;
639 DWORD tid;
640 HSZ hszApp, hszTopic;
641 HCONV hConv;
642 unsigned ret = 31;
643
644 strcpy(endkey, "\\application");
645 applen = sizeof(app);
646 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
647 {
648 FIXME("default app name NIY %s\n", key);
649 return 2;
650 }
651
652 strcpy(endkey, "\\topic");
653 topiclen = sizeof(topic);
654 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
655 {
656 strcpy(topic, "System");
657 }
658
659 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
660 {
661 return 2;
662 }
663
664 hszApp = DdeCreateStringHandleA(ddeInst, app, CP_WINANSI);
665 hszTopic = DdeCreateStringHandleA(ddeInst, topic, CP_WINANSI);
666
667 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
668 exec = ddeexec;
669 if (!hConv)
670 {
671 TRACE("Launching '%s'\n", start);
672 ret = execfunc(start, env, sei, TRUE);
673 if (ret < 32)
674 {
675 TRACE("Couldn't launch\n");
676 goto error;
677 }
678 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
679 if (!hConv)
680 {
681 TRACE("Couldn't connect. ret=%d\n", ret);
682 ret = 30; /* whatever */
683 goto error;
684 }
685 strcpy(endkey, "\\ifexec");
686 ifexeclen = sizeof(ifexec);
687 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
688 {
689 exec = ifexec;
690 }
691 }
692
693 argify(res, sizeof(res), exec, lpFile, pidl, szCommandline);
694 TRACE("%s %s => %s\n", exec, lpFile, res);
695
696 ret = (DdeClientTransaction(res, strlen(res) + 1, hConv, 0L, 0,
697 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
698 DdeDisconnect(hConv);
699 error:
700 DdeUninitialize(ddeInst);
701 return ret;
702 }
703
704 /*************************************************************************
705 * execute_from_key [Internal]
706 */
707 static UINT execute_from_key(LPSTR key, LPCSTR lpFile, void *env,
708 LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc,
709 LPCSTR szCommandline, LPITEMIDLIST pidl)
710 {
711 char cmd[1024] = "";
712 LONG cmdlen = sizeof(cmd);
713 UINT retval = 31;
714
715 /* Get the application for the registry */
716 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
717 {
718 LPSTR tmp;
719 char param[256] = "";
720 LONG paramlen = 256;
721
722 /* Get the parameters needed by the application
723 from the associated ddeexec key */
724 tmp = strstr(key, "command");
725 assert(tmp);
726 strcpy(tmp, "ddeexec");
727
728 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
729 {
730 TRACE("Got ddeexec %s => %s\n", key, param);
731 retval = dde_connect(key, cmd, param, lpFile, env, sei, execfunc, szCommandline, pidl);
732 }
733 else
734 {
735 /* Is there a replace() function anywhere? */
736 cmd[cmdlen] = '\0';
737 argify(param, sizeof(param), cmd, lpFile, pidl, szCommandline);
738 retval = execfunc(param, env, sei, FALSE);
739 }
740 }
741 else TRACE("ooch\n");
742
743 return retval;
744 }
745
746 /*************************************************************************
747 * FindExecutableA [SHELL32.@]
748 */
749 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
750 {
751 UINT retval = 31; /* default - 'No association was found' */
752 char old_dir[1024];
753
754 TRACE("File %s, Dir %s\n",
755 (lpFile != NULL ? lpFile : "-"), (lpDirectory != NULL ? lpDirectory : "-"));
756
757 lpResult[0] = '\0'; /* Start off with an empty return string */
758
759 /* trap NULL parameters on entry */
760 if ((lpFile == NULL) || (lpResult == NULL))
761 {
762 /* FIXME - should throw a warning, perhaps! */
763 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
764 }
765
766 if (lpDirectory)
767 {
768 GetCurrentDirectoryA(sizeof(old_dir), old_dir);
769 SetCurrentDirectoryA(lpDirectory);
770 }
771
772 retval = SHELL_FindExecutable(lpDirectory, lpFile, "open", lpResult, NULL, NULL, NULL, NULL);
773
774 TRACE("returning %s\n", lpResult);
775 if (lpDirectory)
776 SetCurrentDirectoryA(old_dir);
777 return (HINSTANCE)retval;
778 }
779
780 /*************************************************************************
781 * FindExecutableW [SHELL32.@]
782 */
783 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
784 {
785 FIXME("(%p,%p,%p): stub\n", lpFile, lpDirectory, lpResult);
786 return (HINSTANCE)31; /* default - 'No association was found' */
787 }
788
789 /*************************************************************************
790 * ShellExecuteExA32 [Internal]
791 */
792 BOOL WINAPI ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
793 {
794 CHAR szApplicationName[MAX_PATH+2], szCommandline[MAX_PATH], fileName[MAX_PATH];
795 char res[MAX_PATH];
796 void *env;
797 char lpstrProtocol[256];
798 LPCSTR lpFile;
799 UINT retval = 31;
800 char cmd[1024];
801 const char* ext;
802 BOOL done;
803
804 LPITEMIDLIST pidl = sei->lpIDList;
805 LPITEMIDLIST tmpPidl = NULL;
806
807 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
808 sei->fMask, sei->hwnd, debugstr_a(sei->lpVerb),
809 debugstr_a(sei->lpFile), debugstr_a(sei->lpParameters),
810 debugstr_a(sei->lpDirectory), sei->nShow,
811 (sei->fMask & SEE_MASK_CLASSNAME) ? debugstr_a(sei->lpClass) : "not used");
812
813 sei->hProcess = NULL;
814 ZeroMemory(szApplicationName,MAX_PATH);
815 if (sei->lpFile)
816 strcpy(szApplicationName, sei->lpFile);
817
818 ZeroMemory(szCommandline,MAX_PATH);
819 if (sei->lpParameters)
820 strcpy(szCommandline, sei->lpParameters);
821
822 if (sei->fMask & (SEE_MASK_ICON | SEE_MASK_HOTKEY |
823 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
824 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
825 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
826 {
827 FIXME("flags ignored: 0x%08lx\n", sei->fMask);
828 }
829
830 /* process the IDList */
831 if (sei->fMask & SEE_MASK_INVOKEIDLIST) /* 0x0c: includes SEE_MASK_IDLIST */
832 {
833 if (!SHGetPathFromIDListA(pidl, szApplicationName))
834 return FALSE;
835
836 TRACE("-- idlist=%p (%s)\n", pidl, szApplicationName);
837 }
838
839 if (sei->fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
840 {
841 /* launch a document by fileclass like 'WordPad.Document.1' */
842 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
843 /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
844 if (sei->fMask & SEE_MASK_CLASSKEY)
845 HCR_GetExecuteCommandExA(sei->hkeyClass,
846 (sei->fMask & SEE_MASK_CLASSNAME) ? sei->lpClass: NULL,
847 (sei->lpVerb) ? sei->lpVerb : "open", szCommandline, sizeof(szCommandline));
848 else if (sei->fMask & SEE_MASK_CLASSNAME)
849 HCR_GetExecuteCommandA(sei->lpClass, (sei->lpVerb) ? sei->lpVerb :
850 "open", szCommandline, sizeof(szCommandline));
851
852 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
853 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline, szApplicationName);
854
855 cmd[0] = '\0';
856 done = argify(cmd, sizeof(cmd), szCommandline, szApplicationName, pidl, NULL);
857 if (!done && szApplicationName[0])
858 {
859 strcat(cmd, " ");
860 strcat(cmd, szApplicationName);
861 }
862 retval = execfunc(cmd, NULL, sei, FALSE);
863 if (retval > 32)
864 return TRUE;
865 else
866 return FALSE;
867 }
868
869 /* Else, try to execute the filename */
870 TRACE("execute:'%s','%s'\n", szApplicationName, szCommandline);
871
872 /* resolve shell shortcuts */
873 ext = PathFindExtensionA(szApplicationName);
874 if (ext && !strcasecmp(ext, ".lnk")) { /* or check for: shell_attribs & SFGAO_LINK */
875 WCHAR cmd[MAX_PATH], args[MAX_PATH];
876
877 if (MultiByteToWideChar(CP_ACP, 0, szApplicationName, -1, cmd, MAX_PATH)) {
878 if (SUCCEEDED(_ResolveShortCut(cmd, args, sei->hwnd, &sei->nShow, &tmpPidl))) {
879 if (!*cmd && tmpPidl) {
880 /* We got a PIDL instead of a file system path. */
881 IShellFolder* desktop;
882 STRRET str;
883
884 HRESULT hr = SHGetDesktopFolder(&desktop);
885
886 if (SUCCEEDED(hr)) {
887 hr = IShellFolder_GetDisplayNameOf(desktop, tmpPidl, SHGDN_FORPARSING, &str);
888
889 if (SUCCEEDED(hr)) {
890 hr = StrRetToStrNW(cmd, MAX_PATH, &str, tmpPidl);
891 tmpPidl = NULL;
892 }
893
894 IShellFolder_Release(desktop);
895 }
896
897 if (cmd[0]==':' && cmd[1]==':') {
898 /* open shell folder for the specified class GUID */
899 strcpy(szApplicationName, "explorer.exe");
900 WideCharToMultiByte(CP_ACP, 0, cmd, -1, szCommandline, MAX_PATH, NULL, NULL);
901
902 *cmd = '\0';
903 } else if (HCR_GetExecuteCommandA("Folder", sei->lpVerb? sei->lpVerb: "open", szCommandline, sizeof(szCommandline))) {
904 res[0] = '\0';
905
906 /*FIXME: seems to have problems in some cases */
907 if (argify(res, sizeof(res), szCommandline, NULL, tmpPidl, NULL))
908 strcpy(szApplicationName, res);
909
910 szCommandline[0] = '\0';
911 *cmd = '\0';
912 }
913 }
914
915 if (*cmd) {
916 WideCharToMultiByte(CP_ACP, 0, cmd, -1, szApplicationName, MAX_PATH, NULL, NULL);
917 WideCharToMultiByte(CP_ACP, 0, args, -1, szCommandline, MAX_PATH, NULL, NULL);
918 }
919
920 /* If we prevoiusly had a PIDL, it is now resolved, so forget it. */
921 pidl = tmpPidl;
922 } else
923 FIXME("We could not resolve the shell shortcut.\n");
924 }
925 }
926
927 /* The following code is needed for example to resolve a shortcut
928 to control panel applet "Keyboard", since this is accessed using
929 "rundll32.exe shell32.dll,Control_RunDLL %1,%*" with a command line
930 parameter received from ISF_ControlPanel_fnGetDisplayNameOf(). */
931 if (!*szCommandline) {
932 if (*szApplicationName == '"') {
933 LPCSTR src = szApplicationName + 1;
934 LPSTR dst = fileName;
935
936 while(*src && *src!='"')
937 *dst++ = *src++;
938
939 *dst = '\0';
940
941 if (*src == '"')
942 for(++src; isspace(*src); )
943 ++src;
944
945 strcpy(szCommandline, src);
946 }
947 else
948 {
949 LPSTR space, s;
950 char buffer[MAX_PATH], xlpFile[MAX_PATH];
951
952 LPSTR beg = szApplicationName;
953 for(s=beg; space=strchr(s, ' '); s=space+1) {
954 int idx = space-szApplicationName;
955 strncpy(buffer, szApplicationName, idx);
956 buffer[idx] = '\0';
957
958 if (SearchPathA(sei->lpDirectory, buffer, ".exe", sizeof(xlpFile), xlpFile, NULL))
959 {
960 /* separate out command from parameter string */
961 LPCSTR p = space + 1;
962
963 while(isspace(*p))
964 ++p;
965
966 strcpy(szCommandline, p);
967 *space = '\0';
968
969 break;
970 }
971 }
972
973 strcpy(fileName, szApplicationName);
974 }
975 } else
976 strcpy(fileName, szApplicationName);
977
978 lpFile = fileName;
979
980 if (szCommandline[0]) {
981 strcat(szApplicationName, " ");
982 strcat(szApplicationName, szCommandline);
983 }
984
985 retval = execfunc(szApplicationName, NULL, sei, FALSE);
986 if (retval > 32)
987 {
988 /* Now, that we have successfully launched a process, we can free the PIDL.
989 It may have been used before for %I command line options. */
990 if (tmpPidl)
991 SHFree(tmpPidl);
992
993 TRACE("execfunc: retval=%d sei->hInstApp=%p\n", retval, sei->hInstApp);
994 return TRUE;
995 }
996
997 /* Else, try to find the executable */
998 cmd[0] = '\0';
999 retval = SHELL_FindExecutable(sei->lpDirectory, lpFile, sei->lpVerb, cmd, lpstrProtocol, &env, pidl, szCommandline);
1000 if (retval > 32) /* Found */
1001 {
1002 CHAR szQuotedCmd[MAX_PATH+2];
1003 /* Must quote to handle case where cmd contains spaces,
1004 * else security hole if malicious user creates executable file "C:\\Program"
1005 *
1006 * FIXME: If we don't have set explicitly command line arguments, we must first
1007 * split executable path from optional command line arguments. Otherwise we would quote
1008 * the complete string with executable path _and_ arguments, which is not what we want.
1009 */
1010 if (szCommandline[0])
1011 sprintf(szQuotedCmd, "\"%s\" %s", cmd, szCommandline);
1012 else
1013 sprintf(szQuotedCmd, "\"%s\"", cmd);
1014 TRACE("%s/%s => %s/%s\n", szApplicationName, sei->lpVerb?sei->lpVerb:"NULL", szQuotedCmd, lpstrProtocol);
1015 if (*lpstrProtocol)
1016 retval = execute_from_key(lpstrProtocol, lpFile, env, sei, execfunc, szCommandline, pidl);
1017 else
1018 retval = execfunc(szQuotedCmd, env, sei, FALSE);
1019 if (env) HeapFree( GetProcessHeap(), 0, env );
1020 }
1021 else if (PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
1022 {
1023 LPSTR lpstrRes;
1024 INT iSize;
1025
1026 lpstrRes = strchr(lpFile, ':');
1027 if (lpstrRes)
1028 iSize = lpstrRes - lpFile;
1029 else
1030 iSize = strlen(lpFile);
1031
1032 TRACE("Got URL: %s\n", lpFile);
1033 /* Looking for ...protocol\shell\<verb>\command */
1034 strncpy(lpstrProtocol, lpFile, iSize);
1035 lpstrProtocol[iSize] = '\0';
1036 strcat(lpstrProtocol, "\\shell\\");
1037 strcat(lpstrProtocol, sei->lpVerb? sei->lpVerb: "open"); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1038 strcat(lpstrProtocol, "\\command");
1039
1040 /* Remove File Protocol from lpFile */
1041 /* In the case file://path/file */
1042 if (!strncasecmp(lpFile, "file", iSize))
1043 {
1044 lpFile += iSize;
1045 while (*lpFile == ':') lpFile++;
1046 }
1047 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei, execfunc, szCommandline, pidl);
1048 }
1049 /* Check if file specified is in the form www.??????.*** */
1050 else if (!strncasecmp(lpFile, "www", 3))
1051 {
1052 /* if so, append lpFile http:// and call ShellExecute */
1053 char lpstrTmpFile[256] = "http://" ;
1054 strcat(lpstrTmpFile, lpFile);
1055 retval = (UINT)ShellExecuteA(sei->hwnd, sei->lpVerb, lpstrTmpFile, NULL, NULL, 0);
1056 }
1057
1058 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1059 if (tmpPidl)
1060 SHFree(tmpPidl);
1061
1062 TRACE("ShellExecuteExA32 retval=%d\n", retval);
1063
1064 if (retval <= 32)
1065 {
1066 sei->hInstApp = (HINSTANCE)retval;
1067 return FALSE;
1068 }
1069
1070 sei->hInstApp = (HINSTANCE)33;
1071 return TRUE;
1072 }
1073
1074 /*************************************************************************
1075 * ShellExecuteA [SHELL32.290]
1076 */
1077 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1078 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1079 {
1080 SHELLEXECUTEINFOA sei;
1081 HANDLE hProcess = 0;
1082
1083 TRACE("\n");
1084 sei.cbSize = sizeof(sei);
1085 sei.fMask = 0;
1086 sei.hwnd = hWnd;
1087 sei.lpVerb = lpOperation;
1088 sei.lpFile = lpFile;
1089 sei.lpParameters = lpParameters;
1090 sei.lpDirectory = lpDirectory;
1091 sei.nShow = iShowCmd;
1092 sei.lpIDList = 0;
1093 sei.lpClass = 0;
1094 sei.hkeyClass = 0;
1095 sei.dwHotKey = 0;
1096 sei.hProcess = hProcess;
1097
1098 ShellExecuteExA32 (&sei, SHELL_ExecuteA);
1099 return sei.hInstApp;
1100 }
1101
1102 /*************************************************************************
1103 * ShellExecuteEx [SHELL32.291]
1104 *
1105 */
1106 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
1107 {
1108 if (SHELL_OsIsUnicode())
1109 return ShellExecuteExW (sei);
1110 return ShellExecuteExA32 (sei, SHELL_ExecuteA);
1111 }
1112
1113 /*************************************************************************
1114 * ShellExecuteExA [SHELL32.292]
1115 *
1116 */
1117 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1118 {
1119 BOOL ret = ShellExecuteExA32 (sei, SHELL_ExecuteA);
1120
1121 TRACE("ShellExecuteExA(): ret=%d\n", ret);
1122
1123 return ret;
1124 }
1125
1126 /*************************************************************************
1127 * ShellExecuteExW [SHELL32.293]
1128 *
1129 */
1130 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1131 {
1132 SHELLEXECUTEINFOA seiA;
1133 BOOL ret;
1134
1135 TRACE("%p\n", sei);
1136
1137 memcpy(&seiA, sei, sizeof(SHELLEXECUTEINFOA));
1138
1139 if (sei->lpVerb)
1140 seiA.lpVerb = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpVerb);
1141
1142 if (sei->lpFile)
1143 seiA.lpFile = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpFile);
1144
1145 if (sei->lpParameters)
1146 seiA.lpParameters = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpParameters);
1147
1148 if (sei->lpDirectory)
1149 seiA.lpDirectory = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpDirectory);
1150
1151 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
1152 seiA.lpClass = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpClass);
1153 else
1154 seiA.lpClass = NULL;
1155
1156 ret = ShellExecuteExA(&seiA);
1157
1158 if (seiA.lpVerb) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpVerb );
1159 if (seiA.lpFile) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpFile );
1160 if (seiA.lpParameters) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpParameters );
1161 if (seiA.lpDirectory) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpDirectory );
1162 if (seiA.lpClass) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpClass );
1163
1164 sei->hInstApp = seiA.hInstApp;
1165
1166 TRACE("ShellExecuteExW(): ret=%d\n", ret);
1167
1168 return ret;
1169 }
1170
1171 /*************************************************************************
1172 * ShellExecuteW [SHELL32.294]
1173 * from shellapi.h
1174 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1175 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1176 */
1177 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1178 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1179 {
1180 SHELLEXECUTEINFOW sei;
1181 HANDLE hProcess = 0;
1182 int ret;
1183
1184 TRACE("\n");
1185 sei.cbSize = sizeof(sei);
1186 sei.fMask = 0;
1187 sei.hwnd = hwnd;
1188 sei.lpVerb = lpOperation;
1189 sei.lpFile = lpFile;
1190 sei.lpParameters = lpParameters;
1191 sei.lpDirectory = lpDirectory;
1192 sei.nShow = nShowCmd;
1193 sei.lpIDList = 0;
1194 sei.lpClass = 0;
1195 sei.hkeyClass = 0;
1196 sei.dwHotKey = 0;
1197 sei.hProcess = hProcess;
1198
1199 ret = ShellExecuteExW(&sei);
1200
1201 TRACE("ShellExecuteW(): ret=%d module=%p", ret, sei.hInstApp);
1202 return sei.hInstApp;
1203 }