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* param)
92 static char shell_call[2048]="";
93 static int shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
97 strcpy(shell_call, "ShellExecute(");
98 strcat_param(shell_call, operation);
99 strcat(shell_call, ", ");
100 strcat_param(shell_call, file);
101 strcat(shell_call, ", ");
102 strcat_param(shell_call, parameters);
103 strcat(shell_call, ", ");
104 strcat_param(shell_call, directory);
105 strcat(shell_call, ")");
106 if (winetest_debug > 1)
107 trace("%s\n", shell_call);
109 DeleteFile(child_file);
110 SetLastError(0xcafebabe);
112 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
113 * association it displays the 'Open With' dialog and I could not find
114 * a flag to prevent this.
116 rc=(INT_PTR)ShellExecute(NULL, operation, file, parameters, directory, SW_SHOWNORMAL);
121 wait_rc=WaitForSingleObject(hEvent, 5000);
122 if (wait_rc == WAIT_TIMEOUT)
124 HWND wnd = FindWindowA("#32770", "Windows");
127 SendMessage(wnd, WM_CLOSE, 0, 0);
128 win_skip("Skipping shellexecute of file with unassociated extension\n");
129 skip_noassoc_tests = TRUE;
133 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "WaitForSingleObject returned %d\n", wait_rc);
135 /* The child process may have changed the result file, so let profile
136 * functions know about it
138 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
145 static int shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
146 LPCSTR parameters, LPCSTR directory)
148 SHELLEXECUTEINFO sei;
152 strcpy(shell_call, "ShellExecuteEx(");
153 strcat_param(shell_call, operation);
154 strcat(shell_call, ", ");
155 strcat_param(shell_call, file);
156 strcat(shell_call, ", ");
157 strcat_param(shell_call, parameters);
158 strcat(shell_call, ", ");
159 strcat_param(shell_call, directory);
160 strcat(shell_call, ")");
161 if (winetest_debug > 1)
162 trace("%s\n", shell_call);
164 sei.cbSize=sizeof(sei);
165 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
167 sei.lpVerb=operation;
169 sei.lpParameters=parameters;
170 sei.lpDirectory=directory;
171 sei.nShow=SW_SHOWNORMAL;
172 sei.hInstApp=NULL; /* Out */
178 sei.hProcess=NULL; /* Out */
180 DeleteFile(child_file);
181 SetLastError(0xcafebabe);
182 success=ShellExecuteEx(&sei);
183 rc=(INT_PTR)sei.hInstApp;
184 ok((success && rc > 32) || (!success && rc <= 32),
185 "%s rc=%d and hInstApp=%ld is not allowed\n", shell_call, success, rc);
190 if (sei.hProcess!=NULL)
192 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
193 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
195 wait_rc=WaitForSingleObject(hEvent, 5000);
196 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
198 /* The child process may have changed the result file, so let profile
199 * functions know about it
201 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
212 * Functions to create / delete associations wrappers
216 static BOOL create_test_association(const char* extension)
218 HKEY hkey, hkey_shell;
219 char class[MAX_PATH];
222 sprintf(class, "shlexec%s", extension);
223 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
225 if (rc != ERROR_SUCCESS)
228 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
229 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
232 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
233 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
234 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
236 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
237 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
238 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
241 CloseHandle(hkey_shell);
246 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
247 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
250 DWORD dwMaxSubkeyLen, dwMaxValueLen;
251 DWORD dwMaxLen, dwSize;
252 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
257 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
261 /* Get highest length for keys, values */
262 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
263 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
264 if (ret) goto cleanup;
268 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
269 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
271 /* Name too big: alloc a buffer for it */
272 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
274 ret = ERROR_NOT_ENOUGH_MEMORY;
280 /* Recursively delete all the subkeys */
284 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
285 NULL, NULL, NULL)) break;
287 ret = myRegDeleteTreeA(hSubKey, lpszName);
288 if (ret) goto cleanup;
292 ret = RegDeleteKeyA(hKey, lpszSubKey);
297 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
298 NULL, NULL, NULL, NULL)) break;
300 ret = RegDeleteValueA(hKey, lpszName);
301 if (ret) goto cleanup;
305 /* Free buffer if allocated */
306 if (lpszName != szNameBuf)
307 HeapFree( GetProcessHeap(), 0, lpszName);
309 RegCloseKey(hSubKey);
313 static void delete_test_association(const char* extension)
315 char class[MAX_PATH];
317 sprintf(class, "shlexec%s", extension);
318 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
319 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
322 static void create_test_verb_dde(const char* extension, const char* verb,
323 int rawcmd, const char* cmdtail, const char *ddeexec,
324 const char *application, const char *topic,
327 HKEY hkey_shell, hkey_verb, hkey_cmd;
328 char shell[MAX_PATH];
332 sprintf(shell, "shlexec%s\\shell", extension);
333 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
334 KEY_CREATE_SUB_KEY, &hkey_shell);
335 assert(rc==ERROR_SUCCESS);
336 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
337 NULL, &hkey_verb, NULL);
338 assert(rc==ERROR_SUCCESS);
339 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
340 NULL, &hkey_cmd, NULL);
341 assert(rc==ERROR_SUCCESS);
345 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
349 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
350 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
351 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
352 assert(rc==ERROR_SUCCESS);
353 HeapFree(GetProcessHeap(), 0, cmd);
358 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
360 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
361 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
362 assert(rc==ERROR_SUCCESS);
363 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
365 assert(rc==ERROR_SUCCESS);
368 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
369 NULL, &hkey_application, NULL);
370 assert(rc==ERROR_SUCCESS);
371 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
372 strlen(application)+1);
373 assert(rc==ERROR_SUCCESS);
374 CloseHandle(hkey_application);
378 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
379 NULL, &hkey_topic, NULL);
380 assert(rc==ERROR_SUCCESS);
381 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
383 assert(rc==ERROR_SUCCESS);
384 CloseHandle(hkey_topic);
388 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
389 NULL, &hkey_ifexec, NULL);
390 assert(rc==ERROR_SUCCESS);
391 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
393 assert(rc==ERROR_SUCCESS);
394 CloseHandle(hkey_ifexec);
396 CloseHandle(hkey_ddeexec);
399 CloseHandle(hkey_shell);
400 CloseHandle(hkey_verb);
401 CloseHandle(hkey_cmd);
404 static void create_test_verb(const char* extension, const char* verb,
405 int rawcmd, const char* cmdtail)
407 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
413 * Functions to check that the child process was started just right
414 * (borrowed from dlls/kernel32/tests/process.c)
418 static const char* encodeA(const char* str)
420 static char encoded[2*1024+1];
425 len = strlen(str) + 1;
426 if (len >= sizeof(encoded)/2)
428 fprintf(stderr, "string is too long!\n");
432 for (i = 0; i < len; i++)
433 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
438 static unsigned decode_char(char c)
440 if (c >= '0' && c <= '9') return c - '0';
441 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
442 assert(c >= 'A' && c <= 'F');
446 static char* decodeA(const char* str)
448 static char decoded[1024];
452 len = strlen(str) / 2;
453 if (!len--) return NULL;
454 if (len >= sizeof(decoded))
456 fprintf(stderr, "string is too long!\n");
460 for (i = 0; i < len; i++)
461 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
466 static void childPrintf(HANDLE h, const char* fmt, ...)
472 va_start(valist, fmt);
473 vsprintf(buffer, fmt, valist);
475 WriteFile(h, buffer, strlen(buffer), &w, NULL);
478 static DWORD ddeInst;
480 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
481 static BOOL post_quit_on_execute;
483 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
484 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
485 ULONG_PTR dwData1, ULONG_PTR dwData2)
489 if (winetest_debug > 2)
490 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
491 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
496 if (!DdeCmpStringHandles(hsz1, hszTopic))
498 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
499 assert(size < MAX_PATH);
500 return (HDDEDATA)TRUE;
502 return (HDDEDATA)FALSE;
505 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
506 assert(size < MAX_PATH);
507 DdeFreeDataHandle(hData);
508 if (post_quit_on_execute)
510 return (HDDEDATA)DDE_FACK;
518 * This is just to make sure the child won't run forever stuck in a GetMessage()
519 * loop when DDE fails for some reason.
521 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
523 trace("childTimeout called\n");
528 static void doChild(int argc, char** argv)
530 char *filename, longpath[MAX_PATH] = "";
541 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
542 if (hFile == INVALID_HANDLE_VALUE)
546 childPrintf(hFile, "[Arguments]\r\n");
547 if (winetest_debug > 2)
548 trace("argcA=%d\n", argc);
549 childPrintf(hFile, "argcA=%d\r\n", argc);
550 for (i = 0; i < argc; i++)
552 if (winetest_debug > 2)
553 trace("argvA%d=%s\n", i, argv[i]);
554 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
556 GetModuleFileNameA(GetModuleHandleA(NULL), longpath, MAX_PATH);
557 childPrintf(hFile, "longPath=%s\r\n", encodeA(longpath));
559 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
562 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
564 if (shared_block[0] != '\0' || shared_block[1] != '\0')
566 post_quit_on_execute = TRUE;
568 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
569 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
570 assert(rc == DMLERR_NO_ERROR);
571 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
572 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
573 assert(hszApplication && hszTopic);
574 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
576 timer = SetTimer(NULL, 0, 2500, childTimeout);
578 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
580 CloseHandle(dde_ready);
582 while (GetMessage(&msg, NULL, 0, 0))
583 DispatchMessage(&msg);
586 KillTimer(NULL, timer);
587 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
588 assert(DdeFreeStringHandle(ddeInst, hszTopic));
589 assert(DdeFreeStringHandle(ddeInst, hszApplication));
590 assert(DdeUninitialize(ddeInst));
594 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
596 CloseHandle(dde_ready);
599 UnmapViewOfFile(shared_block);
601 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
606 init_event(filename);
611 static char* getChildString(const char* sect, const char* key)
616 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
617 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
618 assert(!(strlen(buf) & 1));
623 static void dump_child(void)
625 if (winetest_debug > 1)
631 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
632 trace("argcA=%d\n",c);
635 sprintf(key, "argvA%d", i);
636 str=getChildString("Arguments", key);
637 trace("%s=%s\n", key, str);
642 static int StrCmpPath(const char* s1, const char* s2)
644 if (!s1 && !s2) return 0;
655 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
657 while (*s1=='/' || *s1=='\\')
659 while (*s2=='/' || *s2=='\\')
662 else if (toupper(*s1)==toupper(*s2))
679 static void _okChildString(const char* file, int line, const char* key, const char* expected)
682 result=getChildString("Arguments", key);
685 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
688 ok_(file, line)(lstrcmpiA(result, expected) == 0,
689 "%s expected '%s', got '%s'\n", key, expected, result);
692 static void _okChildPath(const char* file, int line, const char* key, const char* expected)
695 result=getChildString("Arguments", key);
698 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
701 ok_(file, line)(StrCmpPath(result, expected) == 0,
702 "%s expected '%s', got '%s'\n", key, expected, result);
705 static void _okChildInt(const char* file, int line, const char* key, int expected)
708 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
709 ok_(file, line)(result == expected,
710 "%s expected %d, but got %d\n", key, expected, result);
713 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
714 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
715 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
719 * GetLongPathNameA equivalent that supports Win95 and WinNT
723 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
725 char tmplongpath[MAX_PATH];
727 DWORD sp = 0, lp = 0;
729 WIN32_FIND_DATAA wfd;
732 if (!shortpath || !shortpath[0])
735 if (shortpath[1] == ':')
737 tmplongpath[0] = shortpath[0];
738 tmplongpath[1] = ':';
742 while (shortpath[sp])
744 /* check for path delimiters and reproduce them */
745 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
747 if (!lp || tmplongpath[lp-1] != '\\')
749 /* strip double "\\" */
750 tmplongpath[lp++] = '\\';
752 tmplongpath[lp] = 0; /* terminate string */
758 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
760 tmplongpath[lp++] = *p++;
761 tmplongpath[lp++] = *p++;
763 for (; *p && *p != '/' && *p != '\\'; p++);
764 tmplen = p - (shortpath + sp);
765 lstrcpyn(tmplongpath + lp, shortpath + sp, tmplen + 1);
766 /* Check if the file exists and use the existing file name */
767 goit = FindFirstFileA(tmplongpath, &wfd);
768 if (goit == INVALID_HANDLE_VALUE)
771 strcpy(tmplongpath + lp, wfd.cFileName);
772 lp += strlen(tmplongpath + lp);
775 tmplen = strlen(shortpath) - 1;
776 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
777 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
778 tmplongpath[lp++] = shortpath[tmplen];
781 tmplen = strlen(tmplongpath) + 1;
782 if (tmplen <= longlen)
784 strcpy(longpath, tmplongpath);
785 tmplen--; /* length without 0 */
793 * PathFindFileNameA equivalent that supports WinNT
797 static LPSTR path_find_file_name(LPCSTR lpszPath)
799 LPCSTR lastSlash = lpszPath;
801 while (lpszPath && *lpszPath)
803 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
804 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
805 lastSlash = lpszPath + 1;
806 lpszPath = CharNext(lpszPath);
808 return (LPSTR)lastSlash;
817 static const char* testfiles[]=
819 "%s\\test file.shlexec",
820 "%s\\%%nasty%% $file.shlexec",
821 "%s\\test file.noassoc",
822 "%s\\test file.noassoc.shlexec",
823 "%s\\test file.shlexec.noassoc",
824 "%s\\test_shortcut_shlexec.lnk",
825 "%s\\test_shortcut_exe.lnk",
827 "%s\\test file.shlfoo",
829 "%s\\masked file.shlexec",
834 "%s\\simple.shlexec",
835 "%s\\drawback_file.noassoc",
836 "%s\\drawback_file.noassoc foo.shlexec",
837 "%s\\drawback_nonexist.noassoc foo.shlexec",
844 const char* basename;
849 static filename_tests_t filename_tests[]=
851 /* Test bad / nonexistent filenames */
852 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
853 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
856 {NULL, "%s\\test file.shlexec", 0x0, 33},
857 {NULL, "%s\\test file.shlexec.", 0x0, 33},
858 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
859 {NULL, "%s/test file.shlexec", 0x0, 33},
861 /* Test filenames with no association */
862 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
864 /* Test double extensions */
865 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
866 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
868 /* Test alternate verbs */
869 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
870 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
872 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
873 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
875 /* Test file masked due to space */
876 {NULL, "%s\\masked file.shlexec", 0x1, 33},
877 /* Test if quoting prevents the masking */
878 {NULL, "%s\\masked file.shlexec", 0x40, 33},
883 static filename_tests_t noquotes_tests[]=
885 /* Test unquoted '%1' thingies */
886 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
887 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
888 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
893 static void test_lpFile_parsed(void)
895 /* basename tmpdir */
896 const char* shorttmpdir;
898 const char *testfile;
899 char fileA[MAX_PATH];
903 GetTempPathA(sizeof(fileA), fileA);
904 shorttmpdir = tmpdir + strlen(fileA);
906 /* ensure tmpdir is in %TEMP%: GetTempPath() can succeed even if TEMP is undefined */
907 SetEnvironmentVariableA("TEMP", fileA);
909 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
910 testfile = "%s\\drawback_file.noassoc foo.shlexec";
911 sprintf(fileA, testfile, tmpdir);
912 rc=shell_execute(NULL, fileA, NULL, NULL);
915 "expected success (33), got %s (%d), lpFile: %s\n",
916 rc > 32 ? "success" : "failure", rc, fileA
920 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
921 testfile = "\"%s\\drawback_file.noassoc foo.shlexec\"";
922 sprintf(fileA, testfile, tmpdir);
923 rc=shell_execute(NULL, fileA, NULL, NULL);
924 ok(rc>32 || broken(rc == 2) /* Win95/NT4 */,
925 "expected success (33), got %s (%d), lpFile: %s\n",
926 rc > 32 ? "success" : "failure", rc, fileA
929 /* error should be 2, not 31 */
930 testfile = "\"%s\\drawback_file.noassoc\" foo.shlexec";
931 sprintf(fileA, testfile, tmpdir);
932 rc=shell_execute(NULL, fileA, NULL, NULL);
934 "expected failure (2), got %s (%d), lpFile: %s\n",
935 rc > 32 ? "success" : "failure", rc, fileA
938 /* ""command"" not works on wine (and real win9x and w2k) */
939 testfile = "\"\"%s\\simple.shlexec\"\"";
940 sprintf(fileA, testfile, tmpdir);
941 rc=shell_execute(NULL, fileA, NULL, NULL);
943 ok(rc>32 || broken(rc == 2) /* Win9x/2000 */,
944 "expected success (33), got %s (%d), lpFile: %s\n",
945 rc > 32 ? "success" : "failure", rc, fileA
949 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
950 testfile = "%s\\drawback_nonexist.noassoc foo.shlexec";
951 sprintf(fileA, testfile, tmpdir);
952 rc=shell_execute(NULL, fileA, NULL, NULL);
954 "expected success (33), got %s (%d), lpFile: %s\n",
955 rc > 32 ? "success" : "failure", rc, fileA
958 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
959 testfile = "%%TEMP%%\\%s\\simple.shlexec";
960 sprintf(fileA, testfile, shorttmpdir);
961 rc=shell_execute(NULL, fileA, NULL, NULL);
964 "expected failure (2), got %s (%d), lpFile: %s\n",
965 rc > 32 ? "success" : "failure", rc, fileA
970 testfile = "\"%%TEMP%%\\%s\\simple.shlexec\"";
971 sprintf(fileA, testfile, shorttmpdir);
972 rc=shell_execute(NULL, fileA, NULL, NULL);
975 "expected failure (2), got %s (%d), lpFile: %s\n",
976 rc > 32 ? "success" : "failure", rc, fileA
980 /* test SEE_MASK_DOENVSUBST works */
981 testfile = "%%TEMP%%\\%s\\simple.shlexec";
982 sprintf(fileA, testfile, shorttmpdir);
983 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI, NULL, fileA, NULL, NULL);
985 "expected success (33), got %s (%d), lpFile: %s\n",
986 rc > 32 ? "success" : "failure", rc, fileA
989 /* quoted lpFile not works only on real win95 and nt4 */
990 testfile = "\"%%TEMP%%\\%s\\simple.shlexec\"";
991 sprintf(fileA, testfile, shorttmpdir);
992 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI, NULL, fileA, NULL, NULL);
993 ok(rc>32 || broken(rc == 2) /* Win95/NT4 */,
994 "expected success (33), got %s (%d), lpFile: %s\n",
995 rc > 32 ? "success" : "failure", rc, fileA
1000 static void test_argify(void)
1002 char fileA[MAX_PATH];
1006 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1009 rc=shell_execute("NoQuotesParam2", fileA, "a b", NULL);
1011 "expected success (33), got %s (%d), lpFile: %s\n",
1012 rc > 32 ? "success" : "failure", rc, fileA
1016 okChildInt("argcA", 5);
1017 okChildString("argvA4", "a");
1021 /* '"a"""' -> 'a"' */
1022 rc=shell_execute("NoQuotesParam2", fileA, "\"a:\"\"some string\"\"\"", NULL);
1024 "expected success (33), got %s (%d), lpFile: %s\n",
1025 rc > 32 ? "success" : "failure", rc, fileA
1029 okChildInt("argcA", 5);
1031 okChildString("argvA4", "a:some string");
1036 /* backslash isn't escape char
1037 * '"a\""' -> '"a\""' */
1038 rc=shell_execute("NoQuotesParam2", fileA, "\"a:\\\"some string\\\"\"", NULL);
1040 "expected success (33), got %s (%d), lpFile: %s\n",
1041 rc > 32 ? "success" : "failure", rc, fileA
1045 okChildInt("argcA", 5);
1047 okChildString("argvA4", "a:\\");
1052 /* \t isn't whitespace */
1053 rc=shell_execute("QuotedParam2", fileA, "a\tb c", NULL);
1055 "expected success (33), got %s (%d), lpFile: %s\n",
1056 rc > 32 ? "success" : "failure", rc, fileA
1060 okChildInt("argcA", 5);
1062 okChildString("argvA4", "a\tb");
1067 rc=shell_execute("NoQuotesAllParams", fileA, "a b c d e f g h", NULL);
1069 "expected success (33), got %s (%d), lpFile: %s\n",
1070 rc > 32 ? "success" : "failure", rc, fileA
1075 okChildInt("argcA", 12);
1076 okChildString("argvA4", "a");
1077 okChildString("argvA11", "h");
1081 /* %* can sometimes contain only whitespaces and no args */
1082 rc=shell_execute("QuotedAllParams", fileA, " ", NULL);
1084 "expected success (33), got %s (%d), lpFile: %s\n",
1085 rc > 32 ? "success" : "failure", rc, fileA
1090 okChildInt("argcA", 5);
1091 okChildString("argvA4", " ");
1096 rc=shell_execute("NoQuotesParams345etc", fileA, "a b c d e f g h", NULL);
1098 "expected success (33), got %s (%d), lpFile: %s\n",
1099 rc > 32 ? "success" : "failure", rc, fileA
1104 okChildInt("argcA", 11);
1105 okChildString("argvA4", "b");
1106 okChildString("argvA10", "h");
1110 /* %~3 is rest of command line starting with whitespaces after 2nd arg */
1111 rc=shell_execute("QuotedParams345etc", fileA, "a ", NULL);
1113 "expected success (33), got %s (%d), lpFile: %s\n",
1114 rc > 32 ? "success" : "failure", rc, fileA
1118 okChildInt("argcA", 5);
1120 okChildString("argvA4", " ");
1126 static void test_filename(void)
1128 char filename[MAX_PATH];
1129 const filename_tests_t* test;
1133 test=filename_tests;
1134 while (test->basename)
1136 BOOL quotedfile = FALSE;
1138 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1140 win_skip("Skipping shellexecute of file with unassociated extension\n");
1145 sprintf(filename, test->basename, tmpdir);
1146 if (strchr(filename, '/'))
1156 if ((test->todo & 0x40)==0)
1158 rc=shell_execute(test->verb, filename, NULL, NULL);
1162 char quoted[MAX_PATH + 2];
1165 sprintf(quoted, "\"%s\"", filename);
1166 rc=shell_execute(test->verb, quoted, NULL, NULL);
1170 if ((test->todo & 0x1)==0)
1173 broken(quotedfile && rc == 2), /* NT4 */
1174 "%s failed: rc=%d err=%d\n", shell_call,
1175 rc, GetLastError());
1179 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1180 rc, GetLastError());
1185 if ((test->todo & 0x2)==0)
1187 okChildInt("argcA", 5);
1191 okChildInt("argcA", 5);
1193 verb=(test->verb ? test->verb : "Open");
1194 if ((test->todo & 0x4)==0)
1196 okChildString("argvA3", verb);
1200 okChildString("argvA3", verb);
1202 if ((test->todo & 0x8)==0)
1204 okChildPath("argvA4", filename);
1208 okChildPath("argvA4", filename);
1214 test=noquotes_tests;
1215 while (test->basename)
1217 sprintf(filename, test->basename, tmpdir);
1218 rc=shell_execute(test->verb, filename, NULL, NULL);
1221 if ((test->todo & 0x1)==0)
1223 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1224 rc, GetLastError());
1228 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1229 rc, GetLastError());
1237 verb=(test->verb ? test->verb : "Open");
1238 if ((test->todo & 0x4)==0)
1240 okChildString("argvA3", verb);
1244 okChildString("argvA3", verb);
1253 space=strchr(str, ' ');
1256 sprintf(attrib, "argvA%d", count);
1257 if ((test->todo & 0x8)==0)
1259 okChildPath(attrib, str);
1263 okChildPath(attrib, str);
1270 if ((test->todo & 0x2)==0)
1272 okChildInt("argcA", count);
1276 okChildInt("argcA", count);
1282 if (dllver.dwMajorVersion != 0)
1284 /* The more recent versions of shell32.dll accept quoted filenames
1285 * while older ones (e.g. 4.00) don't. Still we want to test this
1286 * because IE 6 depends on the new behavior.
1287 * One day we may need to check the exact version of the dll but for
1288 * now making sure DllGetVersion() is present is sufficient.
1290 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1291 rc=shell_execute(NULL, filename, NULL, NULL);
1292 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1294 okChildInt("argcA", 5);
1295 okChildString("argvA3", "Open");
1296 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1297 okChildPath("argvA4", filename);
1301 static void test_find_executable(void)
1303 char filename[MAX_PATH];
1304 char command[MAX_PATH];
1305 const filename_tests_t* test;
1308 if (!create_test_association(".sfe"))
1310 skip("Unable to create association for '.sfe'\n");
1313 create_test_verb(".sfe", "Open", 1, "%1");
1315 /* Don't test FindExecutable(..., NULL), it always crashes */
1317 strcpy(command, "your word");
1318 if (0) /* Can crash on Vista! */
1320 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1321 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1322 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1325 strcpy(command, "your word");
1326 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1327 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1328 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1330 sprintf(filename, "%s\\test file.sfe", tmpdir);
1331 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1332 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1333 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1335 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1336 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1338 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1339 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1341 delete_test_association(".sfe");
1343 if (!create_test_association(".shl"))
1345 skip("Unable to create association for '.shl'\n");
1348 create_test_verb(".shl", "Open", 0, "Open");
1350 sprintf(filename, "%s\\test file.shl", tmpdir);
1351 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1352 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1354 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1355 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1357 delete_test_association(".shl");
1361 /* On Windows XP and 2003 FindExecutable() is completely broken.
1362 * Probably what it does is convert the filename to 8.3 format,
1363 * which as a side effect converts the '.shlfoo' extension to '.shl',
1364 * and then tries to find an association for '.shl'. This means it
1365 * will normally fail on most extensions with more than 3 characters,
1366 * like '.mpeg', etc.
1367 * Also it means we cannot do any other test.
1369 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1373 test=filename_tests;
1374 while (test->basename)
1376 sprintf(filename, test->basename, tmpdir);
1377 if (strchr(filename, '/'))
1388 /* Win98 does not '\0'-terminate command! */
1389 memset(command, '\0', sizeof(command));
1390 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1393 if ((test->todo & 0x10)==0)
1395 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1399 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1404 equal=strcmp(command, argv0) == 0 ||
1405 /* NT4 returns an extra 0x8 character! */
1406 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1407 if ((test->todo & 0x20)==0)
1409 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1410 filename, command, argv0);
1414 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1415 filename, command, argv0);
1423 static filename_tests_t lnk_tests[]=
1425 /* Pass bad / nonexistent filenames as a parameter */
1426 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1427 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1429 /* Pass regular paths as a parameter */
1430 {NULL, "%s\\test file.shlexec", 0xa, 33},
1431 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1433 /* Pass filenames with no association as a parameter */
1434 {NULL, "%s\\test file.noassoc", 0xa, 33},
1439 static void test_lnks(void)
1441 char filename[MAX_PATH];
1442 char params[MAX_PATH];
1443 const filename_tests_t* test;
1446 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1447 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1448 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1450 okChildInt("argcA", 5);
1451 okChildString("argvA3", "Open");
1452 sprintf(params, "%s\\test file.shlexec", tmpdir);
1453 get_long_path_name(params, filename, sizeof(filename));
1454 okChildPath("argvA4", filename);
1456 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1457 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1458 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1460 okChildInt("argcA", 4);
1461 okChildString("argvA3", "Lnk");
1463 if (dllver.dwMajorVersion>=6)
1466 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
1467 * Older versions don't or are quite buggy in this regard.
1469 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1477 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1478 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1480 okChildInt("argcA", 4);
1481 okChildString("argvA3", "Lnk");
1484 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1486 while (test->basename)
1489 sprintf(params+1, test->basename, tmpdir);
1490 strcat(params,"\"");
1491 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1495 if ((test->todo & 0x1)==0)
1497 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1498 rc, GetLastError());
1502 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1503 rc, GetLastError());
1507 if ((test->todo & 0x2)==0)
1509 okChildInt("argcA", 5);
1513 okChildInt("argcA", 5);
1515 if ((test->todo & 0x4)==0)
1517 okChildString("argvA3", "Lnk");
1521 okChildString("argvA3", "Lnk");
1523 sprintf(params, test->basename, tmpdir);
1524 if ((test->todo & 0x8)==0)
1526 okChildPath("argvA4", params);
1530 okChildPath("argvA4", params);
1538 static void test_exes(void)
1540 char filename[MAX_PATH];
1544 sprintf(params, "shlexec \"%s\" Exec", child_file);
1546 /* We need NOZONECHECKS on Win2003 to block a dialog */
1547 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1549 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1550 okChildInt("argcA", 4);
1551 okChildString("argvA3", "Exec");
1553 if (! skip_noassoc_tests)
1555 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1556 if (CopyFile(argv0, filename, FALSE))
1558 rc=shell_execute(NULL, filename, params, NULL);
1560 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1566 win_skip("Skipping shellexecute of file with unassociated extension\n");
1570 static void test_exes_long(void)
1572 char filename[MAX_PATH];
1574 char longparam[MAX_PATH];
1577 for (rc = 0; rc < MAX_PATH; rc++)
1578 longparam[rc]='a'+rc%26;
1579 longparam[MAX_PATH-1]=0;
1582 sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1584 /* We need NOZONECHECKS on Win2003 to block a dialog */
1585 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1587 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1588 okChildInt("argcA", 4);
1589 okChildString("argvA3", longparam);
1591 if (! skip_noassoc_tests)
1593 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1594 if (CopyFile(argv0, filename, FALSE))
1596 rc=shell_execute(NULL, filename, params, NULL);
1598 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1604 win_skip("Skipping shellexecute of file with unassociated extension\n");
1610 const char* command;
1611 const char* ddeexec;
1612 const char* application;
1616 const char* expectedDdeExec;
1620 static dde_tests_t dde_tests[] =
1622 /* Test passing and not passing command-line
1623 * argument, no DDE */
1624 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0},
1625 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0},
1627 /* Test passing and not passing command-line
1628 * argument, with DDE */
1629 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
1630 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0},
1632 /* Test unquoted %1 in command and ddeexec
1633 * (test filename has space) */
1634 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0},
1636 /* Test ifexec precedence over ddeexec */
1637 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0},
1639 /* Test default DDE topic */
1640 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0},
1642 /* Test default DDE application */
1643 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
1645 {NULL, NULL, NULL, NULL, NULL, 0, 0x0}
1648 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
1650 return WaitForSingleObject(dde_ready_event, timeout);
1654 * WaitForInputIdle() will normally return immediately for console apps. That's
1655 * a problem for us because ShellExecute will assume that an app is ready to
1656 * receive DDE messages after it has called WaitForInputIdle() on that app.
1657 * To work around that we install our own version of WaitForInputIdle() that
1658 * will wait for the child to explicitly tell us that it is ready. We do that
1659 * by changing the entry for WaitForInputIdle() in the shell32 import address
1662 static void hook_WaitForInputIdle(void *new_func)
1665 PIMAGE_NT_HEADERS nt_headers;
1666 DWORD import_directory_rva;
1667 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
1669 base = (char *) GetModuleHandleA("shell32.dll");
1670 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
1671 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
1673 /* Search for the correct imported module by walking the import descriptors */
1674 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
1675 while (U(*import_descriptor).OriginalFirstThunk != 0)
1677 char *import_module_name;
1679 import_module_name = base + import_descriptor->Name;
1680 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
1681 lstrcmpiA(import_module_name, "user32") == 0)
1683 PIMAGE_THUNK_DATA int_entry;
1684 PIMAGE_THUNK_DATA iat_entry;
1686 /* The import name table and import address table are two parallel
1687 * arrays. We need the import name table to find the imported
1688 * routine and the import address table to patch the address, so
1689 * walk them side by side */
1690 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
1691 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
1692 while (int_entry->u1.Ordinal != 0)
1694 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
1696 PIMAGE_IMPORT_BY_NAME import_by_name;
1697 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
1698 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
1700 /* Found the correct routine in the correct imported module. Patch it. */
1702 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
1703 iat_entry->u1.Function = (ULONG_PTR) new_func;
1704 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
1714 import_descriptor++;
1718 static void test_dde(void)
1720 char filename[MAX_PATH], defApplication[MAX_PATH];
1721 const dde_tests_t* test;
1727 hook_WaitForInputIdle((void *) hooked_WaitForInputIdle);
1729 sprintf(filename, "%s\\test file.sde", tmpdir);
1731 /* Default service is application name minus path and extension */
1732 strcpy(defApplication, strrchr(argv0, '\\')+1);
1733 *strchr(defApplication, '.') = 0;
1735 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
1736 4096, "winetest_shlexec_dde_map");
1737 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
1740 while (test->command)
1742 if (!create_test_association(".sde"))
1744 skip("Unable to create association for '.sde'\n");
1747 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1748 test->application, test->topic, test->ifexec);
1750 if (test->application != NULL || test->topic != NULL)
1752 strcpy(shared_block, test->application ? test->application : defApplication);
1753 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
1757 shared_block[0] = '\0';
1758 shared_block[1] = '\0';
1762 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
1763 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL);
1764 CloseHandle(dde_ready_event);
1765 if ((test->todo & 0x1)==0)
1767 ok(32 < rc, "%s failed: rc=%d err=%d\n", shell_call,
1768 rc, GetLastError());
1772 ok(32 < rc, "%s failed: rc=%d err=%d\n", shell_call,
1773 rc, GetLastError());
1777 if ((test->todo & 0x2)==0)
1779 okChildInt("argcA", test->expectedArgs + 3);
1783 okChildInt("argcA", test->expectedArgs + 3);
1785 if (test->expectedArgs == 1)
1787 if ((test->todo & 0x4) == 0)
1789 okChildPath("argvA3", filename);
1793 okChildPath("argvA3", filename);
1796 if ((test->todo & 0x8) == 0)
1798 sprintf(params, test->expectedDdeExec, filename);
1799 okChildPath("ddeExec", params);
1803 sprintf(params, test->expectedDdeExec, filename);
1804 okChildPath("ddeExec", params);
1808 delete_test_association(".sde");
1812 UnmapViewOfFile(shared_block);
1814 hook_WaitForInputIdle((void *) WaitForInputIdle);
1817 #define DDE_DEFAULT_APP_VARIANTS 2
1820 const char* command;
1821 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1823 int rc[DDE_DEFAULT_APP_VARIANTS];
1824 } dde_default_app_tests_t;
1826 static dde_default_app_tests_t dde_default_app_tests[] =
1828 /* Windows XP and 98 handle default DDE app names in different ways.
1829 * The application name we see in the first test determines the pattern
1830 * of application names and return codes we will look for. */
1832 /* Test unquoted existing filename with a space */
1833 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1834 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1836 /* Test quoted existing filename with a space */
1837 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1838 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1840 /* Test unquoted filename with a space that doesn't exist, but
1842 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1843 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1845 /* Test quoted filename with a space that does not exist */
1846 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1847 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1849 /* Test filename supplied without the extension */
1850 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1851 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1853 /* Test an unquoted nonexistent filename */
1854 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1855 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1857 /* Test an application that will be found on the path */
1858 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1859 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1861 /* Test an application that will not be found on the path */
1862 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1863 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1865 {NULL, {NULL}, 0, {0}}
1871 DWORD threadIdParent;
1872 } dde_thread_info_t;
1874 static DWORD CALLBACK ddeThread(LPVOID arg)
1876 dde_thread_info_t *info = arg;
1877 assert(info && info->filename);
1878 PostThreadMessage(info->threadIdParent,
1880 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1885 static void test_dde_default_app(void)
1887 char filename[MAX_PATH];
1889 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1890 const dde_default_app_tests_t* test;
1896 post_quit_on_execute = FALSE;
1898 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1899 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1900 assert(rc == DMLERR_NO_ERROR);
1902 sprintf(filename, "%s\\test file.sde", tmpdir);
1904 /* It is strictly not necessary to register an application name here, but wine's
1905 * DdeNameService implementation complains if 0L is passed instead of
1906 * hszApplication with DNS_FILTEROFF */
1907 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1908 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1909 assert(hszApplication && hszTopic);
1910 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1912 test = dde_default_app_tests;
1913 while (test->command)
1915 if (!create_test_association(".sde"))
1917 skip("Unable to create association for '.sde'\n");
1920 sprintf(params, test->command, tmpdir);
1921 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1923 ddeApplication[0] = 0;
1925 /* No application will be run as we will respond to the first DDE event,
1926 * so don't wait for it */
1929 assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
1930 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1931 rc = msg.wParam > 32 ? 33 : msg.wParam;
1933 /* First test, find which set of test data we expect to see */
1934 if (test == dde_default_app_tests)
1937 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1939 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1945 if (i == DDE_DEFAULT_APP_VARIANTS)
1946 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1949 if ((test->todo & 0x1)==0)
1951 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1952 rc, GetLastError());
1956 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1957 rc, GetLastError());
1961 if ((test->todo & 0x2)==0)
1963 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1964 "Expected application '%s', got '%s'\n",
1965 test->expectedDdeApplication[which], ddeApplication);
1969 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1970 "Expected application '%s', got '%s'\n",
1971 test->expectedDdeApplication[which], ddeApplication);
1975 delete_test_association(".sde");
1979 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1980 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1981 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1982 assert(DdeUninitialize(ddeInst));
1985 static void init_test(void)
1988 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1989 char filename[MAX_PATH];
1990 WCHAR lnkfile[MAX_PATH];
1992 const char* const * testfile;
1997 hdll=GetModuleHandleA("shell32.dll");
1998 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2001 dllver.cbSize=sizeof(dllver);
2002 pDllGetVersion(&dllver);
2003 trace("major=%d minor=%d build=%d platform=%d\n",
2004 dllver.dwMajorVersion, dllver.dwMinorVersion,
2005 dllver.dwBuildNumber, dllver.dwPlatformID);
2009 memset(&dllver, 0, sizeof(dllver));
2012 r = CoInitialize(NULL);
2013 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2017 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
2018 assert(rc!=0 && rc<sizeof(argv0));
2019 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
2021 strcat(argv0, ".so");
2022 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
2023 "unable to find argv0!\n");
2026 GetTempPathA(sizeof(filename), filename);
2027 GetTempFileNameA(filename, "wt", 0, tmpdir);
2028 DeleteFileA( tmpdir );
2029 rc = CreateDirectoryA( tmpdir, NULL );
2030 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2031 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2033 init_event(child_file);
2035 /* Set up the test files */
2041 sprintf(filename, *testfile, tmpdir);
2042 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2043 FILE_ATTRIBUTE_NORMAL, NULL);
2044 if (hfile==INVALID_HANDLE_VALUE)
2046 trace("unable to create '%s': err=%d\n", filename, GetLastError());
2053 /* Setup the test shortcuts */
2054 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2055 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2056 desc.description=NULL;
2058 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2061 desc.arguments="ignored";
2066 create_lnk(lnkfile, &desc, 0);
2068 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2069 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2070 desc.description=NULL;
2074 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2075 desc.arguments=params;
2080 create_lnk(lnkfile, &desc, 0);
2082 /* Create a basic association suitable for most tests */
2083 if (!create_test_association(".shlexec"))
2085 skip("Unable to create association for '.shlexec'\n");
2088 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2089 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2090 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2091 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2092 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2093 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2095 create_test_verb(".shlexec", "NoQuotesParam2", 0, "NoQuotesParam2 %2");
2096 create_test_verb(".shlexec", "QuotedParam2", 0, "QuotedParam2 \"%2\"");
2098 create_test_verb(".shlexec", "NoQuotesAllParams", 0, "NoQuotesAllParams %*");
2099 create_test_verb(".shlexec", "QuotedAllParams", 0, "QuotedAllParams \"%*\"");
2101 create_test_verb(".shlexec", "NoQuotesParams345etc", 0, "NoQuotesParams345etc %~3");
2102 create_test_verb(".shlexec", "QuotedParams345etc", 0, "QuotedParams345etc \"%~3\"");
2105 static void cleanup_test(void)
2107 char filename[MAX_PATH];
2108 const char* const * testfile;
2110 /* Delete the test files */
2114 sprintf(filename, *testfile, tmpdir);
2115 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2116 SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
2117 DeleteFile(filename);
2120 DeleteFile(child_file);
2121 RemoveDirectoryA(tmpdir);
2123 /* Delete the test association */
2124 delete_test_association(".shlexec");
2126 CloseHandle(hEvent);
2131 static void test_commandline(void)
2133 static const WCHAR one[] = {'o','n','e',0};
2134 static const WCHAR two[] = {'t','w','o',0};
2135 static const WCHAR three[] = {'t','h','r','e','e',0};
2136 static const WCHAR four[] = {'f','o','u','r',0};
2138 static const WCHAR fmt1[] = {'%','s',' ','%','s',' ','%','s',' ','%','s',0};
2139 static const WCHAR fmt2[] = {' ','%','s',' ','%','s',' ','%','s',' ','%','s',0};
2140 static const WCHAR fmt3[] = {'%','s','=','%','s',' ','%','s','=','\"','%','s','\"',0};
2141 static const WCHAR fmt4[] = {'\"','%','s','\"',' ','\"','%','s',' ','%','s','\"',' ','%','s',0};
2142 static const WCHAR fmt5[] = {'\\','\"','%','s','\"',' ','%','s','=','\"','%','s','\\','\"',' ','\"','%','s','\\','\"',0};
2143 static const WCHAR fmt6[] = {0};
2145 static const WCHAR chkfmt1[] = {'%','s','=','%','s',0};
2146 static const WCHAR chkfmt2[] = {'%','s',' ','%','s',0};
2147 static const WCHAR chkfmt3[] = {'\\','\"','%','s','\"',0};
2148 static const WCHAR chkfmt4[] = {'%','s','=','%','s','\"',' ','%','s','\"',0};
2150 LPWSTR *args = (LPWSTR*)0xdeadcafe, pbuf;
2154 wsprintfW(cmdline,fmt1,one,two,three,four);
2155 args=CommandLineToArgvW(cmdline,&numargs);
2156 if (args == NULL && numargs == -1)
2158 win_skip("CommandLineToArgvW not implemented, skipping\n");
2161 ok(numargs == 4, "expected 4 args, got %i\n",numargs);
2162 ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
2163 ok(lstrcmpW(args[1],two)==0,"arg1 is not as expected\n");
2164 ok(lstrcmpW(args[2],three)==0,"arg2 is not as expected\n");
2165 ok(lstrcmpW(args[3],four)==0,"arg3 is not as expected\n");
2167 wsprintfW(cmdline,fmt2,one,two,three,four);
2168 args=CommandLineToArgvW(cmdline,&numargs);
2169 ok(numargs == 5, "expected 5 args, got %i\n",numargs);
2170 ok(args[0][0]==0,"arg0 is not as expected\n");
2171 ok(lstrcmpW(args[1],one)==0,"arg1 is not as expected\n");
2172 ok(lstrcmpW(args[2],two)==0,"arg2 is not as expected\n");
2173 ok(lstrcmpW(args[3],three)==0,"arg3 is not as expected\n");
2174 ok(lstrcmpW(args[4],four)==0,"arg4 is not as expected\n");
2176 wsprintfW(cmdline,fmt3,one,two,three,four);
2177 args=CommandLineToArgvW(cmdline,&numargs);
2178 ok(numargs == 2, "expected 2 args, got %i\n",numargs);
2179 wsprintfW(cmdline,chkfmt1,one,two);
2180 ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
2181 wsprintfW(cmdline,chkfmt1,three,four);
2182 ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
2184 wsprintfW(cmdline,fmt4,one,two,three,four);
2185 args=CommandLineToArgvW(cmdline,&numargs);
2186 ok(numargs == 3, "expected 3 args, got %i\n",numargs);
2187 ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
2188 wsprintfW(cmdline,chkfmt2,two,three);
2189 ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
2190 ok(lstrcmpW(args[2],four)==0,"arg2 is not as expected\n");
2192 wsprintfW(cmdline,fmt5,one,two,three,four);
2193 args=CommandLineToArgvW(cmdline,&numargs);
2194 ok(numargs == 2, "expected 2 args, got %i\n",numargs);
2195 wsprintfW(cmdline,chkfmt3,one);
2196 todo_wine ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
2197 wsprintfW(cmdline,chkfmt4,two,three,four);
2198 todo_wine ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
2200 wsprintfW(cmdline,fmt6);
2201 args=CommandLineToArgvW(cmdline,&numargs);
2202 ok(numargs == 1, "expected 1 args, got %i\n",numargs);
2204 buflen = max(lstrlenW(args[0])+1,256);
2205 pbuf = HeapAlloc(GetProcessHeap(), 0, buflen*sizeof(pbuf[0]));
2206 GetModuleFileNameW(NULL, pbuf, buflen);
2208 /* check args[0] is module file name */
2209 ok(lstrcmpW(args[0],pbuf)==0, "wrong path to the current executable\n");
2210 HeapFree(GetProcessHeap(), 0, pbuf);
2214 static void test_directory(void)
2216 char path[MAX_PATH], newdir[MAX_PATH];
2220 /* copy this executable to a new folder and cd to it */
2221 sprintf(newdir, "%s\\newfolder", tmpdir);
2222 rc = CreateDirectoryA( newdir, NULL );
2223 ok( rc, "failed to create %s err %u\n", newdir, GetLastError() );
2224 sprintf(path, "%s\\%s", newdir, path_find_file_name(argv0));
2225 CopyFileA(argv0, path, FALSE);
2226 SetCurrentDirectory(tmpdir);
2228 sprintf(params, "shlexec \"%s\" Exec", child_file);
2230 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2231 NULL, path_find_file_name(argv0), params, NULL);
2232 todo_wine ok(rc == SE_ERR_FNF, "%s returned %d\n", shell_call, rc);
2234 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2235 NULL, path_find_file_name(argv0), params, newdir);
2236 ok(rc > 32, "%s returned %d\n", shell_call, rc);
2237 okChildInt("argcA", 4);
2238 okChildString("argvA3", "Exec");
2239 todo_wine okChildPath("longPath", path);
2242 RemoveDirectoryA(newdir);
2248 myARGC = winetest_get_mainargs(&myARGV);
2251 doChild(myARGC, myARGV);
2258 test_lpFile_parsed();
2260 test_find_executable();
2265 test_dde_default_app();