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_EnumMonitors(void)
343 /* valid levels are 1 and 2 */
344 for(level
= 0; level
< 4; level
++) {
346 pcReturned
= MAGIC_DEAD
;
347 SetLastError(MAGIC_DEAD
);
348 res
= EnumMonitorsA(NULL
, level
, NULL
, 0, &cbBuf
, &pcReturned
);
350 RETURN_ON_DEACTIVATED_SPOOLER(res
)
352 /* not implemented yet in wine */
353 if (!res
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) continue;
356 /* use only a short test, when we test with an invalid level */
357 if(!level
|| (level
> 2)) {
358 ok( (!res
&& (GetLastError() == ERROR_INVALID_LEVEL
)) ||
359 (res
&& (pcReturned
== 0)),
360 "(%ld) returned %ld with %ld and 0x%08lx (expected '0' with " \
361 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
362 level
, res
, GetLastError(), pcReturned
);
366 /* Level 2 is not supported on win9x */
367 if (!res
&& (GetLastError() == ERROR_INVALID_LEVEL
)) {
368 trace("Level %ld not supported, skipping tests\n", level
);
372 ok((!res
) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
373 "(%ld) returned %ld with %ld (expected '0' with " \
374 "ERROR_INSUFFICIENT_BUFFER)\n", level
, res
, GetLastError());
377 trace("no valid buffer size returned, skipping tests\n");
381 buffer
= HeapAlloc(GetProcessHeap(), 0, cbBuf
*2);
382 if (buffer
== NULL
) continue;
384 SetLastError(MAGIC_DEAD
);
385 pcbNeeded
= MAGIC_DEAD
;
386 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
, &pcbNeeded
, &pcReturned
);
387 ok(res
, "(%ld) returned %ld with %ld (expected '!=0')\n",
388 level
, res
, GetLastError());
389 ok(pcbNeeded
== cbBuf
, "(%ld) returned %ld (expected %ld)\n",
390 level
, pcbNeeded
, cbBuf
);
391 /* We can validate the returned Data with the Registry here */
394 SetLastError(MAGIC_DEAD
);
395 pcReturned
= MAGIC_DEAD
;
396 pcbNeeded
= MAGIC_DEAD
;
397 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
+1, &pcbNeeded
, &pcReturned
);
398 ok(res
, "(%ld) returned %ld with %ld (expected '!=0')\n", level
,
399 res
, GetLastError());
400 ok(pcbNeeded
== cbBuf
, "(%ld) returned %ld (expected %ld)\n", level
,
403 SetLastError(MAGIC_DEAD
);
404 pcbNeeded
= MAGIC_DEAD
;
405 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
-1, &pcbNeeded
, &pcReturned
);
406 ok( !res
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
407 "(%ld) returned %ld with %ld (expected '0' with " \
408 "ERROR_INSUFFICIENT_BUFFER)\n", level
, res
, GetLastError());
410 ok(pcbNeeded
== cbBuf
, "(%ld) returned %ld (expected %ld)\n", level
,
414 Do not add the next test:
415 w2k+: RPC_X_NULL_REF_POINTER
416 NT3.5: ERROR_INVALID_USER_BUFFER
417 win9x: crash in winspool.drv
419 res = EnumMonitorsA(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
422 SetLastError(MAGIC_DEAD
);
423 pcbNeeded
= MAGIC_DEAD
;
424 pcReturned
= MAGIC_DEAD
;
425 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
, NULL
, &pcReturned
);
426 ok( res
|| (!res
&& (GetLastError() == RPC_X_NULL_REF_POINTER
)) ,
427 "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
428 "RPC_X_NULL_REF_POINTER)\n", level
, res
, GetLastError());
430 pcbNeeded
= MAGIC_DEAD
;
431 pcReturned
= MAGIC_DEAD
;
432 SetLastError(MAGIC_DEAD
);
433 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
, &pcbNeeded
, NULL
);
434 ok( res
|| (!res
&& (GetLastError() == RPC_X_NULL_REF_POINTER
)) ,
435 "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
436 "RPC_X_NULL_REF_POINTER)\n", level
, res
, GetLastError());
438 HeapFree(GetProcessHeap(), 0, buffer
);
439 } /* for(level ... */
443 static void test_GetDefaultPrinter(void)
446 DWORD exact
= DEFAULT_PRINTER_SIZE
;
448 char buffer
[DEFAULT_PRINTER_SIZE
];
450 if (!pGetDefaultPrinterA
) return;
451 /* only supported on NT like OSes starting with win2k */
453 SetLastError(ERROR_SUCCESS
);
454 retval
= pGetDefaultPrinterA(buffer
, &exact
);
455 if (!retval
|| !exact
|| !strlen(buffer
) ||
456 (ERROR_SUCCESS
!= GetLastError())) {
457 if ((ERROR_FILE_NOT_FOUND
== GetLastError()) ||
458 (ERROR_INVALID_NAME
== GetLastError()))
459 trace("this test requires a default printer to be set\n");
461 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
462 "function returned %s\n"
463 "last error 0x%08lx\n"
464 "returned buffer size 0x%08lx\n"
465 "returned buffer content %s\n",
466 retval
? "true" : "false", GetLastError(), exact
, buffer
);
470 SetLastError(ERROR_SUCCESS
);
471 retval
= pGetDefaultPrinterA(NULL
, NULL
);
472 ok( !retval
, "function result wrong! False expected\n");
473 ok( ERROR_INVALID_PARAMETER
== GetLastError(),
474 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
477 SetLastError(ERROR_SUCCESS
);
478 retval
= pGetDefaultPrinterA(buffer
, NULL
);
479 ok( !retval
, "function result wrong! False expected\n");
480 ok( ERROR_INVALID_PARAMETER
== GetLastError(),
481 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
484 SetLastError(ERROR_SUCCESS
);
486 retval
= pGetDefaultPrinterA(NULL
, &size
);
487 ok( !retval
, "function result wrong! False expected\n");
488 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
489 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
491 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
494 SetLastError(ERROR_SUCCESS
);
495 size
= DEFAULT_PRINTER_SIZE
;
496 retval
= pGetDefaultPrinterA(NULL
, &size
);
497 ok( !retval
, "function result wrong! False expected\n");
498 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
499 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
501 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
505 retval
= pGetDefaultPrinterA(buffer
, &size
);
506 ok( !retval
, "function result wrong! False expected\n");
507 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
508 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
510 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
514 retval
= pGetDefaultPrinterA(buffer
, &size
);
515 ok( retval
, "function result wrong! True expected\n");
516 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
520 static void test_GetPrinterDriverDirectory(void)
522 LPBYTE buffer
= NULL
;
523 DWORD cbBuf
= 0, pcbNeeded
= 0;
526 SetLastError(MAGIC_DEAD
);
527 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, NULL
, 0, &cbBuf
);
528 trace("first call returned 0x%04x, with %ld: buffer size 0x%08lx\n",
529 res
, GetLastError(), cbBuf
);
531 RETURN_ON_DEACTIVATED_SPOOLER(res
)
532 ok((res
== 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
533 "returned %d with lasterror=%ld (expected '0' with " \
534 "ERROR_INSUFFICIENT_BUFFER)\n", res
, GetLastError());
537 trace("no valid buffer size returned, skipping tests\n");
541 buffer
= HeapAlloc( GetProcessHeap(), 0, cbBuf
*2);
542 if (buffer
== NULL
) return ;
544 res
= GetPrinterDriverDirectoryA(NULL
, NULL
, 1, buffer
, cbBuf
, &pcbNeeded
);
545 ok( res
, "expected result != 0, got %d\n", res
);
546 ok( cbBuf
== pcbNeeded
, "pcbNeeded set to %ld instead of %ld\n",
549 res
= GetPrinterDriverDirectoryA(NULL
, NULL
, 1, buffer
, cbBuf
*2, &pcbNeeded
);
550 ok( res
, "expected result != 0, got %d\n", res
);
551 ok( cbBuf
== pcbNeeded
, "pcbNeeded set to %ld instead of %ld\n",
554 SetLastError(MAGIC_DEAD
);
555 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, buffer
, cbBuf
-1, &pcbNeeded
);
556 ok( !res
, "expected result == 0, got %d\n", res
);
557 ok( cbBuf
== pcbNeeded
, "pcbNeeded set to %ld instead of %ld\n",
560 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
561 "last error set to %ld instead of ERROR_INSUFFICIENT_BUFFER\n",
565 Do not add the next test:
566 XPsp2: crash in this app, when the spooler is not running
567 NT3.5: ERROR_INVALID_USER_BUFFER
568 win9x: ERROR_INVALID_PARAMETER
570 pcbNeeded = MAGIC_DEAD;
571 SetLastError(MAGIC_DEAD);
572 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
575 SetLastError(MAGIC_DEAD
);
576 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, buffer
, cbBuf
, NULL
);
577 ok( (!res
&& RPC_X_NULL_REF_POINTER
== GetLastError()) || res
,
578 "expected either result == 0 and "
579 "last error == RPC_X_NULL_REF_POINTER or result != 0 "
580 "got result %d and last error == %ld\n", res
, GetLastError());
582 SetLastError(MAGIC_DEAD
);
583 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, NULL
, cbBuf
, NULL
);
584 ok(res
|| (GetLastError() == RPC_X_NULL_REF_POINTER
),
585 "returned %d with %ld (expected '!=0' or '0' with " \
586 "RPC_X_NULL_REF_POINTER)\n", res
, GetLastError());
589 /* with a valid buffer, but level is too large */
591 SetLastError(MAGIC_DEAD
);
592 res
= GetPrinterDriverDirectoryA(NULL
, NULL
, 2, buffer
, cbBuf
, &pcbNeeded
);
594 /* Level not checked in win9x and wine:*/
595 if((res
!= FALSE
) && buffer
[0])
597 trace("Level '2' not checked '%s'\n", buffer
);
601 ok( !res
&& (GetLastError() == ERROR_INVALID_LEVEL
),
602 "returned %d with lasterror=%ld (expected '0' with " \
603 "ERROR_INVALID_LEVEL)\n", res
, GetLastError());
606 /* printing environments are case insensitive */
607 /* "Windows 4.0" is valid for win9x and NT */
609 SetLastError(MAGIC_DEAD
);
610 res
= GetPrinterDriverDirectoryA(NULL
, env_win9x_case
, 1,
611 buffer
, cbBuf
*2, &pcbNeeded
);
613 if(!res
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)) {
615 buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, cbBuf
*2);
616 if (buffer
== NULL
) return ;
618 SetLastError(MAGIC_DEAD
);
619 res
= GetPrinterDriverDirectoryA(NULL
, env_win9x_case
, 1,
620 buffer
, cbBuf
*2, &pcbNeeded
);
623 ok(res
&& buffer
[0], "returned %d with " \
624 "lasterror=%ld and len=%d (expected '1' with 'len > 0')\n",
625 res
, GetLastError(), lstrlenA((char *)buffer
));
628 SetLastError(MAGIC_DEAD
);
629 res
= GetPrinterDriverDirectoryA(NULL
, env_x86
, 1,
630 buffer
, cbBuf
*2, &pcbNeeded
);
632 if(!res
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)) {
634 buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, cbBuf
*2);
635 if (buffer
== NULL
) return ;
638 SetLastError(MAGIC_DEAD
);
639 res
= GetPrinterDriverDirectoryA(NULL
, env_x86
, 1,
640 buffer
, cbBuf
*2, &pcbNeeded
);
643 /* "Windows NT x86" is invalid for win9x */
644 ok( (res
&& buffer
[0]) ||
645 (!res
&& (GetLastError() == ERROR_INVALID_ENVIRONMENT
)),
646 "returned %d with lasterror=%ld and len=%d (expected '!= 0' with " \
647 "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
648 res
, GetLastError(), lstrlenA((char *)buffer
));
650 /* A Setup-Programm (PDFCreator_0.8.0) use empty strings */
651 SetLastError(MAGIC_DEAD
);
652 res
= GetPrinterDriverDirectoryA("", "", 1, buffer
, cbBuf
*2, &pcbNeeded
);
653 ok(res
, "returned %d with %ld (expected '!=0')\n", res
, GetLastError() );
655 SetLastError(MAGIC_DEAD
);
656 res
= GetPrinterDriverDirectoryA(NULL
, "", 1, buffer
, cbBuf
*2, &pcbNeeded
);
657 ok(res
, "returned %d with %ld (expected '!=0')\n", res
, GetLastError() );
659 SetLastError(MAGIC_DEAD
);
660 res
= GetPrinterDriverDirectoryA("", NULL
, 1, buffer
, cbBuf
*2, &pcbNeeded
);
661 ok(res
, "returned %d with %ld (expected '!=0')\n", res
, GetLastError() );
663 HeapFree( GetProcessHeap(), 0, buffer
);
666 static void test_OpenPrinter(void)
668 PRINTER_DEFAULTSA defaults
;
670 LPSTR default_printer
;
673 CHAR buffer
[DEFAULT_PRINTER_SIZE
];
676 SetLastError(MAGIC_DEAD
);
677 res
= OpenPrinter(NULL
, NULL
, NULL
);
678 /* The deactivated Spooler is catched here on NT3.51 */
679 RETURN_ON_DEACTIVATED_SPOOLER(res
)
680 ok(!res
&& (GetLastError() == ERROR_INVALID_PARAMETER
),
681 "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
682 res
, GetLastError());
685 /* Get Handle for the local Printserver (NT only)*/
686 hprinter
= (HANDLE
) MAGIC_DEAD
;
687 SetLastError(MAGIC_DEAD
);
688 res
= OpenPrinter(NULL
, &hprinter
, NULL
);
689 /* The deactivated Spooler is catched here on XPsp2 */
690 RETURN_ON_DEACTIVATED_SPOOLER(res
)
691 ok(res
|| (!res
&& GetLastError() == ERROR_INVALID_PARAMETER
),
692 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
693 res
, GetLastError());
695 ClosePrinter(hprinter
);
697 defaults
.pDatatype
=NULL
;
698 defaults
.pDevMode
=NULL
;
700 defaults
.DesiredAccess
=0;
701 hprinter
= (HANDLE
) MAGIC_DEAD
;
702 SetLastError(MAGIC_DEAD
);
703 res
= OpenPrinter(NULL
, &hprinter
, &defaults
);
704 ok(res
, "returned %ld with %ld (expected '!=0')\n", res
, GetLastError());
705 if (res
) ClosePrinter(hprinter
);
707 defaults
.DesiredAccess
=-1;
708 hprinter
= (HANDLE
) MAGIC_DEAD
;
709 SetLastError(MAGIC_DEAD
);
710 res
= OpenPrinter(NULL
, &hprinter
, &defaults
);
711 ok(!res
&& GetLastError() == ERROR_ACCESS_DENIED
,
712 "returned %ld with %ld (expected '0' with ERROR_ACCESS_DENIED)\n",
713 res
, GetLastError());
714 if (res
) ClosePrinter(hprinter
);
718 size
= sizeof(buffer
) - 3 ;
724 if (GetComputerNameA(ptr
, &size
)) {
726 hprinter
= (HANDLE
) MAGIC_DEAD
;
727 SetLastError(MAGIC_DEAD
);
728 res
= OpenPrinter(buffer
, &hprinter
, NULL
);
730 ok(res
|| (!res
&& GetLastError() == ERROR_INVALID_PARAMETER
),
731 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
732 res
, GetLastError());
734 if(res
) ClosePrinter(hprinter
);
737 /* Invalid Printername */
738 hprinter
= (HANDLE
) MAGIC_DEAD
;
739 SetLastError(MAGIC_DEAD
);
740 res
= OpenPrinter("illegal,name", &hprinter
, NULL
);
741 ok(!res
&& ((GetLastError() == ERROR_INVALID_PRINTER_NAME
) ||
742 (GetLastError() == ERROR_INVALID_PARAMETER
) ),
743 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or" \
744 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
745 if(res
) ClosePrinter(hprinter
);
748 /* Get Handle for the default Printer */
749 if ((default_printer
= find_default_printer()))
751 hprinter
= (HANDLE
) MAGIC_DEAD
;
752 SetLastError(MAGIC_DEAD
);
753 res
= OpenPrinter(default_printer
, &hprinter
, NULL
);
754 if((!res
) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE
))
756 trace("The Service 'Spooler' is required for '%s'\n", default_printer
);
759 ok(res
, "returned %ld with %ld (expected '!=0')\n", res
, GetLastError());
760 if(res
) ClosePrinter(hprinter
);
762 defaults
.pDatatype
=NULL
;
763 defaults
.pDevMode
=NULL
;
764 defaults
.DesiredAccess
=0;
766 hprinter
= (HANDLE
) MAGIC_DEAD
;
767 SetLastError(MAGIC_DEAD
);
768 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
769 ok(res
|| GetLastError() == ERROR_ACCESS_DENIED
,
770 "returned %ld with %ld (expected '!=0' or '0' with " \
771 "ERROR_ACCESS_DENIED)\n", res
, GetLastError());
772 if(res
) ClosePrinter(hprinter
);
774 defaults
.pDatatype
="";
776 hprinter
= (HANDLE
) MAGIC_DEAD
;
777 SetLastError(MAGIC_DEAD
);
778 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
779 /* stop here, when a remote Printserver has no RPC-Service running */
780 RETURN_ON_DEACTIVATED_SPOOLER(res
)
781 ok(res
|| ((GetLastError() == ERROR_INVALID_DATATYPE
) ||
782 (GetLastError() == ERROR_ACCESS_DENIED
)),
783 "returned %ld with %ld (expected '!=0' or '0' with: " \
784 "ERROR_INVALID_DATATYPE or ERROR_ACCESS_DENIED)\n",
785 res
, GetLastError());
786 if(res
) ClosePrinter(hprinter
);
789 defaults
.pDatatype
=NULL
;
790 defaults
.DesiredAccess
=PRINTER_ACCESS_USE
;
792 hprinter
= (HANDLE
) MAGIC_DEAD
;
793 SetLastError(MAGIC_DEAD
);
794 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
795 ok(res
|| GetLastError() == ERROR_ACCESS_DENIED
,
796 "returned %ld with %ld (expected '!=0' or '0' with " \
797 "ERROR_ACCESS_DENIED)\n", res
, GetLastError());
798 if(res
) ClosePrinter(hprinter
);
801 defaults
.DesiredAccess
=PRINTER_ALL_ACCESS
;
802 hprinter
= (HANDLE
) MAGIC_DEAD
;
803 SetLastError(MAGIC_DEAD
);
804 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
805 ok(res
|| GetLastError() == ERROR_ACCESS_DENIED
,
806 "returned %ld with %ld (expected '!=0' or '0' with " \
807 "ERROR_ACCESS_DENIED)\n", res
, GetLastError());
808 if(res
) ClosePrinter(hprinter
);
814 static void test_SetDefaultPrinter(void)
817 LPSTR default_printer
;
818 DWORD size
= DEFAULT_PRINTER_SIZE
;
819 CHAR buffer
[DEFAULT_PRINTER_SIZE
];
820 CHAR org_value
[DEFAULT_PRINTER_SIZE
];
823 if (!pSetDefaultPrinterA
) return;
824 /* only supported on win2k and above */
826 default_printer
= find_default_printer();
828 /* backup the original value */
830 SetLastError(MAGIC_DEAD
);
831 res
= GetProfileStringA("windows", "device", NULL
, org_value
, size
);
833 /* first part: with the default Printer */
834 SetLastError(MAGIC_DEAD
);
835 res
= pSetDefaultPrinterA("no_printer_with_this_name");
837 RETURN_ON_DEACTIVATED_SPOOLER(res
)
838 /* spooler is running or we have no spooler here*/
840 /* Not implemented in wine */
841 if (!res
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
842 trace("SetDefaultPrinterA() not implemented yet.\n");
846 ok(!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
),
847 "returned %ld with %ld (expected '0' with " \
848 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
850 WriteProfileStringA("windows", "device", org_value
);
851 SetLastError(MAGIC_DEAD
);
852 res
= pSetDefaultPrinterA("");
853 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
854 "returned %ld with %ld (expected '!=0' or '0' with " \
855 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
857 WriteProfileStringA("windows", "device", org_value
);
858 SetLastError(MAGIC_DEAD
);
859 res
= pSetDefaultPrinterA(NULL
);
860 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
861 "returned %ld with %ld (expected '!=0' or '0' with " \
862 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
864 WriteProfileStringA("windows", "device", org_value
);
865 SetLastError(MAGIC_DEAD
);
866 res
= pSetDefaultPrinterA(default_printer
);
867 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
868 "returned %ld with %ld (expected '!=0' or '0' with " \
869 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
872 /* second part: always without a default Printer */
873 WriteProfileStringA("windows", "device", NULL
);
874 SetLastError(MAGIC_DEAD
);
875 res
= pSetDefaultPrinterA("no_printer_with_this_name");
877 ok(!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
),
878 "returned %ld with %ld (expected '0' with " \
879 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
881 WriteProfileStringA("windows", "device", NULL
);
882 SetLastError(MAGIC_DEAD
);
883 res
= pSetDefaultPrinterA("");
884 /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
885 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
886 "returned %ld with %ld (expected '!=0' or '0' with " \
887 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
889 WriteProfileStringA("windows", "device", NULL
);
890 SetLastError(MAGIC_DEAD
);
891 res
= pSetDefaultPrinterA(NULL
);
892 /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
893 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
894 "returned %ld with %ld (expected '!=0' or '0' with " \
895 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
897 WriteProfileStringA("windows", "device", NULL
);
898 SetLastError(MAGIC_DEAD
);
899 res
= pSetDefaultPrinterA(default_printer
);
900 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
901 "returned %ld with %ld (expected '!=0' or '0' with " \
902 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
904 /* restore the original value */
905 res
= pSetDefaultPrinterA(default_printer
); /* the nice way */
906 WriteProfileStringA("windows", "device", org_value
); /* the old way */
909 SetLastError(MAGIC_DEAD
);
910 res
= GetProfileStringA("windows", "device", NULL
, buffer
, size
);
911 ok(!lstrcmpA(org_value
, buffer
), "'%s' (expected '%s')\n", buffer
, org_value
);
915 static void test_GetPrinterDriver(void)
917 LPSTR default_printer
;
922 DWORD needed
, filled
;
924 default_printer
= find_default_printer();
925 if (!default_printer
)
927 trace("There is no default printer installed, skiping the test\n");
932 ret
= OpenPrinter(default_printer
, &hprn
, NULL
);
935 trace("There is no printers installed, skiping the test\n");
938 ok(hprn
!= 0, "wrong hprn %p\n", hprn
);
940 for (level
= -1; level
<= 7; level
++)
942 SetLastError(0xdeadbeef);
944 ret
= GetPrinterDriver(hprn
, NULL
, level
, NULL
, 0, &needed
);
945 ok(!ret
, "level %d: GetPrinterDriver should fail\n", level
);
946 if (level
>= 1 && level
<= 6)
948 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "wrong error %ld\n", GetLastError());
949 ok(needed
> 0,"not expected needed buffer size %ld\n", needed
);
953 ok(GetLastError() == ERROR_INVALID_LEVEL
, "wrong error %ld\n", GetLastError());
954 ok(needed
== (DWORD
)-1,"not expected needed buffer size %ld\n", needed
);
958 buf
= HeapAlloc(GetProcessHeap(), 0, needed
);
960 SetLastError(0xdeadbeef);
962 ret
= GetPrinterDriver(hprn
, NULL
, level
, buf
, needed
, &filled
);
963 ok(ret
, "level %d: GetPrinterDriver error %ld\n", level
, GetLastError());
964 ok(needed
== filled
, "needed %ld != filled %ld\n", needed
, filled
);
968 DRIVER_INFO_2
*di_2
= (DRIVER_INFO_2
*)buf
;
969 DWORD calculated
= sizeof(*di_2
);
971 ok(di_2
->cVersion
>= 0 && di_2
->cVersion
<= 3, "di_2->cVersion = %ld\n", di_2
->cVersion
);
972 ok(di_2
->pName
!= NULL
, "not expected NULL ptr\n");
973 ok(di_2
->pEnvironment
!= NULL
, "not expected NULL ptr\n");
974 ok(di_2
->pDriverPath
!= NULL
, "not expected NULL ptr\n");
975 ok(di_2
->pDataFile
!= NULL
, "not expected NULL ptr\n");
976 ok(di_2
->pConfigFile
!= NULL
, "not expected NULL ptr\n");
978 trace("cVersion %ld\n", di_2
->cVersion
);
979 trace("pName %s\n", di_2
->pName
);
980 calculated
+= strlen(di_2
->pName
) + 1;
981 trace("pEnvironment %s\n", di_2
->pEnvironment
);
982 calculated
+= strlen(di_2
->pEnvironment
) + 1;
983 trace("pDriverPath %s\n", di_2
->pDriverPath
);
984 calculated
+= strlen(di_2
->pDriverPath
) + 1;
985 trace("pDataFile %s\n", di_2
->pDataFile
);
986 calculated
+= strlen(di_2
->pDataFile
) + 1;
987 trace("pConfigFile %s\n", di_2
->pConfigFile
);
988 calculated
+= strlen(di_2
->pConfigFile
) + 1;
990 /* XP allocates memory for both ANSI and unicode names */
991 ok(filled
>= calculated
,"calculated %ld != filled %ld\n", calculated
, filled
);
994 HeapFree(GetProcessHeap(), 0, buf
);
997 SetLastError(0xdeadbeef);
998 ret
= ClosePrinter(hprn
);
999 ok(ret
, "ClosePrinter error %ld\n", GetLastError());
1004 hwinspool
= GetModuleHandleA("winspool.drv");
1005 pGetDefaultPrinterA
= (void *) GetProcAddress(hwinspool
, "GetDefaultPrinterA");
1006 pSetDefaultPrinterA
= (void *) GetProcAddress(hwinspool
, "SetDefaultPrinterA");
1008 find_default_printer();
1011 test_EnumMonitors();
1012 test_GetDefaultPrinter();
1013 test_GetPrinterDriverDirectory();
1015 test_GetPrinterDriver();
1016 test_SetDefaultPrinter();