2 * Unit test of the ShellExecute function.
4 * Copyright 2005 Francois Gouget for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * - test the default verb selection
23 * - test selection of an alternate class
24 * - try running executables in more ways
25 * - try passing arguments to executables
26 * - ShellExecute("foo.shlexec") with no path should work if foo.shlexec is
28 * - test associations that use %l, %L or "%1" instead of %1
29 * - we may want to test ShellExecuteEx() instead of ShellExecute()
30 * and then we could also check its return value
31 * - ShellExecuteEx() also calls SetLastError() with meaningful values which
35 /* Needed to get SEE_MASK_NOZONECHECKS with the PSDK */
36 #define NTDDI_WINXPSP1 0x05010100
37 #define NTDDI_VERSION NTDDI_WINXPSP1
38 #define _WIN32_WINNT 0x0501
48 #include "wine/test.h"
50 #include "shell32_test.h"
53 static char argv0[MAX_PATH];
56 static char tmpdir[MAX_PATH];
57 static char child_file[MAX_PATH];
58 static DLLVERSIONINFO dllver;
59 static BOOL skip_noassoc_tests = FALSE;
60 static HANDLE dde_ready_event;
65 * ShellExecute wrappers
68 static void dump_child(void);
71 static void init_event(const char* child_file)
74 event_name=strrchr(child_file, '\\')+1;
75 hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
78 static void strcat_param(char* str, const char* name, const char* param)
82 if (str[strlen(str)-1] == '"')
91 static int _todo_wait = 0;
92 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
94 static char shell_call[2048]="";
95 static int bad_shellexecute = 0;
96 static INT_PTR shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
98 INT_PTR rc, rcEmpty = 0;
101 rcEmpty = shell_execute("", file, parameters, directory);
103 strcpy(shell_call, "ShellExecute(");
104 strcat_param(shell_call, "verb", operation);
105 strcat_param(shell_call, "file", file);
106 strcat_param(shell_call, "params", parameters);
107 strcat_param(shell_call, "dir", directory);
108 strcat(shell_call, ")");
109 if (winetest_debug > 1)
110 trace("%s\n", shell_call);
112 DeleteFile(child_file);
113 SetLastError(0xcafebabe);
115 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
116 * association it displays the 'Open With' dialog and I could not find
117 * a flag to prevent this.
119 rc=(INT_PTR)ShellExecute(NULL, operation, file, parameters, directory, SW_SHOWNORMAL);
124 wait_rc=WaitForSingleObject(hEvent, 5000);
125 if (wait_rc == WAIT_TIMEOUT)
127 HWND wnd = FindWindowA("#32770", "Windows");
130 SendMessage(wnd, WM_CLOSE, 0, 0);
131 win_skip("Skipping shellexecute of file with unassociated extension\n");
132 skip_noassoc_tests = TRUE;
137 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
139 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
141 /* The child process may have changed the result file, so let profile
142 * functions know about it
144 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
150 if (rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */
151 bad_shellexecute = 1;
152 ok(rc == rcEmpty || broken(rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */,
153 "%s Got different return value with empty string: %lu %lu\n", shell_call, rc, rcEmpty);
159 static INT_PTR shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
160 LPCSTR parameters, LPCSTR directory,
163 SHELLEXECUTEINFO sei;
167 strcpy(shell_call, "ShellExecuteEx(");
171 sprintf(smask, "0x%x", mask);
172 strcat_param(shell_call, "mask", smask);
174 strcat_param(shell_call, "verb", operation);
175 strcat_param(shell_call, "file", file);
176 strcat_param(shell_call, "params", parameters);
177 strcat_param(shell_call, "dir", directory);
178 strcat_param(shell_call, "class", class);
179 strcat(shell_call, ")");
180 if (winetest_debug > 1)
181 trace("%s\n", shell_call);
183 sei.cbSize=sizeof(sei);
184 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
186 sei.lpVerb=operation;
188 sei.lpParameters=parameters;
189 sei.lpDirectory=directory;
190 sei.nShow=SW_SHOWNORMAL;
191 sei.hInstApp=NULL; /* Out */
197 sei.hProcess=NULL; /* Out */
199 DeleteFile(child_file);
200 SetLastError(0xcafebabe);
201 success=ShellExecuteEx(&sei);
202 rc=(INT_PTR)sei.hInstApp;
203 ok((success && rc > 32) || (!success && rc <= 32),
204 "%s rc=%d and hInstApp=%ld is not allowed\n", shell_call, success, rc);
209 if (sei.hProcess!=NULL)
211 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
212 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
214 wait_rc=WaitForSingleObject(hEvent, 5000);
216 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
218 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
220 /* The child process may have changed the result file, so let profile
221 * functions know about it
223 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
234 * Functions to create / delete associations wrappers
238 static BOOL create_test_association(const char* extension)
240 HKEY hkey, hkey_shell;
241 char class[MAX_PATH];
244 sprintf(class, "shlexec%s", extension);
245 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
247 if (rc != ERROR_SUCCESS)
250 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
251 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
254 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
255 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
256 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
258 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
259 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
260 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
263 CloseHandle(hkey_shell);
268 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
269 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
272 DWORD dwMaxSubkeyLen, dwMaxValueLen;
273 DWORD dwMaxLen, dwSize;
274 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
279 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
283 /* Get highest length for keys, values */
284 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
285 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
286 if (ret) goto cleanup;
290 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
291 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
293 /* Name too big: alloc a buffer for it */
294 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
296 ret = ERROR_NOT_ENOUGH_MEMORY;
302 /* Recursively delete all the subkeys */
306 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
307 NULL, NULL, NULL)) break;
309 ret = myRegDeleteTreeA(hSubKey, lpszName);
310 if (ret) goto cleanup;
314 ret = RegDeleteKeyA(hKey, lpszSubKey);
319 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
320 NULL, NULL, NULL, NULL)) break;
322 ret = RegDeleteValueA(hKey, lpszName);
323 if (ret) goto cleanup;
327 /* Free buffer if allocated */
328 if (lpszName != szNameBuf)
329 HeapFree( GetProcessHeap(), 0, lpszName);
331 RegCloseKey(hSubKey);
335 static void delete_test_association(const char* extension)
337 char class[MAX_PATH];
339 sprintf(class, "shlexec%s", extension);
340 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
341 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
344 static void create_test_verb_dde(const char* extension, const char* verb,
345 int rawcmd, const char* cmdtail, const char *ddeexec,
346 const char *application, const char *topic,
349 HKEY hkey_shell, hkey_verb, hkey_cmd;
350 char shell[MAX_PATH];
354 sprintf(shell, "shlexec%s\\shell", extension);
355 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
356 KEY_CREATE_SUB_KEY, &hkey_shell);
357 assert(rc==ERROR_SUCCESS);
358 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
359 NULL, &hkey_verb, NULL);
360 assert(rc==ERROR_SUCCESS);
361 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
362 NULL, &hkey_cmd, NULL);
363 assert(rc==ERROR_SUCCESS);
367 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
371 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
372 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
373 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
374 assert(rc==ERROR_SUCCESS);
375 HeapFree(GetProcessHeap(), 0, cmd);
380 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
382 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
383 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
384 assert(rc==ERROR_SUCCESS);
385 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
387 assert(rc==ERROR_SUCCESS);
390 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
391 NULL, &hkey_application, NULL);
392 assert(rc==ERROR_SUCCESS);
393 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
394 strlen(application)+1);
395 assert(rc==ERROR_SUCCESS);
396 CloseHandle(hkey_application);
400 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
401 NULL, &hkey_topic, NULL);
402 assert(rc==ERROR_SUCCESS);
403 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
405 assert(rc==ERROR_SUCCESS);
406 CloseHandle(hkey_topic);
410 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
411 NULL, &hkey_ifexec, NULL);
412 assert(rc==ERROR_SUCCESS);
413 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
415 assert(rc==ERROR_SUCCESS);
416 CloseHandle(hkey_ifexec);
418 CloseHandle(hkey_ddeexec);
421 CloseHandle(hkey_shell);
422 CloseHandle(hkey_verb);
423 CloseHandle(hkey_cmd);
426 static void create_test_verb(const char* extension, const char* verb,
427 int rawcmd, const char* cmdtail)
429 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
435 * Functions to check that the child process was started just right
436 * (borrowed from dlls/kernel32/tests/process.c)
440 static const char* encodeA(const char* str)
442 static char encoded[2*1024+1];
447 len = strlen(str) + 1;
448 if (len >= sizeof(encoded)/2)
450 fprintf(stderr, "string is too long!\n");
454 for (i = 0; i < len; i++)
455 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
460 static unsigned decode_char(char c)
462 if (c >= '0' && c <= '9') return c - '0';
463 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
464 assert(c >= 'A' && c <= 'F');
468 static char* decodeA(const char* str)
470 static char decoded[1024];
474 len = strlen(str) / 2;
475 if (!len--) return NULL;
476 if (len >= sizeof(decoded))
478 fprintf(stderr, "string is too long!\n");
482 for (i = 0; i < len; i++)
483 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
488 static void childPrintf(HANDLE h, const char* fmt, ...)
494 va_start(valist, fmt);
495 vsprintf(buffer, fmt, valist);
497 WriteFile(h, buffer, strlen(buffer), &w, NULL);
500 static DWORD ddeInst;
502 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
503 static BOOL post_quit_on_execute;
505 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
506 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
507 ULONG_PTR dwData1, ULONG_PTR dwData2)
511 if (winetest_debug > 2)
512 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
513 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
518 if (!DdeCmpStringHandles(hsz1, hszTopic))
520 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
521 assert(size < MAX_PATH);
522 return (HDDEDATA)TRUE;
524 return (HDDEDATA)FALSE;
527 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
528 assert(size < MAX_PATH);
529 DdeFreeDataHandle(hData);
530 if (post_quit_on_execute)
532 return (HDDEDATA)DDE_FACK;
540 * This is just to make sure the child won't run forever stuck in a GetMessage()
541 * loop when DDE fails for some reason.
543 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
545 trace("childTimeout called\n");
550 static void doChild(int argc, char** argv)
552 char *filename, longpath[MAX_PATH] = "";
563 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
564 if (hFile == INVALID_HANDLE_VALUE)
568 childPrintf(hFile, "[Arguments]\r\n");
569 if (winetest_debug > 2)
571 trace("cmdlineA='%s'\n", GetCommandLineA());
572 trace("argcA=%d\n", argc);
574 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
575 childPrintf(hFile, "argcA=%d\r\n", argc);
576 for (i = 0; i < argc; i++)
578 if (winetest_debug > 2)
579 trace("argvA%d='%s'\n", i, argv[i]);
580 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
582 GetModuleFileNameA(GetModuleHandleA(NULL), longpath, MAX_PATH);
583 childPrintf(hFile, "longPath=%s\r\n", encodeA(longpath));
585 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
588 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
590 if (shared_block[0] != '\0' || shared_block[1] != '\0')
592 post_quit_on_execute = TRUE;
594 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
595 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
596 assert(rc == DMLERR_NO_ERROR);
597 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
598 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
599 assert(hszApplication && hszTopic);
600 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
602 timer = SetTimer(NULL, 0, 2500, childTimeout);
604 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
606 CloseHandle(dde_ready);
608 while (GetMessage(&msg, NULL, 0, 0))
609 DispatchMessage(&msg);
612 KillTimer(NULL, timer);
613 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
614 assert(DdeFreeStringHandle(ddeInst, hszTopic));
615 assert(DdeFreeStringHandle(ddeInst, hszApplication));
616 assert(DdeUninitialize(ddeInst));
620 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
622 CloseHandle(dde_ready);
625 UnmapViewOfFile(shared_block);
627 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
632 init_event(filename);
637 static char* getChildString(const char* sect, const char* key)
642 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
643 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
644 assert(!(strlen(buf) & 1));
649 static void dump_child(void)
651 if (winetest_debug > 1)
657 str=getChildString("Arguments", "cmdlineA");
658 trace("cmdlineA='%s'\n", str);
659 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
660 trace("argcA=%d\n",c);
663 sprintf(key, "argvA%d", i);
664 str=getChildString("Arguments", key);
665 trace("%s='%s'\n", key, str);
670 static int StrCmpPath(const char* s1, const char* s2)
672 if (!s1 && !s2) return 0;
683 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
685 while (*s1=='/' || *s1=='\\')
687 while (*s2=='/' || *s2=='\\')
690 else if (toupper(*s1)==toupper(*s2))
707 static void _okChildString(const char* file, int line, const char* key, const char* expected)
710 result=getChildString("Arguments", key);
713 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
716 ok_(file, line)(lstrcmpiA(result, expected) == 0,
717 "%s expected '%s', got '%s'\n", key, expected, result);
720 static void _okChildPath(const char* file, int line, const char* key, const char* expected)
723 result=getChildString("Arguments", key);
726 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
729 ok_(file, line)(StrCmpPath(result, expected) == 0,
730 "%s expected '%s', got '%s'\n", key, expected, result);
733 static void _okChildInt(const char* file, int line, const char* key, int expected)
736 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
737 ok_(file, line)(result == expected,
738 "%s expected %d, but got %d\n", key, expected, result);
741 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
742 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
743 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
747 * GetLongPathNameA equivalent that supports Win95 and WinNT
751 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
753 char tmplongpath[MAX_PATH];
755 DWORD sp = 0, lp = 0;
757 WIN32_FIND_DATAA wfd;
760 if (!shortpath || !shortpath[0])
763 if (shortpath[1] == ':')
765 tmplongpath[0] = shortpath[0];
766 tmplongpath[1] = ':';
770 while (shortpath[sp])
772 /* check for path delimiters and reproduce them */
773 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
775 if (!lp || tmplongpath[lp-1] != '\\')
777 /* strip double "\\" */
778 tmplongpath[lp++] = '\\';
780 tmplongpath[lp] = 0; /* terminate string */
786 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
788 tmplongpath[lp++] = *p++;
789 tmplongpath[lp++] = *p++;
791 for (; *p && *p != '/' && *p != '\\'; p++);
792 tmplen = p - (shortpath + sp);
793 lstrcpyn(tmplongpath + lp, shortpath + sp, tmplen + 1);
794 /* Check if the file exists and use the existing file name */
795 goit = FindFirstFileA(tmplongpath, &wfd);
796 if (goit == INVALID_HANDLE_VALUE)
799 strcpy(tmplongpath + lp, wfd.cFileName);
800 lp += strlen(tmplongpath + lp);
803 tmplen = strlen(shortpath) - 1;
804 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
805 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
806 tmplongpath[lp++] = shortpath[tmplen];
809 tmplen = strlen(tmplongpath) + 1;
810 if (tmplen <= longlen)
812 strcpy(longpath, tmplongpath);
813 tmplen--; /* length without 0 */
821 * PathFindFileNameA equivalent that supports WinNT
825 static LPSTR path_find_file_name(LPCSTR lpszPath)
827 LPCSTR lastSlash = lpszPath;
829 while (lpszPath && *lpszPath)
831 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
832 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
833 lastSlash = lpszPath + 1;
834 lpszPath = CharNext(lpszPath);
836 return (LPSTR)lastSlash;
845 static const char* testfiles[]=
847 "%s\\test file.shlexec",
848 "%s\\%%nasty%% $file.shlexec",
849 "%s\\test file.noassoc",
850 "%s\\test file.noassoc.shlexec",
851 "%s\\test file.shlexec.noassoc",
852 "%s\\test_shortcut_shlexec.lnk",
853 "%s\\test_shortcut_exe.lnk",
855 "%s\\test file.shlfoo",
857 "%s\\masked file.shlexec",
862 "%s\\simple.shlexec",
863 "%s\\drawback_file.noassoc",
864 "%s\\drawback_file.noassoc foo.shlexec",
865 "%s\\drawback_nonexist.noassoc foo.shlexec",
872 const char* basename;
877 static filename_tests_t filename_tests[]=
879 /* Test bad / nonexistent filenames */
880 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
881 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
884 {NULL, "%s\\test file.shlexec", 0x0, 33},
885 {NULL, "%s\\test file.shlexec.", 0x0, 33},
886 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
887 {NULL, "%s/test file.shlexec", 0x0, 33},
889 /* Test filenames with no association */
890 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
892 /* Test double extensions */
893 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
894 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
896 /* Test alternate verbs */
897 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
898 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
900 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
901 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
903 /* Test file masked due to space */
904 {NULL, "%s\\masked file.shlexec", 0x1, 33},
905 /* Test if quoting prevents the masking */
906 {NULL, "%s\\masked file.shlexec", 0x40, 33},
911 static filename_tests_t noquotes_tests[]=
913 /* Test unquoted '%1' thingies */
914 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
915 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
916 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
921 static void test_lpFile_parsed(void)
923 char fileA[MAX_PATH];
926 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
927 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
928 rc=shell_execute(NULL, fileA, NULL, NULL);
929 todo_wine ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
931 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
932 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
933 rc=shell_execute(NULL, fileA, NULL, NULL);
934 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
935 "%s failed: rc=%lu\n", shell_call, rc);
937 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
938 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
939 rc=shell_execute(NULL, fileA, NULL, NULL);
940 ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
942 /* ""command"" not works on wine (and real win9x and w2k) */
943 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
944 rc=shell_execute(NULL, fileA, NULL, NULL);
945 todo_wine ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
946 "%s failed: rc=%lu\n", shell_call, rc);
948 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
949 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
950 rc=shell_execute(NULL, fileA, NULL, NULL);
951 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
953 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
954 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
955 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
958 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
959 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
961 /* test SEE_MASK_DOENVSUBST works */
962 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
963 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
964 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
966 /* quoted lpFile does not work on real win95 and nt4 */
967 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
968 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
969 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
970 "%s failed: rc=%lu\n", shell_call, rc);
976 const char* args[11];
980 static const cmdline_tests_t cmdline_tests[] =
982 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
983 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
985 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
986 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
988 {"exe arg\"one\" \"second\"arg thirdarg ",
989 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
991 /* cmd's metacharacters have no special meaning */
992 {"exe \"one^\" \"arg\"&two three|four",
993 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
995 /* Environment variables are not interpreted either */
997 {"exe", "%TMPDIR%", "%2", NULL}, 0},
999 /* If not followed by a quote, backslashes go through as is */
1000 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
1001 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1003 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
1004 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1006 /* When followed by a quote their number is halved and the remainder
1009 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1010 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1012 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1013 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1015 /* One can put a quote in an unquoted string by tripling it, that is in
1016 * effect quoting it like so """ -> ". The general rule is as follows:
1017 * 3n quotes -> n quotes
1018 * 3n+1 quotes -> n quotes plus start of a quoted string
1019 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1020 * Nicely, when n is 0 we get the standard rules back.
1022 {"exe two\"\"quotes next",
1023 {"exe", "twoquotes", "next", NULL}, 0},
1025 {"exe three\"\"\"quotes next",
1026 {"exe", "three\"quotes", "next", NULL}, 0x21},
1028 {"exe four\"\"\"\" quotes\" next 4%3=1",
1029 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0x61},
1031 {"exe five\"\"\"\"\"quotes next",
1032 {"exe", "five\"quotes", "next", NULL}, 0x21},
1034 {"exe six\"\"\"\"\"\"quotes next",
1035 {"exe", "six\"\"quotes", "next", NULL}, 0x20},
1037 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1038 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0x20},
1040 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1041 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0x20},
1043 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1044 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0x20},
1046 /* Inside a quoted string the opening quote is added to the set of
1047 * consecutive quotes to get the effective quotes count. This gives:
1048 * 1+3n quotes -> n quotes
1049 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1050 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1052 {"exe \"two\"\"quotes next",
1053 {"exe", "two\"quotes", "next", NULL}, 0x21},
1055 {"exe \"two\"\" next",
1056 {"exe", "two\"", "next", NULL}, 0x21},
1058 {"exe \"three\"\"\" quotes\" next 4%3=1",
1059 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0x61},
1061 {"exe \"four\"\"\"\"quotes next",
1062 {"exe", "four\"quotes", "next", NULL}, 0x21},
1064 {"exe \"five\"\"\"\"\"quotes next",
1065 {"exe", "five\"\"quotes", "next", NULL}, 0x20},
1067 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1068 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0x20},
1070 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1071 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0x20},
1073 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1074 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0x20},
1076 /* The executable path has its own rules!!!
1077 * - Backslashes have no special meaning.
1078 * - If the first character is a quote, then the second quote ends the
1080 * - The previous rule holds even if the next character is not a space!
1081 * - If the first character is not a quote, then quotes have no special
1082 * meaning either and the executable path stops at the first space.
1083 * - The consecutive quotes rules don't apply either.
1084 * - Even if there is no space between the executable path and the first
1085 * argument, the latter is parsed using the regular rules.
1087 {"exe\"file\"path arg1",
1088 {"exe\"file\"path", "arg1", NULL}, 0x30},
1090 {"exe\"path\\ arg1",
1091 {"exe\"path\\", "arg1", NULL}, 0x31},
1093 {"\\\"exe \"arg one\"",
1094 {"\\\"exe", "arg one", NULL}, 0x10},
1096 {"\"spaced exe\" \"next arg\"",
1097 {"spaced exe", "next arg", NULL}, 0},
1099 {"\"exe\"arg\" one\" argtwo",
1100 {"exe", "arg one", "argtwo", NULL}, 0x31},
1102 {"\"spaced exe\\\"arg1 arg2",
1103 {"spaced exe\\", "arg1", "arg2", NULL}, 0x11},
1106 {"two", " arg1 ", NULL}, 0x21},
1108 {"\"three\"\"\" arg2",
1109 {"three", "", "arg2", NULL}, 0x61},
1111 {"\"four\"\"\"\"arg1",
1112 {"four", "\"arg1", NULL}, 0x21},
1114 /* If the first character is a space then the executable path is empty */
1115 {" \"arg\"one argtwo",
1116 {"", "argone", "argtwo", NULL}, 0},
1121 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1123 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1129 /* trace("----- cmd='%s'\n", test->cmd); */
1130 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, sizeof(cmdW)/sizeof(*cmdW));
1131 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1132 if (argsW == NULL && cl2a_count == -1)
1134 win_skip("CommandLineToArgvW not implemented, skipping\n");
1139 while (test->args[count])
1141 if ((test->todo & 0x1) == 0)
1142 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1144 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1146 for (i = 0; i < cl2a_count - 1; i++)
1150 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, sizeof(argW)/sizeof(*argW));
1151 if ((test->todo & (1 << (i+4))) == 0)
1152 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1154 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1156 else if ((test->todo & 0x1) == 0)
1157 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1159 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1166 static void test_commandline2argv(void)
1168 static const WCHAR exeW[] = {'e','x','e',0};
1169 const cmdline_tests_t* test;
1170 WCHAR strW[MAX_PATH];
1175 test = cmdline_tests;
1178 if (!test_one_cmdline(test))
1183 SetLastError(0xdeadbeef);
1184 args = CommandLineToArgvW(exeW, NULL);
1185 le = GetLastError();
1186 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1188 SetLastError(0xdeadbeef);
1189 args = CommandLineToArgvW(NULL, NULL);
1190 le = GetLastError();
1191 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1194 args = CommandLineToArgvW(strW, &numargs);
1195 ok(numargs == 1, "expected 1 args, got %d\n", numargs);
1198 GetModuleFileNameW(NULL, strW, sizeof(strW)/sizeof(*strW));
1199 ok(!lstrcmpW(args[0], strW), "wrong path to the current executable: %s instead of %s\n", wine_dbgstr_w(args[0]), wine_dbgstr_w(strW));
1201 if (args) LocalFree(args);
1204 /* The goal here is to analyze how ShellExecute() builds the command that
1205 * will be run. The tricky part is that there are three transformation
1206 * steps between the 'parameters' string we pass to ShellExecute() and the
1207 * argument list we observe in the child process:
1208 * - The parsing of 'parameters' string into individual arguments. The tests
1209 * show this is done differently from both CreateProcess() and
1210 * CommandLineToArgv()!
1211 * - The way the command 'formatting directives' such as %1, %2, etc are
1213 * - And the way the resulting command line is then parsed to yield the
1214 * argument list we check.
1221 cmdline_tests_t cmd;
1224 static const argify_tests_t argify_tests[] =
1226 /* Start with three simple parameters. Notice that one can reorder and
1227 * duplicate the parameters. Also notice how %* take the raw input
1228 * parameters string, including the trailing spaces, no matter what
1229 * arguments have already been used.
1231 {"Params232S", "p2 p3 p4 ", 0xc2,
1232 {" p2 p3 \"p2\" \"p2 p3 p4 \"",
1233 {"", "p2", "p3", "p2", "p2 p3 p4 ", NULL}, 0}},
1235 /* Unquoted argument references like %2 don't automatically quote their
1236 * argument. Similarly, when they are quoted they don't escape the quotes
1237 * that their argument may contain.
1239 {"Params232S", "\"p two\" p3 p4 ", 0x3f3,
1240 {" p two p3 \"p two\" \"\"p two\" p3 p4 \"",
1241 {"", "p", "two", "p3", "p two", "p", "two p3 p4 ", NULL}, 0}},
1243 /* Only single digits are supported so only %1 to %9. Shown here with %20
1244 * because %10 is a pain.
1246 {"Params20", "p", 0,
1248 {"", "p0", NULL}, 0}},
1250 /* Only (double-)quotes have a special meaning. */
1251 {"Params23456", "'p2 p3` p4\\ $even", 0x40,
1252 {" \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\"",
1253 {"", "'p2", "p3`", "p4\" $even \"", NULL}, 0}},
1255 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", 0x1c2,
1256 {" \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\"",
1257 {"", "p=2", "p-3", "p4\tp4\rp4\np4", "", "", NULL}, 0}},
1259 /* In unquoted strings, quotes are treated are a parameter separator just
1260 * like spaces! However they can be doubled to get a literal quote.
1262 * 2n quotes -> n quotes
1263 * 2n+1 quotes -> n quotes and a parameter separator
1265 {"Params23456789", "one\"quote \"p four\" one\"quote p7", 0xff3,
1266 {" \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\"",
1267 {"", "one", "quote", "p four", "one", "quote", "p7", "", "", NULL}, 0}},
1269 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", 0xf2,
1270 {" \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1271 {"", "twoquotes p", "three twoquotes", "p5", "", "", "", "", NULL}, 0}},
1273 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", 0xff3,
1274 {" \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\"",
1275 {"", "three\"", "quotes", "p four", "three\"", "quotes", "p6", "", "", NULL}, 0x3e1}},
1277 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", 0xf3,
1278 {" \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1279 {"", "four\"quotes p", "three fourquotes p5 \"", "", "", "", NULL}, 0xde1}},
1281 /* Quoted strings cannot be continued by tacking on a non space character
1284 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", 0x1f3,
1285 {" \"p two\" \"p3\" \"p four\" \"p5\" \"p6\"",
1286 {"", "p two", "p3", "p four", "p5", "p6", NULL}, 0}},
1288 /* In quoted strings, the quotes are halved and an odd number closes the
1289 * string. Specifically:
1290 * 2n quotes -> n quotes
1291 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1293 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", 0xff3,
1294 {" \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\"",
1295 {"", "one q", "uote", "p four", "one q", "uote", "p7", "", "", NULL}, 0}},
1297 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", 0x1ff3,
1298 {" \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1299 {"", "two ", "quotes p", "three two", " quotes", "p5", "", "", "", "", NULL}, 0}},
1301 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", 0xff3,
1302 {" \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\"",
1303 {"", "three q\"", "uotes", "p four", "three q\"", "uotes", "p7", "", "", NULL}, 0x7e1}},
1305 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", 0xff3,
1306 {" \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1307 {"", "four \"", "quotes p", "three four", "", "quotes p5 \"", "", "", "", NULL}, 0x3e0}},
1309 /* The quoted string rules also apply to consecutive quotes at the start
1310 * of a parameter but don't count the opening quote!
1312 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", 0xbf3,
1313 {" \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\"",
1314 {"", "", "twoquotes", "p four", "", "twoquotes", "p7", "", "", NULL}, 0}},
1316 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", 0x6f3,
1317 {" \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1318 {"", "three", "quotes p", "three \"three", "quotes p5 \"", "", "", "", NULL}, 0x181}},
1320 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", 0xbf3,
1321 {" \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\"",
1322 {"", "\"", "fourquotes", "p four", "\"", "fourquotes", "p7", "", "", NULL}, 0x3e1}},
1324 /* An unclosed quoted string gets lost! */
1325 {"Params23456", "p2 \"p3\" \"p4 is lost", 0x1c3,
1326 {" \"p2\" \"p3\" \"\" \"\" \"\"",
1327 {"", "p2", "p3", "", "", "", NULL}, 0}},
1329 /* Backslashes have no special meaning even when preceding quotes. All
1330 * they do is start an unquoted string.
1332 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", 0x73,
1333 {" \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\"",
1334 {"", "\" p\\three pfour\"", "pfive", "", NULL}, 0}},
1336 /* Environment variables are left untouched. */
1337 {"Params23456", "%TMPDIR% %t %c", 0x12,
1338 {" \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\"",
1339 {"", "%TMPDIR%", "%t", "%c", "", "", NULL}, 0}},
1341 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1342 * before the parameter!
1343 * (but not the previous parameter's closing quote fortunately)
1345 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", 0x3f3,
1346 {" ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 ",
1347 {"", "~2=p2 p3 p4 p5 p6 ", "~3= p3 p4 p5 p6 ", "~4= p4 p5 p6 ", "~5=", "p5", "p6", NULL}, 0}},
1349 /* %~n works even if there is no nth parameter. */
1350 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", 0x12,
1352 {"", "~9= ", NULL}, 0}},
1354 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", 0x12,
1356 {"", "~9=", NULL}, 0}},
1358 /* The %~n directives also transmit the tenth parameter and beyond. */
1359 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", 0x12,
1360 {" ~9=\" p9 p10 p11 and beyond!\"",
1361 {"", "~9= p9 p10 p11 and beyond!", NULL}, 0}},
1363 /* Bad formatting directives lose their % sign, except those followed by
1364 * a tilde! Environment variables are not expanded but lose their % sign.
1366 {"ParamsBad", "p2 p3 p4 p5", 0x12,
1367 {" \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\"",
1368 {"", "% - %~ %~0 %~1 %~a %~* a b c TMPDIR", NULL}, 0}},
1370 {NULL, NULL, 0, {NULL, {NULL}, 0}}
1373 static void test_argify(void)
1375 BOOL has_cl2a = TRUE;
1376 char fileA[MAX_PATH], params[2*MAX_PATH+12];
1378 const argify_tests_t* test;
1382 create_test_verb(".shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1383 create_test_verb(".shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1384 create_test_verb(".shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1385 create_test_verb(".shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1386 create_test_verb(".shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1387 create_test_verb(".shlexec", "Params20", 0, "Params20 \"%20\"");
1388 create_test_verb(".shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1390 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1392 test = argify_tests;
1393 while (test->params)
1395 /* trace("***** verb='%s' params='%s'\n", test->verb, test->params); */
1396 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1397 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1400 while (test->cmd.args[count])
1402 if ((test->todo & 0x1) == 0)
1403 /* +4 for the shlexec arguments, -1 because of the added ""
1404 * argument for the CommandLineToArgvW() tests.
1406 okChildInt("argcA", 4 + count - 1);
1408 okChildInt("argcA", 4 + count - 1);
1410 cmd = getChildString("Arguments", "cmdlineA");
1411 /* Our commands are such that the verb immediately precedes the
1412 * part we are interested in.
1414 if (cmd) cmd = strstr(cmd, test->verb);
1415 if (cmd) cmd += strlen(test->verb);
1416 if (!cmd) cmd = "(null)";
1417 if ((test->todo & 0x2) == 0)
1418 ok(!strcmp(cmd, test->cmd.cmd), "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1420 ok(!strcmp(cmd, test->cmd.cmd), "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1422 for (i = 0; i < count - 1; i++)
1425 sprintf(argname, "argvA%d", 4 + i);
1426 if ((test->todo & (1 << (i+4))) == 0)
1427 okChildString(argname, test->cmd.args[i+1]);
1429 okChildString(argname, test->cmd.args[i+1]);
1433 has_cl2a = test_one_cmdline(&(test->cmd));
1437 /* Test with a long parameter */
1438 for (rc = 0; rc < MAX_PATH; rc++)
1439 fileA[rc] = 'a' + rc % 26;
1440 fileA[MAX_PATH-1] = '\0';
1441 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1443 /* We need NOZONECHECKS on Win2003 to block a dialog */
1444 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1445 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1446 okChildInt("argcA", 4);
1447 okChildString("argvA3", fileA);
1450 static void test_filename(void)
1452 char filename[MAX_PATH];
1453 const filename_tests_t* test;
1457 test=filename_tests;
1458 while (test->basename)
1460 BOOL quotedfile = FALSE;
1462 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1464 win_skip("Skipping shellexecute of file with unassociated extension\n");
1469 sprintf(filename, test->basename, tmpdir);
1470 if (strchr(filename, '/'))
1480 if ((test->todo & 0x40)==0)
1482 rc=shell_execute(test->verb, filename, NULL, NULL);
1486 char quoted[MAX_PATH + 2];
1489 sprintf(quoted, "\"%s\"", filename);
1490 rc=shell_execute(test->verb, quoted, NULL, NULL);
1494 if ((test->todo & 0x1)==0)
1497 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1498 "%s failed: rc=%ld err=%u\n", shell_call,
1499 rc, GetLastError());
1503 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1504 rc, GetLastError());
1509 if ((test->todo & 0x2)==0)
1511 okChildInt("argcA", 5);
1515 okChildInt("argcA", 5);
1517 verb=(test->verb ? test->verb : "Open");
1518 if ((test->todo & 0x4)==0)
1520 okChildString("argvA3", verb);
1524 okChildString("argvA3", verb);
1526 if ((test->todo & 0x8)==0)
1528 okChildPath("argvA4", filename);
1532 okChildPath("argvA4", filename);
1538 test=noquotes_tests;
1539 while (test->basename)
1541 sprintf(filename, test->basename, tmpdir);
1542 rc=shell_execute(test->verb, filename, NULL, NULL);
1545 if ((test->todo & 0x1)==0)
1547 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1548 rc, GetLastError());
1552 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1553 rc, GetLastError());
1561 verb=(test->verb ? test->verb : "Open");
1562 if ((test->todo & 0x4)==0)
1564 okChildString("argvA3", verb);
1568 okChildString("argvA3", verb);
1577 space=strchr(str, ' ');
1580 sprintf(attrib, "argvA%d", count);
1581 if ((test->todo & 0x8)==0)
1583 okChildPath(attrib, str);
1587 okChildPath(attrib, str);
1594 if ((test->todo & 0x2)==0)
1596 okChildInt("argcA", count);
1600 okChildInt("argcA", count);
1606 if (dllver.dwMajorVersion != 0)
1608 /* The more recent versions of shell32.dll accept quoted filenames
1609 * while older ones (e.g. 4.00) don't. Still we want to test this
1610 * because IE 6 depends on the new behavior.
1611 * One day we may need to check the exact version of the dll but for
1612 * now making sure DllGetVersion() is present is sufficient.
1614 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1615 rc=shell_execute(NULL, filename, NULL, NULL);
1616 ok(rc > 32, "%s failed: rc=%ld err=%u\n", shell_call, rc,
1618 okChildInt("argcA", 5);
1619 okChildString("argvA3", "Open");
1620 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1621 okChildPath("argvA4", filename);
1627 const char* urlprefix;
1628 const char* basename;
1633 #define URL_SUCCESS 0x1
1634 #define USE_COLON 0x2
1635 #define USE_BSLASH 0x4
1637 static fileurl_tests_t fileurl_tests[]=
1639 /* How many slashes does it take... */
1640 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1641 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1642 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1643 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1644 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1645 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1646 {"file://///", "%s\\test file.shlexec", 0, 0x1},
1648 /* Test with Windows-style paths */
1649 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0x1},
1650 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0x1},
1652 /* Check handling of hostnames */
1653 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1654 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0x1},
1655 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1656 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0x1},
1657 {"file://::1/", "%s\\test file.shlexec", 0, 0x1},
1658 {"file://notahost/", "%s\\test file.shlexec", 0, 0x1},
1660 /* Environment variables are not expanded in URLs */
1661 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1662 {"file:///", "%s\\%%urlenvvar%% file.shlexec", 0, 0x1},
1667 static void test_fileurl(void)
1669 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1670 char command[MAX_PATH];
1671 const fileurl_tests_t* test;
1675 rc = (INT_PTR)ShellExecute(NULL, NULL, "file:///nosuchfile.shlexec", NULL, NULL, SW_SHOWNORMAL);
1678 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1682 get_long_path_name(tmpdir, longtmpdir, sizeof(longtmpdir)/sizeof(*longtmpdir));
1683 SetEnvironmentVariable("urlprefix", "file:///");
1684 SetEnvironmentVariable("urlenvvar", "test");
1687 while (test->basename)
1689 /* Build the file URL */
1690 sprintf(filename, test->basename, longtmpdir);
1691 strcpy(fileurl, test->urlprefix);
1692 strcat(fileurl, filename);
1693 s = fileurl + strlen(test->urlprefix);
1696 if (!(test->flags & USE_COLON) && *s == ':')
1698 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1703 /* Test it first with FindExecutable() */
1704 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1705 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%lu\n", fileurl, rc);
1707 /* Then ShellExecute() */
1708 rc = shell_execute(NULL, fileurl, NULL, NULL);
1709 if (bad_shellexecute)
1711 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1714 if (test->flags & URL_SUCCESS)
1716 if ((test->todo & 0x1) == 0)
1717 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1719 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1723 if ((test->todo & 0x1) == 0)
1724 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1725 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1726 "%s failed: bad rc=%lu\n", shell_call, rc);
1728 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1729 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1730 "%s failed: bad rc=%lu\n", shell_call, rc);
1734 if ((test->todo & 0x2) == 0)
1735 okChildInt("argcA", 5);
1737 okChildInt("argcA", 5);
1739 if ((test->todo & 0x4) == 0)
1740 okChildString("argvA3", "Open");
1742 okChildString("argvA3", "Open");
1744 if ((test->todo & 0x8) == 0)
1745 okChildPath("argvA4", filename);
1747 okChildPath("argvA4", filename);
1752 SetEnvironmentVariable("urlprefix", NULL);
1753 SetEnvironmentVariable("urlenvvar", NULL);
1756 static void test_find_executable(void)
1758 char notepad_path[MAX_PATH];
1759 char filename[MAX_PATH];
1760 char command[MAX_PATH];
1761 const filename_tests_t* test;
1764 if (!create_test_association(".sfe"))
1766 skip("Unable to create association for '.sfe'\n");
1769 create_test_verb(".sfe", "Open", 1, "%1");
1771 /* Don't test FindExecutable(..., NULL), it always crashes */
1773 strcpy(command, "your word");
1774 if (0) /* Can crash on Vista! */
1776 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1777 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1778 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1781 GetSystemDirectoryA( notepad_path, MAX_PATH );
1782 strcat( notepad_path, "\\notepad.exe" );
1784 /* Search for something that should be in the system-wide search path (no default directory) */
1785 strcpy(command, "your word");
1786 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
1787 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1788 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1790 /* Search for something that should be in the system-wide search path (with default directory) */
1791 strcpy(command, "your word");
1792 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
1793 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1794 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1796 strcpy(command, "your word");
1797 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1798 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1799 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1801 sprintf(filename, "%s\\test file.sfe", tmpdir);
1802 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1803 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1804 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1806 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1807 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1809 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1810 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1812 delete_test_association(".sfe");
1814 if (!create_test_association(".shl"))
1816 skip("Unable to create association for '.shl'\n");
1819 create_test_verb(".shl", "Open", 0, "Open");
1821 sprintf(filename, "%s\\test file.shl", tmpdir);
1822 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1823 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1825 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1826 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1828 delete_test_association(".shl");
1832 /* On Windows XP and 2003 FindExecutable() is completely broken.
1833 * Probably what it does is convert the filename to 8.3 format,
1834 * which as a side effect converts the '.shlfoo' extension to '.shl',
1835 * and then tries to find an association for '.shl'. This means it
1836 * will normally fail on most extensions with more than 3 characters,
1837 * like '.mpeg', etc.
1838 * Also it means we cannot do any other test.
1840 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1844 test=filename_tests;
1845 while (test->basename)
1847 sprintf(filename, test->basename, tmpdir);
1848 if (strchr(filename, '/'))
1859 /* Win98 does not '\0'-terminate command! */
1860 memset(command, '\0', sizeof(command));
1861 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1864 if ((test->todo & 0x10)==0)
1866 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1870 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1875 equal=strcmp(command, argv0) == 0 ||
1876 /* NT4 returns an extra 0x8 character! */
1877 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1878 if ((test->todo & 0x20)==0)
1880 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1881 filename, command, argv0);
1885 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1886 filename, command, argv0);
1894 static filename_tests_t lnk_tests[]=
1896 /* Pass bad / nonexistent filenames as a parameter */
1897 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1898 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1900 /* Pass regular paths as a parameter */
1901 {NULL, "%s\\test file.shlexec", 0xa, 33},
1902 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1904 /* Pass filenames with no association as a parameter */
1905 {NULL, "%s\\test file.noassoc", 0xa, 33},
1910 static void test_lnks(void)
1912 char filename[MAX_PATH];
1913 char params[MAX_PATH];
1914 const filename_tests_t* test;
1917 /* Should open through our association */
1918 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1919 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1920 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1921 okChildInt("argcA", 5);
1922 okChildString("argvA3", "Open");
1923 sprintf(params, "%s\\test file.shlexec", tmpdir);
1924 get_long_path_name(params, filename, sizeof(filename));
1925 okChildPath("argvA4", filename);
1927 todo_wait rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
1928 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1929 okChildInt("argcA", 5);
1930 todo_wine okChildString("argvA3", "Open");
1931 sprintf(params, "%s\\test file.shlexec", tmpdir);
1932 get_long_path_name(params, filename, sizeof(filename));
1933 todo_wine okChildPath("argvA4", filename);
1935 /* Should just run our executable */
1936 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1937 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1938 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1939 okChildInt("argcA", 4);
1940 okChildString("argvA3", "Lnk");
1942 /* Lnk's ContextMenuHandler has priority over an explicit class */
1943 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
1944 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1945 okChildInt("argcA", 4);
1946 okChildString("argvA3", "Lnk");
1948 if (dllver.dwMajorVersion>=6)
1951 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
1952 * Older versions don't or are quite buggy in this regard.
1954 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1962 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1963 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc,
1965 okChildInt("argcA", 4);
1966 okChildString("argvA3", "Lnk");
1969 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1971 while (test->basename)
1974 sprintf(params+1, test->basename, tmpdir);
1975 strcat(params,"\"");
1976 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1980 if ((test->todo & 0x1)==0)
1982 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
1983 rc, GetLastError());
1987 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
1988 rc, GetLastError());
1992 if ((test->todo & 0x2)==0)
1994 okChildInt("argcA", 5);
1998 okChildInt("argcA", 5);
2000 if ((test->todo & 0x4)==0)
2002 okChildString("argvA3", "Lnk");
2006 okChildString("argvA3", "Lnk");
2008 sprintf(params, test->basename, tmpdir);
2009 if ((test->todo & 0x8)==0)
2011 okChildPath("argvA4", params);
2015 okChildPath("argvA4", params);
2023 static void test_exes(void)
2025 char filename[MAX_PATH];
2029 sprintf(params, "shlexec \"%s\" Exec", child_file);
2031 /* We need NOZONECHECKS on Win2003 to block a dialog */
2032 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2034 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2035 okChildInt("argcA", 4);
2036 okChildString("argvA3", "Exec");
2038 if (! skip_noassoc_tests)
2040 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2041 if (CopyFile(argv0, filename, FALSE))
2043 rc=shell_execute(NULL, filename, params, NULL);
2045 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%lu\n", shell_call, rc);
2051 win_skip("Skipping shellexecute of file with unassociated extension\n");
2057 const char* command;
2058 const char* ddeexec;
2059 const char* application;
2063 const char* expectedDdeExec;
2067 static dde_tests_t dde_tests[] =
2069 /* Test passing and not passing command-line
2070 * argument, no DDE */
2071 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0},
2072 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0},
2074 /* Test passing and not passing command-line
2075 * argument, with DDE */
2076 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
2077 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0},
2079 /* Test unquoted %1 in command and ddeexec
2080 * (test filename has space) */
2081 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0},
2083 /* Test ifexec precedence over ddeexec */
2084 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0},
2086 /* Test default DDE topic */
2087 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0},
2089 /* Test default DDE application */
2090 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
2092 {NULL, NULL, NULL, NULL, NULL, 0, 0x0}
2095 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2097 return WaitForSingleObject(dde_ready_event, timeout);
2101 * WaitForInputIdle() will normally return immediately for console apps. That's
2102 * a problem for us because ShellExecute will assume that an app is ready to
2103 * receive DDE messages after it has called WaitForInputIdle() on that app.
2104 * To work around that we install our own version of WaitForInputIdle() that
2105 * will wait for the child to explicitly tell us that it is ready. We do that
2106 * by changing the entry for WaitForInputIdle() in the shell32 import address
2109 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2112 PIMAGE_NT_HEADERS nt_headers;
2113 DWORD import_directory_rva;
2114 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2116 base = (char *) GetModuleHandleA("shell32.dll");
2117 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2118 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2120 /* Search for the correct imported module by walking the import descriptors */
2121 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2122 while (U(*import_descriptor).OriginalFirstThunk != 0)
2124 char *import_module_name;
2126 import_module_name = base + import_descriptor->Name;
2127 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2128 lstrcmpiA(import_module_name, "user32") == 0)
2130 PIMAGE_THUNK_DATA int_entry;
2131 PIMAGE_THUNK_DATA iat_entry;
2133 /* The import name table and import address table are two parallel
2134 * arrays. We need the import name table to find the imported
2135 * routine and the import address table to patch the address, so
2136 * walk them side by side */
2137 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
2138 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2139 while (int_entry->u1.Ordinal != 0)
2141 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2143 PIMAGE_IMPORT_BY_NAME import_by_name;
2144 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2145 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2147 /* Found the correct routine in the correct imported module. Patch it. */
2149 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2150 iat_entry->u1.Function = (ULONG_PTR) new_func;
2151 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2161 import_descriptor++;
2165 static void test_dde(void)
2167 char filename[MAX_PATH], defApplication[MAX_PATH];
2168 const dde_tests_t* test;
2174 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2176 sprintf(filename, "%s\\test file.sde", tmpdir);
2178 /* Default service is application name minus path and extension */
2179 strcpy(defApplication, strrchr(argv0, '\\')+1);
2180 *strchr(defApplication, '.') = 0;
2182 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2183 4096, "winetest_shlexec_dde_map");
2184 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2187 while (test->command)
2189 if (!create_test_association(".sde"))
2191 skip("Unable to create association for '.sde'\n");
2194 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
2195 test->application, test->topic, test->ifexec);
2197 if (test->application != NULL || test->topic != NULL)
2199 strcpy(shared_block, test->application ? test->application : defApplication);
2200 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2204 shared_block[0] = '\0';
2205 shared_block[1] = '\0';
2209 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
2210 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL, NULL);
2211 CloseHandle(dde_ready_event);
2212 if ((test->todo & 0x1)==0)
2214 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call,
2215 rc, GetLastError());
2219 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call,
2220 rc, GetLastError());
2224 if ((test->todo & 0x2)==0)
2226 okChildInt("argcA", test->expectedArgs + 3);
2230 okChildInt("argcA", test->expectedArgs + 3);
2232 if (test->expectedArgs == 1)
2234 if ((test->todo & 0x4) == 0)
2236 okChildPath("argvA3", filename);
2240 okChildPath("argvA3", filename);
2243 if ((test->todo & 0x8) == 0)
2245 sprintf(params, test->expectedDdeExec, filename);
2246 okChildPath("ddeExec", params);
2250 sprintf(params, test->expectedDdeExec, filename);
2251 okChildPath("ddeExec", params);
2255 delete_test_association(".sde");
2259 UnmapViewOfFile(shared_block);
2261 hook_WaitForInputIdle((void *) WaitForInputIdle);
2264 #define DDE_DEFAULT_APP_VARIANTS 2
2267 const char* command;
2268 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2270 int rc[DDE_DEFAULT_APP_VARIANTS];
2271 } dde_default_app_tests_t;
2273 static dde_default_app_tests_t dde_default_app_tests[] =
2275 /* Windows XP and 98 handle default DDE app names in different ways.
2276 * The application name we see in the first test determines the pattern
2277 * of application names and return codes we will look for. */
2279 /* Test unquoted existing filename with a space */
2280 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
2281 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
2283 /* Test quoted existing filename with a space */
2284 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
2285 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
2287 /* Test unquoted filename with a space that doesn't exist, but
2289 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
2290 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
2292 /* Test quoted filename with a space that does not exist */
2293 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
2294 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
2296 /* Test filename supplied without the extension */
2297 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
2298 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
2300 /* Test an unquoted nonexistent filename */
2301 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
2302 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
2304 /* Test an application that will be found on the path */
2305 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
2306 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
2308 /* Test an application that will not be found on the path */
2309 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2310 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2312 {NULL, {NULL}, 0, {0}}
2318 DWORD threadIdParent;
2319 } dde_thread_info_t;
2321 static DWORD CALLBACK ddeThread(LPVOID arg)
2323 dde_thread_info_t *info = arg;
2324 assert(info && info->filename);
2325 PostThreadMessage(info->threadIdParent,
2327 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2332 static void test_dde_default_app(void)
2334 char filename[MAX_PATH];
2336 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2337 const dde_default_app_tests_t* test;
2344 post_quit_on_execute = FALSE;
2346 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2347 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
2348 assert(rc == DMLERR_NO_ERROR);
2350 sprintf(filename, "%s\\test file.sde", tmpdir);
2352 /* It is strictly not necessary to register an application name here, but wine's
2353 * DdeNameService implementation complains if 0L is passed instead of
2354 * hszApplication with DNS_FILTEROFF */
2355 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2356 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2357 assert(hszApplication && hszTopic);
2358 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
2360 test = dde_default_app_tests;
2361 while (test->command)
2363 if (!create_test_association(".sde"))
2365 skip("Unable to create association for '.sde'\n");
2368 sprintf(params, test->command, tmpdir);
2369 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
2371 ddeApplication[0] = 0;
2373 /* No application will be run as we will respond to the first DDE event,
2374 * so don't wait for it */
2377 assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
2378 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
2379 rc = msg.wParam > 32 ? 33 : msg.wParam;
2381 /* First test, find which set of test data we expect to see */
2382 if (test == dde_default_app_tests)
2385 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
2387 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
2393 if (i == DDE_DEFAULT_APP_VARIANTS)
2394 skip("Default DDE application test does not match any available results, using first expected data set.\n");
2397 if ((test->todo & 0x1)==0)
2399 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2400 rc, GetLastError());
2404 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2405 rc, GetLastError());
2409 if ((test->todo & 0x2)==0)
2411 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2412 "Expected application '%s', got '%s'\n",
2413 test->expectedDdeApplication[which], ddeApplication);
2417 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2418 "Expected application '%s', got '%s'\n",
2419 test->expectedDdeApplication[which], ddeApplication);
2423 delete_test_association(".sde");
2427 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
2428 assert(DdeFreeStringHandle(ddeInst, hszTopic));
2429 assert(DdeFreeStringHandle(ddeInst, hszApplication));
2430 assert(DdeUninitialize(ddeInst));
2433 static void init_test(void)
2436 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2437 char filename[MAX_PATH];
2438 WCHAR lnkfile[MAX_PATH];
2440 const char* const * testfile;
2445 hdll=GetModuleHandleA("shell32.dll");
2446 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2449 dllver.cbSize=sizeof(dllver);
2450 pDllGetVersion(&dllver);
2451 trace("major=%d minor=%d build=%d platform=%d\n",
2452 dllver.dwMajorVersion, dllver.dwMinorVersion,
2453 dllver.dwBuildNumber, dllver.dwPlatformID);
2457 memset(&dllver, 0, sizeof(dllver));
2460 r = CoInitialize(NULL);
2461 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2465 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
2466 assert(rc!=0 && rc<sizeof(argv0));
2467 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
2469 strcat(argv0, ".so");
2470 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
2471 "unable to find argv0!\n");
2474 GetTempPathA(sizeof(filename), filename);
2475 GetTempFileNameA(filename, "wt", 0, tmpdir);
2476 DeleteFileA( tmpdir );
2477 rc = CreateDirectoryA( tmpdir, NULL );
2478 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2479 /* Set %TMPDIR% for the tests */
2480 SetEnvironmentVariableA("TMPDIR", tmpdir);
2482 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2484 init_event(child_file);
2486 /* Set up the test files */
2492 sprintf(filename, *testfile, tmpdir);
2493 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2494 FILE_ATTRIBUTE_NORMAL, NULL);
2495 if (hfile==INVALID_HANDLE_VALUE)
2497 trace("unable to create '%s': err=%u\n", filename, GetLastError());
2504 /* Setup the test shortcuts */
2505 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2506 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2507 desc.description=NULL;
2509 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2512 desc.arguments="ignored";
2517 create_lnk(lnkfile, &desc, 0);
2519 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2520 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2521 desc.description=NULL;
2525 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2526 desc.arguments=params;
2531 create_lnk(lnkfile, &desc, 0);
2533 /* Create a basic association suitable for most tests */
2534 if (!create_test_association(".shlexec"))
2536 skip("Unable to create association for '.shlexec'\n");
2539 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2540 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2541 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2542 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2543 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2544 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2547 static void cleanup_test(void)
2549 char filename[MAX_PATH];
2550 const char* const * testfile;
2552 /* Delete the test files */
2556 sprintf(filename, *testfile, tmpdir);
2557 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2558 SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
2559 DeleteFile(filename);
2562 DeleteFile(child_file);
2563 RemoveDirectoryA(tmpdir);
2565 /* Delete the test association */
2566 delete_test_association(".shlexec");
2568 CloseHandle(hEvent);
2573 static void test_directory(void)
2575 char path[MAX_PATH], newdir[MAX_PATH];
2579 /* copy this executable to a new folder and cd to it */
2580 sprintf(newdir, "%s\\newfolder", tmpdir);
2581 rc = CreateDirectoryA( newdir, NULL );
2582 ok( rc, "failed to create %s err %u\n", newdir, GetLastError() );
2583 sprintf(path, "%s\\%s", newdir, path_find_file_name(argv0));
2584 CopyFileA(argv0, path, FALSE);
2585 SetCurrentDirectory(tmpdir);
2587 sprintf(params, "shlexec \"%s\" Exec", child_file);
2589 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2590 NULL, path_find_file_name(argv0), params, NULL, NULL);
2591 todo_wine ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2593 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2594 NULL, path_find_file_name(argv0), params, newdir, NULL);
2595 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2596 okChildInt("argcA", 4);
2597 okChildString("argvA3", "Exec");
2598 todo_wine okChildPath("longPath", path);
2601 RemoveDirectoryA(newdir);
2607 myARGC = winetest_get_mainargs(&myARGV);
2610 doChild(myARGC, myARGV);
2616 test_commandline2argv();
2618 test_lpFile_parsed();
2621 test_find_executable();
2625 test_dde_default_app();