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
23 #include "wine/port.h"
44 #include "wine/winbase16.h"
45 #include "shell32_main.h"
46 #include "undocshell.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(exec);
52 /***********************************************************************
53 * this function is supposed to expand the escape sequences found in the registry
54 * some diving reported that the following were used:
55 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
60 * %I adress of a global item ID (explorer switch /idlist)
61 * %L seems to be %1 as long filename followed by the 8+3 variation
63 * %* all following parameters (see batfile)
65 static BOOL argify(char* res, int len, const char* fmt, const char* lpFile)
82 if (!done || (*fmt == '1'))
84 if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
87 res += strlen(xlpFile);
92 res += strlen(lpFile);
96 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
108 /*************************************************************************
109 * SHELL_ExecuteA [Internal]
112 static UINT SHELL_ExecuteA(char *lpCmd, LPSHELLEXECUTEINFOA sei, BOOL shWait)
114 STARTUPINFOA startup;
115 PROCESS_INFORMATION info;
118 TRACE("Execute %s from directory %s\n", lpCmd, sei->lpDirectory);
119 ZeroMemory(&startup,sizeof(STARTUPINFOA));
120 startup.cb = sizeof(STARTUPINFOA);
121 startup.dwFlags = STARTF_USESHOWWINDOW;
122 startup.wShowWindow = sei->nShow;
123 if (CreateProcessA(NULL, lpCmd, NULL, NULL, FALSE, 0,
124 NULL, sei->lpDirectory, &startup, &info))
126 /* Give 30 seconds to the app to come up, if desired. Probably only needed
127 when starting app immediately before making a DDE connection. */
129 if (WaitForInputIdle( info.hProcess, 30000 ) == -1)
130 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
132 if(sei->fMask & SEE_MASK_NOCLOSEPROCESS)
133 sei->hProcess = info.hProcess;
135 CloseHandle( info.hProcess );
136 CloseHandle( info.hThread );
138 else if ((retval = GetLastError()) >= 32)
140 FIXME("Strange error set by CreateProcess: %d\n", retval);
141 retval = ERROR_BAD_FORMAT;
144 sei->hInstApp = (HINSTANCE)retval;
148 /*************************************************************************
149 * SHELL_FindExecutable [Internal]
151 * Utility for code sharing between FindExecutable and ShellExecute
153 * lpFile the name of a file
154 * lpOperation the operation on it (open)
156 * lpResult a buffer, big enough :-(, to store the command to do the
157 * operation on the file
158 * key a buffer, big enough, to get the key name to do actually the
159 * command (it'll be used afterwards for more information
162 static UINT SHELL_FindExecutable(LPCSTR lpPath, LPCSTR lpFile, LPCSTR lpOperation,
163 LPSTR lpResult, LPSTR key)
165 char *extension = NULL; /* pointer to file extension */
166 char tmpext[5]; /* local copy to mung as we please */
167 char filetype[256]; /* registry name for this filetype */
168 LONG filetypelen = 256; /* length of above */
169 char command[256]; /* command from registry */
170 LONG commandlen = 256; /* This is the most DOS can handle :) */
171 char buffer[256]; /* Used to GetProfileString */
172 UINT retval = 31; /* default - 'No association was found' */
173 char *tok; /* token pointer */
174 char xlpFile[256] = ""; /* result of SearchPath */
176 TRACE("%s\n", (lpFile != NULL) ? lpFile : "-");
178 lpResult[0] = '\0'; /* Start off with an empty return string */
180 /* trap NULL parameters on entry */
181 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
183 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
184 lpFile, lpOperation, lpResult);
185 return 2; /* File not found. Close enough, I guess. */
188 if (SearchPathA(lpPath, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
190 TRACE("SearchPathA returned non-zero\n");
194 /* First thing we need is the file's extension */
195 extension = strrchr(xlpFile, '.'); /* Assume last "." is the one; */
196 /* File->Run in progman uses */
198 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
200 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
202 WARN("Returning 31 - No association\n");
203 return 31; /* no association */
206 /* Make local copy & lowercase it for reg & 'programs=' lookup */
207 lstrcpynA(tmpext, extension, 5);
209 TRACE("%s file\n", tmpext);
211 /* Three places to check: */
212 /* 1. win.ini, [windows], programs (NB no leading '.') */
213 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
214 /* 3. win.ini, [extensions], extension (NB no leading '.' */
215 /* All I know of the order is that registry is checked before */
216 /* extensions; however, it'd make sense to check the programs */
217 /* section first, so that's what happens here. */
219 if (key) *key = '\0';
221 /* See if it's a program - if GetProfileString fails, we skip this
222 * section. Actually, if GetProfileString fails, we've probably
223 * got a lot more to worry about than running a program... */
224 if (GetProfileStringA("windows", "programs", "exe pif bat com",
225 buffer, sizeof(buffer)) > 0)
229 for (i = 0;i<strlen(buffer); i++) buffer[i] = tolower(buffer[i]);
231 tok = strtok(buffer, " \t"); /* ? */
234 if (strcmp(tok, &tmpext[1]) == 0) /* have to skip the leading "." */
236 strcpy(lpResult, xlpFile);
237 /* Need to perhaps check that the file has a path
239 TRACE("found %s\n", lpResult);
242 /* Greater than 32 to indicate success FIXME According to the
243 * docs, I should be returning a handle for the
244 * executable. Does this mean I'm supposed to open the
245 * executable file or something? More RTFM, I guess... */
247 tok = strtok(NULL, " \t");
252 if (RegQueryValueA(HKEY_CLASSES_ROOT, tmpext, filetype,
253 &filetypelen) == ERROR_SUCCESS)
255 filetype[filetypelen] = '\0';
256 TRACE("File type: %s\n", filetype);
258 /* Looking for ...buffer\shell\lpOperation\command */
259 strcat(filetype, "\\shell\\");
260 strcat(filetype, lpOperation);
261 strcat(filetype, "\\command");
263 if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, command,
264 &commandlen) == ERROR_SUCCESS)
266 if (key) strcpy(key, filetype);
272 /* FIXME: it seems all Windows version don't behave the same here.
273 * the doc states that this ddeexec information can be found after
275 * on Win98, it doesn't appear, but I think it does on Win2k
277 /* Get the parameters needed by the application
278 from the associated ddeexec key */
279 tmp = strstr(filetype, "command");
281 strcat(filetype, "ddeexec");
283 if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, param, ¶mlen) == ERROR_SUCCESS)
285 strcat(command, " ");
286 strcat(command, param);
287 commandlen += paramlen;
290 command[commandlen] = '\0';
291 argify(lpResult, sizeof(lpResult), command, xlpFile);
292 retval = 33; /* FIXME see above */
295 else /* Check win.ini */
297 /* Toss the leading dot */
299 if (GetProfileStringA("extensions", extension, "", command,
300 sizeof(command)) > 0)
302 if (strlen(command) != 0)
304 strcpy(lpResult, command);
305 tok = strstr(lpResult, "^"); /* should be ^.extension? */
309 strcat(lpResult, xlpFile); /* what if no dir in xlpFile? */
310 tok = strstr(command, "^"); /* see above */
311 if ((tok != NULL) && (strlen(tok)>5))
313 strcat(lpResult, &tok[5]);
316 retval = 33; /* FIXME - see above */
321 TRACE("returning %s\n", lpResult);
325 /******************************************************************
328 * callback for the DDE connection. not really usefull
330 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
332 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
337 /******************************************************************
340 * ShellExecute helper. Used to do an operation with a DDE connection
342 * Handles both the direct connection (try #1), and if it fails,
343 * launching an application and trying (#2) to connect to it
346 static unsigned dde_connect(char* key, char* start, char* ddeexec,
348 LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
350 char* endkey = key + strlen(key);
351 char app[256], topic[256], ifexec[256], res[256];
352 LONG applen, topiclen, ifexeclen;
356 HSZ hszApp, hszTopic;
360 strcpy(endkey, "\\application");
361 applen = sizeof(app);
362 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
364 FIXME("default app name NIY %s\n", key);
368 strcpy(endkey, "\\topic");
369 topiclen = sizeof(topic);
370 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
372 strcpy(topic, "System");
375 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
380 hszApp = DdeCreateStringHandleA(ddeInst, app, CP_WINANSI);
381 hszTopic = DdeCreateStringHandleA(ddeInst, topic, CP_WINANSI);
383 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
387 TRACE("Launching '%s'\n", start);
388 ret = execfunc(start, sei, TRUE);
391 TRACE("Couldn't launch\n");
394 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
397 TRACE("Couldn't connect. ret=%d\n", ret);
398 ret = 30; /* whatever */
401 strcpy(endkey, "\\ifexec");
402 ifexeclen = sizeof(ifexec);
403 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
409 argify(res, sizeof(res), exec, lpFile);
410 TRACE("%s %s => %s\n", exec, lpFile, res);
412 ret = (DdeClientTransaction(res, strlen(res) + 1, hConv, 0L, 0,
413 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
414 DdeDisconnect(hConv);
416 DdeUninitialize(ddeInst);
420 /*************************************************************************
421 * execute_from_key [Internal]
423 static UINT execute_from_key(LPSTR key, LPCSTR lpFile, LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
426 LONG cmdlen = sizeof(cmd);
429 /* Get the application for the registry */
430 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
433 char param[256] = "";
436 /* Get the parameters needed by the application
437 from the associated ddeexec key */
438 tmp = strstr(key, "command");
440 strcpy(tmp, "ddeexec");
442 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, param, ¶mlen) == ERROR_SUCCESS)
444 TRACE("Got ddeexec %s => %s\n", key, param);
445 retval = dde_connect(key, cmd, param, lpFile, sei, execfunc);
449 /* Is there a replace() function anywhere? */
451 argify(param, sizeof(param), cmd, lpFile);
452 retval = execfunc(param, sei, FALSE);
455 else TRACE("ooch\n");
460 /*************************************************************************
461 * FindExecutableA [SHELL32.@]
463 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
465 UINT retval = 31; /* default - 'No association was found' */
468 TRACE("File %s, Dir %s\n",
469 (lpFile != NULL ? lpFile : "-"), (lpDirectory != NULL ? lpDirectory : "-"));
471 lpResult[0] = '\0'; /* Start off with an empty return string */
473 /* trap NULL parameters on entry */
474 if ((lpFile == NULL) || (lpResult == NULL))
476 /* FIXME - should throw a warning, perhaps! */
477 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
482 GetCurrentDirectoryA(sizeof(old_dir), old_dir);
483 SetCurrentDirectoryA(lpDirectory);
486 retval = SHELL_FindExecutable(lpDirectory, lpFile, "open", lpResult, NULL);
488 TRACE("returning %s\n", lpResult);
490 SetCurrentDirectoryA(old_dir);
491 return (HINSTANCE)retval;
494 /*************************************************************************
495 * FindExecutableW [SHELL32.@]
497 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
499 FIXME("(%p,%p,%p): stub\n", lpFile, lpDirectory, lpResult);
500 return (HINSTANCE)31; /* default - 'No association was found' */
503 /*************************************************************************
504 * ShellExecuteExA32 [Internal]
506 BOOL WINAPI ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
508 CHAR szApplicationName[MAX_PATH],szCommandline[MAX_PATH],szPidl[20],fileName[MAX_PATH];
511 char lpstrProtocol[256];
512 LPCSTR lpFile,lpOperation;
517 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
518 sei->fMask, sei->hwnd, debugstr_a(sei->lpVerb),
519 debugstr_a(sei->lpFile), debugstr_a(sei->lpParameters),
520 debugstr_a(sei->lpDirectory), sei->nShow,
521 (sei->fMask & SEE_MASK_CLASSNAME) ? debugstr_a(sei->lpClass) : "not used");
523 sei->hProcess = (HANDLE)NULL;
524 ZeroMemory(szApplicationName,MAX_PATH);
526 strcpy(szApplicationName, sei->lpFile);
528 ZeroMemory(szCommandline,MAX_PATH);
529 if (sei->lpParameters)
530 strcpy(szCommandline, sei->lpParameters);
532 if (sei->fMask & ((SEE_MASK_CLASSKEY & ~SEE_MASK_CLASSNAME) |
533 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
534 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
535 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
536 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
538 FIXME("flags ignored: 0x%08lx\n", sei->fMask);
541 /* process the IDList */
542 if ( (sei->fMask & SEE_MASK_INVOKEIDLIST) == SEE_MASK_INVOKEIDLIST) /*0x0c*/
544 SHGetPathFromIDListA (sei->lpIDList,szApplicationName);
545 TRACE("-- idlist=%p (%s)\n", sei->lpIDList, szApplicationName);
549 if (sei->fMask & SEE_MASK_IDLIST )
551 pos = strstr(szCommandline, "%I");
555 HGLOBAL hmem = SHAllocShared ( sei->lpIDList, ILGetSize(sei->lpIDList), 0);
556 pv = SHLockShared(hmem,0);
557 sprintf(szPidl,":%p",pv );
560 gap = strlen(szPidl);
562 memmove(pos+gap,pos+2,len);
563 memcpy(pos,szPidl,gap);
568 if (sei->fMask & SEE_MASK_CLASSNAME)
570 /* launch a document by fileclass like 'WordPad.Document.1' */
571 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
572 /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
573 HCR_GetExecuteCommandA(sei->lpClass, (sei->lpVerb) ? sei->lpVerb : "open", szCommandline, sizeof(szCommandline));
574 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
575 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline, szApplicationName);
578 done = argify(cmd, sizeof(cmd), szCommandline, szApplicationName);
579 if (!done && szApplicationName[0])
582 strcat(cmd, szApplicationName);
584 retval = execfunc(cmd, sei, FALSE);
591 /* We set the default to open, and that should generally work.
592 But that is not really the way the MS docs say to do it. */
593 if (sei->lpVerb == NULL)
594 lpOperation = "open";
596 lpOperation = sei->lpVerb;
598 /* Else, try to execute the filename */
599 TRACE("execute:'%s','%s'\n",szApplicationName, szCommandline);
601 strcpy(fileName, szApplicationName);
603 if (szCommandline[0]) {
604 strcat(szApplicationName, " ");
605 strcat(szApplicationName, szCommandline);
608 retval = execfunc(szApplicationName, sei, FALSE);
612 /* Else, try to find the executable */
614 retval = SHELL_FindExecutable(sei->lpDirectory, lpFile, lpOperation, cmd, lpstrProtocol);
615 if (retval > 32) /* Found */
617 if (szCommandline[0]) {
619 strcat(cmd, szCommandline);
621 TRACE("%s/%s => %s/%s\n", szApplicationName, lpOperation, cmd, lpstrProtocol);
623 retval = execute_from_key(lpstrProtocol, szApplicationName, sei, execfunc);
625 retval = execfunc(cmd, sei, FALSE);
627 else if (PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
632 lpstrRes = strchr(lpFile, ':');
634 iSize = lpstrRes - lpFile;
636 iSize = strlen(lpFile);
638 TRACE("Got URL: %s\n", lpFile);
639 /* Looking for ...protocol\shell\lpOperation\command */
640 strncpy(lpstrProtocol, lpFile, iSize);
641 lpstrProtocol[iSize] = '\0';
642 strcat(lpstrProtocol, "\\shell\\");
643 strcat(lpstrProtocol, lpOperation);
644 strcat(lpstrProtocol, "\\command");
646 /* Remove File Protocol from lpFile */
647 /* In the case file://path/file */
648 if (!strncasecmp(lpFile, "file", iSize))
651 while (*lpFile == ':') lpFile++;
653 retval = execute_from_key(lpstrProtocol, lpFile, sei, execfunc);
655 /* Check if file specified is in the form www.??????.*** */
656 else if (!strncasecmp(lpFile, "www", 3))
658 /* if so, append lpFile http:// and call ShellExecute */
659 char lpstrTmpFile[256] = "http://" ;
660 strcat(lpstrTmpFile, lpFile);
661 retval = (UINT)ShellExecuteA(sei->hwnd, lpOperation, lpstrTmpFile, NULL, NULL, 0);
666 sei->hInstApp = (HINSTANCE)retval;
670 sei->hInstApp = (HINSTANCE)33;
674 /*************************************************************************
675 * ShellExecuteA [SHELL32.290]
677 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
678 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
680 SHELLEXECUTEINFOA sei;
684 sei.cbSize = sizeof(sei);
687 sei.lpVerb = lpOperation;
689 sei.lpParameters = lpParameters;
690 sei.lpDirectory = lpDirectory;
691 sei.nShow = iShowCmd;
696 sei.hProcess = hProcess;
698 ShellExecuteExA32 (&sei, SHELL_ExecuteA);
702 /*************************************************************************
703 * ShellExecuteEx [SHELL32.291]
706 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
708 if (SHELL_OsIsUnicode())
709 return ShellExecuteExW (sei);
710 return ShellExecuteExA32 (sei, SHELL_ExecuteA);
713 /*************************************************************************
714 * ShellExecuteExA [SHELL32.292]
717 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
719 return ShellExecuteExA32 (sei, SHELL_ExecuteA);
722 /*************************************************************************
723 * ShellExecuteExW [SHELL32.293]
726 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
728 SHELLEXECUTEINFOA seiA;
733 memcpy(&seiA, sei, sizeof(SHELLEXECUTEINFOA));
736 seiA.lpVerb = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpVerb);
739 seiA.lpFile = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpFile);
741 if (sei->lpParameters)
742 seiA.lpParameters = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpParameters);
744 if (sei->lpDirectory)
745 seiA.lpDirectory = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpDirectory);
747 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
748 seiA.lpClass = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpClass);
752 ret = ShellExecuteExA(&seiA);
754 if (seiA.lpVerb) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpVerb );
755 if (seiA.lpFile) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpFile );
756 if (seiA.lpParameters) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpParameters );
757 if (seiA.lpDirectory) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpDirectory );
758 if (seiA.lpClass) HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpClass );
763 /*************************************************************************
764 * ShellExecuteW [SHELL32.294]
766 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
767 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
769 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
770 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
772 SHELLEXECUTEINFOW sei;
776 sei.cbSize = sizeof(sei);
779 sei.lpVerb = lpOperation;
781 sei.lpParameters = lpParameters;
782 sei.lpDirectory = lpDirectory;
783 sei.nShow = nShowCmd;
788 sei.hProcess = hProcess;
790 ShellExecuteExW (&sei);