2 * Copyright (C) 2003, 2004 Stefan Leichter
3 * Copyright (C) 2005, 2006 Detlef Riekenberg
4 * Copyright (C) 2006 Dmitry Timoshkov
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/test.h"
31 #define MAGIC_DEAD 0x00dead00
32 #define DEFAULT_PRINTER_SIZE 1000
34 static char env_x86[] = "Windows NT x86";
35 static char env_win9x_case[] = "windowS 4.0";
36 static char winetest_monitor[] = "winetest";
38 static HANDLE hwinspool;
39 static FARPROC pGetDefaultPrinterA;
40 static FARPROC pSetDefaultPrinterA;
42 struct monitor_entry {
47 /* report common behavior only once */
48 static DWORD report_deactivated_spooler = 1;
49 #define RETURN_ON_DEACTIVATED_SPOOLER(res) \
50 if((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE)) \
52 if(report_deactivated_spooler > 0) { \
53 report_deactivated_spooler--; \
54 trace("The Service 'Spooler' is required for many test\n"); \
60 static LPSTR find_default_printer(VOID)
62 static LPSTR default_printer = NULL;
63 static char buffer[DEFAULT_PRINTER_SIZE];
68 if ((default_printer == NULL) && (pGetDefaultPrinterA))
71 needed = sizeof(buffer);
72 res = pGetDefaultPrinterA(buffer, &needed);
73 if(res) default_printer = buffer;
74 trace("default_printer: '%s'\n", default_printer);
76 if (default_printer == NULL)
80 /* NT 3.x and above */
81 if (RegOpenKeyEx(HKEY_CURRENT_USER,
82 "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
83 0, KEY_QUERY_VALUE, &hwindows) == NO_ERROR) {
85 needed = sizeof(buffer);
86 if (RegQueryValueEx(hwindows, "device", NULL,
87 &type, (LPBYTE)buffer, &needed) == NO_ERROR) {
89 ptr = strchr(buffer, ',');
92 default_printer = buffer;
95 RegCloseKey(hwindows);
97 trace("default_printer: '%s'\n", default_printer);
99 if (default_printer == NULL)
102 needed = sizeof(buffer);
103 res = GetProfileStringA("windows", "device", "*", buffer, needed);
105 ptr = strchr(buffer, ',');
108 default_printer = buffer;
111 trace("default_printer: '%s'\n", default_printer);
113 return default_printer;
117 static struct monitor_entry * find_installed_monitor(void)
119 MONITOR_INFO_2A mi2a;
120 static struct monitor_entry * entry = NULL;
125 static struct monitor_entry monitor_table[] = {
126 {env_win9x_case, "localspl.dll"},
127 {env_x86, "localspl.dll"},
128 {env_win9x_case, "localmon.dll"},
129 {env_x86, "localmon.dll"},
130 {env_win9x_case, "tcpmon.dll"},
131 {env_x86, "tcpmon.dll"},
132 {env_win9x_case, "usbmon.dll"},
133 {env_x86, "usbmon.dll"},
134 {env_win9x_case, "mspp32.dll"},
135 {env_x86, "win32spl.dll"},
136 {env_x86, "redmonnt.dll"},
137 {env_x86, "redmon35.dll"},
138 {env_win9x_case, "redmon95.dll"},
139 {env_x86, "pdfcmnnt.dll"},
140 {env_win9x_case, "pdfcmn95.dll"},
143 if (entry) return entry;
145 num_tests = (sizeof(monitor_table)/sizeof(struct monitor_entry));
147 SetLastError(MAGIC_DEAD);
148 res = DeleteMonitorA(NULL, NULL, NULL);
149 if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
150 trace("DeleteMonitorA() not implemented yet\n");
154 SetLastError(MAGIC_DEAD);
155 res = AddMonitorA(NULL, 0, NULL);
156 if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
157 trace("AddMonitorA() not implemented yet\n");
162 DeleteMonitorA(NULL, env_x86, winetest_monitor);
163 DeleteMonitorA(NULL, env_win9x_case, winetest_monitor);
165 /* find a usable monitor from the table */
166 mi2a.pName = winetest_monitor;
167 while ((entry == NULL) && (i < num_tests)) {
168 entry = &monitor_table[i];
170 mi2a.pEnvironment = entry->env;
171 mi2a.pDLLName = entry->dllname;
173 if (AddMonitorA(NULL, 2, (LPBYTE) &mi2a)) {
175 trace("using '%s', '%s'\n", entry->env, entry->dllname);
176 DeleteMonitorA(NULL, entry->env, winetest_monitor);
186 /* ########################### */
189 static void test_AddMonitor(void)
191 MONITOR_INFO_2A mi2a;
192 struct monitor_entry * entry = NULL;
195 entry = find_installed_monitor();
197 SetLastError(MAGIC_DEAD);
198 res = AddMonitorA(NULL, 1, NULL);
199 ok(!res && (GetLastError() == ERROR_INVALID_LEVEL),
200 "returned %ld with %ld (expected '0' with ERROR_INVALID_LEVEL)\n",
201 res, GetLastError());
203 SetLastError(MAGIC_DEAD);
204 res = AddMonitorA(NULL, 3, NULL);
205 ok(!res && (GetLastError() == ERROR_INVALID_LEVEL),
206 "returned %ld with %ld (expected '0' with ERROR_INVALID_LEVEL)\n",
207 res, GetLastError());
209 SetLastError(MAGIC_DEAD);
210 res = AddMonitorA(NULL, 2, NULL);
211 /* NT: unchanged, 9x: ERROR_PRIVILEGE_NOT_HELD */
213 ((GetLastError() == MAGIC_DEAD) ||
214 (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)),
215 "returned %ld with %ld (expected '0' with: MAGIC_DEAD or " \
216 "ERROR_PRIVILEGE_NOT_HELD)\n", res, GetLastError());
218 ZeroMemory(&mi2a, sizeof(MONITOR_INFO_2A));
219 SetLastError(MAGIC_DEAD);
220 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
221 RETURN_ON_DEACTIVATED_SPOOLER(res)
223 if (!res && (GetLastError() == ERROR_ACCESS_DENIED)) {
224 trace("skip tests (ACCESS_DENIED)\n");
228 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_INVALID_ENVIRONMENT */
229 ok(!res && ((GetLastError() == ERROR_INVALID_PARAMETER) ||
230 (GetLastError() == ERROR_INVALID_ENVIRONMENT)),
231 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
232 "ERROR_INVALID_ENVIRONMENT)\n", res, GetLastError());
235 trace("No usable Monitor found: Skip tests\n");
240 /* The Test is deactivated, because when mi2a.pName is NULL, the subkey
241 HKLM\System\CurrentControlSet\Control\Print\Monitors\C:\WINDOWS\SYSTEM
242 or HKLM\System\CurrentControlSet\Control\Print\Monitors\ì
243 is created on win9x and we do not want to hit this bug here. */
245 mi2a.pEnvironment = entry->env;
246 SetLastError(MAGIC_DEAD);
247 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
248 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_PRIVILEGE_NOT_HELD */
251 mi2a.pEnvironment = entry->env;
253 SetLastError(MAGIC_DEAD);
254 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
255 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_PRIVILEGE_NOT_HELD */
257 ((GetLastError() == ERROR_INVALID_PARAMETER) ||
258 (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)),
259 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
260 "ERROR_PRIVILEGE_NOT_HELD)\n",
261 res, GetLastError());
263 mi2a.pName = winetest_monitor;
264 SetLastError(MAGIC_DEAD);
265 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
266 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_PRIVILEGE_NOT_HELD */
268 ((GetLastError() == ERROR_INVALID_PARAMETER) ||
269 (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)),
270 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
271 "ERROR_PRIVILEGE_NOT_HELD)\n",
272 res, GetLastError());
275 SetLastError(MAGIC_DEAD);
276 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
277 ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
278 "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
279 res, GetLastError());
282 mi2a.pDLLName = "does_not_exists.dll";
283 SetLastError(MAGIC_DEAD);
284 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
285 /* NT: ERROR_MOD_NOT_FOUND, 9x: ERROR_INVALID_PARAMETER */
287 ((GetLastError() == ERROR_MOD_NOT_FOUND) ||
288 (GetLastError() == ERROR_INVALID_PARAMETER)),
289 "returned %ld with %ld (expected '0' with: ERROR_MOD_NOT_FOUND or " \
290 "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
292 mi2a.pDLLName = "version.dll";
293 SetLastError(MAGIC_DEAD);
294 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
295 /* NT: ERROR_PROC_NOT_FOUND, 9x: ERROR_INVALID_PARAMETER */
298 ((GetLastError() == ERROR_PROC_NOT_FOUND) ||
299 (GetLastError() == ERROR_INVALID_PARAMETER)),
300 "returned %ld with %ld (expected '0' with: ERROR_PROC_NOT_FOUND or " \
301 "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
302 if (res) DeleteMonitorA(NULL, entry->env, winetest_monitor);
305 /* Test AddMonitor with real options */
306 mi2a.pDLLName = entry->dllname;
307 SetLastError(MAGIC_DEAD);
308 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
309 ok(res, "returned %ld with %ld (expected '!= 0')\n", res, GetLastError());
311 /* add a monitor twice */
312 SetLastError(MAGIC_DEAD);
313 res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
314 /* NT: ERROR_PRINT_MONITOR_ALREADY_INSTALLED (3006), 9x: ERROR_ALREADY_EXISTS (183) */
316 ((GetLastError() == ERROR_PRINT_MONITOR_ALREADY_INSTALLED) ||
317 (GetLastError() == ERROR_ALREADY_EXISTS)),
318 "returned %ld with %ld (expected '0' with: " \
319 "ERROR_PRINT_MONITOR_ALREADY_INSTALLED or ERROR_ALREADY_EXISTS)\n",
320 res, GetLastError());
322 DeleteMonitorA(NULL, entry->env, winetest_monitor);
323 SetLastError(MAGIC_DEAD);
324 res = AddMonitorA("", 2, (LPBYTE) &mi2a);
325 ok(res, "returned %ld with %ld (expected '!= 0')\n", res, GetLastError());
328 DeleteMonitorA(NULL, entry->env, winetest_monitor);
332 /* ########################### */
334 static void test_DeleteMonitor(void)
336 MONITOR_INFO_2A mi2a;
337 struct monitor_entry * entry = NULL;
340 entry = find_installed_monitor();
343 trace("No usable Monitor found: Skip tests\n");
347 mi2a.pName = winetest_monitor;
348 mi2a.pEnvironment = entry->env;
349 mi2a.pDLLName = entry->dllname;
351 /* Testing DeleteMonitor with real options */
352 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
354 SetLastError(MAGIC_DEAD);
355 res = DeleteMonitorA(NULL, entry->env, winetest_monitor);
356 ok(res, "returned %ld with %ld (expected '!= 0')\n", res, GetLastError());
358 /* Delete the Monitor twice */
359 SetLastError(MAGIC_DEAD);
360 res = DeleteMonitorA(NULL, entry->env, winetest_monitor);
361 /* NT: ERROR_UNKNOWN_PRINT_MONITOR (3000), 9x: ERROR_INVALID_PARAMETER (87) */
363 ((GetLastError() == ERROR_UNKNOWN_PRINT_MONITOR) ||
364 (GetLastError() == ERROR_INVALID_PARAMETER)),
365 "returned %ld with %ld (expected '0' with: ERROR_UNKNOWN_PRINT_MONITOR" \
366 " or ERROR_INVALID_PARAMETER)\n", res, GetLastError());
368 /* the environment */
369 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
370 SetLastError(MAGIC_DEAD);
371 res = DeleteMonitorA(NULL, NULL, winetest_monitor);
372 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
374 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
375 SetLastError(MAGIC_DEAD);
376 res = DeleteMonitorA(NULL, "", winetest_monitor);
377 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
379 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
380 SetLastError(MAGIC_DEAD);
381 res = DeleteMonitorA(NULL, "bad_env", winetest_monitor);
382 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
384 /* the monitor-name */
385 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
386 SetLastError(MAGIC_DEAD);
387 res = DeleteMonitorA(NULL, entry->env, NULL);
388 /* NT: ERROR_INVALID_PARAMETER (87), 9x: ERROR_INVALID_NAME (123)*/
390 ((GetLastError() == ERROR_INVALID_PARAMETER) ||
391 (GetLastError() == ERROR_INVALID_NAME)),
392 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
393 "ERROR_INVALID_NAME)\n", res, GetLastError());
395 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
396 SetLastError(MAGIC_DEAD);
397 res = DeleteMonitorA(NULL, entry->env, "");
398 /* NT: ERROR_INVALID_PARAMETER (87), 9x: ERROR_INVALID_NAME (123)*/
400 ((GetLastError() == ERROR_INVALID_PARAMETER) ||
401 (GetLastError() == ERROR_INVALID_NAME)),
402 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or " \
403 "ERROR_INVALID_NAME)\n", res, GetLastError());
405 AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
406 SetLastError(MAGIC_DEAD);
407 res = DeleteMonitorA("", entry->env, winetest_monitor);
408 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
411 DeleteMonitorA(NULL, entry->env, winetest_monitor);
415 /* ########################### */
417 static void test_EnumMonitors(void)
426 /* valid levels are 1 and 2 */
427 for(level = 0; level < 4; level++) {
429 pcReturned = MAGIC_DEAD;
430 SetLastError(MAGIC_DEAD);
431 res = EnumMonitorsA(NULL, level, NULL, 0, &cbBuf, &pcReturned);
433 RETURN_ON_DEACTIVATED_SPOOLER(res)
435 /* not implemented yet in wine */
436 if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) continue;
439 /* use only a short test, when we test with an invalid level */
440 if(!level || (level > 2)) {
441 ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
442 (res && (pcReturned == 0)),
443 "(%ld) returned %ld with %ld and 0x%08lx (expected '0' with " \
444 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
445 level, res, GetLastError(), pcReturned);
449 /* Level 2 is not supported on win9x */
450 if (!res && (GetLastError() == ERROR_INVALID_LEVEL)) {
451 trace("Level %ld not supported, skipping tests\n", level);
455 ok((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
456 "(%ld) returned %ld with %ld (expected '0' with " \
457 "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
460 trace("no valid buffer size returned, skipping tests\n");
464 buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf *2);
465 if (buffer == NULL) continue;
467 SetLastError(MAGIC_DEAD);
468 pcbNeeded = MAGIC_DEAD;
469 res = EnumMonitorsA(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
470 ok(res, "(%ld) returned %ld with %ld (expected '!=0')\n",
471 level, res, GetLastError());
472 ok(pcbNeeded == cbBuf, "(%ld) returned %ld (expected %ld)\n",
473 level, pcbNeeded, cbBuf);
474 /* We can validate the returned Data with the Registry here */
477 SetLastError(MAGIC_DEAD);
478 pcReturned = MAGIC_DEAD;
479 pcbNeeded = MAGIC_DEAD;
480 res = EnumMonitorsA(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
481 ok(res, "(%ld) returned %ld with %ld (expected '!=0')\n", level,
482 res, GetLastError());
483 ok(pcbNeeded == cbBuf, "(%ld) returned %ld (expected %ld)\n", level,
486 SetLastError(MAGIC_DEAD);
487 pcbNeeded = MAGIC_DEAD;
488 res = EnumMonitorsA(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
489 ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
490 "(%ld) returned %ld with %ld (expected '0' with " \
491 "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
493 ok(pcbNeeded == cbBuf, "(%ld) returned %ld (expected %ld)\n", level,
497 Do not add the next test:
498 w2k+: RPC_X_NULL_REF_POINTER
499 NT3.5: ERROR_INVALID_USER_BUFFER
500 win9x: crash in winspool.drv
502 res = EnumMonitorsA(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
505 SetLastError(MAGIC_DEAD);
506 pcbNeeded = MAGIC_DEAD;
507 pcReturned = MAGIC_DEAD;
508 res = EnumMonitorsA(NULL, level, buffer, cbBuf, NULL, &pcReturned);
509 ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
510 "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
511 "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
513 pcbNeeded = MAGIC_DEAD;
514 pcReturned = MAGIC_DEAD;
515 SetLastError(MAGIC_DEAD);
516 res = EnumMonitorsA(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
517 ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
518 "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
519 "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
521 HeapFree(GetProcessHeap(), 0, buffer);
522 } /* for(level ... */
526 static void test_GetDefaultPrinter(void)
529 DWORD exact = DEFAULT_PRINTER_SIZE;
531 char buffer[DEFAULT_PRINTER_SIZE];
533 if (!pGetDefaultPrinterA) return;
534 /* only supported on NT like OSes starting with win2k */
536 SetLastError(ERROR_SUCCESS);
537 retval = pGetDefaultPrinterA(buffer, &exact);
538 if (!retval || !exact || !strlen(buffer) ||
539 (ERROR_SUCCESS != GetLastError())) {
540 if ((ERROR_FILE_NOT_FOUND == GetLastError()) ||
541 (ERROR_INVALID_NAME == GetLastError()))
542 trace("this test requires a default printer to be set\n");
544 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
545 "function returned %s\n"
546 "last error 0x%08lx\n"
547 "returned buffer size 0x%08lx\n"
548 "returned buffer content %s\n",
549 retval ? "true" : "false", GetLastError(), exact, buffer);
553 SetLastError(ERROR_SUCCESS);
554 retval = pGetDefaultPrinterA(NULL, NULL);
555 ok( !retval, "function result wrong! False expected\n");
556 ok( ERROR_INVALID_PARAMETER == GetLastError(),
557 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
560 SetLastError(ERROR_SUCCESS);
561 retval = pGetDefaultPrinterA(buffer, NULL);
562 ok( !retval, "function result wrong! False expected\n");
563 ok( ERROR_INVALID_PARAMETER == GetLastError(),
564 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
567 SetLastError(ERROR_SUCCESS);
569 retval = pGetDefaultPrinterA(NULL, &size);
570 ok( !retval, "function result wrong! False expected\n");
571 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
572 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
574 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
577 SetLastError(ERROR_SUCCESS);
578 size = DEFAULT_PRINTER_SIZE;
579 retval = pGetDefaultPrinterA(NULL, &size);
580 ok( !retval, "function result wrong! False expected\n");
581 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
582 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
584 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
588 retval = pGetDefaultPrinterA(buffer, &size);
589 ok( !retval, "function result wrong! False expected\n");
590 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
591 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
593 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
597 retval = pGetDefaultPrinterA(buffer, &size);
598 ok( retval, "function result wrong! True expected\n");
599 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
603 static void test_GetPrinterDriverDirectory(void)
605 LPBYTE buffer = NULL;
606 DWORD cbBuf = 0, pcbNeeded = 0;
609 SetLastError(MAGIC_DEAD);
610 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, 0, &cbBuf);
611 trace("first call returned 0x%04x, with %ld: buffer size 0x%08lx\n",
612 res, GetLastError(), cbBuf);
614 RETURN_ON_DEACTIVATED_SPOOLER(res)
615 ok((res == 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
616 "returned %d with lasterror=%ld (expected '0' with " \
617 "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError());
620 trace("no valid buffer size returned, skipping tests\n");
624 buffer = HeapAlloc( GetProcessHeap(), 0, cbBuf*2);
625 if (buffer == NULL) return ;
627 res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded);
628 ok( res, "expected result != 0, got %d\n", res);
629 ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
632 res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
633 ok( res, "expected result != 0, got %d\n", res);
634 ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
637 SetLastError(MAGIC_DEAD);
638 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded);
639 ok( !res , "expected result == 0, got %d\n", res);
640 ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
643 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
644 "last error set to %ld instead of ERROR_INSUFFICIENT_BUFFER\n",
648 Do not add the next test:
649 XPsp2: crash in this app, when the spooler is not running
650 NT3.5: ERROR_INVALID_USER_BUFFER
651 win9x: ERROR_INVALID_PARAMETER
653 pcbNeeded = MAGIC_DEAD;
654 SetLastError(MAGIC_DEAD);
655 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
658 SetLastError(MAGIC_DEAD);
659 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf, NULL);
660 ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) || res,
661 "expected either result == 0 and "
662 "last error == RPC_X_NULL_REF_POINTER or result != 0 "
663 "got result %d and last error == %ld\n", res, GetLastError());
665 SetLastError(MAGIC_DEAD);
666 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, NULL);
667 ok(res || (GetLastError() == RPC_X_NULL_REF_POINTER),
668 "returned %d with %ld (expected '!=0' or '0' with " \
669 "RPC_X_NULL_REF_POINTER)\n", res, GetLastError());
672 /* with a valid buffer, but level is too large */
674 SetLastError(MAGIC_DEAD);
675 res = GetPrinterDriverDirectoryA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded);
677 /* Level not checked in win9x and wine:*/
678 if((res != FALSE) && buffer[0])
680 trace("Level '2' not checked '%s'\n", buffer);
684 ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
685 "returned %d with lasterror=%ld (expected '0' with " \
686 "ERROR_INVALID_LEVEL)\n", res, GetLastError());
689 /* printing environments are case insensitive */
690 /* "Windows 4.0" is valid for win9x and NT */
692 SetLastError(MAGIC_DEAD);
693 res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1,
694 buffer, cbBuf*2, &pcbNeeded);
696 if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
698 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
699 if (buffer == NULL) return ;
701 SetLastError(MAGIC_DEAD);
702 res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1,
703 buffer, cbBuf*2, &pcbNeeded);
706 ok(res && buffer[0], "returned %d with " \
707 "lasterror=%ld and len=%d (expected '1' with 'len > 0')\n",
708 res, GetLastError(), lstrlenA((char *)buffer));
711 SetLastError(MAGIC_DEAD);
712 res = GetPrinterDriverDirectoryA(NULL, env_x86, 1,
713 buffer, cbBuf*2, &pcbNeeded);
715 if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
717 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
718 if (buffer == NULL) return ;
721 SetLastError(MAGIC_DEAD);
722 res = GetPrinterDriverDirectoryA(NULL, env_x86, 1,
723 buffer, cbBuf*2, &pcbNeeded);
726 /* "Windows NT x86" is invalid for win9x */
727 ok( (res && buffer[0]) ||
728 (!res && (GetLastError() == ERROR_INVALID_ENVIRONMENT)),
729 "returned %d with lasterror=%ld and len=%d (expected '!= 0' with " \
730 "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
731 res, GetLastError(), lstrlenA((char *)buffer));
733 /* A Setup-Programm (PDFCreator_0.8.0) use empty strings */
734 SetLastError(MAGIC_DEAD);
735 res = GetPrinterDriverDirectoryA("", "", 1, buffer, cbBuf*2, &pcbNeeded);
736 ok(res, "returned %d with %ld (expected '!=0')\n", res, GetLastError() );
738 SetLastError(MAGIC_DEAD);
739 res = GetPrinterDriverDirectoryA(NULL, "", 1, buffer, cbBuf*2, &pcbNeeded);
740 ok(res, "returned %d with %ld (expected '!=0')\n", res, GetLastError() );
742 SetLastError(MAGIC_DEAD);
743 res = GetPrinterDriverDirectoryA("", NULL, 1, buffer, cbBuf*2, &pcbNeeded);
744 ok(res, "returned %d with %ld (expected '!=0')\n", res, GetLastError() );
746 HeapFree( GetProcessHeap(), 0, buffer);
749 static void test_OpenPrinter(void)
751 PRINTER_DEFAULTSA defaults;
753 LPSTR default_printer;
756 CHAR buffer[DEFAULT_PRINTER_SIZE];
759 SetLastError(MAGIC_DEAD);
760 res = OpenPrinter(NULL, NULL, NULL);
761 /* The deactivated Spooler is catched here on NT3.51 */
762 RETURN_ON_DEACTIVATED_SPOOLER(res)
763 ok(!res && (GetLastError() == ERROR_INVALID_PARAMETER),
764 "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
765 res, GetLastError());
768 /* Get Handle for the local Printserver (NT only)*/
769 hprinter = (HANDLE) MAGIC_DEAD;
770 SetLastError(MAGIC_DEAD);
771 res = OpenPrinter(NULL, &hprinter, NULL);
772 /* The deactivated Spooler is catched here on XPsp2 */
773 RETURN_ON_DEACTIVATED_SPOOLER(res)
774 ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
775 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
776 res, GetLastError());
778 ClosePrinter(hprinter);
780 defaults.pDatatype=NULL;
781 defaults.pDevMode=NULL;
783 defaults.DesiredAccess=0;
784 hprinter = (HANDLE) MAGIC_DEAD;
785 SetLastError(MAGIC_DEAD);
786 res = OpenPrinter(NULL, &hprinter, &defaults);
787 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
788 if (res) ClosePrinter(hprinter);
790 defaults.DesiredAccess=-1;
791 hprinter = (HANDLE) MAGIC_DEAD;
792 SetLastError(MAGIC_DEAD);
793 res = OpenPrinter(NULL, &hprinter, &defaults);
794 ok(!res && GetLastError() == ERROR_ACCESS_DENIED,
795 "returned %ld with %ld (expected '0' with ERROR_ACCESS_DENIED)\n",
796 res, GetLastError());
797 if (res) ClosePrinter(hprinter);
801 size = sizeof(buffer) - 3 ;
807 if (GetComputerNameA(ptr, &size)) {
809 hprinter = (HANDLE) MAGIC_DEAD;
810 SetLastError(MAGIC_DEAD);
811 res = OpenPrinter(buffer, &hprinter, NULL);
813 ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
814 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
815 res, GetLastError());
817 if(res) ClosePrinter(hprinter);
820 /* Invalid Printername */
821 hprinter = (HANDLE) MAGIC_DEAD;
822 SetLastError(MAGIC_DEAD);
823 res = OpenPrinter("illegal,name", &hprinter, NULL);
824 ok(!res && ((GetLastError() == ERROR_INVALID_PRINTER_NAME) ||
825 (GetLastError() == ERROR_INVALID_PARAMETER) ),
826 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or" \
827 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
828 if(res) ClosePrinter(hprinter);
831 /* Get Handle for the default Printer */
832 if ((default_printer = find_default_printer()))
834 hprinter = (HANDLE) MAGIC_DEAD;
835 SetLastError(MAGIC_DEAD);
836 res = OpenPrinter(default_printer, &hprinter, NULL);
837 if((!res) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE))
839 trace("The Service 'Spooler' is required for '%s'\n", default_printer);
842 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
843 if(res) ClosePrinter(hprinter);
845 defaults.pDatatype=NULL;
846 defaults.pDevMode=NULL;
847 defaults.DesiredAccess=0;
849 hprinter = (HANDLE) MAGIC_DEAD;
850 SetLastError(MAGIC_DEAD);
851 res = OpenPrinter(default_printer, &hprinter, &defaults);
852 ok(res || GetLastError() == ERROR_ACCESS_DENIED,
853 "returned %ld with %ld (expected '!=0' or '0' with " \
854 "ERROR_ACCESS_DENIED)\n", res, GetLastError());
855 if(res) ClosePrinter(hprinter);
857 defaults.pDatatype="";
859 hprinter = (HANDLE) MAGIC_DEAD;
860 SetLastError(MAGIC_DEAD);
861 res = OpenPrinter(default_printer, &hprinter, &defaults);
862 /* stop here, when a remote Printserver has no RPC-Service running */
863 RETURN_ON_DEACTIVATED_SPOOLER(res)
864 ok(res || ((GetLastError() == ERROR_INVALID_DATATYPE) ||
865 (GetLastError() == ERROR_ACCESS_DENIED)),
866 "returned %ld with %ld (expected '!=0' or '0' with: " \
867 "ERROR_INVALID_DATATYPE or ERROR_ACCESS_DENIED)\n",
868 res, GetLastError());
869 if(res) ClosePrinter(hprinter);
872 defaults.pDatatype=NULL;
873 defaults.DesiredAccess=PRINTER_ACCESS_USE;
875 hprinter = (HANDLE) MAGIC_DEAD;
876 SetLastError(MAGIC_DEAD);
877 res = OpenPrinter(default_printer, &hprinter, &defaults);
878 ok(res || GetLastError() == ERROR_ACCESS_DENIED,
879 "returned %ld with %ld (expected '!=0' or '0' with " \
880 "ERROR_ACCESS_DENIED)\n", res, GetLastError());
881 if(res) ClosePrinter(hprinter);
884 defaults.DesiredAccess=PRINTER_ALL_ACCESS;
885 hprinter = (HANDLE) MAGIC_DEAD;
886 SetLastError(MAGIC_DEAD);
887 res = OpenPrinter(default_printer, &hprinter, &defaults);
888 ok(res || GetLastError() == ERROR_ACCESS_DENIED,
889 "returned %ld with %ld (expected '!=0' or '0' with " \
890 "ERROR_ACCESS_DENIED)\n", res, GetLastError());
891 if(res) ClosePrinter(hprinter);
897 static void test_SetDefaultPrinter(void)
900 LPSTR default_printer;
901 DWORD size = DEFAULT_PRINTER_SIZE;
902 CHAR buffer[DEFAULT_PRINTER_SIZE];
903 CHAR org_value[DEFAULT_PRINTER_SIZE];
906 if (!pSetDefaultPrinterA) return;
907 /* only supported on win2k and above */
909 default_printer = find_default_printer();
911 /* backup the original value */
913 SetLastError(MAGIC_DEAD);
914 res = GetProfileStringA("windows", "device", NULL, org_value, size);
916 /* first part: with the default Printer */
917 SetLastError(MAGIC_DEAD);
918 res = pSetDefaultPrinterA("no_printer_with_this_name");
920 RETURN_ON_DEACTIVATED_SPOOLER(res)
921 /* spooler is running or we have no spooler here*/
923 /* Not implemented in wine */
924 if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
925 trace("SetDefaultPrinterA() not implemented yet.\n");
929 ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
930 "returned %ld with %ld (expected '0' with " \
931 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
933 WriteProfileStringA("windows", "device", org_value);
934 SetLastError(MAGIC_DEAD);
935 res = pSetDefaultPrinterA("");
936 ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
937 "returned %ld with %ld (expected '!=0' or '0' with " \
938 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
940 WriteProfileStringA("windows", "device", org_value);
941 SetLastError(MAGIC_DEAD);
942 res = pSetDefaultPrinterA(NULL);
943 ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
944 "returned %ld with %ld (expected '!=0' or '0' with " \
945 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
947 WriteProfileStringA("windows", "device", org_value);
948 SetLastError(MAGIC_DEAD);
949 res = pSetDefaultPrinterA(default_printer);
950 ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
951 "returned %ld with %ld (expected '!=0' or '0' with " \
952 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
955 /* second part: always without a default Printer */
956 WriteProfileStringA("windows", "device", NULL);
957 SetLastError(MAGIC_DEAD);
958 res = pSetDefaultPrinterA("no_printer_with_this_name");
960 ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
961 "returned %ld with %ld (expected '0' with " \
962 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
964 WriteProfileStringA("windows", "device", NULL);
965 SetLastError(MAGIC_DEAD);
966 res = pSetDefaultPrinterA("");
967 /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
968 ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
969 "returned %ld with %ld (expected '!=0' or '0' with " \
970 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
972 WriteProfileStringA("windows", "device", NULL);
973 SetLastError(MAGIC_DEAD);
974 res = pSetDefaultPrinterA(NULL);
975 /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
976 ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
977 "returned %ld with %ld (expected '!=0' or '0' with " \
978 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
980 WriteProfileStringA("windows", "device", NULL);
981 SetLastError(MAGIC_DEAD);
982 res = pSetDefaultPrinterA(default_printer);
983 ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
984 "returned %ld with %ld (expected '!=0' or '0' with " \
985 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
987 /* restore the original value */
988 res = pSetDefaultPrinterA(default_printer); /* the nice way */
989 WriteProfileStringA("windows", "device", org_value); /* the old way */
992 SetLastError(MAGIC_DEAD);
993 res = GetProfileStringA("windows", "device", NULL, buffer, size);
994 ok(!lstrcmpA(org_value, buffer), "'%s' (expected '%s')\n", buffer, org_value);
998 static void test_GetPrinterDriver(void)
1000 LPSTR default_printer;
1005 DWORD needed, filled;
1007 default_printer = find_default_printer();
1008 if (!default_printer)
1010 trace("There is no default printer installed, skiping the test\n");
1015 ret = OpenPrinter(default_printer, &hprn, NULL);
1018 trace("There is no printers installed, skiping the test\n");
1021 ok(hprn != 0, "wrong hprn %p\n", hprn);
1023 for (level = -1; level <= 7; level++)
1025 SetLastError(0xdeadbeef);
1027 ret = GetPrinterDriver(hprn, NULL, level, NULL, 0, &needed);
1028 ok(!ret, "level %d: GetPrinterDriver should fail\n", level);
1029 if (level >= 1 && level <= 6)
1031 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %ld\n", GetLastError());
1032 ok(needed > 0,"not expected needed buffer size %ld\n", needed);
1036 ok(GetLastError() == ERROR_INVALID_LEVEL, "wrong error %ld\n", GetLastError());
1037 ok(needed == (DWORD)-1,"not expected needed buffer size %ld\n", needed);
1041 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1043 SetLastError(0xdeadbeef);
1045 ret = GetPrinterDriver(hprn, NULL, level, buf, needed, &filled);
1046 ok(ret, "level %d: GetPrinterDriver error %ld\n", level, GetLastError());
1047 ok(needed == filled, "needed %ld != filled %ld\n", needed, filled);
1051 DRIVER_INFO_2 *di_2 = (DRIVER_INFO_2 *)buf;
1052 DWORD calculated = sizeof(*di_2);
1054 ok(di_2->cVersion >= 0 && di_2->cVersion <= 3, "di_2->cVersion = %ld\n", di_2->cVersion);
1055 ok(di_2->pName != NULL, "not expected NULL ptr\n");
1056 ok(di_2->pEnvironment != NULL, "not expected NULL ptr\n");
1057 ok(di_2->pDriverPath != NULL, "not expected NULL ptr\n");
1058 ok(di_2->pDataFile != NULL, "not expected NULL ptr\n");
1059 ok(di_2->pConfigFile != NULL, "not expected NULL ptr\n");
1061 trace("cVersion %ld\n", di_2->cVersion);
1062 trace("pName %s\n", di_2->pName);
1063 calculated += strlen(di_2->pName) + 1;
1064 trace("pEnvironment %s\n", di_2->pEnvironment);
1065 calculated += strlen(di_2->pEnvironment) + 1;
1066 trace("pDriverPath %s\n", di_2->pDriverPath);
1067 calculated += strlen(di_2->pDriverPath) + 1;
1068 trace("pDataFile %s\n", di_2->pDataFile);
1069 calculated += strlen(di_2->pDataFile) + 1;
1070 trace("pConfigFile %s\n", di_2->pConfigFile);
1071 calculated += strlen(di_2->pConfigFile) + 1;
1073 /* XP allocates memory for both ANSI and unicode names */
1074 ok(filled >= calculated,"calculated %ld != filled %ld\n", calculated, filled);
1077 HeapFree(GetProcessHeap(), 0, buf);
1080 SetLastError(0xdeadbeef);
1081 ret = ClosePrinter(hprn);
1082 ok(ret, "ClosePrinter error %ld\n", GetLastError());
1087 hwinspool = GetModuleHandleA("winspool.drv");
1088 pGetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "GetDefaultPrinterA");
1089 pSetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "SetDefaultPrinterA");
1091 find_default_printer();
1094 test_DeleteMonitor();
1095 test_EnumMonitors();
1096 test_GetDefaultPrinter();
1097 test_GetPrinterDriverDirectory();
1099 test_GetPrinterDriver();
1100 test_SetDefaultPrinter();