Fixed ShellExecute functions when ddeexec was present in registry.
[wine.git] / dlls / shell32 / shlexec.c
blob8002ee733d331b7c9051de16c63453def5a1aab1
1 /*
2 * Shell Library Functions
4 * 1998 Marcus Meissner
5 * 2002 Eric Pouech
6 */
8 #include "config.h"
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <assert.h>
16 #include "windef.h"
17 #include "winerror.h"
18 #include "winreg.h"
19 #include "shellapi.h"
20 #include "shlobj.h"
21 #include "shlwapi.h"
22 #include "ddeml.h"
24 #include "wine/winbase16.h"
25 #include "shell32_main.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(exec);
31 /* this function is supposed to expand the escape sequences found in the registry
32 * some diving reported that the following were used:
33 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
34 + + %* seem to report to all parameter (or all remaining, ie after removing
35 * the already used %1 %2...)
36 * + %L seems to be %1 as long filename followed by the 8+3 variation
37 * + %l unknown
38 * + %S unknown
39 * + %I unknown
41 static void argify(char* res, int len, const char* fmt, const char* lpFile)
43 char xlpFile[1024];
45 while (*fmt)
47 if (*fmt == '%')
49 switch (*++fmt)
51 case '\0':
52 case '%':
53 *res++ = '%';
54 break;
55 case '1':
56 if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
58 strcpy(res, xlpFile);
59 res += strlen(xlpFile);
61 else
63 strcpy(res, lpFile);
64 res += strlen(lpFile);
66 break;
67 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
69 fmt++;
71 else
72 *res++ = *fmt++;
74 *res = '\0';
77 /*************************************************************************
78 * SHELL_FindExecutable [Internal]
80 * Utility for code sharing between FindExecutable and ShellExecute
81 * in:
82 * lpFile the name of a file
83 * lpOperation the operation on it (open)
84 * out:
85 * lpResult a buffer, big enough :-(, to store the command to do the
86 * operation on the file
87 * key a buffer, big enough, to get the key name to do actually the
88 * command (it'll be used afterwards for more information
89 * on the operation)
91 static HINSTANCE SHELL_FindExecutable(LPCSTR lpFile, LPCSTR lpOperation,
92 LPSTR lpResult, LPSTR key)
94 char *extension = NULL; /* pointer to file extension */
95 char tmpext[5]; /* local copy to mung as we please */
96 char filetype[256]; /* registry name for this filetype */
97 LONG filetypelen = 256; /* length of above */
98 char command[256]; /* command from registry */
99 LONG commandlen = 256; /* This is the most DOS can handle :) */
100 char buffer[256]; /* Used to GetProfileString */
101 HINSTANCE retval = 31; /* default - 'No association was found' */
102 char *tok; /* token pointer */
103 int i; /* random counter */
104 char xlpFile[256] = ""; /* result of SearchPath */
106 TRACE("%s\n", (lpFile != NULL) ? lpFile : "-");
108 lpResult[0] = '\0'; /* Start off with an empty return string */
110 /* trap NULL parameters on entry */
111 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
113 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
114 lpFile, lpOperation, lpResult);
115 return 2; /* File not found. Close enough, I guess. */
118 if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
120 TRACE("SearchPathA returned non-zero\n");
121 lpFile = xlpFile;
124 /* First thing we need is the file's extension */
125 extension = strrchr(xlpFile, '.'); /* Assume last "." is the one; */
126 /* File->Run in progman uses */
127 /* .\FILE.EXE :( */
128 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
130 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
132 WARN("Returning 31 - No association\n");
133 return 31; /* no association */
136 /* Make local copy & lowercase it for reg & 'programs=' lookup */
137 lstrcpynA(tmpext, extension, 5);
138 CharLowerA(tmpext);
139 TRACE("%s file\n", tmpext);
141 /* Three places to check: */
142 /* 1. win.ini, [windows], programs (NB no leading '.') */
143 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
144 /* 3. win.ini, [extensions], extension (NB no leading '.' */
145 /* All I know of the order is that registry is checked before */
146 /* extensions; however, it'd make sense to check the programs */
147 /* section first, so that's what happens here. */
149 if (key) *key = '\0';
151 /* See if it's a program - if GetProfileString fails, we skip this
152 * section. Actually, if GetProfileString fails, we've probably
153 * got a lot more to worry about than running a program... */
154 if (GetProfileStringA("windows", "programs", "exe pif bat com",
155 buffer, sizeof(buffer)) > 0)
157 for (i = 0;i<strlen(buffer); i++) buffer[i] = tolower(buffer[i]);
159 tok = strtok(buffer, " \t"); /* ? */
160 while (tok!= NULL)
162 if (strcmp(tok, &tmpext[1]) == 0) /* have to skip the leading "." */
164 strcpy(lpResult, xlpFile);
165 /* Need to perhaps check that the file has a path
166 * attached */
167 TRACE("found %s\n", lpResult);
168 return 33;
170 /* Greater than 32 to indicate success FIXME According to the
171 * docs, I should be returning a handle for the
172 * executable. Does this mean I'm supposed to open the
173 * executable file or something? More RTFM, I guess... */
175 tok = strtok(NULL, " \t");
179 /* Check registry */
180 if (RegQueryValueA(HKEY_CLASSES_ROOT, tmpext, filetype,
181 &filetypelen) == ERROR_SUCCESS)
183 filetype[filetypelen] = '\0';
184 TRACE("File type: %s\n", filetype);
186 /* Looking for ...buffer\shell\lpOperation\command */
187 strcat(filetype, "\\shell\\");
188 strcat(filetype, lpOperation);
189 strcat(filetype, "\\command");
191 if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, command,
192 &commandlen) == ERROR_SUCCESS)
194 if (key) strcpy(key, filetype);
195 #if 0
196 LPSTR tmp;
197 char param[256];
198 LONG paramlen = 256;
200 /* FIXME: it seems all Windows version don't behave the same here.
201 * the doc states that this ddeexec information can be found after
202 * the exec names.
203 * on Win98, it doesn't appear, but I think it does on Win2k
205 /* Get the parameters needed by the application
206 from the associated ddeexec key */
207 tmp = strstr(filetype, "command");
208 tmp[0] = '\0';
209 strcat(filetype, "ddeexec");
211 if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, param, &paramlen) == ERROR_SUCCESS)
213 strcat(command, " ");
214 strcat(command, param);
215 commandlen += paramlen;
217 #endif
218 command[commandlen] = '\0';
219 argify(lpResult, sizeof(lpResult), command, xlpFile);
220 retval = 33; /* FIXME see above */
223 else /* Check win.ini */
225 /* Toss the leading dot */
226 extension++;
227 if (GetProfileStringA("extensions", extension, "", command,
228 sizeof(command)) > 0)
230 if (strlen(command) != 0)
232 strcpy(lpResult, command);
233 tok = strstr(lpResult, "^"); /* should be ^.extension? */
234 if (tok != NULL)
236 tok[0] = '\0';
237 strcat(lpResult, xlpFile); /* what if no dir in xlpFile? */
238 tok = strstr(command, "^"); /* see above */
239 if ((tok != NULL) && (strlen(tok)>5))
241 strcat(lpResult, &tok[5]);
244 retval = 33; /* FIXME - see above */
249 TRACE("returning %s\n", lpResult);
250 return retval;
253 /******************************************************************
254 * dde_cb
256 * callback for the DDE connection. not really usefull
258 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
259 HSZ hsz1, HSZ hsz2,
260 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
262 return (HDDEDATA)0;
265 /******************************************************************
266 * dde_connect
268 * ShellExecute helper. Used to do an operation with a DDE connection
270 * Handles both the direct connection (try #1), and if it fails,
271 * launching an application and trying (#2) to connect to it
274 static unsigned dde_connect(char* key, char* start, char* ddeexec,
275 const char* lpFile,
276 int iCmdShow, BOOL is32)
278 char* endkey = key + strlen(key);
279 char app[256], topic[256], ifexec[256], res[256];
280 LONG applen, topiclen, ifexeclen;
281 char* exec;
282 DWORD ddeInst = 0;
283 DWORD tid;
284 HSZ hszApp, hszTopic;
285 HCONV hConv;
286 unsigned ret = 31;
288 strcpy(endkey, "\\application");
289 applen = sizeof(app);
290 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
292 FIXME("default app name NIY %s\n", key);
293 return 2;
296 strcpy(endkey, "\\topic");
297 topiclen = sizeof(topic);
298 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
300 strcpy(topic, "System");
303 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
305 return 2;
308 hszApp = DdeCreateStringHandleA(ddeInst, app, CP_WINANSI);
309 hszTopic = DdeCreateStringHandleA(ddeInst, topic, CP_WINANSI);
311 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
312 exec = ddeexec;
313 if (!hConv)
315 TRACE("Launching '%s'\n", start);
316 ret = (is32) ? WinExec(start, iCmdShow) : WinExec16(start, iCmdShow);
317 if (ret < 32)
319 TRACE("Couldn't launch\n");
320 goto error;
322 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
323 if (!hConv)
325 ret = 30; /* whatever */
326 goto error;
328 strcpy(endkey, "\\ifexec");
329 ifexeclen = sizeof(ifexec);
330 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
332 exec = ifexec;
336 argify(res, sizeof(res), exec, lpFile);
337 TRACE("%s %s => %s\n", exec, lpFile, res);
339 ret = (DdeClientTransaction(res, strlen(res) + 1, hConv, 0L, 0,
340 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 32;
341 DdeDisconnect(hConv);
342 error:
343 DdeUninitialize(ddeInst);
344 return ret;
347 static HINSTANCE execute_from_key(LPSTR key, LPCSTR lpFile, INT iShowCmd, BOOL is32)
349 char cmd[1024] = "";
350 LONG cmdlen = sizeof(cmd);
351 HINSTANCE retval = 31;
353 /* Get the application for the registry */
354 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
356 LPSTR tmp;
357 char param[256] = "";
358 LONG paramlen = 256;
360 /* Get the parameters needed by the application
361 from the associated ddeexec key */
362 tmp = strstr(key, "command");
363 assert(tmp);
364 strcpy(tmp, "ddeexec");
366 if (RegQueryValueA(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
368 TRACE("Got ddeexec %s => %s\n", key, param);
369 retval = dde_connect(key, cmd, param, lpFile, iShowCmd, is32);
371 else
373 /* Is there a replace() function anywhere? */
374 cmd[cmdlen] = '\0';
375 argify(param, sizeof(param), cmd, lpFile);
377 retval = (is32) ? WinExec(param, iShowCmd) : WinExec16(param, iShowCmd);
380 else TRACE("ooch\n");
382 return retval;
385 static HINSTANCE SHELL_Execute(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
386 LPCSTR lpParameters, LPCSTR lpDirectory,
387 INT iShowCmd, BOOL is32)
389 HINSTANCE retval = 31;
390 char old_dir[1024];
391 char cmd[1024];
393 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
394 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
395 lpParameters ? lpParameters : "<null>",
396 lpDirectory ? lpDirectory : "<null>", iShowCmd);
398 if (lpFile == NULL) return 0; /* should not happen */
399 if (lpOperation == NULL) /* default is open */
400 lpOperation = "open";
402 if (lpDirectory)
404 GetCurrentDirectoryA(sizeof(old_dir), old_dir);
405 SetCurrentDirectoryA(lpDirectory);
408 /* First try to execute lpFile with lpParameters directly */
409 strcpy(cmd, lpFile);
410 if (lpParameters)
412 strcat(cmd, " ");
413 strcat(cmd, lpParameters);
416 retval = (is32) ? WinExec(cmd, iShowCmd) : WinExec16(cmd, iShowCmd);
418 /* Unable to execute lpFile directly
419 Check if we can match an application to lpFile */
420 if (retval < 32)
422 char lpstrProtocol[256];
424 cmd[0] = '\0';
425 retval = SHELL_FindExecutable(lpFile, lpOperation, cmd, lpstrProtocol);
427 if (retval > 32) /* Found */
429 TRACE("%s/%s => %s/%s\n", lpFile, lpOperation, cmd, lpstrProtocol);
430 if (*lpstrProtocol)
431 retval = execute_from_key(lpstrProtocol, lpFile, iShowCmd, is32);
432 else
433 retval = (is32) ? WinExec(cmd, iShowCmd) : WinExec16(cmd, iShowCmd);
435 else if (PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
437 LPSTR lpstrRes;
438 INT iSize;
440 lpstrRes = strchr(lpFile, ':');
441 iSize = lpstrRes - lpFile;
443 TRACE("Got URL: %s\n", lpFile);
444 /* Looking for ...protocol\shell\lpOperation\command */
445 strncpy(lpstrProtocol, lpFile, iSize);
446 lpstrProtocol[iSize] = '\0';
447 strcat(lpstrProtocol, "\\shell\\");
448 strcat(lpstrProtocol, lpOperation);
449 strcat(lpstrProtocol, "\\command");
451 /* Remove File Protocol from lpFile */
452 /* In the case file://path/file */
453 if (!strncasecmp(lpFile, "file", iSize))
455 lpFile += iSize;
456 while (*lpFile == ':') lpFile++;
459 retval = execute_from_key(lpstrProtocol, lpFile, iShowCmd, is32);
461 /* Check if file specified is in the form www.??????.*** */
462 else if (!strncasecmp(lpFile, "www", 3))
464 /* if so, append lpFile http:// and call ShellExecute */
465 char lpstrTmpFile[256] = "http://" ;
466 strcat(lpstrTmpFile, lpFile);
467 retval = ShellExecuteA(hWnd, lpOperation, lpstrTmpFile, NULL, NULL, 0);
470 if (lpDirectory)
471 SetCurrentDirectoryA(old_dir);
472 return retval;
475 /*************************************************************************
476 * FindExecutableA [SHELL32.@]
478 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
480 HINSTANCE retval = 31; /* default - 'No association was found' */
481 char old_dir[1024];
483 TRACE("File %s, Dir %s\n",
484 (lpFile != NULL ? lpFile : "-"), (lpDirectory != NULL ? lpDirectory : "-"));
486 lpResult[0] = '\0'; /* Start off with an empty return string */
488 /* trap NULL parameters on entry */
489 if ((lpFile == NULL) || (lpResult == NULL))
491 /* FIXME - should throw a warning, perhaps! */
492 return 2; /* File not found. Close enough, I guess. */
495 if (lpDirectory)
497 GetCurrentDirectoryA(sizeof(old_dir), old_dir);
498 SetCurrentDirectoryA(lpDirectory);
501 retval = SHELL_FindExecutable(lpFile, "open", lpResult, NULL);
503 TRACE("returning %s\n", lpResult);
504 if (lpDirectory)
505 SetCurrentDirectoryA(old_dir);
506 return retval;
509 /*************************************************************************
510 * FindExecutableW [SHELL32.@]
512 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
514 FIXME("(%p,%p,%p): stub\n", lpFile, lpDirectory, lpResult);
515 return 31; /* default - 'No association was found' */
518 /*************************************************************************
519 * ShellExecute [SHELL.20]
521 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
522 LPCSTR lpFile, LPCSTR lpParameters,
523 LPCSTR lpDirectory, INT16 iShowCmd )
525 return (HINSTANCE16)SHELL_Execute(hWnd, lpOperation, lpFile,
526 lpParameters, lpDirectory, iShowCmd, FALSE );
529 /*************************************************************************
530 * ShellExecuteA [SHELL32.290]
532 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
533 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
535 TRACE("\n");
536 return SHELL_Execute( hWnd, lpOperation, lpFile, lpParameters,
537 lpDirectory, iShowCmd, TRUE );
540 /*************************************************************************
541 * ShellExecuteW [SHELL32.294]
542 * from shellapi.h
543 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
544 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
546 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
547 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
549 FIXME(": stub\n");
550 return 0;