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);
683 ok_(file, line)(lstrcmpiA(result, expected) == 0,
684 "%s expected '%s', got '%s'\n", key, expected, result);
687 static void _okChildPath(const char* file, int line, const char* key, const char* expected)
690 result=getChildString("Arguments", key);
691 ok_(file, line)(StrCmpPath(result, expected) == 0,
692 "%s expected '%s', got '%s'\n", key, expected, result);
695 static void _okChildInt(const char* file, int line, const char* key, int expected)
698 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
699 ok_(file, line)(result == expected,
700 "%s expected %d, but got %d\n", key, expected, result);
703 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
704 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
705 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
709 * GetLongPathNameA equivalent that supports Win95 and WinNT
713 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
715 char tmplongpath[MAX_PATH];
717 DWORD sp = 0, lp = 0;
719 WIN32_FIND_DATAA wfd;
722 if (!shortpath || !shortpath[0])
725 if (shortpath[1] == ':')
727 tmplongpath[0] = shortpath[0];
728 tmplongpath[1] = ':';
732 while (shortpath[sp])
734 /* check for path delimiters and reproduce them */
735 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
737 if (!lp || tmplongpath[lp-1] != '\\')
739 /* strip double "\\" */
740 tmplongpath[lp++] = '\\';
742 tmplongpath[lp] = 0; /* terminate string */
748 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
750 tmplongpath[lp++] = *p++;
751 tmplongpath[lp++] = *p++;
753 for (; *p && *p != '/' && *p != '\\'; p++);
754 tmplen = p - (shortpath + sp);
755 lstrcpyn(tmplongpath + lp, shortpath + sp, tmplen + 1);
756 /* Check if the file exists and use the existing file name */
757 goit = FindFirstFileA(tmplongpath, &wfd);
758 if (goit == INVALID_HANDLE_VALUE)
761 strcpy(tmplongpath + lp, wfd.cFileName);
762 lp += strlen(tmplongpath + lp);
765 tmplen = strlen(shortpath) - 1;
766 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
767 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
768 tmplongpath[lp++] = shortpath[tmplen];
771 tmplen = strlen(tmplongpath) + 1;
772 if (tmplen <= longlen)
774 strcpy(longpath, tmplongpath);
775 tmplen--; /* length without 0 */
783 * PathFindFileNameA equivalent that supports WinNT
787 static LPSTR path_find_file_name(LPCSTR lpszPath)
789 LPCSTR lastSlash = lpszPath;
791 while (lpszPath && *lpszPath)
793 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
794 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
795 lastSlash = lpszPath + 1;
796 lpszPath = CharNext(lpszPath);
798 return (LPSTR)lastSlash;
807 static const char* testfiles[]=
809 "%s\\test file.shlexec",
810 "%s\\%%nasty%% $file.shlexec",
811 "%s\\test file.noassoc",
812 "%s\\test file.noassoc.shlexec",
813 "%s\\test file.shlexec.noassoc",
814 "%s\\test_shortcut_shlexec.lnk",
815 "%s\\test_shortcut_exe.lnk",
817 "%s\\test file.shlfoo",
819 "%s\\masked file.shlexec",
824 "%s\\simple.shlexec",
825 "%s\\drawback_file.noassoc",
826 "%s\\drawback_file.noassoc foo.shlexec",
827 "%s\\drawback_nonexist.noassoc foo.shlexec",
834 const char* basename;
839 static filename_tests_t filename_tests[]=
841 /* Test bad / nonexistent filenames */
842 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
843 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
846 {NULL, "%s\\test file.shlexec", 0x0, 33},
847 {NULL, "%s\\test file.shlexec.", 0x0, 33},
848 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
849 {NULL, "%s/test file.shlexec", 0x0, 33},
851 /* Test filenames with no association */
852 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
854 /* Test double extensions */
855 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
856 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
858 /* Test alternate verbs */
859 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
860 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
862 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
863 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
865 /* Test file masked due to space */
866 {NULL, "%s\\masked file.shlexec", 0x1, 33},
867 /* Test if quoting prevents the masking */
868 {NULL, "%s\\masked file.shlexec", 0x40, 33},
873 static filename_tests_t noquotes_tests[]=
875 /* Test unquoted '%1' thingies */
876 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
877 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
878 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
883 static void test_lpFile_parsed(void)
885 /* basename tmpdir */
886 const char* shorttmpdir;
888 const char *testfile;
889 char fileA[MAX_PATH];
893 GetTempPathA(sizeof(fileA), fileA);
894 shorttmpdir = tmpdir + strlen(fileA);
896 /* ensure tmpdir is in %TEMP%: GetTempPath() can succeed even if TEMP is undefined */
897 SetEnvironmentVariableA("TEMP", fileA);
899 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
900 testfile = "%s\\drawback_file.noassoc foo.shlexec";
901 sprintf(fileA, testfile, tmpdir);
902 rc=shell_execute(NULL, fileA, NULL, NULL);
905 "expected success (33), got %s (%d), lpFile: %s\n",
906 rc > 32 ? "success" : "failure", rc, fileA
910 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
911 testfile = "\"%s\\drawback_file.noassoc foo.shlexec\"";
912 sprintf(fileA, testfile, tmpdir);
913 rc=shell_execute(NULL, fileA, NULL, NULL);
914 ok(rc>32 || broken(rc == 2) /* Win95/NT4 */,
915 "expected success (33), got %s (%d), lpFile: %s\n",
916 rc > 32 ? "success" : "failure", rc, fileA
919 /* error should be 2, not 31 */
920 testfile = "\"%s\\drawback_file.noassoc\" foo.shlexec";
921 sprintf(fileA, testfile, tmpdir);
922 rc=shell_execute(NULL, fileA, NULL, NULL);
924 "expected failure (2), got %s (%d), lpFile: %s\n",
925 rc > 32 ? "success" : "failure", rc, fileA
928 /* ""command"" not works on wine (and real win9x and w2k) */
929 testfile = "\"\"%s\\simple.shlexec\"\"";
930 sprintf(fileA, testfile, tmpdir);
931 rc=shell_execute(NULL, fileA, NULL, NULL);
933 ok(rc>32 || broken(rc == 2) /* Win9x/2000 */,
934 "expected success (33), got %s (%d), lpFile: %s\n",
935 rc > 32 ? "success" : "failure", rc, fileA
939 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
940 testfile = "%s\\drawback_nonexist.noassoc foo.shlexec";
941 sprintf(fileA, testfile, tmpdir);
942 rc=shell_execute(NULL, fileA, NULL, NULL);
944 "expected success (33), got %s (%d), lpFile: %s\n",
945 rc > 32 ? "success" : "failure", rc, fileA
948 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
949 testfile = "%%TEMP%%\\%s\\simple.shlexec";
950 sprintf(fileA, testfile, shorttmpdir);
951 rc=shell_execute(NULL, fileA, NULL, NULL);
954 "expected failure (2), got %s (%d), lpFile: %s\n",
955 rc > 32 ? "success" : "failure", rc, fileA
960 testfile = "\"%%TEMP%%\\%s\\simple.shlexec\"";
961 sprintf(fileA, testfile, shorttmpdir);
962 rc=shell_execute(NULL, fileA, NULL, NULL);
965 "expected failure (2), got %s (%d), lpFile: %s\n",
966 rc > 32 ? "success" : "failure", rc, fileA
970 /* test SEE_MASK_DOENVSUBST works */
971 testfile = "%%TEMP%%\\%s\\simple.shlexec";
972 sprintf(fileA, testfile, shorttmpdir);
973 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI, NULL, fileA, NULL, NULL);
975 "expected success (33), got %s (%d), lpFile: %s\n",
976 rc > 32 ? "success" : "failure", rc, fileA
979 /* quoted lpFile not works only on real win95 and nt4 */
980 testfile = "\"%%TEMP%%\\%s\\simple.shlexec\"";
981 sprintf(fileA, testfile, shorttmpdir);
982 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI, NULL, fileA, NULL, NULL);
983 ok(rc>32 || broken(rc == 2) /* Win95/NT4 */,
984 "expected success (33), got %s (%d), lpFile: %s\n",
985 rc > 32 ? "success" : "failure", rc, fileA
990 static void test_argify(void)
992 char fileA[MAX_PATH];
996 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
999 rc=shell_execute("NoQuotesParam2", fileA, "a b", NULL);
1001 "expected success (33), got %s (%d), lpFile: %s\n",
1002 rc > 32 ? "success" : "failure", rc, fileA
1006 okChildInt("argcA", 5);
1007 okChildString("argvA4", "a");
1011 /* '"a"""' -> 'a"' */
1012 rc=shell_execute("NoQuotesParam2", fileA, "\"a:\"\"some string\"\"\"", NULL);
1014 "expected success (33), got %s (%d), lpFile: %s\n",
1015 rc > 32 ? "success" : "failure", rc, fileA
1019 okChildInt("argcA", 5);
1021 okChildString("argvA4", "a:some string");
1026 /* backslash isn't escape char
1027 * '"a\""' -> '"a\""' */
1028 rc=shell_execute("NoQuotesParam2", fileA, "\"a:\\\"some string\\\"\"", NULL);
1030 "expected success (33), got %s (%d), lpFile: %s\n",
1031 rc > 32 ? "success" : "failure", rc, fileA
1035 okChildInt("argcA", 5);
1037 okChildString("argvA4", "a:\\");
1042 /* \t isn't whitespace */
1043 rc=shell_execute("QuotedParam2", fileA, "a\tb c", NULL);
1045 "expected success (33), got %s (%d), lpFile: %s\n",
1046 rc > 32 ? "success" : "failure", rc, fileA
1050 okChildInt("argcA", 5);
1052 okChildString("argvA4", "a\tb");
1057 rc=shell_execute("NoQuotesAllParams", fileA, "a b c d e f g h", NULL);
1059 "expected success (33), got %s (%d), lpFile: %s\n",
1060 rc > 32 ? "success" : "failure", rc, fileA
1065 okChildInt("argcA", 12);
1066 okChildString("argvA4", "a");
1067 okChildString("argvA11", "h");
1071 /* %* can sometimes contain only whitespaces and no args */
1072 rc=shell_execute("QuotedAllParams", fileA, " ", NULL);
1074 "expected success (33), got %s (%d), lpFile: %s\n",
1075 rc > 32 ? "success" : "failure", rc, fileA
1080 okChildInt("argcA", 5);
1081 okChildString("argvA4", " ");
1086 rc=shell_execute("NoQuotesParams345etc", fileA, "a b c d e f g h", NULL);
1088 "expected success (33), got %s (%d), lpFile: %s\n",
1089 rc > 32 ? "success" : "failure", rc, fileA
1094 okChildInt("argcA", 11);
1095 okChildString("argvA4", "b");
1096 okChildString("argvA10", "h");
1100 /* %~3 is rest of command line starting with whitespaces after 2nd arg */
1101 rc=shell_execute("QuotedParams345etc", fileA, "a ", NULL);
1103 "expected success (33), got %s (%d), lpFile: %s\n",
1104 rc > 32 ? "success" : "failure", rc, fileA
1108 okChildInt("argcA", 5);
1110 okChildString("argvA4", " ");
1116 static void test_filename(void)
1118 char filename[MAX_PATH];
1119 const filename_tests_t* test;
1123 test=filename_tests;
1124 while (test->basename)
1126 BOOL quotedfile = FALSE;
1128 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1130 win_skip("Skipping shellexecute of file with unassociated extension\n");
1135 sprintf(filename, test->basename, tmpdir);
1136 if (strchr(filename, '/'))
1146 if ((test->todo & 0x40)==0)
1148 rc=shell_execute(test->verb, filename, NULL, NULL);
1152 char quoted[MAX_PATH + 2];
1155 sprintf(quoted, "\"%s\"", filename);
1156 rc=shell_execute(test->verb, quoted, NULL, NULL);
1160 if ((test->todo & 0x1)==0)
1163 broken(quotedfile && rc == 2), /* NT4 */
1164 "%s failed: rc=%d err=%d\n", shell_call,
1165 rc, GetLastError());
1169 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1170 rc, GetLastError());
1175 if ((test->todo & 0x2)==0)
1177 okChildInt("argcA", 5);
1181 okChildInt("argcA", 5);
1183 verb=(test->verb ? test->verb : "Open");
1184 if ((test->todo & 0x4)==0)
1186 okChildString("argvA3", verb);
1190 okChildString("argvA3", verb);
1192 if ((test->todo & 0x8)==0)
1194 okChildPath("argvA4", filename);
1198 okChildPath("argvA4", filename);
1204 test=noquotes_tests;
1205 while (test->basename)
1207 sprintf(filename, test->basename, tmpdir);
1208 rc=shell_execute(test->verb, filename, NULL, NULL);
1211 if ((test->todo & 0x1)==0)
1213 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1214 rc, GetLastError());
1218 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1219 rc, GetLastError());
1227 verb=(test->verb ? test->verb : "Open");
1228 if ((test->todo & 0x4)==0)
1230 okChildString("argvA3", verb);
1234 okChildString("argvA3", verb);
1243 space=strchr(str, ' ');
1246 sprintf(attrib, "argvA%d", count);
1247 if ((test->todo & 0x8)==0)
1249 okChildPath(attrib, str);
1253 okChildPath(attrib, str);
1260 if ((test->todo & 0x2)==0)
1262 okChildInt("argcA", count);
1266 okChildInt("argcA", count);
1272 if (dllver.dwMajorVersion != 0)
1274 /* The more recent versions of shell32.dll accept quoted filenames
1275 * while older ones (e.g. 4.00) don't. Still we want to test this
1276 * because IE 6 depends on the new behavior.
1277 * One day we may need to check the exact version of the dll but for
1278 * now making sure DllGetVersion() is present is sufficient.
1280 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1281 rc=shell_execute(NULL, filename, NULL, NULL);
1282 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1284 okChildInt("argcA", 5);
1285 okChildString("argvA3", "Open");
1286 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1287 okChildPath("argvA4", filename);
1291 static void test_find_executable(void)
1293 char filename[MAX_PATH];
1294 char command[MAX_PATH];
1295 const filename_tests_t* test;
1298 if (!create_test_association(".sfe"))
1300 skip("Unable to create association for '.sfe'\n");
1303 create_test_verb(".sfe", "Open", 1, "%1");
1305 /* Don't test FindExecutable(..., NULL), it always crashes */
1307 strcpy(command, "your word");
1308 if (0) /* Can crash on Vista! */
1310 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1311 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1312 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1315 strcpy(command, "your word");
1316 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1317 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1318 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1320 sprintf(filename, "%s\\test file.sfe", tmpdir);
1321 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1322 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1323 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1325 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1326 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1328 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1329 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1331 delete_test_association(".sfe");
1333 if (!create_test_association(".shl"))
1335 skip("Unable to create association for '.shl'\n");
1338 create_test_verb(".shl", "Open", 0, "Open");
1340 sprintf(filename, "%s\\test file.shl", tmpdir);
1341 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1342 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1344 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1345 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1347 delete_test_association(".shl");
1351 /* On Windows XP and 2003 FindExecutable() is completely broken.
1352 * Probably what it does is convert the filename to 8.3 format,
1353 * which as a side effect converts the '.shlfoo' extension to '.shl',
1354 * and then tries to find an association for '.shl'. This means it
1355 * will normally fail on most extensions with more than 3 characters,
1356 * like '.mpeg', etc.
1357 * Also it means we cannot do any other test.
1359 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1363 test=filename_tests;
1364 while (test->basename)
1366 sprintf(filename, test->basename, tmpdir);
1367 if (strchr(filename, '/'))
1378 /* Win98 does not '\0'-terminate command! */
1379 memset(command, '\0', sizeof(command));
1380 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1383 if ((test->todo & 0x10)==0)
1385 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1389 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1394 equal=strcmp(command, argv0) == 0 ||
1395 /* NT4 returns an extra 0x8 character! */
1396 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1397 if ((test->todo & 0x20)==0)
1399 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1400 filename, command, argv0);
1404 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1405 filename, command, argv0);
1413 static filename_tests_t lnk_tests[]=
1415 /* Pass bad / nonexistent filenames as a parameter */
1416 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1417 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1419 /* Pass regular paths as a parameter */
1420 {NULL, "%s\\test file.shlexec", 0xa, 33},
1421 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1423 /* Pass filenames with no association as a parameter */
1424 {NULL, "%s\\test file.noassoc", 0xa, 33},
1429 static void test_lnks(void)
1431 char filename[MAX_PATH];
1432 char params[MAX_PATH];
1433 const filename_tests_t* test;
1436 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1437 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1438 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1440 okChildInt("argcA", 5);
1441 okChildString("argvA3", "Open");
1442 sprintf(params, "%s\\test file.shlexec", tmpdir);
1443 get_long_path_name(params, filename, sizeof(filename));
1444 okChildPath("argvA4", filename);
1446 sprintf(filename, "%s\\test_shortcut_exe.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", 4);
1451 okChildString("argvA3", "Lnk");
1453 if (dllver.dwMajorVersion>=6)
1456 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
1457 * Older versions don't or are quite buggy in this regard.
1459 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1467 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL);
1468 ok(rc > 32, "%s failed: rc=%d err=%d\n", shell_call, rc,
1470 okChildInt("argcA", 4);
1471 okChildString("argvA3", "Lnk");
1474 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1476 while (test->basename)
1479 sprintf(params+1, test->basename, tmpdir);
1480 strcat(params,"\"");
1481 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1485 if ((test->todo & 0x1)==0)
1487 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1488 rc, GetLastError());
1492 ok(rc==test->rc, "%s failed: rc=%d err=%d\n", shell_call,
1493 rc, GetLastError());
1497 if ((test->todo & 0x2)==0)
1499 okChildInt("argcA", 5);
1503 okChildInt("argcA", 5);
1505 if ((test->todo & 0x4)==0)
1507 okChildString("argvA3", "Lnk");
1511 okChildString("argvA3", "Lnk");
1513 sprintf(params, test->basename, tmpdir);
1514 if ((test->todo & 0x8)==0)
1516 okChildPath("argvA4", params);
1520 okChildPath("argvA4", params);
1528 static void test_exes(void)
1530 char filename[MAX_PATH];
1534 sprintf(params, "shlexec \"%s\" Exec", child_file);
1536 /* We need NOZONECHECKS on Win2003 to block a dialog */
1537 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1539 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1540 okChildInt("argcA", 4);
1541 okChildString("argvA3", "Exec");
1543 if (! skip_noassoc_tests)
1545 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1546 if (CopyFile(argv0, filename, FALSE))
1548 rc=shell_execute(NULL, filename, params, NULL);
1550 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1556 win_skip("Skipping shellexecute of file with unassociated extension\n");
1560 static void test_exes_long(void)
1562 char filename[MAX_PATH];
1564 char longparam[MAX_PATH];
1567 for (rc = 0; rc < MAX_PATH; rc++)
1568 longparam[rc]='a'+rc%26;
1569 longparam[MAX_PATH-1]=0;
1572 sprintf(params, "shlexec \"%s\" %s", child_file,longparam);
1574 /* We need NOZONECHECKS on Win2003 to block a dialog */
1575 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
1577 ok(rc > 32, "%s returned %d\n", shell_call, rc);
1578 okChildInt("argcA", 4);
1579 okChildString("argvA3", longparam);
1581 if (! skip_noassoc_tests)
1583 sprintf(filename, "%s\\test file.noassoc", tmpdir);
1584 if (CopyFile(argv0, filename, FALSE))
1586 rc=shell_execute(NULL, filename, params, NULL);
1588 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%d\n", shell_call, rc);
1594 win_skip("Skipping shellexecute of file with unassociated extension\n");
1600 const char* command;
1601 const char* ddeexec;
1602 const char* application;
1606 const char* expectedDdeExec;
1610 static dde_tests_t dde_tests[] =
1612 /* Test passing and not passing command-line
1613 * argument, no DDE */
1614 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0},
1615 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0},
1617 /* Test passing and not passing command-line
1618 * argument, with DDE */
1619 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
1620 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0},
1622 /* Test unquoted %1 in command and ddeexec
1623 * (test filename has space) */
1624 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0},
1626 /* Test ifexec precedence over ddeexec */
1627 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0},
1629 /* Test default DDE topic */
1630 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0},
1632 /* Test default DDE application */
1633 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
1635 {NULL, NULL, NULL, NULL, NULL, 0, 0x0}
1638 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
1640 return WaitForSingleObject(dde_ready_event, timeout);
1644 * WaitForInputIdle() will normally return immediately for console apps. That's
1645 * a problem for us because ShellExecute will assume that an app is ready to
1646 * receive DDE messages after it has called WaitForInputIdle() on that app.
1647 * To work around that we install our own version of WaitForInputIdle() that
1648 * will wait for the child to explicitly tell us that it is ready. We do that
1649 * by changing the entry for WaitForInputIdle() in the shell32 import address
1652 static void hook_WaitForInputIdle(void *new_func)
1655 PIMAGE_NT_HEADERS nt_headers;
1656 DWORD import_directory_rva;
1657 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
1659 base = (char *) GetModuleHandleA("shell32.dll");
1660 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
1661 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
1663 /* Search for the correct imported module by walking the import descriptors */
1664 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
1665 while (U(*import_descriptor).OriginalFirstThunk != 0)
1667 char *import_module_name;
1669 import_module_name = base + import_descriptor->Name;
1670 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
1671 lstrcmpiA(import_module_name, "user32") == 0)
1673 PIMAGE_THUNK_DATA int_entry;
1674 PIMAGE_THUNK_DATA iat_entry;
1676 /* The import name table and import address table are two parallel
1677 * arrays. We need the import name table to find the imported
1678 * routine and the import address table to patch the address, so
1679 * walk them side by side */
1680 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
1681 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
1682 while (int_entry->u1.Ordinal != 0)
1684 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
1686 PIMAGE_IMPORT_BY_NAME import_by_name;
1687 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
1688 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
1690 /* Found the correct routine in the correct imported module. Patch it. */
1692 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
1693 iat_entry->u1.Function = (ULONG_PTR) new_func;
1694 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
1704 import_descriptor++;
1708 static void test_dde(void)
1710 char filename[MAX_PATH], defApplication[MAX_PATH];
1711 const dde_tests_t* test;
1717 hook_WaitForInputIdle((void *) hooked_WaitForInputIdle);
1719 sprintf(filename, "%s\\test file.sde", tmpdir);
1721 /* Default service is application name minus path and extension */
1722 strcpy(defApplication, strrchr(argv0, '\\')+1);
1723 *strchr(defApplication, '.') = 0;
1725 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
1726 4096, "winetest_shlexec_dde_map");
1727 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
1730 while (test->command)
1732 if (!create_test_association(".sde"))
1734 skip("Unable to create association for '.sde'\n");
1737 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
1738 test->application, test->topic, test->ifexec);
1740 if (test->application != NULL || test->topic != NULL)
1742 strcpy(shared_block, test->application ? test->application : defApplication);
1743 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
1747 shared_block[0] = '\0';
1748 shared_block[1] = '\0';
1752 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
1753 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL);
1754 CloseHandle(dde_ready_event);
1755 if ((test->todo & 0x1)==0)
1757 ok(32 < rc, "%s failed: rc=%d err=%d\n", shell_call,
1758 rc, GetLastError());
1762 ok(32 < rc, "%s failed: rc=%d err=%d\n", shell_call,
1763 rc, GetLastError());
1767 if ((test->todo & 0x2)==0)
1769 okChildInt("argcA", test->expectedArgs + 3);
1773 okChildInt("argcA", test->expectedArgs + 3);
1775 if (test->expectedArgs == 1)
1777 if ((test->todo & 0x4) == 0)
1779 okChildPath("argvA3", filename);
1783 okChildPath("argvA3", filename);
1786 if ((test->todo & 0x8) == 0)
1788 sprintf(params, test->expectedDdeExec, filename);
1789 okChildPath("ddeExec", params);
1793 sprintf(params, test->expectedDdeExec, filename);
1794 okChildPath("ddeExec", params);
1798 delete_test_association(".sde");
1802 UnmapViewOfFile(shared_block);
1804 hook_WaitForInputIdle((void *) WaitForInputIdle);
1807 #define DDE_DEFAULT_APP_VARIANTS 2
1810 const char* command;
1811 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
1813 int rc[DDE_DEFAULT_APP_VARIANTS];
1814 } dde_default_app_tests_t;
1816 static dde_default_app_tests_t dde_default_app_tests[] =
1818 /* Windows XP and 98 handle default DDE app names in different ways.
1819 * The application name we see in the first test determines the pattern
1820 * of application names and return codes we will look for. */
1822 /* Test unquoted existing filename with a space */
1823 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
1824 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
1826 /* Test quoted existing filename with a space */
1827 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
1828 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
1830 /* Test unquoted filename with a space that doesn't exist, but
1832 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
1833 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
1835 /* Test quoted filename with a space that does not exist */
1836 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
1837 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
1839 /* Test filename supplied without the extension */
1840 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
1841 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
1843 /* Test an unquoted nonexistent filename */
1844 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
1845 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
1847 /* Test an application that will be found on the path */
1848 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
1849 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
1851 /* Test an application that will not be found on the path */
1852 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1853 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
1855 {NULL, {NULL}, 0, {0}}
1861 DWORD threadIdParent;
1862 } dde_thread_info_t;
1864 static DWORD CALLBACK ddeThread(LPVOID arg)
1866 dde_thread_info_t *info = arg;
1867 assert(info && info->filename);
1868 PostThreadMessage(info->threadIdParent,
1870 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL),
1875 static void test_dde_default_app(void)
1877 char filename[MAX_PATH];
1879 dde_thread_info_t info = { filename, GetCurrentThreadId() };
1880 const dde_default_app_tests_t* test;
1886 post_quit_on_execute = FALSE;
1888 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
1889 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
1890 assert(rc == DMLERR_NO_ERROR);
1892 sprintf(filename, "%s\\test file.sde", tmpdir);
1894 /* It is strictly not necessary to register an application name here, but wine's
1895 * DdeNameService implementation complains if 0L is passed instead of
1896 * hszApplication with DNS_FILTEROFF */
1897 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1898 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
1899 assert(hszApplication && hszTopic);
1900 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
1902 test = dde_default_app_tests;
1903 while (test->command)
1905 if (!create_test_association(".sde"))
1907 skip("Unable to create association for '.sde'\n");
1910 sprintf(params, test->command, tmpdir);
1911 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
1913 ddeApplication[0] = 0;
1915 /* No application will be run as we will respond to the first DDE event,
1916 * so don't wait for it */
1919 assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
1920 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
1921 rc = msg.wParam > 32 ? 33 : msg.wParam;
1923 /* First test, find which set of test data we expect to see */
1924 if (test == dde_default_app_tests)
1927 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
1929 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
1935 if (i == DDE_DEFAULT_APP_VARIANTS)
1936 skip("Default DDE application test does not match any available results, using first expected data set.\n");
1939 if ((test->todo & 0x1)==0)
1941 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1942 rc, GetLastError());
1946 ok(rc==test->rc[which], "%s failed: rc=%d err=%d\n", shell_call,
1947 rc, GetLastError());
1951 if ((test->todo & 0x2)==0)
1953 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1954 "Expected application '%s', got '%s'\n",
1955 test->expectedDdeApplication[which], ddeApplication);
1959 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
1960 "Expected application '%s', got '%s'\n",
1961 test->expectedDdeApplication[which], ddeApplication);
1965 delete_test_association(".sde");
1969 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
1970 assert(DdeFreeStringHandle(ddeInst, hszTopic));
1971 assert(DdeFreeStringHandle(ddeInst, hszApplication));
1972 assert(DdeUninitialize(ddeInst));
1975 static void init_test(void)
1978 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
1979 char filename[MAX_PATH];
1980 WCHAR lnkfile[MAX_PATH];
1982 const char* const * testfile;
1987 hdll=GetModuleHandleA("shell32.dll");
1988 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
1991 dllver.cbSize=sizeof(dllver);
1992 pDllGetVersion(&dllver);
1993 trace("major=%d minor=%d build=%d platform=%d\n",
1994 dllver.dwMajorVersion, dllver.dwMinorVersion,
1995 dllver.dwBuildNumber, dllver.dwPlatformID);
1999 memset(&dllver, 0, sizeof(dllver));
2002 r = CoInitialize(NULL);
2003 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2007 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
2008 assert(rc!=0 && rc<sizeof(argv0));
2009 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
2011 strcat(argv0, ".so");
2012 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
2013 "unable to find argv0!\n");
2016 GetTempPathA(sizeof(filename), filename);
2017 GetTempFileNameA(filename, "wt", 0, tmpdir);
2018 DeleteFileA( tmpdir );
2019 rc = CreateDirectoryA( tmpdir, NULL );
2020 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2021 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2023 init_event(child_file);
2025 /* Set up the test files */
2031 sprintf(filename, *testfile, tmpdir);
2032 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2033 FILE_ATTRIBUTE_NORMAL, NULL);
2034 if (hfile==INVALID_HANDLE_VALUE)
2036 trace("unable to create '%s': err=%d\n", filename, GetLastError());
2043 /* Setup the test shortcuts */
2044 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2045 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2046 desc.description=NULL;
2048 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2051 desc.arguments="ignored";
2056 create_lnk(lnkfile, &desc, 0);
2058 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2059 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2060 desc.description=NULL;
2064 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2065 desc.arguments=params;
2070 create_lnk(lnkfile, &desc, 0);
2072 /* Create a basic association suitable for most tests */
2073 if (!create_test_association(".shlexec"))
2075 skip("Unable to create association for '.shlexec'\n");
2078 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2079 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2080 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2081 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2082 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2083 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2085 create_test_verb(".shlexec", "NoQuotesParam2", 0, "NoQuotesParam2 %2");
2086 create_test_verb(".shlexec", "QuotedParam2", 0, "QuotedParam2 \"%2\"");
2088 create_test_verb(".shlexec", "NoQuotesAllParams", 0, "NoQuotesAllParams %*");
2089 create_test_verb(".shlexec", "QuotedAllParams", 0, "QuotedAllParams \"%*\"");
2091 create_test_verb(".shlexec", "NoQuotesParams345etc", 0, "NoQuotesParams345etc %~3");
2092 create_test_verb(".shlexec", "QuotedParams345etc", 0, "QuotedParams345etc \"%~3\"");
2095 static void cleanup_test(void)
2097 char filename[MAX_PATH];
2098 const char* const * testfile;
2100 /* Delete the test files */
2104 sprintf(filename, *testfile, tmpdir);
2105 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2106 SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
2107 DeleteFile(filename);
2110 DeleteFile(child_file);
2111 RemoveDirectoryA(tmpdir);
2113 /* Delete the test association */
2114 delete_test_association(".shlexec");
2116 CloseHandle(hEvent);
2121 static void test_commandline(void)
2123 static const WCHAR one[] = {'o','n','e',0};
2124 static const WCHAR two[] = {'t','w','o',0};
2125 static const WCHAR three[] = {'t','h','r','e','e',0};
2126 static const WCHAR four[] = {'f','o','u','r',0};
2128 static const WCHAR fmt1[] = {'%','s',' ','%','s',' ','%','s',' ','%','s',0};
2129 static const WCHAR fmt2[] = {' ','%','s',' ','%','s',' ','%','s',' ','%','s',0};
2130 static const WCHAR fmt3[] = {'%','s','=','%','s',' ','%','s','=','\"','%','s','\"',0};
2131 static const WCHAR fmt4[] = {'\"','%','s','\"',' ','\"','%','s',' ','%','s','\"',' ','%','s',0};
2132 static const WCHAR fmt5[] = {'\\','\"','%','s','\"',' ','%','s','=','\"','%','s','\\','\"',' ','\"','%','s','\\','\"',0};
2133 static const WCHAR fmt6[] = {0};
2135 static const WCHAR chkfmt1[] = {'%','s','=','%','s',0};
2136 static const WCHAR chkfmt2[] = {'%','s',' ','%','s',0};
2137 static const WCHAR chkfmt3[] = {'\\','\"','%','s','\"',0};
2138 static const WCHAR chkfmt4[] = {'%','s','=','%','s','\"',' ','%','s','\"',0};
2140 LPWSTR *args = (LPWSTR*)0xdeadcafe, pbuf;
2144 wsprintfW(cmdline,fmt1,one,two,three,four);
2145 args=CommandLineToArgvW(cmdline,&numargs);
2146 if (args == NULL && numargs == -1)
2148 win_skip("CommandLineToArgvW not implemented, skipping\n");
2151 ok(numargs == 4, "expected 4 args, got %i\n",numargs);
2152 ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
2153 ok(lstrcmpW(args[1],two)==0,"arg1 is not as expected\n");
2154 ok(lstrcmpW(args[2],three)==0,"arg2 is not as expected\n");
2155 ok(lstrcmpW(args[3],four)==0,"arg3 is not as expected\n");
2157 wsprintfW(cmdline,fmt2,one,two,three,four);
2158 args=CommandLineToArgvW(cmdline,&numargs);
2159 ok(numargs == 5, "expected 5 args, got %i\n",numargs);
2160 ok(args[0][0]==0,"arg0 is not as expected\n");
2161 ok(lstrcmpW(args[1],one)==0,"arg1 is not as expected\n");
2162 ok(lstrcmpW(args[2],two)==0,"arg2 is not as expected\n");
2163 ok(lstrcmpW(args[3],three)==0,"arg3 is not as expected\n");
2164 ok(lstrcmpW(args[4],four)==0,"arg4 is not as expected\n");
2166 wsprintfW(cmdline,fmt3,one,two,three,four);
2167 args=CommandLineToArgvW(cmdline,&numargs);
2168 ok(numargs == 2, "expected 2 args, got %i\n",numargs);
2169 wsprintfW(cmdline,chkfmt1,one,two);
2170 ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
2171 wsprintfW(cmdline,chkfmt1,three,four);
2172 ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
2174 wsprintfW(cmdline,fmt4,one,two,three,four);
2175 args=CommandLineToArgvW(cmdline,&numargs);
2176 ok(numargs == 3, "expected 3 args, got %i\n",numargs);
2177 ok(lstrcmpW(args[0],one)==0,"arg0 is not as expected\n");
2178 wsprintfW(cmdline,chkfmt2,two,three);
2179 ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
2180 ok(lstrcmpW(args[2],four)==0,"arg2 is not as expected\n");
2182 wsprintfW(cmdline,fmt5,one,two,three,four);
2183 args=CommandLineToArgvW(cmdline,&numargs);
2184 ok(numargs == 2, "expected 2 args, got %i\n",numargs);
2185 wsprintfW(cmdline,chkfmt3,one);
2186 todo_wine ok(lstrcmpW(args[0],cmdline)==0,"arg0 is not as expected\n");
2187 wsprintfW(cmdline,chkfmt4,two,three,four);
2188 todo_wine ok(lstrcmpW(args[1],cmdline)==0,"arg1 is not as expected\n");
2190 wsprintfW(cmdline,fmt6);
2191 args=CommandLineToArgvW(cmdline,&numargs);
2192 ok(numargs == 1, "expected 1 args, got %i\n",numargs);
2194 buflen = max(lstrlenW(args[0])+1,256);
2195 pbuf = HeapAlloc(GetProcessHeap(), 0, buflen*sizeof(pbuf[0]));
2196 GetModuleFileNameW(NULL, pbuf, buflen);
2198 /* check args[0] is module file name */
2199 ok(lstrcmpW(args[0],pbuf)==0, "wrong path to the current executable\n");
2200 HeapFree(GetProcessHeap(), 0, pbuf);
2204 static void test_directory(void)
2206 char path[MAX_PATH], newdir[MAX_PATH];
2210 /* copy this executable to a new folder and cd to it */
2211 sprintf(newdir, "%s\\newfolder", tmpdir);
2212 rc = CreateDirectoryA( newdir, NULL );
2213 ok( rc, "failed to create %s err %u\n", path, GetLastError() );
2214 sprintf(path, "%s\\%s", newdir, path_find_file_name(argv0));
2215 CopyFileA(argv0, path, FALSE);
2216 SetCurrentDirectory(tmpdir),
2218 sprintf(params, "shlexec \"%s\" Exec", child_file);
2220 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2221 NULL, path_find_file_name(argv0), params, NULL);
2222 todo_wine ok(rc == SE_ERR_FNF, "%s returned %d\n", shell_call, rc);
2224 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2225 NULL, path_find_file_name(argv0), params, newdir);
2226 ok(rc > 32, "%s returned %d\n", shell_call, rc);
2227 okChildInt("argcA", 4);
2228 okChildString("argvA3", "Exec");
2229 todo_wine okChildPath("longPath", path);
2232 RemoveDirectoryA(newdir);
2238 myARGC = winetest_get_mainargs(&myARGV);
2241 doChild(myARGC, myARGV);
2248 test_lpFile_parsed();
2250 test_find_executable();
2255 test_dde_default_app();