shell32/tests: Fix test_one_cmdline() and add a few more tests.
[wine.git] / dlls / shell32 / tests / shlexec.c
blobadcd95787efffce70b22d280af9ec7a8e753ce25
1 /*
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
21 /* TODO:
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
27 * in the PATH
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
32 * we could check
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
40 #include <stdio.h>
41 #include <assert.h>
43 #include "wtypes.h"
44 #include "winbase.h"
45 #include "windef.h"
46 #include "shellapi.h"
47 #include "shlwapi.h"
48 #include "wine/test.h"
50 #include "shell32_test.h"
53 static char argv0[MAX_PATH];
54 static int myARGC;
55 static char** myARGV;
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;
63 /***
65 * ShellExecute wrappers
67 ***/
68 static void dump_child(void);
70 static HANDLE hEvent;
71 static void init_event(const char* child_file)
73 char* event_name;
74 event_name=strrchr(child_file, '\\')+1;
75 hEvent=CreateEvent(NULL, FALSE, FALSE, event_name);
78 static void strcat_param(char* str, const char* name, const char* param)
80 if (param)
82 if (str[strlen(str)-1] == '"')
83 strcat(str, ", ");
84 strcat(str, name);
85 strcat(str, "=\"");
86 strcat(str, param);
87 strcat(str, "\"");
91 static int _todo_wait = 0;
92 #define todo_wait for (_todo_wait = 1; _todo_wait; _todo_wait = 0)
94 static char shell_call[2048]="";
95 static int bad_shellexecute = 0;
96 static INT_PTR shell_execute(LPCSTR operation, LPCSTR file, LPCSTR parameters, LPCSTR directory)
98 INT_PTR rc, rcEmpty = 0;
100 if(!operation)
101 rcEmpty = shell_execute("", file, parameters, directory);
103 strcpy(shell_call, "ShellExecute(");
104 strcat_param(shell_call, "verb", operation);
105 strcat_param(shell_call, "file", file);
106 strcat_param(shell_call, "params", parameters);
107 strcat_param(shell_call, "dir", directory);
108 strcat(shell_call, ")");
109 if (winetest_debug > 1)
110 trace("%s\n", shell_call);
112 DeleteFile(child_file);
113 SetLastError(0xcafebabe);
115 /* FIXME: We cannot use ShellExecuteEx() here because if there is no
116 * association it displays the 'Open With' dialog and I could not find
117 * a flag to prevent this.
119 rc=(INT_PTR)ShellExecute(NULL, operation, file, parameters, directory, SW_SHOWNORMAL);
121 if (rc > 32)
123 int wait_rc;
124 wait_rc=WaitForSingleObject(hEvent, 5000);
125 if (wait_rc == WAIT_TIMEOUT)
127 HWND wnd = FindWindowA("#32770", "Windows");
128 if (wnd != NULL)
130 SendMessage(wnd, WM_CLOSE, 0, 0);
131 win_skip("Skipping shellexecute of file with unassociated extension\n");
132 skip_noassoc_tests = TRUE;
133 rc = SE_ERR_NOASSOC;
136 if (!_todo_wait)
137 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
138 else todo_wine
139 ok(wait_rc==WAIT_OBJECT_0 || rc <= 32, "%s WaitForSingleObject returned %d\n", shell_call, wait_rc);
141 /* The child process may have changed the result file, so let profile
142 * functions know about it
144 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
145 if (rc > 32)
146 dump_child();
148 if(!operation)
150 if (rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */
151 bad_shellexecute = 1;
152 ok(rc == rcEmpty || broken(rc != rcEmpty && rcEmpty == SE_ERR_NOASSOC) /* NT4 */,
153 "%s Got different return value with empty string: %lu %lu\n", shell_call, rc, rcEmpty);
156 return rc;
159 static INT_PTR shell_execute_ex(DWORD mask, LPCSTR operation, LPCSTR file,
160 LPCSTR parameters, LPCSTR directory,
161 LPCSTR class)
163 SHELLEXECUTEINFO sei;
164 BOOL success;
165 INT_PTR rc;
167 strcpy(shell_call, "ShellExecuteEx(");
168 if (mask)
170 char smask[11];
171 sprintf(smask, "0x%x", mask);
172 strcat_param(shell_call, "mask", smask);
174 strcat_param(shell_call, "verb", operation);
175 strcat_param(shell_call, "file", file);
176 strcat_param(shell_call, "params", parameters);
177 strcat_param(shell_call, "dir", directory);
178 strcat_param(shell_call, "class", class);
179 strcat(shell_call, ")");
180 if (winetest_debug > 1)
181 trace("%s\n", shell_call);
183 sei.cbSize=sizeof(sei);
184 sei.fMask=SEE_MASK_NOCLOSEPROCESS | mask;
185 sei.hwnd=NULL;
186 sei.lpVerb=operation;
187 sei.lpFile=file;
188 sei.lpParameters=parameters;
189 sei.lpDirectory=directory;
190 sei.nShow=SW_SHOWNORMAL;
191 sei.hInstApp=NULL; /* Out */
192 sei.lpIDList=NULL;
193 sei.lpClass=class;
194 sei.hkeyClass=NULL;
195 sei.dwHotKey=0;
196 U(sei).hIcon=NULL;
197 sei.hProcess=NULL; /* Out */
199 DeleteFile(child_file);
200 SetLastError(0xcafebabe);
201 success=ShellExecuteEx(&sei);
202 rc=(INT_PTR)sei.hInstApp;
203 ok((success && rc > 32) || (!success && rc <= 32),
204 "%s rc=%d and hInstApp=%ld is not allowed\n", shell_call, success, rc);
206 if (rc > 32)
208 int wait_rc;
209 if (sei.hProcess!=NULL)
211 wait_rc=WaitForSingleObject(sei.hProcess, 5000);
212 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject(hProcess) returned %d\n", wait_rc);
214 wait_rc=WaitForSingleObject(hEvent, 5000);
215 if (!_todo_wait)
216 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
217 else todo_wine
218 ok(wait_rc==WAIT_OBJECT_0, "WaitForSingleObject returned %d\n", wait_rc);
220 /* The child process may have changed the result file, so let profile
221 * functions know about it
223 WritePrivateProfileStringA(NULL, NULL, NULL, child_file);
224 if (rc > 32)
225 dump_child();
227 return rc;
232 /***
234 * Functions to create / delete associations wrappers
236 ***/
238 static BOOL create_test_association(const char* extension)
240 HKEY hkey, hkey_shell;
241 char class[MAX_PATH];
242 LONG rc;
244 sprintf(class, "shlexec%s", extension);
245 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, KEY_SET_VALUE,
246 NULL, &hkey, NULL);
247 if (rc != ERROR_SUCCESS)
248 return FALSE;
250 rc=RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) class, strlen(class)+1);
251 ok(rc==ERROR_SUCCESS, "RegSetValueEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
252 CloseHandle(hkey);
254 rc=RegCreateKeyEx(HKEY_CLASSES_ROOT, class, 0, NULL, 0,
255 KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS, NULL, &hkey, NULL);
256 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx '%s' failed, expected ERROR_SUCCESS, got %d\n", class, rc);
258 rc=RegCreateKeyEx(hkey, "shell", 0, NULL, 0,
259 KEY_CREATE_SUB_KEY, NULL, &hkey_shell, NULL);
260 ok(rc==ERROR_SUCCESS, "RegCreateKeyEx 'shell' failed, expected ERROR_SUCCESS, got %d\n", rc);
262 CloseHandle(hkey);
263 CloseHandle(hkey_shell);
265 return TRUE;
268 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
269 static LSTATUS myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
271 LONG ret;
272 DWORD dwMaxSubkeyLen, dwMaxValueLen;
273 DWORD dwMaxLen, dwSize;
274 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
275 HKEY hSubKey = hKey;
277 if(lpszSubKey)
279 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
280 if (ret) return ret;
283 /* Get highest length for keys, values */
284 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
285 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
286 if (ret) goto cleanup;
288 dwMaxSubkeyLen++;
289 dwMaxValueLen++;
290 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
291 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
293 /* Name too big: alloc a buffer for it */
294 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
296 ret = ERROR_NOT_ENOUGH_MEMORY;
297 goto cleanup;
302 /* Recursively delete all the subkeys */
303 while (TRUE)
305 dwSize = dwMaxLen;
306 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
307 NULL, NULL, NULL)) break;
309 ret = myRegDeleteTreeA(hSubKey, lpszName);
310 if (ret) goto cleanup;
313 if (lpszSubKey)
314 ret = RegDeleteKeyA(hKey, lpszSubKey);
315 else
316 while (TRUE)
318 dwSize = dwMaxLen;
319 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
320 NULL, NULL, NULL, NULL)) break;
322 ret = RegDeleteValueA(hKey, lpszName);
323 if (ret) goto cleanup;
326 cleanup:
327 /* Free buffer if allocated */
328 if (lpszName != szNameBuf)
329 HeapFree( GetProcessHeap(), 0, lpszName);
330 if(lpszSubKey)
331 RegCloseKey(hSubKey);
332 return ret;
335 static void delete_test_association(const char* extension)
337 char class[MAX_PATH];
339 sprintf(class, "shlexec%s", extension);
340 myRegDeleteTreeA(HKEY_CLASSES_ROOT, class);
341 myRegDeleteTreeA(HKEY_CLASSES_ROOT, extension);
344 static void create_test_verb_dde(const char* extension, const char* verb,
345 int rawcmd, const char* cmdtail, const char *ddeexec,
346 const char *application, const char *topic,
347 const char *ifexec)
349 HKEY hkey_shell, hkey_verb, hkey_cmd;
350 char shell[MAX_PATH];
351 char* cmd;
352 LONG rc;
354 sprintf(shell, "shlexec%s\\shell", extension);
355 rc=RegOpenKeyEx(HKEY_CLASSES_ROOT, shell, 0,
356 KEY_CREATE_SUB_KEY, &hkey_shell);
357 assert(rc==ERROR_SUCCESS);
358 rc=RegCreateKeyEx(hkey_shell, verb, 0, NULL, 0, KEY_CREATE_SUB_KEY,
359 NULL, &hkey_verb, NULL);
360 assert(rc==ERROR_SUCCESS);
361 rc=RegCreateKeyEx(hkey_verb, "command", 0, NULL, 0, KEY_SET_VALUE,
362 NULL, &hkey_cmd, NULL);
363 assert(rc==ERROR_SUCCESS);
365 if (rawcmd)
367 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmdtail, strlen(cmdtail)+1);
369 else
371 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(child_file)+2+strlen(cmdtail)+1);
372 sprintf(cmd,"%s shlexec \"%s\" %s", argv0, child_file, cmdtail);
373 rc=RegSetValueEx(hkey_cmd, NULL, 0, REG_SZ, (LPBYTE)cmd, strlen(cmd)+1);
374 assert(rc==ERROR_SUCCESS);
375 HeapFree(GetProcessHeap(), 0, cmd);
378 if (ddeexec)
380 HKEY hkey_ddeexec, hkey_application, hkey_topic, hkey_ifexec;
382 rc=RegCreateKeyEx(hkey_verb, "ddeexec", 0, NULL, 0, KEY_SET_VALUE |
383 KEY_CREATE_SUB_KEY, NULL, &hkey_ddeexec, NULL);
384 assert(rc==ERROR_SUCCESS);
385 rc=RegSetValueEx(hkey_ddeexec, NULL, 0, REG_SZ, (LPBYTE)ddeexec,
386 strlen(ddeexec)+1);
387 assert(rc==ERROR_SUCCESS);
388 if (application)
390 rc=RegCreateKeyEx(hkey_ddeexec, "application", 0, NULL, 0, KEY_SET_VALUE,
391 NULL, &hkey_application, NULL);
392 assert(rc==ERROR_SUCCESS);
393 rc=RegSetValueEx(hkey_application, NULL, 0, REG_SZ, (LPBYTE)application,
394 strlen(application)+1);
395 assert(rc==ERROR_SUCCESS);
396 CloseHandle(hkey_application);
398 if (topic)
400 rc=RegCreateKeyEx(hkey_ddeexec, "topic", 0, NULL, 0, KEY_SET_VALUE,
401 NULL, &hkey_topic, NULL);
402 assert(rc==ERROR_SUCCESS);
403 rc=RegSetValueEx(hkey_topic, NULL, 0, REG_SZ, (LPBYTE)topic,
404 strlen(topic)+1);
405 assert(rc==ERROR_SUCCESS);
406 CloseHandle(hkey_topic);
408 if (ifexec)
410 rc=RegCreateKeyEx(hkey_ddeexec, "ifexec", 0, NULL, 0, KEY_SET_VALUE,
411 NULL, &hkey_ifexec, NULL);
412 assert(rc==ERROR_SUCCESS);
413 rc=RegSetValueEx(hkey_ifexec, NULL, 0, REG_SZ, (LPBYTE)ifexec,
414 strlen(ifexec)+1);
415 assert(rc==ERROR_SUCCESS);
416 CloseHandle(hkey_ifexec);
418 CloseHandle(hkey_ddeexec);
421 CloseHandle(hkey_shell);
422 CloseHandle(hkey_verb);
423 CloseHandle(hkey_cmd);
426 static void create_test_verb(const char* extension, const char* verb,
427 int rawcmd, const char* cmdtail)
429 create_test_verb_dde(extension, verb, rawcmd, cmdtail, NULL, NULL,
430 NULL, NULL);
433 /***
435 * Functions to check that the child process was started just right
436 * (borrowed from dlls/kernel32/tests/process.c)
438 ***/
440 static const char* encodeA(const char* str)
442 static char encoded[2*1024+1];
443 char* ptr;
444 size_t len,i;
446 if (!str) return "";
447 len = strlen(str) + 1;
448 if (len >= sizeof(encoded)/2)
450 fprintf(stderr, "string is too long!\n");
451 assert(0);
453 ptr = encoded;
454 for (i = 0; i < len; i++)
455 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
456 ptr[2 * len] = '\0';
457 return ptr;
460 static unsigned decode_char(char c)
462 if (c >= '0' && c <= '9') return c - '0';
463 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
464 assert(c >= 'A' && c <= 'F');
465 return c - 'A' + 10;
468 static char* decodeA(const char* str)
470 static char decoded[1024];
471 char* ptr;
472 size_t len,i;
474 len = strlen(str) / 2;
475 if (!len--) return NULL;
476 if (len >= sizeof(decoded))
478 fprintf(stderr, "string is too long!\n");
479 assert(0);
481 ptr = decoded;
482 for (i = 0; i < len; i++)
483 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
484 ptr[len] = '\0';
485 return ptr;
488 static void childPrintf(HANDLE h, const char* fmt, ...)
490 va_list valist;
491 char buffer[1024];
492 DWORD w;
494 va_start(valist, fmt);
495 vsprintf(buffer, fmt, valist);
496 va_end(valist);
497 WriteFile(h, buffer, strlen(buffer), &w, NULL);
500 static DWORD ddeInst;
501 static HSZ hszTopic;
502 static char ddeExec[MAX_PATH], ddeApplication[MAX_PATH];
503 static BOOL post_quit_on_execute;
505 static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
506 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
507 ULONG_PTR dwData1, ULONG_PTR dwData2)
509 DWORD size = 0;
511 if (winetest_debug > 2)
512 trace("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
513 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
515 switch (uType)
517 case XTYP_CONNECT:
518 if (!DdeCmpStringHandles(hsz1, hszTopic))
520 size = DdeQueryString(ddeInst, hsz2, ddeApplication, MAX_PATH, CP_WINANSI);
521 assert(size < MAX_PATH);
522 return (HDDEDATA)TRUE;
524 return (HDDEDATA)FALSE;
526 case XTYP_EXECUTE:
527 size = DdeGetData(hData, (LPBYTE)ddeExec, MAX_PATH, 0L);
528 assert(size < MAX_PATH);
529 DdeFreeDataHandle(hData);
530 if (post_quit_on_execute)
531 PostQuitMessage(0);
532 return (HDDEDATA)DDE_FACK;
534 default:
535 return NULL;
540 * This is just to make sure the child won't run forever stuck in a GetMessage()
541 * loop when DDE fails for some reason.
543 static void CALLBACK childTimeout(HWND wnd, UINT msg, UINT_PTR timer, DWORD time)
545 trace("childTimeout called\n");
547 PostQuitMessage(0);
550 static void doChild(int argc, char** argv)
552 char *filename, longpath[MAX_PATH] = "";
553 HANDLE hFile, map;
554 int i;
555 int rc;
556 HSZ hszApplication;
557 UINT_PTR timer;
558 HANDLE dde_ready;
559 MSG msg;
560 char *shared_block;
562 filename=argv[2];
563 hFile=CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
564 if (hFile == INVALID_HANDLE_VALUE)
565 return;
567 /* Arguments */
568 childPrintf(hFile, "[Arguments]\r\n");
569 if (winetest_debug > 2)
571 trace("cmdlineA='%s'\n", GetCommandLineA());
572 trace("argcA=%d\n", argc);
574 childPrintf(hFile, "cmdlineA=%s\r\n", encodeA(GetCommandLineA()));
575 childPrintf(hFile, "argcA=%d\r\n", argc);
576 for (i = 0; i < argc; i++)
578 if (winetest_debug > 2)
579 trace("argvA%d='%s'\n", i, argv[i]);
580 childPrintf(hFile, "argvA%d=%s\r\n", i, encodeA(argv[i]));
582 GetModuleFileNameA(GetModuleHandleA(NULL), longpath, MAX_PATH);
583 childPrintf(hFile, "longPath=%s\r\n", encodeA(longpath));
585 map = OpenFileMappingA(FILE_MAP_READ, FALSE, "winetest_shlexec_dde_map");
586 if (map != NULL)
588 shared_block = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 4096);
589 CloseHandle(map);
590 if (shared_block[0] != '\0' || shared_block[1] != '\0')
592 post_quit_on_execute = TRUE;
593 ddeInst = 0;
594 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
595 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
596 assert(rc == DMLERR_NO_ERROR);
597 hszApplication = DdeCreateStringHandleA(ddeInst, shared_block, CP_WINANSI);
598 hszTopic = DdeCreateStringHandleA(ddeInst, shared_block + strlen(shared_block) + 1, CP_WINANSI);
599 assert(hszApplication && hszTopic);
600 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
602 timer = SetTimer(NULL, 0, 2500, childTimeout);
604 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
605 SetEvent(dde_ready);
606 CloseHandle(dde_ready);
608 while (GetMessage(&msg, NULL, 0, 0))
609 DispatchMessage(&msg);
611 Sleep(500);
612 KillTimer(NULL, timer);
613 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
614 assert(DdeFreeStringHandle(ddeInst, hszTopic));
615 assert(DdeFreeStringHandle(ddeInst, hszApplication));
616 assert(DdeUninitialize(ddeInst));
618 else
620 dde_ready = OpenEvent(EVENT_MODIFY_STATE, FALSE, "winetest_shlexec_dde_ready");
621 SetEvent(dde_ready);
622 CloseHandle(dde_ready);
625 UnmapViewOfFile(shared_block);
627 childPrintf(hFile, "ddeExec=%s\r\n", encodeA(ddeExec));
630 CloseHandle(hFile);
632 init_event(filename);
633 SetEvent(hEvent);
634 CloseHandle(hEvent);
637 static char* getChildString(const char* sect, const char* key)
639 char buf[1024];
640 char* ret;
642 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
643 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
644 assert(!(strlen(buf) & 1));
645 ret = decodeA(buf);
646 return ret;
649 static void dump_child(void)
651 if (winetest_debug > 1)
653 char key[18];
654 char* str;
655 int i, c;
657 str=getChildString("Arguments", "cmdlineA");
658 trace("cmdlineA='%s'\n", str);
659 c=GetPrivateProfileIntA("Arguments", "argcA", -1, child_file);
660 trace("argcA=%d\n",c);
661 for (i=0;i<c;i++)
663 sprintf(key, "argvA%d", i);
664 str=getChildString("Arguments", key);
665 trace("%s='%s'\n", key, str);
670 static int StrCmpPath(const char* s1, const char* s2)
672 if (!s1 && !s2) return 0;
673 if (!s2) return 1;
674 if (!s1) return -1;
675 while (*s1)
677 if (!*s2)
679 if (*s1=='.')
680 s1++;
681 return (*s1-*s2);
683 if ((*s1=='/' || *s1=='\\') && (*s2=='/' || *s2=='\\'))
685 while (*s1=='/' || *s1=='\\')
686 s1++;
687 while (*s2=='/' || *s2=='\\')
688 s2++;
690 else if (toupper(*s1)==toupper(*s2))
692 s1++;
693 s2++;
695 else
697 return (*s1-*s2);
700 if (*s2=='.')
701 s2++;
702 if (*s2)
703 return -1;
704 return 0;
707 static void _okChildString(const char* file, int line, const char* key, const char* expected)
709 char* result;
710 result=getChildString("Arguments", key);
711 if (!result)
713 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
714 return;
716 ok_(file, line)(lstrcmpiA(result, expected) == 0,
717 "%s expected '%s', got '%s'\n", key, expected, result);
720 static void _okChildPath(const char* file, int line, const char* key, const char* expected)
722 char* result;
723 result=getChildString("Arguments", key);
724 if (!result)
726 ok_(file, line)(FALSE, "%s expected '%s', but key not found or empty\n", key, expected);
727 return;
729 ok_(file, line)(StrCmpPath(result, expected) == 0,
730 "%s expected '%s', got '%s'\n", key, expected, result);
733 static void _okChildInt(const char* file, int line, const char* key, int expected)
735 INT result;
736 result=GetPrivateProfileIntA("Arguments", key, expected, child_file);
737 ok_(file, line)(result == expected,
738 "%s expected %d, but got %d\n", key, expected, result);
741 #define okChildString(key, expected) _okChildString(__FILE__, __LINE__, (key), (expected))
742 #define okChildPath(key, expected) _okChildPath(__FILE__, __LINE__, (key), (expected))
743 #define okChildInt(key, expected) _okChildInt(__FILE__, __LINE__, (key), (expected))
745 /***
747 * GetLongPathNameA equivalent that supports Win95 and WinNT
749 ***/
751 static DWORD get_long_path_name(const char* shortpath, char* longpath, DWORD longlen)
753 char tmplongpath[MAX_PATH];
754 const char* p;
755 DWORD sp = 0, lp = 0;
756 DWORD tmplen;
757 WIN32_FIND_DATAA wfd;
758 HANDLE goit;
760 if (!shortpath || !shortpath[0])
761 return 0;
763 if (shortpath[1] == ':')
765 tmplongpath[0] = shortpath[0];
766 tmplongpath[1] = ':';
767 lp = sp = 2;
770 while (shortpath[sp])
772 /* check for path delimiters and reproduce them */
773 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
775 if (!lp || tmplongpath[lp-1] != '\\')
777 /* strip double "\\" */
778 tmplongpath[lp++] = '\\';
780 tmplongpath[lp] = 0; /* terminate string */
781 sp++;
782 continue;
785 p = shortpath + sp;
786 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
788 tmplongpath[lp++] = *p++;
789 tmplongpath[lp++] = *p++;
791 for (; *p && *p != '/' && *p != '\\'; p++);
792 tmplen = p - (shortpath + sp);
793 lstrcpyn(tmplongpath + lp, shortpath + sp, tmplen + 1);
794 /* Check if the file exists and use the existing file name */
795 goit = FindFirstFileA(tmplongpath, &wfd);
796 if (goit == INVALID_HANDLE_VALUE)
797 return 0;
798 FindClose(goit);
799 strcpy(tmplongpath + lp, wfd.cFileName);
800 lp += strlen(tmplongpath + lp);
801 sp += tmplen;
803 tmplen = strlen(shortpath) - 1;
804 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
805 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
806 tmplongpath[lp++] = shortpath[tmplen];
807 tmplongpath[lp] = 0;
809 tmplen = strlen(tmplongpath) + 1;
810 if (tmplen <= longlen)
812 strcpy(longpath, tmplongpath);
813 tmplen--; /* length without 0 */
816 return tmplen;
819 /***
821 * PathFindFileNameA equivalent that supports WinNT
823 ***/
825 static LPSTR path_find_file_name(LPCSTR lpszPath)
827 LPCSTR lastSlash = lpszPath;
829 while (lpszPath && *lpszPath)
831 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
832 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
833 lastSlash = lpszPath + 1;
834 lpszPath = CharNext(lpszPath);
836 return (LPSTR)lastSlash;
839 /***
841 * Tests
843 ***/
845 static const char* testfiles[]=
847 "%s\\test file.shlexec",
848 "%s\\%%nasty%% $file.shlexec",
849 "%s\\test file.noassoc",
850 "%s\\test file.noassoc.shlexec",
851 "%s\\test file.shlexec.noassoc",
852 "%s\\test_shortcut_shlexec.lnk",
853 "%s\\test_shortcut_exe.lnk",
854 "%s\\test file.shl",
855 "%s\\test file.shlfoo",
856 "%s\\test file.sfe",
857 "%s\\masked file.shlexec",
858 "%s\\masked",
859 "%s\\test file.sde",
860 "%s\\test file.exe",
861 "%s\\test2.exe",
862 "%s\\simple.shlexec",
863 "%s\\drawback_file.noassoc",
864 "%s\\drawback_file.noassoc foo.shlexec",
865 "%s\\drawback_nonexist.noassoc foo.shlexec",
866 NULL
869 typedef struct
871 const char* verb;
872 const char* basename;
873 int todo;
874 INT_PTR rc;
875 } filename_tests_t;
877 static filename_tests_t filename_tests[]=
879 /* Test bad / nonexistent filenames */
880 {NULL, "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
881 {NULL, "%s\\nonexistent.noassoc", 0x0, SE_ERR_FNF},
883 /* Standard tests */
884 {NULL, "%s\\test file.shlexec", 0x0, 33},
885 {NULL, "%s\\test file.shlexec.", 0x0, 33},
886 {NULL, "%s\\%%nasty%% $file.shlexec", 0x0, 33},
887 {NULL, "%s/test file.shlexec", 0x0, 33},
889 /* Test filenames with no association */
890 {NULL, "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
892 /* Test double extensions */
893 {NULL, "%s\\test file.noassoc.shlexec", 0x0, 33},
894 {NULL, "%s\\test file.shlexec.noassoc", 0x0, SE_ERR_NOASSOC},
896 /* Test alternate verbs */
897 {"LowerL", "%s\\nonexistent.shlexec", 0x0, SE_ERR_FNF},
898 {"LowerL", "%s\\test file.noassoc", 0x0, SE_ERR_NOASSOC},
900 {"QuotedLowerL", "%s\\test file.shlexec", 0x0, 33},
901 {"QuotedUpperL", "%s\\test file.shlexec", 0x0, 33},
903 /* Test file masked due to space */
904 {NULL, "%s\\masked file.shlexec", 0x1, 33},
905 /* Test if quoting prevents the masking */
906 {NULL, "%s\\masked file.shlexec", 0x40, 33},
908 {NULL, NULL, 0}
911 static filename_tests_t noquotes_tests[]=
913 /* Test unquoted '%1' thingies */
914 {"NoQuotes", "%s\\test file.shlexec", 0xa, 33},
915 {"LowerL", "%s\\test file.shlexec", 0xa, 33},
916 {"UpperL", "%s\\test file.shlexec", 0xa, 33},
918 {NULL, NULL, 0}
921 static void test_lpFile_parsed(void)
923 char fileA[MAX_PATH];
924 INT_PTR rc;
926 /* existing "drawback_file.noassoc" prevents finding "drawback_file.noassoc foo.shlexec" on wine */
927 sprintf(fileA, "%s\\drawback_file.noassoc foo.shlexec", tmpdir);
928 rc=shell_execute(NULL, fileA, NULL, NULL);
929 todo_wine ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
931 /* if quoted, existing "drawback_file.noassoc" not prevents finding "drawback_file.noassoc foo.shlexec" on wine */
932 sprintf(fileA, "\"%s\\drawback_file.noassoc foo.shlexec\"", tmpdir);
933 rc=shell_execute(NULL, fileA, NULL, NULL);
934 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
935 "%s failed: rc=%lu\n", shell_call, rc);
937 /* error should be SE_ERR_FNF, not SE_ERR_NOASSOC */
938 sprintf(fileA, "\"%s\\drawback_file.noassoc\" foo.shlexec", tmpdir);
939 rc=shell_execute(NULL, fileA, NULL, NULL);
940 ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
942 /* ""command"" not works on wine (and real win9x and w2k) */
943 sprintf(fileA, "\"\"%s\\simple.shlexec\"\"", tmpdir);
944 rc=shell_execute(NULL, fileA, NULL, NULL);
945 todo_wine ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win9x/2000 */,
946 "%s failed: rc=%lu\n", shell_call, rc);
948 /* nonexisting "drawback_nonexist.noassoc" not prevents finding "drawback_nonexist.noassoc foo.shlexec" on wine */
949 sprintf(fileA, "%s\\drawback_nonexist.noassoc foo.shlexec", tmpdir);
950 rc=shell_execute(NULL, fileA, NULL, NULL);
951 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
953 /* is SEE_MASK_DOENVSUBST default flag? Should only be when XP emulates 9x (XP bug or real 95 or ME behavior ?) */
954 rc=shell_execute(NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL);
955 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
957 /* quoted */
958 rc=shell_execute(NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL);
959 todo_wine ok(rc == SE_ERR_FNF, "%s succeeded: rc=%lu\n", shell_call, rc);
961 /* test SEE_MASK_DOENVSUBST works */
962 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
963 NULL, "%TMPDIR%\\simple.shlexec", NULL, NULL, NULL);
964 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
966 /* quoted lpFile does not work on real win95 and nt4 */
967 rc=shell_execute_ex(SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI,
968 NULL, "\"%TMPDIR%\\simple.shlexec\"", NULL, NULL, NULL);
969 ok(rc > 32 || broken(rc == SE_ERR_FNF) /* Win95/NT4 */,
970 "%s failed: rc=%lu\n", shell_call, rc);
973 typedef struct
975 const char* cmd;
976 const char* args[11];
977 int todo;
978 } cmdline_tests_t;
980 static const cmdline_tests_t cmdline_tests[] =
982 {"exe",
983 {"exe", NULL}, 0},
985 {"exe arg1 arg2 \"arg three\" 'four five` six\\ $even)",
986 {"exe", "arg1", "arg2", "arg three", "'four", "five`", "six\\", "$even)", NULL}, 0},
988 {"exe arg=1 arg-2 three\tfour\rfour\nfour ",
989 {"exe", "arg=1", "arg-2", "three", "four\rfour\nfour", NULL}, 0},
991 {"exe arg\"one\" \"second\"arg thirdarg ",
992 {"exe", "argone", "secondarg", "thirdarg", NULL}, 0},
994 /* Don't lose unclosed quoted arguments */
995 {"exe arg1 \"unclosed",
996 {"exe", "arg1", "unclosed", NULL}, 0},
998 {"exe arg1 \"",
999 {"exe", "arg1", "", NULL}, 0},
1001 /* cmd's metacharacters have no special meaning */
1002 {"exe \"one^\" \"arg\"&two three|four",
1003 {"exe", "one^", "arg&two", "three|four", NULL}, 0},
1005 /* Environment variables are not interpreted either */
1006 {"exe %TMPDIR% %2",
1007 {"exe", "%TMPDIR%", "%2", NULL}, 0},
1009 /* If not followed by a quote, backslashes go through as is */
1010 {"exe o\\ne t\\\\wo t\\\\\\ree f\\\\\\\\our ",
1011 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1013 {"exe \"o\\ne\" \"t\\\\wo\" \"t\\\\\\ree\" \"f\\\\\\\\our\" ",
1014 {"exe", "o\\ne", "t\\\\wo", "t\\\\\\ree", "f\\\\\\\\our", NULL}, 0},
1016 /* When followed by a quote their number is halved and the remainder
1017 * escapes the quote
1019 {"exe \\\"one \\\\\"two\" \\\\\\\"three \\\\\\\\\"four\" end",
1020 {"exe", "\"one", "\\two", "\\\"three", "\\\\four", "end", NULL}, 0},
1022 {"exe \"one\\\" still\" \"two\\\\\" \"three\\\\\\\" still\" \"four\\\\\\\\\" end",
1023 {"exe", "one\" still", "two\\", "three\\\" still", "four\\\\", "end", NULL}, 0},
1025 /* One can put a quote in an unquoted string by tripling it, that is in
1026 * effect quoting it like so """ -> ". The general rule is as follows:
1027 * 3n quotes -> n quotes
1028 * 3n+1 quotes -> n quotes plus start of a quoted string
1029 * 3n+2 quotes -> n quotes (plus an empty string from the remaining pair)
1030 * Nicely, when n is 0 we get the standard rules back.
1032 {"exe two\"\"quotes next",
1033 {"exe", "twoquotes", "next", NULL}, 0},
1035 {"exe three\"\"\"quotes next",
1036 {"exe", "three\"quotes", "next", NULL}, 0x21},
1038 {"exe four\"\"\"\" quotes\" next 4%3=1",
1039 {"exe", "four\" quotes", "next", "4%3=1", NULL}, 0x61},
1041 {"exe five\"\"\"\"\"quotes next",
1042 {"exe", "five\"quotes", "next", NULL}, 0x21},
1044 {"exe six\"\"\"\"\"\"quotes next",
1045 {"exe", "six\"\"quotes", "next", NULL}, 0x20},
1047 {"exe seven\"\"\"\"\"\"\" quotes\" next 7%3=1",
1048 {"exe", "seven\"\" quotes", "next", "7%3=1", NULL}, 0x20},
1050 {"exe twelve\"\"\"\"\"\"\"\"\"\"\"\"quotes next",
1051 {"exe", "twelve\"\"\"\"quotes", "next", NULL}, 0x20},
1053 {"exe thirteen\"\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1054 {"exe", "thirteen\"\"\"\" quotes", "next", "13%3=1", NULL}, 0x20},
1056 /* Inside a quoted string the opening quote is added to the set of
1057 * consecutive quotes to get the effective quotes count. This gives:
1058 * 1+3n quotes -> n quotes
1059 * 1+3n+1 quotes -> n quotes plus closes the quoted string
1060 * 1+3n+2 quotes -> n+1 quotes plus closes the quoted string
1062 {"exe \"two\"\"quotes next",
1063 {"exe", "two\"quotes", "next", NULL}, 0x21},
1065 {"exe \"two\"\" next",
1066 {"exe", "two\"", "next", NULL}, 0x21},
1068 {"exe \"three\"\"\" quotes\" next 4%3=1",
1069 {"exe", "three\" quotes", "next", "4%3=1", NULL}, 0x61},
1071 {"exe \"four\"\"\"\"quotes next",
1072 {"exe", "four\"quotes", "next", NULL}, 0x21},
1074 {"exe \"five\"\"\"\"\"quotes next",
1075 {"exe", "five\"\"quotes", "next", NULL}, 0x20},
1077 {"exe \"six\"\"\"\"\"\" quotes\" next 7%3=1",
1078 {"exe", "six\"\" quotes", "next", "7%3=1", NULL}, 0x20},
1080 {"exe \"eleven\"\"\"\"\"\"\"\"\"\"\"quotes next",
1081 {"exe", "eleven\"\"\"\"quotes", "next", NULL}, 0x20},
1083 {"exe \"twelve\"\"\"\"\"\"\"\"\"\"\"\" quotes\" next 13%3=1",
1084 {"exe", "twelve\"\"\"\" quotes", "next", "13%3=1", NULL}, 0x20},
1086 /* Escaped consecutive quotes are fun */
1087 {"exe \"the crazy \\\\\"\"\"\\\\\" quotes",
1088 {"exe", "the crazy \\\"\\", "quotes", NULL}, 0x21},
1090 /* The executable path has its own rules!!!
1091 * - Backslashes have no special meaning.
1092 * - If the first character is a quote, then the second quote ends the
1093 * executable path.
1094 * - The previous rule holds even if the next character is not a space!
1095 * - If the first character is not a quote, then quotes have no special
1096 * meaning either and the executable path stops at the first space.
1097 * - The consecutive quotes rules don't apply either.
1098 * - Even if there is no space between the executable path and the first
1099 * argument, the latter is parsed using the regular rules.
1101 {"exe\"file\"path arg1",
1102 {"exe\"file\"path", "arg1", NULL}, 0x10},
1104 {"exe\"file\"path\targ1",
1105 {"exe\"file\"path", "arg1", NULL}, 0x10},
1107 {"exe\"path\\ arg1",
1108 {"exe\"path\\", "arg1", NULL}, 0x31},
1110 {"\\\"exe \"arg one\"",
1111 {"\\\"exe", "arg one", NULL}, 0x10},
1113 {"\"spaced exe\" \"next arg\"",
1114 {"spaced exe", "next arg", NULL}, 0},
1116 {"\"spaced exe\"\t\"next arg\"",
1117 {"spaced exe", "next arg", NULL}, 0},
1119 {"\"exe\"arg\" one\" argtwo",
1120 {"exe", "arg one", "argtwo", NULL}, 0x31},
1122 {"\"spaced exe\\\"arg1 arg2",
1123 {"spaced exe\\", "arg1", "arg2", NULL}, 0x11},
1125 {"\"two\"\" arg1 ",
1126 {"two", " arg1 ", NULL}, 0x11},
1128 {"\"three\"\"\" arg2",
1129 {"three", "", "arg2", NULL}, 0x61},
1131 {"\"four\"\"\"\"arg1",
1132 {"four", "\"arg1", NULL}, 0x11},
1134 /* If the first character is a space then the executable path is empty */
1135 {" \"arg\"one argtwo",
1136 {"", "argone", "argtwo", NULL}, 0},
1138 {NULL, {NULL}, 0}
1141 static BOOL test_one_cmdline(const cmdline_tests_t* test)
1143 WCHAR cmdW[MAX_PATH], argW[MAX_PATH];
1144 LPWSTR *cl2a;
1145 int cl2a_count;
1146 LPWSTR *argsW;
1147 int i, count;
1149 /* trace("----- cmd='%s'\n", test->cmd); */
1150 MultiByteToWideChar(CP_ACP, 0, test->cmd, -1, cmdW, sizeof(cmdW)/sizeof(*cmdW));
1151 argsW = cl2a = CommandLineToArgvW(cmdW, &cl2a_count);
1152 if (argsW == NULL && cl2a_count == -1)
1154 win_skip("CommandLineToArgvW not implemented, skipping\n");
1155 return FALSE;
1158 count = 0;
1159 while (test->args[count])
1160 count++;
1161 if ((test->todo & 0x1) == 0)
1162 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1163 else todo_wine
1164 ok(cl2a_count == count, "%s: expected %d arguments, but got %d\n", test->cmd, count, cl2a_count);
1166 for (i = 0; i < cl2a_count; i++)
1168 if (i < count)
1170 MultiByteToWideChar(CP_ACP, 0, test->args[i], -1, argW, sizeof(argW)/sizeof(*argW));
1171 if ((test->todo & (1 << (i+4))) == 0)
1172 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1173 else todo_wine
1174 ok(!lstrcmpW(*argsW, argW), "%s: arg[%d] expected %s but got %s\n", test->cmd, i, wine_dbgstr_w(argW), wine_dbgstr_w(*argsW));
1176 else if ((test->todo & 0x1) == 0)
1177 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1178 else todo_wine
1179 ok(0, "%s: got extra arg[%d]=%s\n", test->cmd, i, wine_dbgstr_w(*argsW));
1180 argsW++;
1182 LocalFree(cl2a);
1183 return TRUE;
1186 static void test_commandline2argv(void)
1188 static const WCHAR exeW[] = {'e','x','e',0};
1189 const cmdline_tests_t* test;
1190 WCHAR strW[MAX_PATH];
1191 LPWSTR *args;
1192 int numargs;
1193 DWORD le;
1195 test = cmdline_tests;
1196 while (test->cmd)
1198 if (!test_one_cmdline(test))
1199 return;
1200 test++;
1203 SetLastError(0xdeadbeef);
1204 args = CommandLineToArgvW(exeW, NULL);
1205 le = GetLastError();
1206 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1208 SetLastError(0xdeadbeef);
1209 args = CommandLineToArgvW(NULL, NULL);
1210 le = GetLastError();
1211 ok(args == NULL && le == ERROR_INVALID_PARAMETER, "expected NULL with ERROR_INVALID_PARAMETER got %p with %u\n", args, le);
1213 *strW = 0;
1214 args = CommandLineToArgvW(strW, &numargs);
1215 ok(numargs == 1, "expected 1 args, got %d\n", numargs);
1216 if (numargs == 1)
1218 GetModuleFileNameW(NULL, strW, sizeof(strW)/sizeof(*strW));
1219 ok(!lstrcmpW(args[0], strW), "wrong path to the current executable: %s instead of %s\n", wine_dbgstr_w(args[0]), wine_dbgstr_w(strW));
1221 if (args) LocalFree(args);
1224 /* The goal here is to analyze how ShellExecute() builds the command that
1225 * will be run. The tricky part is that there are three transformation
1226 * steps between the 'parameters' string we pass to ShellExecute() and the
1227 * argument list we observe in the child process:
1228 * - The parsing of 'parameters' string into individual arguments. The tests
1229 * show this is done differently from both CreateProcess() and
1230 * CommandLineToArgv()!
1231 * - The way the command 'formatting directives' such as %1, %2, etc are
1232 * handled.
1233 * - And the way the resulting command line is then parsed to yield the
1234 * argument list we check.
1236 typedef struct
1238 const char* verb;
1239 const char* params;
1240 int todo;
1241 cmdline_tests_t cmd;
1242 } argify_tests_t;
1244 static const argify_tests_t argify_tests[] =
1246 /* Start with three simple parameters. Notice that one can reorder and
1247 * duplicate the parameters. Also notice how %* take the raw input
1248 * parameters string, including the trailing spaces, no matter what
1249 * arguments have already been used.
1251 {"Params232S", "p2 p3 p4 ", 0xc2,
1252 {" p2 p3 \"p2\" \"p2 p3 p4 \"",
1253 {"", "p2", "p3", "p2", "p2 p3 p4 ", NULL}, 0}},
1255 /* Unquoted argument references like %2 don't automatically quote their
1256 * argument. Similarly, when they are quoted they don't escape the quotes
1257 * that their argument may contain.
1259 {"Params232S", "\"p two\" p3 p4 ", 0x3f3,
1260 {" p two p3 \"p two\" \"\"p two\" p3 p4 \"",
1261 {"", "p", "two", "p3", "p two", "p", "two p3 p4 ", NULL}, 0}},
1263 /* Only single digits are supported so only %1 to %9. Shown here with %20
1264 * because %10 is a pain.
1266 {"Params20", "p", 0,
1267 {" \"p0\"",
1268 {"", "p0", NULL}, 0}},
1270 /* Only (double-)quotes have a special meaning. */
1271 {"Params23456", "'p2 p3` p4\\ $even", 0x40,
1272 {" \"'p2\" \"p3`\" \"p4\\\" \"$even\" \"\"",
1273 {"", "'p2", "p3`", "p4\" $even \"", NULL}, 0x80}},
1275 {"Params23456", "p=2 p-3 p4\tp4\rp4\np4", 0x1c2,
1276 {" \"p=2\" \"p-3\" \"p4\tp4\rp4\np4\" \"\" \"\"",
1277 {"", "p=2", "p-3", "p4\tp4\rp4\np4", "", "", NULL}, 0}},
1279 /* In unquoted strings, quotes are treated are a parameter separator just
1280 * like spaces! However they can be doubled to get a literal quote.
1281 * Specifically:
1282 * 2n quotes -> n quotes
1283 * 2n+1 quotes -> n quotes and a parameter separator
1285 {"Params23456789", "one\"quote \"p four\" one\"quote p7", 0xff3,
1286 {" \"one\" \"quote\" \"p four\" \"one\" \"quote\" \"p7\" \"\" \"\"",
1287 {"", "one", "quote", "p four", "one", "quote", "p7", "", "", NULL}, 0}},
1289 {"Params23456789", "two\"\"quotes \"p three\" two\"\"quotes p5", 0xf2,
1290 {" \"two\"quotes\" \"p three\" \"two\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1291 {"", "twoquotes p", "three twoquotes", "p5", "", "", "", "", NULL}, 0}},
1293 {"Params23456789", "three\"\"\"quotes \"p four\" three\"\"\"quotes p6", 0xff3,
1294 {" \"three\"\" \"quotes\" \"p four\" \"three\"\" \"quotes\" \"p6\" \"\" \"\"",
1295 {"", "three\"", "quotes", "p four", "three\"", "quotes", "p6", "", "", NULL}, 0x7e1}},
1297 {"Params23456789", "four\"\"\"\"quotes \"p three\" four\"\"\"\"quotes p5", 0xf3,
1298 {" \"four\"\"quotes\" \"p three\" \"four\"\"quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1299 {"", "four\"quotes p", "three fourquotes p5 \"", "", "", "", NULL}, 0xde1}},
1301 /* Quoted strings cannot be continued by tacking on a non space character
1302 * either.
1304 {"Params23456", "\"p two\"p3 \"p four\"p5 p6", 0x1f3,
1305 {" \"p two\" \"p3\" \"p four\" \"p5\" \"p6\"",
1306 {"", "p two", "p3", "p four", "p5", "p6", NULL}, 0}},
1308 /* In quoted strings, the quotes are halved and an odd number closes the
1309 * string. Specifically:
1310 * 2n quotes -> n quotes
1311 * 2n+1 quotes -> n quotes and closes the string and hence the parameter
1313 {"Params23456789", "\"one q\"uote \"p four\" \"one q\"uote p7", 0xff3,
1314 {" \"one q\" \"uote\" \"p four\" \"one q\" \"uote\" \"p7\" \"\" \"\"",
1315 {"", "one q", "uote", "p four", "one q", "uote", "p7", "", "", NULL}, 0}},
1317 {"Params23456789", "\"two \"\" quotes\" \"p three\" \"two \"\" quotes\" p5", 0x1ff3,
1318 {" \"two \" quotes\" \"p three\" \"two \" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1319 {"", "two ", "quotes p", "three two", " quotes", "p5", "", "", "", "", NULL}, 0}},
1321 {"Params23456789", "\"three q\"\"\"uotes \"p four\" \"three q\"\"\"uotes p7", 0xff3,
1322 {" \"three q\"\" \"uotes\" \"p four\" \"three q\"\" \"uotes\" \"p7\" \"\" \"\"",
1323 {"", "three q\"", "uotes", "p four", "three q\"", "uotes", "p7", "", "", NULL}, 0x7e1}},
1325 {"Params23456789", "\"four \"\"\"\" quotes\" \"p three\" \"four \"\"\"\" quotes\" p5", 0xff3,
1326 {" \"four \"\" quotes\" \"p three\" \"four \"\" quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1327 {"", "four \"", "quotes p", "three four", "", "quotes p5 \"", "", "", "", NULL}, 0x3e0}},
1329 /* The quoted string rules also apply to consecutive quotes at the start
1330 * of a parameter but don't count the opening quote!
1332 {"Params23456789", "\"\"twoquotes \"p four\" \"\"twoquotes p7", 0xbf3,
1333 {" \"\" \"twoquotes\" \"p four\" \"\" \"twoquotes\" \"p7\" \"\" \"\"",
1334 {"", "", "twoquotes", "p four", "", "twoquotes", "p7", "", "", NULL}, 0}},
1336 {"Params23456789", "\"\"\"three quotes\" \"p three\" \"\"\"three quotes\" p5", 0x6f3,
1337 {" \"\"three quotes\" \"p three\" \"\"three quotes\" \"p5\" \"\" \"\" \"\" \"\"",
1338 {"", "three", "quotes p", "three \"three", "quotes p5 \"", "", "", "", NULL}, 0x181}},
1340 {"Params23456789", "\"\"\"\"fourquotes \"p four\" \"\"\"\"fourquotes p7", 0xbf3,
1341 {" \"\"\" \"fourquotes\" \"p four\" \"\"\" \"fourquotes\" \"p7\" \"\" \"\"",
1342 {"", "\"", "fourquotes", "p four", "\"", "fourquotes", "p7", "", "", NULL}, 0x7e1}},
1344 /* An unclosed quoted string gets lost! */
1345 {"Params23456", "p2 \"p3\" \"p4 is lost", 0x1c3,
1346 {" \"p2\" \"p3\" \"\" \"\" \"\"",
1347 {"", "p2", "p3", "", "", "", NULL}, 0}},
1349 /* Backslashes have no special meaning even when preceding quotes. All
1350 * they do is start an unquoted string.
1352 {"Params23456", "\\\"p\\three \"pfour\\\" pfive", 0x73,
1353 {" \"\\\" \"p\\three\" \"pfour\\\" \"pfive\" \"\"",
1354 {"", "\" p\\three pfour\"", "pfive", "", NULL}, 0}},
1356 /* Environment variables are left untouched. */
1357 {"Params23456", "%TMPDIR% %t %c", 0x12,
1358 {" \"%TMPDIR%\" \"%t\" \"%c\" \"\" \"\"",
1359 {"", "%TMPDIR%", "%t", "%c", "", "", NULL}, 0}},
1361 /* %~2 is equivalent to %*. However %~3 and higher include the spaces
1362 * before the parameter!
1363 * (but not the previous parameter's closing quote fortunately)
1365 {"Params2345Etc", "p2 p3 \"p4\" p5 p6 ", 0x3f3,
1366 {" ~2=\"p2 p3 \"p4\" p5 p6 \" ~3=\" p3 \"p4\" p5 p6 \" ~4=\" \"p4\" p5 p6 \" ~5= p5 p6 ",
1367 {"", "~2=p2 p3 p4 p5 p6 ", "~3= p3 p4 p5 p6 ", "~4= p4 p5 p6 ", "~5=", "p5", "p6", NULL}, 0}},
1369 /* %~n works even if there is no nth parameter. */
1370 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 ", 0x12,
1371 {" ~9=\" \"",
1372 {"", "~9= ", NULL}, 0}},
1374 {"Params9Etc", "p2 p3 p4 p5 p6 p7 ", 0x12,
1375 {" ~9=\"\"",
1376 {"", "~9=", NULL}, 0}},
1378 /* The %~n directives also transmit the tenth parameter and beyond. */
1379 {"Params9Etc", "p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 and beyond!", 0x12,
1380 {" ~9=\" p9 p10 p11 and beyond!\"",
1381 {"", "~9= p9 p10 p11 and beyond!", NULL}, 0}},
1383 /* Bad formatting directives lose their % sign, except those followed by
1384 * a tilde! Environment variables are not expanded but lose their % sign.
1386 {"ParamsBad", "p2 p3 p4 p5", 0x12,
1387 {" \"% - %~ %~0 %~1 %~a %~* a b c TMPDIR\"",
1388 {"", "% - %~ %~0 %~1 %~a %~* a b c TMPDIR", NULL}, 0}},
1390 {NULL, NULL, 0, {NULL, {NULL}, 0}}
1393 static void test_argify(void)
1395 BOOL has_cl2a = TRUE;
1396 char fileA[MAX_PATH], params[2*MAX_PATH+12];
1397 INT_PTR rc;
1398 const argify_tests_t* test;
1399 const char* cmd;
1400 unsigned i, count;
1402 create_test_verb(".shlexec", "Params232S", 0, "Params232S %2 %3 \"%2\" \"%*\"");
1403 create_test_verb(".shlexec", "Params23456", 0, "Params23456 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\"");
1404 create_test_verb(".shlexec", "Params23456789", 0, "Params23456789 \"%2\" \"%3\" \"%4\" \"%5\" \"%6\" \"%7\" \"%8\" \"%9\"");
1405 create_test_verb(".shlexec", "Params2345Etc", 0, "Params2345Etc ~2=\"%~2\" ~3=\"%~3\" ~4=\"%~4\" ~5=%~5");
1406 create_test_verb(".shlexec", "Params9Etc", 0, "Params9Etc ~9=\"%~9\"");
1407 create_test_verb(".shlexec", "Params20", 0, "Params20 \"%20\"");
1408 create_test_verb(".shlexec", "ParamsBad", 0, "ParamsBad \"%% %- %~ %~0 %~1 %~a %~* %a %b %c %TMPDIR%\"");
1410 sprintf(fileA, "%s\\test file.shlexec", tmpdir);
1412 test = argify_tests;
1413 while (test->params)
1415 /* trace("***** verb='%s' params='%s'\n", test->verb, test->params); */
1416 rc = shell_execute_ex(SEE_MASK_DOENVSUBST, test->verb, fileA, test->params, NULL, NULL);
1417 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1419 count = 0;
1420 while (test->cmd.args[count])
1421 count++;
1422 if ((test->todo & 0x1) == 0)
1423 /* +4 for the shlexec arguments, -1 because of the added ""
1424 * argument for the CommandLineToArgvW() tests.
1426 okChildInt("argcA", 4 + count - 1);
1427 else todo_wine
1428 okChildInt("argcA", 4 + count - 1);
1430 cmd = getChildString("Arguments", "cmdlineA");
1431 /* Our commands are such that the verb immediately precedes the
1432 * part we are interested in.
1434 if (cmd) cmd = strstr(cmd, test->verb);
1435 if (cmd) cmd += strlen(test->verb);
1436 if (!cmd) cmd = "(null)";
1437 if ((test->todo & 0x2) == 0)
1438 ok(!strcmp(cmd, test->cmd.cmd), "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1439 else todo_wine
1440 ok(!strcmp(cmd, test->cmd.cmd), "%s: the cmdline is '%s' instead of '%s'\n", shell_call, cmd, test->cmd.cmd);
1442 for (i = 0; i < count - 1; i++)
1444 char argname[18];
1445 sprintf(argname, "argvA%d", 4 + i);
1446 if ((test->todo & (1 << (i+4))) == 0)
1447 okChildString(argname, test->cmd.args[i+1]);
1448 else todo_wine
1449 okChildString(argname, test->cmd.args[i+1]);
1452 if (has_cl2a)
1453 has_cl2a = test_one_cmdline(&(test->cmd));
1454 test++;
1457 /* Test with a long parameter */
1458 for (rc = 0; rc < MAX_PATH; rc++)
1459 fileA[rc] = 'a' + rc % 26;
1460 fileA[MAX_PATH-1] = '\0';
1461 sprintf(params, "shlexec \"%s\" %s", child_file, fileA);
1463 /* We need NOZONECHECKS on Win2003 to block a dialog */
1464 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params, NULL, NULL);
1465 ok(rc > 32, "%s failed: rc=%lu\n", shell_call, rc);
1466 okChildInt("argcA", 4);
1467 okChildString("argvA3", fileA);
1470 static void test_filename(void)
1472 char filename[MAX_PATH];
1473 const filename_tests_t* test;
1474 char* c;
1475 INT_PTR rc;
1477 test=filename_tests;
1478 while (test->basename)
1480 BOOL quotedfile = FALSE;
1482 if (skip_noassoc_tests && test->rc == SE_ERR_NOASSOC)
1484 win_skip("Skipping shellexecute of file with unassociated extension\n");
1485 test++;
1486 continue;
1489 sprintf(filename, test->basename, tmpdir);
1490 if (strchr(filename, '/'))
1492 c=filename;
1493 while (*c)
1495 if (*c=='\\')
1496 *c='/';
1497 c++;
1500 if ((test->todo & 0x40)==0)
1502 rc=shell_execute(test->verb, filename, NULL, NULL);
1504 else
1506 char quoted[MAX_PATH + 2];
1508 quotedfile = TRUE;
1509 sprintf(quoted, "\"%s\"", filename);
1510 rc=shell_execute(test->verb, quoted, NULL, NULL);
1512 if (rc > 32)
1513 rc=33;
1514 if ((test->todo & 0x1)==0)
1516 ok(rc==test->rc ||
1517 broken(quotedfile && rc == SE_ERR_FNF), /* NT4 */
1518 "%s failed: rc=%ld err=%u\n", shell_call,
1519 rc, GetLastError());
1521 else todo_wine
1523 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1524 rc, GetLastError());
1526 if (rc == 33)
1528 const char* verb;
1529 if ((test->todo & 0x2)==0)
1531 okChildInt("argcA", 5);
1533 else todo_wine
1535 okChildInt("argcA", 5);
1537 verb=(test->verb ? test->verb : "Open");
1538 if ((test->todo & 0x4)==0)
1540 okChildString("argvA3", verb);
1542 else todo_wine
1544 okChildString("argvA3", verb);
1546 if ((test->todo & 0x8)==0)
1548 okChildPath("argvA4", filename);
1550 else todo_wine
1552 okChildPath("argvA4", filename);
1555 test++;
1558 test=noquotes_tests;
1559 while (test->basename)
1561 sprintf(filename, test->basename, tmpdir);
1562 rc=shell_execute(test->verb, filename, NULL, NULL);
1563 if (rc > 32)
1564 rc=33;
1565 if ((test->todo & 0x1)==0)
1567 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1568 rc, GetLastError());
1570 else todo_wine
1572 ok(rc==test->rc, "%s failed: rc=%ld err=%u\n", shell_call,
1573 rc, GetLastError());
1575 if (rc==0)
1577 int count;
1578 const char* verb;
1579 char* str;
1581 verb=(test->verb ? test->verb : "Open");
1582 if ((test->todo & 0x4)==0)
1584 okChildString("argvA3", verb);
1586 else todo_wine
1588 okChildString("argvA3", verb);
1591 count=4;
1592 str=filename;
1593 while (1)
1595 char attrib[18];
1596 char* space;
1597 space=strchr(str, ' ');
1598 if (space)
1599 *space='\0';
1600 sprintf(attrib, "argvA%d", count);
1601 if ((test->todo & 0x8)==0)
1603 okChildPath(attrib, str);
1605 else todo_wine
1607 okChildPath(attrib, str);
1609 count++;
1610 if (!space)
1611 break;
1612 str=space+1;
1614 if ((test->todo & 0x2)==0)
1616 okChildInt("argcA", count);
1618 else todo_wine
1620 okChildInt("argcA", count);
1623 test++;
1626 if (dllver.dwMajorVersion != 0)
1628 /* The more recent versions of shell32.dll accept quoted filenames
1629 * while older ones (e.g. 4.00) don't. Still we want to test this
1630 * because IE 6 depends on the new behavior.
1631 * One day we may need to check the exact version of the dll but for
1632 * now making sure DllGetVersion() is present is sufficient.
1634 sprintf(filename, "\"%s\\test file.shlexec\"", tmpdir);
1635 rc=shell_execute(NULL, filename, NULL, NULL);
1636 ok(rc > 32, "%s failed: rc=%ld err=%u\n", shell_call, rc,
1637 GetLastError());
1638 okChildInt("argcA", 5);
1639 okChildString("argvA3", "Open");
1640 sprintf(filename, "%s\\test file.shlexec", tmpdir);
1641 okChildPath("argvA4", filename);
1645 typedef struct
1647 const char* urlprefix;
1648 const char* basename;
1649 int flags;
1650 int todo;
1651 } fileurl_tests_t;
1653 #define URL_SUCCESS 0x1
1654 #define USE_COLON 0x2
1655 #define USE_BSLASH 0x4
1657 static fileurl_tests_t fileurl_tests[]=
1659 /* How many slashes does it take... */
1660 {"file:", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1661 {"file:/", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1662 {"file://", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1663 {"file:///", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1664 {"File:///", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1665 {"file:////", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1666 {"file://///", "%s\\test file.shlexec", 0, 0x1},
1668 /* Test with Windows-style paths */
1669 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_COLON, 0x1},
1670 {"file:///", "%s\\test file.shlexec", URL_SUCCESS | USE_BSLASH, 0x1},
1672 /* Check handling of hostnames */
1673 {"file://localhost/", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1674 {"file://localhost:80/", "%s\\test file.shlexec", 0, 0x1},
1675 {"file://LocalHost/", "%s\\test file.shlexec", URL_SUCCESS, 0x1},
1676 {"file://127.0.0.1/", "%s\\test file.shlexec", 0, 0x1},
1677 {"file://::1/", "%s\\test file.shlexec", 0, 0x1},
1678 {"file://notahost/", "%s\\test file.shlexec", 0, 0x1},
1680 /* Environment variables are not expanded in URLs */
1681 {"%urlprefix%", "%s\\test file.shlexec", 0, 0x1},
1682 {"file:///", "%s\\%%urlenvvar%% file.shlexec", 0, 0x1},
1684 {NULL, NULL, 0, 0}
1687 static void test_fileurl(void)
1689 char filename[MAX_PATH], fileurl[MAX_PATH], longtmpdir[MAX_PATH];
1690 char command[MAX_PATH];
1691 const fileurl_tests_t* test;
1692 char *s;
1693 INT_PTR rc;
1695 rc = (INT_PTR)ShellExecute(NULL, NULL, "file:///nosuchfile.shlexec", NULL, NULL, SW_SHOWNORMAL);
1696 if (rc > 32)
1698 win_skip("shell32 is too old (likely < 4.72). Skipping the file URL tests\n");
1699 return;
1702 get_long_path_name(tmpdir, longtmpdir, sizeof(longtmpdir)/sizeof(*longtmpdir));
1703 SetEnvironmentVariable("urlprefix", "file:///");
1704 SetEnvironmentVariable("urlenvvar", "test");
1706 test=fileurl_tests;
1707 while (test->basename)
1709 /* Build the file URL */
1710 sprintf(filename, test->basename, longtmpdir);
1711 strcpy(fileurl, test->urlprefix);
1712 strcat(fileurl, filename);
1713 s = fileurl + strlen(test->urlprefix);
1714 while (*s)
1716 if (!(test->flags & USE_COLON) && *s == ':')
1717 *s = '|';
1718 else if (!(test->flags & USE_BSLASH) && *s == '\\')
1719 *s = '/';
1720 s++;
1723 /* Test it first with FindExecutable() */
1724 rc = (INT_PTR)FindExecutableA(fileurl, NULL, command);
1725 ok(rc == SE_ERR_FNF, "FindExecutable(%s) failed: bad rc=%lu\n", fileurl, rc);
1727 /* Then ShellExecute() */
1728 rc = shell_execute(NULL, fileurl, NULL, NULL);
1729 if (bad_shellexecute)
1731 win_skip("shell32 is too old (likely 4.72). Skipping the file URL tests\n");
1732 break;
1734 if (test->flags & URL_SUCCESS)
1736 if ((test->todo & 0x1) == 0)
1737 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1738 else todo_wine
1739 ok(rc > 32, "%s failed: bad rc=%lu\n", shell_call, rc);
1741 else
1743 if ((test->todo & 0x1) == 0)
1744 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1745 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1746 "%s failed: bad rc=%lu\n", shell_call, rc);
1747 else todo_wine
1748 ok(rc == SE_ERR_FNF || rc == SE_ERR_PNF ||
1749 broken(rc == SE_ERR_ACCESSDENIED) /* win2000 */,
1750 "%s failed: bad rc=%lu\n", shell_call, rc);
1752 if (rc == 33)
1754 if ((test->todo & 0x2) == 0)
1755 okChildInt("argcA", 5);
1756 else todo_wine
1757 okChildInt("argcA", 5);
1759 if ((test->todo & 0x4) == 0)
1760 okChildString("argvA3", "Open");
1761 else todo_wine
1762 okChildString("argvA3", "Open");
1764 if ((test->todo & 0x8) == 0)
1765 okChildPath("argvA4", filename);
1766 else todo_wine
1767 okChildPath("argvA4", filename);
1769 test++;
1772 SetEnvironmentVariable("urlprefix", NULL);
1773 SetEnvironmentVariable("urlenvvar", NULL);
1776 static void test_find_executable(void)
1778 char notepad_path[MAX_PATH];
1779 char filename[MAX_PATH];
1780 char command[MAX_PATH];
1781 const filename_tests_t* test;
1782 INT_PTR rc;
1784 if (!create_test_association(".sfe"))
1786 skip("Unable to create association for '.sfe'\n");
1787 return;
1789 create_test_verb(".sfe", "Open", 1, "%1");
1791 /* Don't test FindExecutable(..., NULL), it always crashes */
1793 strcpy(command, "your word");
1794 if (0) /* Can crash on Vista! */
1796 rc=(INT_PTR)FindExecutableA(NULL, NULL, command);
1797 ok(rc == SE_ERR_FNF || rc > 32 /* nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1798 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1801 GetSystemDirectoryA( notepad_path, MAX_PATH );
1802 strcat( notepad_path, "\\notepad.exe" );
1804 /* Search for something that should be in the system-wide search path (no default directory) */
1805 strcpy(command, "your word");
1806 rc=(INT_PTR)FindExecutableA("notepad.exe", NULL, command);
1807 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1808 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1810 /* Search for something that should be in the system-wide search path (with default directory) */
1811 strcpy(command, "your word");
1812 rc=(INT_PTR)FindExecutableA("notepad.exe", tmpdir, command);
1813 ok(rc > 32, "FindExecutable(%s) returned %ld\n", "notepad.exe", rc);
1814 ok(strcasecmp(command, notepad_path) == 0, "FindExecutable(%s) returned command=[%s]\n", "notepad.exe", command);
1816 strcpy(command, "your word");
1817 rc=(INT_PTR)FindExecutableA(tmpdir, NULL, command);
1818 ok(rc == SE_ERR_NOASSOC /* >= win2000 */ || rc > 32 /* win98, nt4 */, "FindExecutable(NULL) returned %ld\n", rc);
1819 ok(strcmp(command, "your word") != 0, "FindExecutable(NULL) returned command=[%s]\n", command);
1821 sprintf(filename, "%s\\test file.sfe", tmpdir);
1822 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1823 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1824 /* Depending on the platform, command could be '%1' or 'test file.sfe' */
1826 rc=(INT_PTR)FindExecutableA("test file.sfe", tmpdir, command);
1827 ok(rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1829 rc=(INT_PTR)FindExecutableA("test file.sfe", NULL, command);
1830 ok(rc == SE_ERR_FNF, "FindExecutable(%s) returned %ld\n", filename, rc);
1832 delete_test_association(".sfe");
1834 if (!create_test_association(".shl"))
1836 skip("Unable to create association for '.shl'\n");
1837 return;
1839 create_test_verb(".shl", "Open", 0, "Open");
1841 sprintf(filename, "%s\\test file.shl", tmpdir);
1842 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1843 ok(rc == SE_ERR_FNF /* NT4 */ || rc > 32, "FindExecutable(%s) returned %ld\n", filename, rc);
1845 sprintf(filename, "%s\\test file.shlfoo", tmpdir);
1846 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1848 delete_test_association(".shl");
1850 if (rc > 32)
1852 /* On Windows XP and 2003 FindExecutable() is completely broken.
1853 * Probably what it does is convert the filename to 8.3 format,
1854 * which as a side effect converts the '.shlfoo' extension to '.shl',
1855 * and then tries to find an association for '.shl'. This means it
1856 * will normally fail on most extensions with more than 3 characters,
1857 * like '.mpeg', etc.
1858 * Also it means we cannot do any other test.
1860 win_skip("FindExecutable() is broken -> not running 4+ character extension tests\n");
1861 return;
1864 test=filename_tests;
1865 while (test->basename)
1867 sprintf(filename, test->basename, tmpdir);
1868 if (strchr(filename, '/'))
1870 char* c;
1871 c=filename;
1872 while (*c)
1874 if (*c=='\\')
1875 *c='/';
1876 c++;
1879 /* Win98 does not '\0'-terminate command! */
1880 memset(command, '\0', sizeof(command));
1881 rc=(INT_PTR)FindExecutableA(filename, NULL, command);
1882 if (rc > 32)
1883 rc=33;
1884 if ((test->todo & 0x10)==0)
1886 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1888 else todo_wine
1890 ok(rc==test->rc, "FindExecutable(%s) failed: rc=%ld\n", filename, rc);
1892 if (rc > 32)
1894 int equal;
1895 equal=strcmp(command, argv0) == 0 ||
1896 /* NT4 returns an extra 0x8 character! */
1897 (strlen(command) == strlen(argv0)+1 && strncmp(command, argv0, strlen(argv0)) == 0);
1898 if ((test->todo & 0x20)==0)
1900 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1901 filename, command, argv0);
1903 else todo_wine
1905 ok(equal, "FindExecutable(%s) returned command='%s' instead of '%s'\n",
1906 filename, command, argv0);
1909 test++;
1914 static filename_tests_t lnk_tests[]=
1916 /* Pass bad / nonexistent filenames as a parameter */
1917 {NULL, "%s\\nonexistent.shlexec", 0xa, 33},
1918 {NULL, "%s\\nonexistent.noassoc", 0xa, 33},
1920 /* Pass regular paths as a parameter */
1921 {NULL, "%s\\test file.shlexec", 0xa, 33},
1922 {NULL, "%s/%%nasty%% $file.shlexec", 0xa, 33},
1924 /* Pass filenames with no association as a parameter */
1925 {NULL, "%s\\test file.noassoc", 0xa, 33},
1927 {NULL, NULL, 0}
1930 static void test_lnks(void)
1932 char filename[MAX_PATH];
1933 char params[MAX_PATH];
1934 const filename_tests_t* test;
1935 INT_PTR rc;
1937 /* Should open through our association */
1938 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
1939 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1940 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1941 okChildInt("argcA", 5);
1942 okChildString("argvA3", "Open");
1943 sprintf(params, "%s\\test file.shlexec", tmpdir);
1944 get_long_path_name(params, filename, sizeof(filename));
1945 okChildPath("argvA4", filename);
1947 todo_wait rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_DOENVSUBST, NULL, "%TMPDIR%\\test_shortcut_shlexec.lnk", NULL, NULL, NULL);
1948 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1949 okChildInt("argcA", 5);
1950 todo_wine okChildString("argvA3", "Open");
1951 sprintf(params, "%s\\test file.shlexec", tmpdir);
1952 get_long_path_name(params, filename, sizeof(filename));
1953 todo_wine okChildPath("argvA4", filename);
1955 /* Should just run our executable */
1956 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1957 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1958 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1959 okChildInt("argcA", 4);
1960 okChildString("argvA3", "Lnk");
1962 /* Lnk's ContextMenuHandler has priority over an explicit class */
1963 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, "shlexec.shlexec");
1964 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc, GetLastError());
1965 okChildInt("argcA", 4);
1966 okChildString("argvA3", "Lnk");
1968 if (dllver.dwMajorVersion>=6)
1970 char* c;
1971 /* Recent versions of shell32.dll accept '/'s in shortcut paths.
1972 * Older versions don't or are quite buggy in this regard.
1974 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1975 c=filename;
1976 while (*c)
1978 if (*c=='\\')
1979 *c='/';
1980 c++;
1982 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, NULL, NULL, NULL);
1983 ok(rc > 32, "%s failed: rc=%lu err=%u\n", shell_call, rc,
1984 GetLastError());
1985 okChildInt("argcA", 4);
1986 okChildString("argvA3", "Lnk");
1989 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
1990 test=lnk_tests;
1991 while (test->basename)
1993 params[0]='\"';
1994 sprintf(params+1, test->basename, tmpdir);
1995 strcat(params,"\"");
1996 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, filename, params,
1997 NULL, NULL);
1998 if (rc > 32)
1999 rc=33;
2000 if ((test->todo & 0x1)==0)
2002 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
2003 rc, GetLastError());
2005 else todo_wine
2007 ok(rc==test->rc, "%s failed: rc=%lu err=%u\n", shell_call,
2008 rc, GetLastError());
2010 if (rc==0)
2012 if ((test->todo & 0x2)==0)
2014 okChildInt("argcA", 5);
2016 else
2018 okChildInt("argcA", 5);
2020 if ((test->todo & 0x4)==0)
2022 okChildString("argvA3", "Lnk");
2024 else todo_wine
2026 okChildString("argvA3", "Lnk");
2028 sprintf(params, test->basename, tmpdir);
2029 if ((test->todo & 0x8)==0)
2031 okChildPath("argvA4", params);
2033 else
2035 okChildPath("argvA4", params);
2038 test++;
2043 static void test_exes(void)
2045 char filename[MAX_PATH];
2046 char params[1024];
2047 INT_PTR rc;
2049 sprintf(params, "shlexec \"%s\" Exec", child_file);
2051 /* We need NOZONECHECKS on Win2003 to block a dialog */
2052 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS, NULL, argv0, params,
2053 NULL, NULL);
2054 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2055 okChildInt("argcA", 4);
2056 okChildString("argvA3", "Exec");
2058 if (! skip_noassoc_tests)
2060 sprintf(filename, "%s\\test file.noassoc", tmpdir);
2061 if (CopyFile(argv0, filename, FALSE))
2063 rc=shell_execute(NULL, filename, params, NULL);
2064 todo_wine {
2065 ok(rc==SE_ERR_NOASSOC, "%s succeeded: rc=%lu\n", shell_call, rc);
2069 else
2071 win_skip("Skipping shellexecute of file with unassociated extension\n");
2075 typedef struct
2077 const char* command;
2078 const char* ddeexec;
2079 const char* application;
2080 const char* topic;
2081 const char* ifexec;
2082 int expectedArgs;
2083 const char* expectedDdeExec;
2084 int todo;
2085 } dde_tests_t;
2087 static dde_tests_t dde_tests[] =
2089 /* Test passing and not passing command-line
2090 * argument, no DDE */
2091 {"", NULL, NULL, NULL, NULL, FALSE, "", 0x0},
2092 {"\"%1\"", NULL, NULL, NULL, NULL, TRUE, "", 0x0},
2094 /* Test passing and not passing command-line
2095 * argument, with DDE */
2096 {"", "[open(\"%1\")]", "shlexec", "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
2097 {"\"%1\"", "[open(\"%1\")]", "shlexec", "dde", NULL, TRUE, "[open(\"%s\")]", 0x0},
2099 /* Test unquoted %1 in command and ddeexec
2100 * (test filename has space) */
2101 {"%1", "[open(%1)]", "shlexec", "dde", NULL, 2, "[open(%s)]", 0x0},
2103 /* Test ifexec precedence over ddeexec */
2104 {"", "[open(\"%1\")]", "shlexec", "dde", "[ifexec(\"%1\")]", FALSE, "[ifexec(\"%s\")]", 0x0},
2106 /* Test default DDE topic */
2107 {"", "[open(\"%1\")]", "shlexec", NULL, NULL, FALSE, "[open(\"%s\")]", 0x0},
2109 /* Test default DDE application */
2110 {"", "[open(\"%1\")]", NULL, "dde", NULL, FALSE, "[open(\"%s\")]", 0x0},
2112 {NULL, NULL, NULL, NULL, NULL, 0, 0x0}
2115 static DWORD WINAPI hooked_WaitForInputIdle(HANDLE process, DWORD timeout)
2117 return WaitForSingleObject(dde_ready_event, timeout);
2121 * WaitForInputIdle() will normally return immediately for console apps. That's
2122 * a problem for us because ShellExecute will assume that an app is ready to
2123 * receive DDE messages after it has called WaitForInputIdle() on that app.
2124 * To work around that we install our own version of WaitForInputIdle() that
2125 * will wait for the child to explicitly tell us that it is ready. We do that
2126 * by changing the entry for WaitForInputIdle() in the shell32 import address
2127 * table.
2129 static void hook_WaitForInputIdle(DWORD (WINAPI *new_func)(HANDLE, DWORD))
2131 char *base;
2132 PIMAGE_NT_HEADERS nt_headers;
2133 DWORD import_directory_rva;
2134 PIMAGE_IMPORT_DESCRIPTOR import_descriptor;
2136 base = (char *) GetModuleHandleA("shell32.dll");
2137 nt_headers = (PIMAGE_NT_HEADERS)(base + ((PIMAGE_DOS_HEADER) base)->e_lfanew);
2138 import_directory_rva = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
2140 /* Search for the correct imported module by walking the import descriptors */
2141 import_descriptor = (PIMAGE_IMPORT_DESCRIPTOR)(base + import_directory_rva);
2142 while (U(*import_descriptor).OriginalFirstThunk != 0)
2144 char *import_module_name;
2146 import_module_name = base + import_descriptor->Name;
2147 if (lstrcmpiA(import_module_name, "user32.dll") == 0 ||
2148 lstrcmpiA(import_module_name, "user32") == 0)
2150 PIMAGE_THUNK_DATA int_entry;
2151 PIMAGE_THUNK_DATA iat_entry;
2153 /* The import name table and import address table are two parallel
2154 * arrays. We need the import name table to find the imported
2155 * routine and the import address table to patch the address, so
2156 * walk them side by side */
2157 int_entry = (PIMAGE_THUNK_DATA)(base + U(*import_descriptor).OriginalFirstThunk);
2158 iat_entry = (PIMAGE_THUNK_DATA)(base + import_descriptor->FirstThunk);
2159 while (int_entry->u1.Ordinal != 0)
2161 if (! IMAGE_SNAP_BY_ORDINAL(int_entry->u1.Ordinal))
2163 PIMAGE_IMPORT_BY_NAME import_by_name;
2164 import_by_name = (PIMAGE_IMPORT_BY_NAME)(base + int_entry->u1.AddressOfData);
2165 if (lstrcmpA((char *) import_by_name->Name, "WaitForInputIdle") == 0)
2167 /* Found the correct routine in the correct imported module. Patch it. */
2168 DWORD old_prot;
2169 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), PAGE_READWRITE, &old_prot);
2170 iat_entry->u1.Function = (ULONG_PTR) new_func;
2171 VirtualProtect(&iat_entry->u1.Function, sizeof(ULONG_PTR), old_prot, &old_prot);
2172 break;
2175 int_entry++;
2176 iat_entry++;
2178 break;
2181 import_descriptor++;
2185 static void test_dde(void)
2187 char filename[MAX_PATH], defApplication[MAX_PATH];
2188 const dde_tests_t* test;
2189 char params[1024];
2190 INT_PTR rc;
2191 HANDLE map;
2192 char *shared_block;
2194 hook_WaitForInputIdle(hooked_WaitForInputIdle);
2196 sprintf(filename, "%s\\test file.sde", tmpdir);
2198 /* Default service is application name minus path and extension */
2199 strcpy(defApplication, strrchr(argv0, '\\')+1);
2200 *strchr(defApplication, '.') = 0;
2202 map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
2203 4096, "winetest_shlexec_dde_map");
2204 shared_block = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 4096);
2206 test = dde_tests;
2207 while (test->command)
2209 if (!create_test_association(".sde"))
2211 skip("Unable to create association for '.sde'\n");
2212 return;
2214 create_test_verb_dde(".sde", "Open", 0, test->command, test->ddeexec,
2215 test->application, test->topic, test->ifexec);
2217 if (test->application != NULL || test->topic != NULL)
2219 strcpy(shared_block, test->application ? test->application : defApplication);
2220 strcpy(shared_block + strlen(shared_block) + 1, test->topic ? test->topic : SZDDESYS_TOPIC);
2222 else
2224 shared_block[0] = '\0';
2225 shared_block[1] = '\0';
2227 ddeExec[0] = 0;
2229 dde_ready_event = CreateEventA(NULL, FALSE, FALSE, "winetest_shlexec_dde_ready");
2230 rc = shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, filename, NULL, NULL, NULL);
2231 CloseHandle(dde_ready_event);
2232 if ((test->todo & 0x1)==0)
2234 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call,
2235 rc, GetLastError());
2237 else todo_wine
2239 ok(32 < rc, "%s failed: rc=%lu err=%u\n", shell_call,
2240 rc, GetLastError());
2242 if (32 < rc)
2244 if ((test->todo & 0x2)==0)
2246 okChildInt("argcA", test->expectedArgs + 3);
2248 else todo_wine
2250 okChildInt("argcA", test->expectedArgs + 3);
2252 if (test->expectedArgs == 1)
2254 if ((test->todo & 0x4) == 0)
2256 okChildPath("argvA3", filename);
2258 else todo_wine
2260 okChildPath("argvA3", filename);
2263 if ((test->todo & 0x8) == 0)
2265 sprintf(params, test->expectedDdeExec, filename);
2266 okChildPath("ddeExec", params);
2268 else todo_wine
2270 sprintf(params, test->expectedDdeExec, filename);
2271 okChildPath("ddeExec", params);
2275 delete_test_association(".sde");
2276 test++;
2279 UnmapViewOfFile(shared_block);
2280 CloseHandle(map);
2281 hook_WaitForInputIdle((void *) WaitForInputIdle);
2284 #define DDE_DEFAULT_APP_VARIANTS 2
2285 typedef struct
2287 const char* command;
2288 const char* expectedDdeApplication[DDE_DEFAULT_APP_VARIANTS];
2289 int todo;
2290 int rc[DDE_DEFAULT_APP_VARIANTS];
2291 } dde_default_app_tests_t;
2293 static dde_default_app_tests_t dde_default_app_tests[] =
2295 /* Windows XP and 98 handle default DDE app names in different ways.
2296 * The application name we see in the first test determines the pattern
2297 * of application names and return codes we will look for. */
2299 /* Test unquoted existing filename with a space */
2300 {"%s\\test file.exe", {"test file", "test"}, 0x0, {33, 33}},
2301 {"%s\\test file.exe param", {"test file", "test"}, 0x0, {33, 33}},
2303 /* Test quoted existing filename with a space */
2304 {"\"%s\\test file.exe\"", {"test file", "test file"}, 0x0, {33, 33}},
2305 {"\"%s\\test file.exe\" param", {"test file", "test file"}, 0x0, {33, 33}},
2307 /* Test unquoted filename with a space that doesn't exist, but
2308 * test2.exe does */
2309 {"%s\\test2 file.exe", {"test2", "test2"}, 0x0, {33, 33}},
2310 {"%s\\test2 file.exe param", {"test2", "test2"}, 0x0, {33, 33}},
2312 /* Test quoted filename with a space that does not exist */
2313 {"\"%s\\test2 file.exe\"", {"", "test2 file"}, 0x0, {5, 33}},
2314 {"\"%s\\test2 file.exe\" param", {"", "test2 file"}, 0x0, {5, 33}},
2316 /* Test filename supplied without the extension */
2317 {"%s\\test2", {"test2", "test2"}, 0x0, {33, 33}},
2318 {"%s\\test2 param", {"test2", "test2"}, 0x0, {33, 33}},
2320 /* Test an unquoted nonexistent filename */
2321 {"%s\\notexist.exe", {"", "notexist"}, 0x0, {5, 33}},
2322 {"%s\\notexist.exe param", {"", "notexist"}, 0x0, {5, 33}},
2324 /* Test an application that will be found on the path */
2325 {"cmd", {"cmd", "cmd"}, 0x0, {33, 33}},
2326 {"cmd param", {"cmd", "cmd"}, 0x0, {33, 33}},
2328 /* Test an application that will not be found on the path */
2329 {"xyzwxyzwxyz", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2330 {"xyzwxyzwxyz param", {"", "xyzwxyzwxyz"}, 0x0, {5, 33}},
2332 {NULL, {NULL}, 0, {0}}
2335 typedef struct
2337 char *filename;
2338 DWORD threadIdParent;
2339 } dde_thread_info_t;
2341 static DWORD CALLBACK ddeThread(LPVOID arg)
2343 dde_thread_info_t *info = arg;
2344 assert(info && info->filename);
2345 PostThreadMessage(info->threadIdParent,
2346 WM_QUIT,
2347 shell_execute_ex(SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI, NULL, info->filename, NULL, NULL, NULL),
2348 0L);
2349 ExitThread(0);
2352 static void test_dde_default_app(void)
2354 char filename[MAX_PATH];
2355 HSZ hszApplication;
2356 dde_thread_info_t info = { filename, GetCurrentThreadId() };
2357 const dde_default_app_tests_t* test;
2358 char params[1024];
2359 DWORD threadId;
2360 MSG msg;
2361 INT_PTR rc;
2362 int which = 0;
2364 post_quit_on_execute = FALSE;
2365 ddeInst = 0;
2366 rc = DdeInitializeA(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES |
2367 CBF_FAIL_POKES | CBF_FAIL_REQUESTS, 0L);
2368 assert(rc == DMLERR_NO_ERROR);
2370 sprintf(filename, "%s\\test file.sde", tmpdir);
2372 /* It is strictly not necessary to register an application name here, but wine's
2373 * DdeNameService implementation complains if 0L is passed instead of
2374 * hszApplication with DNS_FILTEROFF */
2375 hszApplication = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2376 hszTopic = DdeCreateStringHandleA(ddeInst, "shlexec", CP_WINANSI);
2377 assert(hszApplication && hszTopic);
2378 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_REGISTER | DNS_FILTEROFF));
2380 test = dde_default_app_tests;
2381 while (test->command)
2383 if (!create_test_association(".sde"))
2385 skip("Unable to create association for '.sde'\n");
2386 return;
2388 sprintf(params, test->command, tmpdir);
2389 create_test_verb_dde(".sde", "Open", 1, params, "[test]", NULL,
2390 "shlexec", NULL);
2391 ddeApplication[0] = 0;
2393 /* No application will be run as we will respond to the first DDE event,
2394 * so don't wait for it */
2395 SetEvent(hEvent);
2397 assert(CreateThread(NULL, 0, ddeThread, &info, 0, &threadId));
2398 while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg);
2399 rc = msg.wParam > 32 ? 33 : msg.wParam;
2401 /* First test, find which set of test data we expect to see */
2402 if (test == dde_default_app_tests)
2404 int i;
2405 for (i=0; i<DDE_DEFAULT_APP_VARIANTS; i++)
2407 if (!strcmp(ddeApplication, test->expectedDdeApplication[i]))
2409 which = i;
2410 break;
2413 if (i == DDE_DEFAULT_APP_VARIANTS)
2414 skip("Default DDE application test does not match any available results, using first expected data set.\n");
2417 if ((test->todo & 0x1)==0)
2419 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2420 rc, GetLastError());
2422 else todo_wine
2424 ok(rc==test->rc[which], "%s failed: rc=%lu err=%u\n", shell_call,
2425 rc, GetLastError());
2427 if (rc == 33)
2429 if ((test->todo & 0x2)==0)
2431 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2432 "Expected application '%s', got '%s'\n",
2433 test->expectedDdeApplication[which], ddeApplication);
2435 else todo_wine
2437 ok(!strcmp(ddeApplication, test->expectedDdeApplication[which]),
2438 "Expected application '%s', got '%s'\n",
2439 test->expectedDdeApplication[which], ddeApplication);
2443 delete_test_association(".sde");
2444 test++;
2447 assert(DdeNameService(ddeInst, hszApplication, 0L, DNS_UNREGISTER));
2448 assert(DdeFreeStringHandle(ddeInst, hszTopic));
2449 assert(DdeFreeStringHandle(ddeInst, hszApplication));
2450 assert(DdeUninitialize(ddeInst));
2453 static void init_test(void)
2455 HMODULE hdll;
2456 HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO*);
2457 char filename[MAX_PATH];
2458 WCHAR lnkfile[MAX_PATH];
2459 char params[1024];
2460 const char* const * testfile;
2461 lnk_desc_t desc;
2462 DWORD rc;
2463 HRESULT r;
2465 hdll=GetModuleHandleA("shell32.dll");
2466 pDllGetVersion=(void*)GetProcAddress(hdll, "DllGetVersion");
2467 if (pDllGetVersion)
2469 dllver.cbSize=sizeof(dllver);
2470 pDllGetVersion(&dllver);
2471 trace("major=%d minor=%d build=%d platform=%d\n",
2472 dllver.dwMajorVersion, dllver.dwMinorVersion,
2473 dllver.dwBuildNumber, dllver.dwPlatformID);
2475 else
2477 memset(&dllver, 0, sizeof(dllver));
2480 r = CoInitialize(NULL);
2481 ok(r == S_OK, "CoInitialize failed (0x%08x)\n", r);
2482 if (FAILED(r))
2483 exit(1);
2485 rc=GetModuleFileName(NULL, argv0, sizeof(argv0));
2486 assert(rc!=0 && rc<sizeof(argv0));
2487 if (GetFileAttributes(argv0)==INVALID_FILE_ATTRIBUTES)
2489 strcat(argv0, ".so");
2490 ok(GetFileAttributes(argv0)!=INVALID_FILE_ATTRIBUTES,
2491 "unable to find argv0!\n");
2494 GetTempPathA(sizeof(filename), filename);
2495 GetTempFileNameA(filename, "wt", 0, tmpdir);
2496 DeleteFileA( tmpdir );
2497 rc = CreateDirectoryA( tmpdir, NULL );
2498 ok( rc, "failed to create %s err %u\n", tmpdir, GetLastError() );
2499 /* Set %TMPDIR% for the tests */
2500 SetEnvironmentVariableA("TMPDIR", tmpdir);
2502 rc = GetTempFileNameA(tmpdir, "wt", 0, child_file);
2503 assert(rc != 0);
2504 init_event(child_file);
2506 /* Set up the test files */
2507 testfile=testfiles;
2508 while (*testfile)
2510 HANDLE hfile;
2512 sprintf(filename, *testfile, tmpdir);
2513 hfile=CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2514 FILE_ATTRIBUTE_NORMAL, NULL);
2515 if (hfile==INVALID_HANDLE_VALUE)
2517 trace("unable to create '%s': err=%u\n", filename, GetLastError());
2518 assert(0);
2520 CloseHandle(hfile);
2521 testfile++;
2524 /* Setup the test shortcuts */
2525 sprintf(filename, "%s\\test_shortcut_shlexec.lnk", tmpdir);
2526 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2527 desc.description=NULL;
2528 desc.workdir=NULL;
2529 sprintf(filename, "%s\\test file.shlexec", tmpdir);
2530 desc.path=filename;
2531 desc.pidl=NULL;
2532 desc.arguments="ignored";
2533 desc.showcmd=0;
2534 desc.icon=NULL;
2535 desc.icon_id=0;
2536 desc.hotkey=0;
2537 create_lnk(lnkfile, &desc, 0);
2539 sprintf(filename, "%s\\test_shortcut_exe.lnk", tmpdir);
2540 MultiByteToWideChar(CP_ACP, 0, filename, -1, lnkfile, sizeof(lnkfile)/sizeof(*lnkfile));
2541 desc.description=NULL;
2542 desc.workdir=NULL;
2543 desc.path=argv0;
2544 desc.pidl=NULL;
2545 sprintf(params, "shlexec \"%s\" Lnk", child_file);
2546 desc.arguments=params;
2547 desc.showcmd=0;
2548 desc.icon=NULL;
2549 desc.icon_id=0;
2550 desc.hotkey=0;
2551 create_lnk(lnkfile, &desc, 0);
2553 /* Create a basic association suitable for most tests */
2554 if (!create_test_association(".shlexec"))
2556 skip("Unable to create association for '.shlexec'\n");
2557 return;
2559 create_test_verb(".shlexec", "Open", 0, "Open \"%1\"");
2560 create_test_verb(".shlexec", "NoQuotes", 0, "NoQuotes %1");
2561 create_test_verb(".shlexec", "LowerL", 0, "LowerL %l");
2562 create_test_verb(".shlexec", "QuotedLowerL", 0, "QuotedLowerL \"%l\"");
2563 create_test_verb(".shlexec", "UpperL", 0, "UpperL %L");
2564 create_test_verb(".shlexec", "QuotedUpperL", 0, "QuotedUpperL \"%L\"");
2567 static void cleanup_test(void)
2569 char filename[MAX_PATH];
2570 const char* const * testfile;
2572 /* Delete the test files */
2573 testfile=testfiles;
2574 while (*testfile)
2576 sprintf(filename, *testfile, tmpdir);
2577 /* Make sure we can delete the files ('test file.noassoc' is read-only now) */
2578 SetFileAttributes(filename, FILE_ATTRIBUTE_NORMAL);
2579 DeleteFile(filename);
2580 testfile++;
2582 DeleteFile(child_file);
2583 RemoveDirectoryA(tmpdir);
2585 /* Delete the test association */
2586 delete_test_association(".shlexec");
2588 CloseHandle(hEvent);
2590 CoUninitialize();
2593 static void test_directory(void)
2595 char path[MAX_PATH], newdir[MAX_PATH];
2596 char params[1024];
2597 INT_PTR rc;
2599 /* copy this executable to a new folder and cd to it */
2600 sprintf(newdir, "%s\\newfolder", tmpdir);
2601 rc = CreateDirectoryA( newdir, NULL );
2602 ok( rc, "failed to create %s err %u\n", newdir, GetLastError() );
2603 sprintf(path, "%s\\%s", newdir, path_find_file_name(argv0));
2604 CopyFileA(argv0, path, FALSE);
2605 SetCurrentDirectory(tmpdir);
2607 sprintf(params, "shlexec \"%s\" Exec", child_file);
2609 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2610 NULL, path_find_file_name(argv0), params, NULL, NULL);
2611 todo_wine ok(rc == SE_ERR_FNF, "%s returned %lu\n", shell_call, rc);
2613 rc=shell_execute_ex(SEE_MASK_NOZONECHECKS|SEE_MASK_FLAG_NO_UI,
2614 NULL, path_find_file_name(argv0), params, newdir, NULL);
2615 ok(rc > 32, "%s returned %lu\n", shell_call, rc);
2616 okChildInt("argcA", 4);
2617 okChildString("argvA3", "Exec");
2618 todo_wine okChildPath("longPath", path);
2620 DeleteFile(path);
2621 RemoveDirectoryA(newdir);
2624 START_TEST(shlexec)
2627 myARGC = winetest_get_mainargs(&myARGV);
2628 if (myARGC >= 3)
2630 doChild(myARGC, myARGV);
2631 exit(0);
2634 init_test();
2636 test_commandline2argv();
2637 test_argify();
2638 test_lpFile_parsed();
2639 test_filename();
2640 test_fileurl();
2641 test_find_executable();
2642 test_lnks();
2643 test_exes();
2644 test_dde();
2645 test_dde_default_app();
2646 test_directory();
2648 cleanup_test();