2 * Shell Library Functions
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
41 static void argify(char* res
, int len
, const char* fmt
, const char* lpFile
)
56 if (SearchPathA(NULL
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
59 res
+= strlen(xlpFile
);
64 res
+= strlen(lpFile
);
67 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
77 /*************************************************************************
78 * SHELL_FindExecutable [Internal]
80 * Utility for code sharing between FindExecutable and ShellExecute
82 * lpFile the name of a file
83 * lpOperation the operation on it (open)
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
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");
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 */
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);
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"); /* ? */
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
167 TRACE("found %s\n", lpResult
);
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");
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
);
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
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");
209 strcat(filetype
, "ddeexec");
211 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, param
, ¶mlen
) == ERROR_SUCCESS
)
213 strcat(command
, " ");
214 strcat(command
, param
);
215 commandlen
+= paramlen
;
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 */
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? */
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
);
253 /******************************************************************
256 * callback for the DDE connection. not really usefull
258 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
260 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
265 /******************************************************************
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
,
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
;
284 HSZ hszApp
, hszTopic
;
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
);
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
)
308 hszApp
= DdeCreateStringHandleA(ddeInst
, app
, CP_WINANSI
);
309 hszTopic
= DdeCreateStringHandleA(ddeInst
, topic
, CP_WINANSI
);
311 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
315 TRACE("Launching '%s'\n", start
);
316 ret
= (is32
) ? WinExec(start
, iCmdShow
) : WinExec16(start
, iCmdShow
);
319 TRACE("Couldn't launch\n");
322 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
325 ret
= 30; /* whatever */
328 strcpy(endkey
, "\\ifexec");
329 ifexeclen
= sizeof(ifexec
);
330 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
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
);
343 DdeUninitialize(ddeInst
);
347 static HINSTANCE
execute_from_key(LPSTR key
, LPCSTR lpFile
, INT iShowCmd
, BOOL is32
)
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
)
357 char param
[256] = "";
360 /* Get the parameters needed by the application
361 from the associated ddeexec key */
362 tmp
= strstr(key
, "command");
364 strcpy(tmp
, "ddeexec");
366 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
368 TRACE("Got ddeexec %s => %s\n", key
, param
);
369 retval
= dde_connect(key
, cmd
, param
, lpFile
, iShowCmd
, is32
);
373 /* Is there a replace() function anywhere? */
375 argify(param
, sizeof(param
), cmd
, lpFile
);
377 retval
= (is32
) ? WinExec(param
, iShowCmd
) : WinExec16(param
, iShowCmd
);
380 else TRACE("ooch\n");
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;
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";
404 GetCurrentDirectoryA(sizeof(old_dir
), old_dir
);
405 SetCurrentDirectoryA(lpDirectory
);
408 /* First try to execute lpFile with lpParameters directly */
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 */
422 char lpstrProtocol
[256];
425 retval
= SHELL_FindExecutable(lpFile
, lpOperation
, cmd
, lpstrProtocol
);
427 if (retval
> 32) /* Found */
429 TRACE("%s/%s => %s/%s\n", lpFile
, lpOperation
, cmd
, lpstrProtocol
);
431 retval
= execute_from_key(lpstrProtocol
, lpFile
, iShowCmd
, is32
);
433 retval
= (is32
) ? WinExec(cmd
, iShowCmd
) : WinExec16(cmd
, iShowCmd
);
435 else if (PathIsURLA((LPSTR
)lpFile
)) /* File not found, check for URL */
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
))
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);
471 SetCurrentDirectoryA(old_dir
);
475 /*************************************************************************
476 * FindExecutableA [SHELL32.@]
478 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
480 HINSTANCE retval
= 31; /* default - 'No association was found' */
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. */
497 GetCurrentDirectoryA(sizeof(old_dir
), old_dir
);
498 SetCurrentDirectoryA(lpDirectory
);
501 retval
= SHELL_FindExecutable(lpFile
, "open", lpResult
, NULL
);
503 TRACE("returning %s\n", lpResult
);
505 SetCurrentDirectoryA(old_dir
);
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
)
536 return SHELL_Execute( hWnd
, lpOperation
, lpFile
, lpParameters
,
537 lpDirectory
, iShowCmd
, TRUE
);
540 /*************************************************************************
541 * ShellExecuteW [SHELL32.294]
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
)