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"
47 #include "wine/winbase16.h"
48 #include "shell32_main.h"
49 #include "undocshell.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
55 static const WCHAR wszOpen
[] = {'o','p','e','n',0};
56 static const WCHAR wszExe
[] = {'.','e','x','e',0};
57 static const WCHAR wszILPtr
[] = {':','%','p',0};
58 static const WCHAR wszShell
[] = {'\\','s','h','e','l','l','\\',0};
61 /***********************************************************************
62 * SHELL_ArgifyW [Internal]
64 * this function is supposed to expand the escape sequences found in the registry
65 * some diving reported that the following were used:
66 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
71 * %I address of a global item ID (explorer switch /idlist)
72 * %L seems to be %1 as long filename followed by the 8+3 variation
74 * %* all following parameters (see batfile)
78 static BOOL
SHELL_ArgifyW(WCHAR
* out
, int len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
)
118 while(*args
&& !isspace(*args
))
121 while(isspace(*args
))
126 /* else fall through */
128 if (!done
|| (*fmt
== '1'))
130 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
131 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
136 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
137 if (res
==out
|| res
[-1]!='"')
153 * IE uses this alot for activating things such as windows media
154 * player. This is not verified to be fully correct but it appears
160 strcpyW(res
, lpFile
);
161 res
+= strlenW(lpFile
);
168 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
169 pv
= SHLockShared(hmem
, 0);
170 res
+= sprintfW(res
, wszILPtr
, pv
);
175 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
189 /*************************************************************************
190 * SHELL_ExecuteW [Internal]
193 static UINT
SHELL_ExecuteW(const WCHAR
*lpCmd
, void *env
, BOOL shWait
,
194 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
196 STARTUPINFOW startup
;
197 PROCESS_INFORMATION info
;
200 WCHAR curdir
[MAX_PATH
];
202 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
203 /* ShellExecute specifies the command from psei->lpDirectory
204 * if present. Not from the current dir as CreateProcess does */
205 if( psei
->lpDirectory
&& psei
->lpDirectory
[0] )
206 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
207 if( !SetCurrentDirectoryW( psei
->lpDirectory
))
208 ERR("cannot set directory %s\n", debugstr_w(psei
->lpDirectory
));
209 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
210 startup
.cb
= sizeof(STARTUPINFOW
);
211 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
212 startup
.wShowWindow
= psei
->nShow
;
213 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, 0,
214 env
, *psei
->lpDirectory
? psei
->lpDirectory
: NULL
, &startup
, &info
))
216 /* Give 30 seconds to the app to come up, if desired. Probably only needed
217 when starting app immediately before making a DDE connection. */
219 if (WaitForInputIdle( info
.hProcess
, 30000 ) == -1)
220 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
222 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
223 psei_out
->hProcess
= info
.hProcess
;
225 CloseHandle( info
.hProcess
);
226 CloseHandle( info
.hThread
);
228 else if ((retval
= GetLastError()) >= 32)
230 FIXME("Strange error set by CreateProcess: %d\n", retval
);
231 retval
= ERROR_BAD_FORMAT
;
234 TRACE("returning %u\n", retval
);
236 psei_out
->hInstApp
= (HINSTANCE
)retval
;
238 if( !SetCurrentDirectoryW( curdir
))
239 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
245 /***********************************************************************
246 * SHELL_BuildEnvW [Internal]
248 * Build the environment for the new process, adding the specified
249 * path to the PATH variable. Returned pointer must be freed by caller.
251 static void *SHELL_BuildEnvW( const WCHAR
*path
)
253 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
254 WCHAR
*strings
, *new_env
;
256 int total
= strlenW(path
) + 1;
257 BOOL got_path
= FALSE
;
259 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
263 int len
= strlenW(p
) + 1;
264 if (!strncmpiW( p
, wPath
, 5 )) got_path
= TRUE
;
268 if (!got_path
) total
+= 5; /* we need to create PATH */
269 total
++; /* terminating null */
271 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) )))
273 FreeEnvironmentStringsW( strings
);
280 int len
= strlenW(p
) + 1;
281 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
282 if (!strncmpiW( p
, wPath
, 5 ))
285 strcpyW( p2
+ len
, path
);
286 p2
+= strlenW(path
) + 1;
293 strcpyW( p2
, wPath
);
295 p2
+= strlenW(p2
) + 1;
298 FreeEnvironmentStringsW( strings
);
303 /***********************************************************************
304 * SHELL_TryAppPathW [Internal]
306 * Helper function for SHELL_FindExecutable
307 * @param lpResult - pointer to a buffer of size MAX_PATH
308 * On entry: szName is a filename (probably without path separators).
309 * On exit: if szName found in "App Path", place full path in lpResult, and return true
311 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, void**env
)
313 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',
314 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
315 static const WCHAR wPath
[] = {'P','a','t','h',0};
322 if (env
) *env
= NULL
;
323 strcpyW(buffer
, wszKeyAppPaths
);
324 strcatW(buffer
, szName
);
325 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
328 len
= MAX_PATH
*sizeof(WCHAR
);
329 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
335 DWORD count
= sizeof(buffer
);
336 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
337 *env
= SHELL_BuildEnvW( buffer
);
341 if (hkApp
) RegCloseKey(hkApp
);
345 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
347 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
349 /* Looking for ...buffer\shell\<verb>\command */
350 strcatW(filetype
, wszShell
);
351 strcatW(filetype
, lpOperation
);
352 strcatW(filetype
, wCommand
);
354 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
355 &commandlen
) == ERROR_SUCCESS
)
357 commandlen
/= sizeof(WCHAR
);
358 if (key
) strcpyW(key
, filetype
);
362 LONG paramlen
= sizeof(param
);
363 static const WCHAR wSpace
[] = {' ',0};
365 /* FIXME: it seems all Windows version don't behave the same here.
366 * the doc states that this ddeexec information can be found after
368 * on Win98, it doesn't appear, but I think it does on Win2k
370 /* Get the parameters needed by the application
371 from the associated ddeexec key */
372 tmp
= strstrW(filetype
, wCommand
);
374 strcatW(filetype
, wDdeexec
);
375 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
376 ¶mlen
) == ERROR_SUCCESS
)
378 paramlen
/= sizeof(WCHAR
);
379 strcatW(command
, wSpace
);
380 strcatW(command
, param
);
381 commandlen
+= paramlen
;
385 command
[commandlen
] = '\0';
387 return 33; /* FIXME see SHELL_FindExecutable() */
390 return 31; /* default - 'No association was found' */
393 /*************************************************************************
394 * SHELL_FindExecutable [Internal]
396 * Utility for code sharing between FindExecutable and ShellExecute
398 * lpFile the name of a file
399 * lpOperation the operation on it (open)
401 * lpResult a buffer, big enough :-(, to store the command to do the
402 * operation on the file
403 * key a buffer, big enough, to get the key name to do actually the
404 * command (it'll be used afterwards for more information
407 UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
408 LPWSTR lpResult
, int resultLen
, LPWSTR key
, void **env
, LPITEMIDLIST pidl
, LPCWSTR args
)
410 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
411 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
412 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
413 WCHAR
*extension
= NULL
; /* pointer to file extension */
414 WCHAR wtmpext
[5]; /* local copy to mung as we please */
415 WCHAR filetype
[256]; /* registry name for this filetype */
416 LONG filetypelen
= sizeof(filetype
); /* length of above */
417 WCHAR command
[256]; /* command from registry */
418 WCHAR wBuffer
[256]; /* Used to GetProfileString */
419 UINT retval
= 31; /* default - 'No association was found' */
420 WCHAR
*tok
; /* token pointer */
421 WCHAR xlpFile
[256] = {0}; /* result of SearchPath */
423 TRACE("%s\n", (lpFile
!= NULL
) ? debugstr_w(lpFile
) : "-");
425 lpResult
[0] = '\0'; /* Start off with an empty return string */
426 if (key
) *key
= '\0';
428 /* trap NULL parameters on entry */
429 if ((lpFile
== NULL
) || (lpResult
== NULL
) || (lpOperation
== NULL
))
431 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
432 debugstr_w(lpFile
), debugstr_w(lpOperation
), debugstr_w(lpResult
));
433 return 2; /* File not found. Close enough, I guess. */
436 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
438 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
442 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
444 TRACE("SearchPathW returned non-zero\n");
446 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
449 /* First thing we need is the file's extension */
450 extension
= strrchrW(xlpFile
, '.'); /* Assume last "." is the one; */
451 /* File->Run in progman uses */
453 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
455 if ((extension
== NULL
) || (extension
== &xlpFile
[strlenW(xlpFile
)]))
457 WARN("Returning 31 - No association\n");
458 return 31; /* no association */
461 /* Make local copy & lowercase it for reg & 'programs=' lookup */
462 lstrcpynW(wtmpext
, extension
, 5);
464 TRACE("%s file\n", debugstr_w(wtmpext
));
466 /* Three places to check: */
467 /* 1. win.ini, [windows], programs (NB no leading '.') */
468 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
469 /* 3. win.ini, [extensions], extension (NB no leading '.' */
470 /* All I know of the order is that registry is checked before */
471 /* extensions; however, it'd make sense to check the programs */
472 /* section first, so that's what happens here. */
474 /* See if it's a program - if GetProfileString fails, we skip this
475 * section. Actually, if GetProfileString fails, we've probably
476 * got a lot more to worry about than running a program... */
477 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, sizeof(wBuffer
)/sizeof(WCHAR
)) > 0)
484 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
488 while (*p
== ' ' || *p
== '\t') p
++;
491 if (strcmpW(tok
, &wtmpext
[1]) == 0) /* have to skip the leading "." */
493 strcpyW(lpResult
, xlpFile
);
494 /* Need to perhaps check that the file has a path
496 TRACE("found %s\n", debugstr_w(lpResult
));
499 /* Greater than 32 to indicate success FIXME According to the
500 * docs, I should be returning a handle for the
501 * executable. Does this mean I'm supposed to open the
502 * executable file or something? More RTFM, I guess... */
509 if (RegQueryValueW(HKEY_CLASSES_ROOT
, wtmpext
, filetype
,
510 &filetypelen
) == ERROR_SUCCESS
)
512 filetypelen
/= sizeof(WCHAR
);
513 filetype
[filetypelen
] = '\0';
514 TRACE("File type: %s\n", debugstr_w(filetype
));
521 /* pass the operation string to SHELL_FindExecutableByOperation() */
522 filetype
[filetypelen
] = '\0';
523 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
, sizeof(command
));
527 WCHAR operation
[MAX_PATH
];
530 /* Looking for ...buffer\shell\<operation>\command */
531 strcatW(filetype
, wszShell
);
533 /* enumerate the operation subkeys in the registry and search for one with an associated command */
534 if (RegOpenKeyW(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
539 if (RegEnumKeyW(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
542 filetype
[filetypelen
] = '\0';
543 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
, sizeof(command
));
554 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
);
556 /* Remove double quotation marks and command line arguments */
557 if (*lpResult
== '"')
560 while (*(p
+ 1) != '"')
569 else /* Check win.ini */
571 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
572 static const WCHAR wEmpty
[] = {0};
574 /* Toss the leading dot */
576 if (GetProfileStringW(wExtensions
, extension
, wEmpty
, command
, sizeof(command
)/sizeof(WCHAR
)) > 0)
578 if (strlenW(command
) != 0)
580 strcpyW(lpResult
, command
);
581 tok
= strchrW(lpResult
, '^'); /* should be ^.extension? */
585 strcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
586 tok
= strchrW(command
, '^'); /* see above */
587 if ((tok
!= NULL
) && (strlenW(tok
)>5))
589 strcatW(lpResult
, &tok
[5]);
592 retval
= 33; /* FIXME - see above */
597 TRACE("returning %s\n", debugstr_w(lpResult
));
601 /******************************************************************
604 * callback for the DDE connection. not really usefull
606 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
608 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
613 /******************************************************************
616 * ShellExecute helper. Used to do an operation with a DDE connection
618 * Handles both the direct connection (try #1), and if it fails,
619 * launching an application and trying (#2) to connect to it
622 static unsigned dde_connect(WCHAR
* key
, WCHAR
* start
, WCHAR
* ddeexec
,
623 const WCHAR
* lpFile
, void *env
,
624 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
625 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
627 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
628 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
629 WCHAR
* endkey
= key
+ strlenW(key
);
630 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
631 LONG applen
, topiclen
, ifexeclen
;
635 HSZ hszApp
, hszTopic
;
639 strcpyW(endkey
, wApplication
);
640 applen
= sizeof(app
);
641 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
643 FIXME("default app name NIY %s\n", debugstr_w(key
));
647 strcpyW(endkey
, wTopic
);
648 topiclen
= sizeof(topic
);
649 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
651 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
652 strcpyW(topic
, wSystem
);
655 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
660 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINANSI
);
661 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINANSI
);
663 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
667 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
668 TRACE("Launching '%s'\n", debugstr_w(start
));
669 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
672 TRACE("Couldn't launch\n");
675 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
678 TRACE("Couldn't connect. ret=%d\n", ret
);
679 DdeUninitialize(ddeInst
);
680 SetLastError(ERROR_DDE_FAIL
);
681 return 30; /* whatever */
683 strcpyW(endkey
, wIfexec
);
684 ifexeclen
= sizeof(ifexec
);
685 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
691 SHELL_ArgifyW(res
, sizeof(res
)/sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
);
692 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
694 ret
= (DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
695 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
696 DdeDisconnect(hConv
);
699 DdeUninitialize(ddeInst
);
704 /*************************************************************************
705 * execute_from_key [Internal]
707 static UINT
execute_from_key(LPWSTR key
, LPCWSTR lpFile
, void *env
, LPCWSTR szCommandline
,
708 SHELL_ExecuteW32 execfunc
,
709 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
711 WCHAR cmd
[1024] = {0};
712 LONG cmdlen
= sizeof(cmd
);
715 /* Get the application for the registry */
716 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
718 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
719 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
721 WCHAR param
[256] = {0};
722 LONG paramlen
= sizeof(param
);
724 /* Get the parameters needed by the application
725 from the associated ddeexec key */
726 tmp
= strstrW(key
, wCommand
);
728 strcpyW(tmp
, wDdeexec
);
730 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
732 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(param
));
733 retval
= dde_connect(key
, cmd
, param
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
737 /* Is there a replace() function anywhere? */
738 cmdlen
/= sizeof(WCHAR
);
740 SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
);
741 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
744 else TRACE("ooch\n");
749 /*************************************************************************
750 * FindExecutableA [SHELL32.@]
752 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
755 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
756 WCHAR wResult
[MAX_PATH
];
758 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
759 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
761 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
762 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
763 if (wFile
) SHFree( wFile
);
764 if (wDirectory
) SHFree( wDirectory
);
766 TRACE("returning %s\n", lpResult
);
767 return (HINSTANCE
)retval
;
770 /*************************************************************************
771 * FindExecutableW [SHELL32.@]
773 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
775 UINT retval
= 31; /* default - 'No association was found' */
778 TRACE("File %s, Dir %s\n",
779 (lpFile
!= NULL
? debugstr_w(lpFile
) : "-"), (lpDirectory
!= NULL
? debugstr_w(lpDirectory
) : "-"));
781 lpResult
[0] = '\0'; /* Start off with an empty return string */
783 /* trap NULL parameters on entry */
784 if ((lpFile
== NULL
) || (lpResult
== NULL
))
786 /* FIXME - should throw a warning, perhaps! */
787 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
792 GetCurrentDirectoryW(sizeof(old_dir
)/sizeof(WCHAR
), old_dir
);
793 SetCurrentDirectoryW(lpDirectory
);
796 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
798 TRACE("returning %s\n", debugstr_w(lpResult
));
800 SetCurrentDirectoryW(old_dir
);
801 return (HINSTANCE
)retval
;
804 /*************************************************************************
805 * ShellExecuteExW32 [Internal]
807 BOOL WINAPI
ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
809 static const WCHAR wQuote
[] = {'"',0};
810 static const WCHAR wSpace
[] = {' ',0};
811 static const WCHAR wWww
[] = {'w','w','w',0};
812 static const WCHAR wFile
[] = {'f','i','l','e',0};
813 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
815 WCHAR wszApplicationName
[MAX_PATH
+2], wszCommandline
[1024], wszDir
[MAX_PATH
];
816 SHELLEXECUTEINFOW sei_tmp
; /* modifyable copy of SHELLEXECUTEINFO struct */
817 WCHAR wfileName
[MAX_PATH
];
819 WCHAR lpstrProtocol
[256];
823 WCHAR buffer
[MAX_PATH
];
826 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
827 memcpy(&sei_tmp
, sei
, sizeof(sei_tmp
));
829 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
830 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
831 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
832 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
833 (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
) ? debugstr_w(sei_tmp
.lpClass
) : "not used");
835 sei
->hProcess
= NULL
;
837 /* make copies of all path/command strings */
839 strcpyW(wszApplicationName
, sei_tmp
.lpFile
);
841 *wszApplicationName
= '\0';
843 if (sei_tmp
.lpParameters
)
844 strcpyW(wszCommandline
, sei_tmp
.lpParameters
);
846 *wszCommandline
= '\0';
848 if (sei_tmp
.lpDirectory
)
849 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
853 /* adjust string pointers to point to the new buffers */
854 sei_tmp
.lpFile
= wszApplicationName
;
855 sei_tmp
.lpParameters
= wszCommandline
;
856 sei_tmp
.lpDirectory
= wszDir
;
858 if (sei_tmp
.fMask
& (SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
859 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
860 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
861 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
863 FIXME("flags ignored: 0x%08lx\n", sei_tmp
.fMask
);
866 /* process the IDList */
867 if ((sei_tmp
.fMask
& SEE_MASK_INVOKEIDLIST
) == SEE_MASK_INVOKEIDLIST
) /*0x0c*/
869 wszApplicationName
[0] = '"';
870 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
+1);
871 strcatW(wszApplicationName
, wQuote
);
872 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
875 if (sei_tmp
.fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
877 /* launch a document by fileclass like 'WordPad.Document.1' */
878 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
879 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
880 HCR_GetExecuteCommandW((sei_tmp
.fMask
& SEE_MASK_CLASSKEY
) ? sei_tmp
.hkeyClass
: NULL
,
881 (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
) ? sei_tmp
.lpClass
: NULL
,
882 (sei_tmp
.lpVerb
) ? sei_tmp
.lpVerb
: wszOpen
,
883 wszCommandline
, sizeof(wszCommandline
)/sizeof(WCHAR
));
885 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
886 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszCommandline
), debugstr_w(wszApplicationName
));
889 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
)/sizeof(WCHAR
), wszCommandline
, wszApplicationName
, sei_tmp
.lpIDList
, NULL
);
890 if (!done
&& wszApplicationName
[0])
892 strcatW(wcmd
, wSpace
);
893 strcatW(wcmd
, wszApplicationName
);
895 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
902 /* expand environment strings */
903 if (ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buffer
, MAX_PATH
))
904 lstrcpyW(wszApplicationName
, buffer
);
906 if (*sei_tmp
.lpParameters
)
907 if (ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buffer
, MAX_PATH
))
908 lstrcpyW(wszCommandline
, buffer
);
910 if (*sei_tmp
.lpDirectory
)
911 if (ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buffer
, MAX_PATH
))
912 lstrcpyW(wszDir
, buffer
);
914 /* Else, try to execute the filename */
915 TRACE("execute:'%s','%s','%s'\n", debugstr_w(wszApplicationName
), debugstr_w(wszCommandline
), debugstr_w(wszDir
));
917 strcpyW(wfileName
, wszApplicationName
);
919 if (wszCommandline
[0]) {
920 strcatW(wszApplicationName
, wSpace
);
921 strcatW(wszApplicationName
, wszCommandline
);
924 retval
= execfunc(wszApplicationName
, NULL
, FALSE
, &sei_tmp
, sei
);
928 /* Else, try to find the executable */
930 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, 1024, lpstrProtocol
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
931 if (retval
> 32) /* Found */
933 WCHAR wszQuotedCmd
[MAX_PATH
+2];
934 /* Must quote to handle case where cmd contains spaces,
935 * else security hole if malicious user creates executable file "C:\\Program"
937 strcpyW(wszQuotedCmd
, wQuote
);
938 strcatW(wszQuotedCmd
, wcmd
);
939 strcatW(wszQuotedCmd
, wQuote
);
940 if (wszCommandline
[0]) {
941 strcatW(wszQuotedCmd
, wSpace
);
942 strcatW(wszQuotedCmd
, wszCommandline
);
944 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(sei_tmp
.lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
946 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, sei
);
948 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, &sei_tmp
, sei
);
949 if (env
) HeapFree( GetProcessHeap(), 0, env
);
951 else if (PathIsURLW((LPWSTR
)lpFile
)) /* File not found, check for URL */
953 static const WCHAR wShell
[] = {'\\','s','h','e','l','l','\\',0};
954 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
958 lpstrRes
= strchrW(lpFile
, ':');
960 iSize
= lpstrRes
- lpFile
;
962 iSize
= strlenW(lpFile
);
964 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
965 /* Looking for ...protocol\shell\lpOperation\command */
966 strncpyW(lpstrProtocol
, lpFile
, iSize
);
967 lpstrProtocol
[iSize
] = '\0';
968 strcatW(lpstrProtocol
, wShell
);
969 strcatW(lpstrProtocol
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
);
970 strcatW(lpstrProtocol
, wCommand
);
972 /* Remove File Protocol from lpFile */
973 /* In the case file://path/file */
974 if (!strncmpiW(lpFile
, wFile
, iSize
))
977 while (*lpFile
== ':') lpFile
++;
979 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, sei
);
981 /* Check if file specified is in the form www.??????.*** */
982 else if (!strncmpiW(lpFile
, wWww
, 3))
984 /* if so, append lpFile http:// and call ShellExecute */
985 WCHAR lpstrTmpFile
[256];
986 strcpyW(lpstrTmpFile
, wHttp
);
987 strcatW(lpstrTmpFile
, lpFile
);
988 retval
= (UINT
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
991 TRACE("retval %u\n", retval
);
995 sei
->hInstApp
= (HINSTANCE
)retval
;
999 sei
->hInstApp
= (HINSTANCE
)33;
1003 /*************************************************************************
1004 * ShellExecuteA [SHELL32.290]
1006 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1007 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1009 SHELLEXECUTEINFOA sei
;
1010 HANDLE hProcess
= 0;
1013 sei
.cbSize
= sizeof(sei
);
1016 sei
.lpVerb
= lpOperation
;
1017 sei
.lpFile
= lpFile
;
1018 sei
.lpParameters
= lpParameters
;
1019 sei
.lpDirectory
= lpDirectory
;
1020 sei
.nShow
= iShowCmd
;
1025 sei
.hProcess
= hProcess
;
1027 ShellExecuteExA (&sei
);
1028 return sei
.hInstApp
;
1031 /*************************************************************************
1032 * ShellExecuteEx [SHELL32.291]
1035 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1037 if (SHELL_OsIsUnicode())
1038 return ShellExecuteExW32 (sei
, SHELL_ExecuteW
);
1039 return ShellExecuteExA (sei
);
1042 /*************************************************************************
1043 * ShellExecuteExA [SHELL32.292]
1046 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1048 SHELLEXECUTEINFOW seiW
;
1050 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1054 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1057 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1060 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1062 if (sei
->lpParameters
)
1063 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1065 if (sei
->lpDirectory
)
1066 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1068 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1069 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1071 seiW
.lpClass
= NULL
;
1073 ret
= ShellExecuteExW32 (&seiW
, SHELL_ExecuteW
);
1075 sei
->hInstApp
= seiW
.hInstApp
;
1077 if (wVerb
) SHFree(wVerb
);
1078 if (wFile
) SHFree(wFile
);
1079 if (wParameters
) SHFree(wParameters
);
1080 if (wDirectory
) SHFree(wDirectory
);
1081 if (wClass
) SHFree(wClass
);
1086 /*************************************************************************
1087 * ShellExecuteExW [SHELL32.293]
1090 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1092 return ShellExecuteExW32 (sei
, SHELL_ExecuteW
);
1095 /*************************************************************************
1096 * ShellExecuteW [SHELL32.294]
1098 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1099 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1101 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1102 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1104 SHELLEXECUTEINFOW sei
;
1105 HANDLE hProcess
= 0;
1108 sei
.cbSize
= sizeof(sei
);
1111 sei
.lpVerb
= lpOperation
;
1112 sei
.lpFile
= lpFile
;
1113 sei
.lpParameters
= lpParameters
;
1114 sei
.lpDirectory
= lpDirectory
;
1115 sei
.nShow
= nShowCmd
;
1120 sei
.hProcess
= hProcess
;
1122 ShellExecuteExW32 (&sei
, SHELL_ExecuteW
);
1123 return sei
.hInstApp
;