2 * Copyright (C) 2003, 2004 Stefan Leichter
3 * Copyright (C) 2005 Detlef Riekenberg
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/test.h"
30 #define MAGIC_DEAD 0x00dead00
31 #define DEFAULT_PRINTER_SIZE 1000
33 static char env_x86
[] = "Windows NT x86";
34 static char env_win9x_case
[] = "windowS 4.0";
36 static HANDLE hwinspool
;
37 static FARPROC pGetDefaultPrinterA
;
38 static FARPROC pSetDefaultPrinterA
;
40 /* report common behavior only once */
41 static DWORD report_deactivated_spooler
= 1;
42 #define RETURN_ON_DEACTIVATED_SPOOLER(res) \
43 if((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE)) \
45 if(report_deactivated_spooler > 0) { \
46 report_deactivated_spooler--; \
47 trace("The Service 'Spooler' is required for many test\n"); \
53 static LPSTR
find_default_printer(VOID
)
55 static LPSTR default_printer
= NULL
;
56 static char buffer
[DEFAULT_PRINTER_SIZE
];
61 if ((default_printer
== NULL
) && (pGetDefaultPrinterA
))
64 needed
= sizeof(buffer
);
65 res
= pGetDefaultPrinterA(buffer
, &needed
);
66 if(res
) default_printer
= buffer
;
67 trace("default_printer: '%s'\n", default_printer
);
69 if (default_printer
== NULL
)
73 /* NT 3.x and above */
74 if (RegOpenKeyEx(HKEY_CURRENT_USER
,
75 "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
76 0, KEY_QUERY_VALUE
, &hwindows
) == NO_ERROR
) {
78 needed
= sizeof(buffer
);
79 if (RegQueryValueEx(hwindows
, "device", NULL
,
80 &type
, (LPBYTE
)buffer
, &needed
) == NO_ERROR
) {
82 ptr
= strchr(buffer
, ',');
85 default_printer
= buffer
;
88 RegCloseKey(hwindows
);
90 trace("default_printer: '%s'\n", default_printer
);
92 if (default_printer
== NULL
)
95 needed
= sizeof(buffer
);
96 res
= GetProfileStringA("windows", "device", "*", buffer
, needed
);
98 ptr
= strchr(buffer
, ',');
101 default_printer
= buffer
;
104 trace("default_printer: '%s'\n", default_printer
);
106 return default_printer
;
109 /* ########################### */
111 static void test_EnumMonitors(void)
120 /* valid levels are 1 and 2 */
121 for(level
= 0; level
< 4; level
++) {
123 pcReturned
= MAGIC_DEAD
;
124 SetLastError(MAGIC_DEAD
);
125 res
= EnumMonitorsA(NULL
, level
, NULL
, 0, &cbBuf
, &pcReturned
);
127 RETURN_ON_DEACTIVATED_SPOOLER(res
)
129 /* not implemented yet in wine */
130 if (!res
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) continue;
133 /* use only a short test, when we test with an invalid level */
134 if(!level
|| (level
> 2)) {
135 ok( (!res
&& (GetLastError() == ERROR_INVALID_LEVEL
)) ||
136 (res
&& (pcReturned
== 0)),
137 "(%ld) returned %ld with %ld and 0x%08lx (expected '0' with " \
138 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
139 level
, res
, GetLastError(), pcReturned
);
143 /* Level 2 is not supported on win9x */
144 if (!res
&& (GetLastError() == ERROR_INVALID_LEVEL
)) {
145 trace("Level %ld not supported, skipping tests\n", level
);
149 ok((!res
) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
150 "(%ld) returned %ld with %ld (expected '0' with " \
151 "ERROR_INSUFFICIENT_BUFFER)\n", level
, res
, GetLastError());
154 trace("no valid buffer size returned, skipping tests\n");
158 buffer
= HeapAlloc(GetProcessHeap(), 0, cbBuf
*2);
159 if (buffer
== NULL
) continue;
161 SetLastError(MAGIC_DEAD
);
162 pcbNeeded
= MAGIC_DEAD
;
163 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
, &pcbNeeded
, &pcReturned
);
164 ok(res
, "(%ld) returned %ld with %ld (expected '!=0')\n",
165 level
, res
, GetLastError());
166 ok(pcbNeeded
== cbBuf
, "(%ld) returned %ld (expected %ld)\n",
167 level
, pcbNeeded
, cbBuf
);
168 /* We can validate the returned Data with the Registry here */
171 SetLastError(MAGIC_DEAD
);
172 pcReturned
= MAGIC_DEAD
;
173 pcbNeeded
= MAGIC_DEAD
;
174 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
+1, &pcbNeeded
, &pcReturned
);
175 ok(res
, "(%ld) returned %ld with %ld (expected '!=0')\n", level
,
176 res
, GetLastError());
177 ok(pcbNeeded
== cbBuf
, "(%ld) returned %ld (expected %ld)\n", level
,
180 SetLastError(MAGIC_DEAD
);
181 pcbNeeded
= MAGIC_DEAD
;
182 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
-1, &pcbNeeded
, &pcReturned
);
183 ok( !res
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
184 "(%ld) returned %ld with %ld (expected '0' with " \
185 "ERROR_INSUFFICIENT_BUFFER)\n", level
, res
, GetLastError());
187 ok(pcbNeeded
== cbBuf
, "(%ld) returned %ld (expected %ld)\n", level
,
191 Do not add the next test:
192 w2k+: RPC_X_NULL_REF_POINTER
193 NT3.5: ERROR_INVALID_USER_BUFFER
194 win9x: crash in winspool.drv
196 res = EnumMonitorsA(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
199 SetLastError(MAGIC_DEAD
);
200 pcbNeeded
= MAGIC_DEAD
;
201 pcReturned
= MAGIC_DEAD
;
202 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
, NULL
, &pcReturned
);
203 ok( res
|| (!res
&& (GetLastError() == RPC_X_NULL_REF_POINTER
)) ,
204 "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
205 "RPC_X_NULL_REF_POINTER)\n", level
, res
, GetLastError());
207 pcbNeeded
= MAGIC_DEAD
;
208 pcReturned
= MAGIC_DEAD
;
209 SetLastError(MAGIC_DEAD
);
210 res
= EnumMonitorsA(NULL
, level
, buffer
, cbBuf
, &pcbNeeded
, NULL
);
211 ok( res
|| (!res
&& (GetLastError() == RPC_X_NULL_REF_POINTER
)) ,
212 "(%ld) returned %ld with %ld (expected '!=0' or '0' with "\
213 "RPC_X_NULL_REF_POINTER)\n", level
, res
, GetLastError());
215 HeapFree(GetProcessHeap(), 0, buffer
);
216 } /* for(level ... */
220 static void test_GetDefaultPrinter(void)
223 DWORD exact
= DEFAULT_PRINTER_SIZE
;
225 char buffer
[DEFAULT_PRINTER_SIZE
];
227 if (!pGetDefaultPrinterA
) return;
228 /* only supported on NT like OSes starting with win2k */
230 SetLastError(ERROR_SUCCESS
);
231 retval
= pGetDefaultPrinterA(buffer
, &exact
);
232 if (!retval
|| !exact
|| !strlen(buffer
) ||
233 (ERROR_SUCCESS
!= GetLastError())) {
234 if ((ERROR_FILE_NOT_FOUND
== GetLastError()) ||
235 (ERROR_INVALID_NAME
== GetLastError()))
236 trace("this test requires a default printer to be set\n");
238 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
239 "function returned %s\n"
240 "last error 0x%08lx\n"
241 "returned buffer size 0x%08lx\n"
242 "returned buffer content %s\n",
243 retval
? "true" : "false", GetLastError(), exact
, buffer
);
247 SetLastError(ERROR_SUCCESS
);
248 retval
= pGetDefaultPrinterA(NULL
, NULL
);
249 ok( !retval
, "function result wrong! False expected\n");
250 ok( ERROR_INVALID_PARAMETER
== GetLastError(),
251 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
254 SetLastError(ERROR_SUCCESS
);
255 retval
= pGetDefaultPrinterA(buffer
, NULL
);
256 ok( !retval
, "function result wrong! False expected\n");
257 ok( ERROR_INVALID_PARAMETER
== GetLastError(),
258 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
261 SetLastError(ERROR_SUCCESS
);
263 retval
= pGetDefaultPrinterA(NULL
, &size
);
264 ok( !retval
, "function result wrong! False expected\n");
265 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
266 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
268 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
271 SetLastError(ERROR_SUCCESS
);
272 size
= DEFAULT_PRINTER_SIZE
;
273 retval
= pGetDefaultPrinterA(NULL
, &size
);
274 ok( !retval
, "function result wrong! False expected\n");
275 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
276 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
278 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
282 retval
= pGetDefaultPrinterA(buffer
, &size
);
283 ok( !retval
, "function result wrong! False expected\n");
284 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
285 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
287 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
291 retval
= pGetDefaultPrinterA(buffer
, &size
);
292 ok( retval
, "function result wrong! True expected\n");
293 ok( size
== exact
, "Parameter size wrong! %ld expected got %ld\n",
297 static void test_GetPrinterDriverDirectory(void)
299 LPBYTE buffer
= NULL
;
300 DWORD cbBuf
= 0, pcbNeeded
= 0;
303 SetLastError(MAGIC_DEAD
);
304 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, NULL
, 0, &cbBuf
);
305 trace("first call returned 0x%04x, with %ld: buffer size 0x%08lx\n",
306 res
, GetLastError(), cbBuf
);
308 RETURN_ON_DEACTIVATED_SPOOLER(res
)
309 ok((res
== 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER
),
310 "returned %d with lasterror=%ld (expected '0' with " \
311 "ERROR_INSUFFICIENT_BUFFER)\n", res
, GetLastError());
314 trace("no valid buffer size returned, skipping tests\n");
318 buffer
= HeapAlloc( GetProcessHeap(), 0, cbBuf
*2);
319 if (buffer
== NULL
) return ;
321 res
= GetPrinterDriverDirectoryA(NULL
, NULL
, 1, buffer
, cbBuf
, &pcbNeeded
);
322 ok( res
, "expected result != 0, got %d\n", res
);
323 ok( cbBuf
== pcbNeeded
, "pcbNeeded set to %ld instead of %ld\n",
326 res
= GetPrinterDriverDirectoryA(NULL
, NULL
, 1, buffer
, cbBuf
*2, &pcbNeeded
);
327 ok( res
, "expected result != 0, got %d\n", res
);
328 ok( cbBuf
== pcbNeeded
, "pcbNeeded set to %ld instead of %ld\n",
331 SetLastError(MAGIC_DEAD
);
332 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, buffer
, cbBuf
-1, &pcbNeeded
);
333 ok( !res
, "expected result == 0, got %d\n", res
);
334 ok( cbBuf
== pcbNeeded
, "pcbNeeded set to %ld instead of %ld\n",
337 ok( ERROR_INSUFFICIENT_BUFFER
== GetLastError(),
338 "last error set to %ld instead of ERROR_INSUFFICIENT_BUFFER\n",
342 Do not add the next test:
343 XPsp2: crash in this app, when the spooler is not running
344 NT3.5: ERROR_INVALID_USER_BUFFER
345 win9x: ERROR_INVALID_PARAMETER
347 pcbNeeded = MAGIC_DEAD;
348 SetLastError(MAGIC_DEAD);
349 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
352 SetLastError(MAGIC_DEAD
);
353 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, buffer
, cbBuf
, NULL
);
354 ok( (!res
&& RPC_X_NULL_REF_POINTER
== GetLastError()) || res
,
355 "expected either result == 0 and "
356 "last error == RPC_X_NULL_REF_POINTER or result != 0 "
357 "got result %d and last error == %ld\n", res
, GetLastError());
359 SetLastError(MAGIC_DEAD
);
360 res
= GetPrinterDriverDirectoryA( NULL
, NULL
, 1, NULL
, cbBuf
, NULL
);
361 ok(res
|| (GetLastError() == RPC_X_NULL_REF_POINTER
),
362 "returned %d with %ld (expected '!=0' or '0' with " \
363 "RPC_X_NULL_REF_POINTER)\n", res
, GetLastError());
366 /* with a valid buffer, but level is too large */
368 SetLastError(MAGIC_DEAD
);
369 res
= GetPrinterDriverDirectoryA(NULL
, NULL
, 2, buffer
, cbBuf
, &pcbNeeded
);
371 /* Level not checked in win9x and wine:*/
372 if((res
!= FALSE
) && buffer
[0])
374 trace("Level '2' not checked '%s'\n", buffer
);
378 ok( !res
&& (GetLastError() == ERROR_INVALID_LEVEL
),
379 "returned %d with lasterror=%ld (expected '0' with " \
380 "ERROR_INVALID_LEVEL)\n", res
, GetLastError());
383 /* printing environments are case insensitive */
384 /* "Windows 4.0" is valid for win9x and NT */
386 SetLastError(MAGIC_DEAD
);
387 res
= GetPrinterDriverDirectoryA(NULL
, env_win9x_case
, 1,
388 buffer
, cbBuf
*2, &pcbNeeded
);
390 if(!res
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)) {
392 buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, cbBuf
*2);
393 if (buffer
== NULL
) return ;
395 SetLastError(MAGIC_DEAD
);
396 res
= GetPrinterDriverDirectoryA(NULL
, env_win9x_case
, 1,
397 buffer
, cbBuf
*2, &pcbNeeded
);
400 ok(res
&& buffer
[0], "returned %d with " \
401 "lasterror=%ld and len=%d (expected '1' with 'len > 0')\n",
402 res
, GetLastError(), lstrlenA((char *)buffer
));
405 SetLastError(MAGIC_DEAD
);
406 res
= GetPrinterDriverDirectoryA(NULL
, env_x86
, 1,
407 buffer
, cbBuf
*2, &pcbNeeded
);
409 if(!res
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)) {
411 buffer
= HeapReAlloc(GetProcessHeap(), 0, buffer
, cbBuf
*2);
412 if (buffer
== NULL
) return ;
415 SetLastError(MAGIC_DEAD
);
416 res
= GetPrinterDriverDirectoryA(NULL
, env_x86
, 1,
417 buffer
, cbBuf
*2, &pcbNeeded
);
420 /* "Windows NT x86" is invalid for win9x */
421 ok( (res
&& buffer
[0]) ||
422 (!res
&& (GetLastError() == ERROR_INVALID_ENVIRONMENT
)),
423 "returned %d with lasterror=%ld and len=%d (expected '!= 0' with " \
424 "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
425 res
, GetLastError(), lstrlenA((char *)buffer
));
427 /* A Setup-Programm (PDFCreator_0.8.0) use empty strings */
428 SetLastError(MAGIC_DEAD
);
429 res
= GetPrinterDriverDirectoryA("", "", 1, buffer
, cbBuf
*2, &pcbNeeded
);
430 ok(res
, "returned %d with %ld (expected '!=0')\n", res
, GetLastError() );
432 SetLastError(MAGIC_DEAD
);
433 res
= GetPrinterDriverDirectoryA(NULL
, "", 1, buffer
, cbBuf
*2, &pcbNeeded
);
434 ok(res
, "returned %d with %ld (expected '!=0')\n", res
, GetLastError() );
436 SetLastError(MAGIC_DEAD
);
437 res
= GetPrinterDriverDirectoryA("", NULL
, 1, buffer
, cbBuf
*2, &pcbNeeded
);
438 ok(res
, "returned %d with %ld (expected '!=0')\n", res
, GetLastError() );
440 HeapFree( GetProcessHeap(), 0, buffer
);
443 static void test_OpenPrinter(void)
445 PRINTER_DEFAULTSA defaults
;
447 LPSTR default_printer
;
450 CHAR buffer
[DEFAULT_PRINTER_SIZE
];
453 SetLastError(MAGIC_DEAD
);
454 res
= OpenPrinter(NULL
, NULL
, NULL
);
455 /* The deactivated Spooler is catched here on NT3.51 */
456 RETURN_ON_DEACTIVATED_SPOOLER(res
)
457 ok(!res
&& (GetLastError() == ERROR_INVALID_PARAMETER
),
458 "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
459 res
, GetLastError());
462 /* Get Handle for the local Printserver (NT only)*/
463 hprinter
= (HANDLE
) MAGIC_DEAD
;
464 SetLastError(MAGIC_DEAD
);
465 res
= OpenPrinter(NULL
, &hprinter
, NULL
);
466 /* The deactivated Spooler is catched here on XPsp2 */
467 RETURN_ON_DEACTIVATED_SPOOLER(res
)
468 ok(res
|| (!res
&& GetLastError() == ERROR_INVALID_PARAMETER
),
469 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
470 res
, GetLastError());
472 ClosePrinter(hprinter
);
474 defaults
.pDatatype
=NULL
;
475 defaults
.pDevMode
=NULL
;
477 defaults
.DesiredAccess
=0;
478 hprinter
= (HANDLE
) MAGIC_DEAD
;
479 SetLastError(MAGIC_DEAD
);
480 res
= OpenPrinter(NULL
, &hprinter
, &defaults
);
481 ok(res
, "returned %ld with %ld (expected '!=0')\n", res
, GetLastError());
482 if (res
) ClosePrinter(hprinter
);
484 defaults
.DesiredAccess
=-1;
485 hprinter
= (HANDLE
) MAGIC_DEAD
;
486 SetLastError(MAGIC_DEAD
);
487 res
= OpenPrinter(NULL
, &hprinter
, &defaults
);
488 ok(!res
&& GetLastError() == ERROR_ACCESS_DENIED
,
489 "returned %ld with %ld (expected '0' with ERROR_ACCESS_DENIED)\n",
490 res
, GetLastError());
491 if (res
) ClosePrinter(hprinter
);
495 size
= sizeof(buffer
) - 3 ;
501 if (GetComputerNameA(ptr
, &size
)) {
503 hprinter
= (HANDLE
) MAGIC_DEAD
;
504 SetLastError(MAGIC_DEAD
);
505 res
= OpenPrinter(buffer
, &hprinter
, NULL
);
507 ok(res
|| (!res
&& GetLastError() == ERROR_INVALID_PARAMETER
),
508 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
509 res
, GetLastError());
511 if(res
) ClosePrinter(hprinter
);
514 /* Invalid Printername */
515 hprinter
= (HANDLE
) MAGIC_DEAD
;
516 SetLastError(MAGIC_DEAD
);
517 res
= OpenPrinter("illegal,name", &hprinter
, NULL
);
518 ok(!res
&& ((GetLastError() == ERROR_INVALID_PRINTER_NAME
) ||
519 (GetLastError() == ERROR_INVALID_PARAMETER
) ),
520 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or" \
521 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
522 if(res
) ClosePrinter(hprinter
);
525 /* Get Handle for the default Printer */
526 if ((default_printer
= find_default_printer()))
528 hprinter
= (HANDLE
) MAGIC_DEAD
;
529 SetLastError(MAGIC_DEAD
);
530 res
= OpenPrinter(default_printer
, &hprinter
, NULL
);
531 if((!res
) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE
))
533 trace("The Service 'Spooler' is required for '%s'\n", default_printer
);
536 ok(res
, "returned %ld with %ld (expected '!=0')\n", res
, GetLastError());
537 if(res
) ClosePrinter(hprinter
);
539 defaults
.pDatatype
=NULL
;
540 defaults
.pDevMode
=NULL
;
541 defaults
.DesiredAccess
=0;
543 hprinter
= (HANDLE
) MAGIC_DEAD
;
544 SetLastError(MAGIC_DEAD
);
545 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
546 ok(res
|| GetLastError() == ERROR_ACCESS_DENIED
,
547 "returned %ld with %ld (expected '!=0' or '0' with " \
548 "ERROR_ACCESS_DENIED)\n", res
, GetLastError());
549 if(res
) ClosePrinter(hprinter
);
551 defaults
.pDatatype
="";
553 hprinter
= (HANDLE
) MAGIC_DEAD
;
554 SetLastError(MAGIC_DEAD
);
555 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
556 /* stop here, when a remote Printserver has no RPC-Service running */
557 RETURN_ON_DEACTIVATED_SPOOLER(res
)
558 ok(res
|| ((GetLastError() == ERROR_INVALID_DATATYPE
) ||
559 (GetLastError() == ERROR_ACCESS_DENIED
)),
560 "returned %ld with %ld (expected '!=0' or '0' with: " \
561 "ERROR_INVALID_DATATYPE or ERROR_ACCESS_DENIED)\n",
562 res
, GetLastError());
563 if(res
) ClosePrinter(hprinter
);
566 defaults
.pDatatype
=NULL
;
567 defaults
.DesiredAccess
=PRINTER_ACCESS_USE
;
569 hprinter
= (HANDLE
) MAGIC_DEAD
;
570 SetLastError(MAGIC_DEAD
);
571 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
572 ok(res
|| GetLastError() == ERROR_ACCESS_DENIED
,
573 "returned %ld with %ld (expected '!=0' or '0' with " \
574 "ERROR_ACCESS_DENIED)\n", res
, GetLastError());
575 if(res
) ClosePrinter(hprinter
);
578 defaults
.DesiredAccess
=PRINTER_ALL_ACCESS
;
579 hprinter
= (HANDLE
) MAGIC_DEAD
;
580 SetLastError(MAGIC_DEAD
);
581 res
= OpenPrinter(default_printer
, &hprinter
, &defaults
);
582 ok(res
|| GetLastError() == ERROR_ACCESS_DENIED
,
583 "returned %ld with %ld (expected '!=0' or '0' with " \
584 "ERROR_ACCESS_DENIED)\n", res
, GetLastError());
585 if(res
) ClosePrinter(hprinter
);
591 static void test_SetDefaultPrinter(void)
594 LPSTR default_printer
;
595 DWORD size
= DEFAULT_PRINTER_SIZE
;
596 CHAR buffer
[DEFAULT_PRINTER_SIZE
];
597 CHAR org_value
[DEFAULT_PRINTER_SIZE
];
600 if (!pSetDefaultPrinterA
) return;
601 /* only supported on win2k and above */
603 default_printer
= find_default_printer();
605 /* backup the original value */
607 SetLastError(MAGIC_DEAD
);
608 res
= GetProfileStringA("windows", "device", NULL
, org_value
, size
);
610 /* first part: with the default Printer */
611 SetLastError(MAGIC_DEAD
);
612 res
= pSetDefaultPrinterA("no_printer_with_this_name");
614 RETURN_ON_DEACTIVATED_SPOOLER(res
)
615 /* spooler is running or we have no spooler here*/
617 /* Not implemented in wine */
618 if (!res
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)) {
619 trace("SetDefaultPrinterA() not implemented yet.\n");
623 ok(!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
),
624 "returned %ld with %ld (expected '0' with " \
625 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
627 WriteProfileStringA("windows", "device", org_value
);
628 SetLastError(MAGIC_DEAD
);
629 res
= pSetDefaultPrinterA("");
630 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
631 "returned %ld with %ld (expected '!=0' or '0' with " \
632 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
634 WriteProfileStringA("windows", "device", org_value
);
635 SetLastError(MAGIC_DEAD
);
636 res
= pSetDefaultPrinterA(NULL
);
637 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
638 "returned %ld with %ld (expected '!=0' or '0' with " \
639 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
641 WriteProfileStringA("windows", "device", org_value
);
642 SetLastError(MAGIC_DEAD
);
643 res
= pSetDefaultPrinterA(default_printer
);
644 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
645 "returned %ld with %ld (expected '!=0' or '0' with " \
646 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
649 /* second part: always without a default Printer */
650 WriteProfileStringA("windows", "device", NULL
);
651 SetLastError(MAGIC_DEAD
);
652 res
= pSetDefaultPrinterA("no_printer_with_this_name");
654 ok(!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
),
655 "returned %ld with %ld (expected '0' with " \
656 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
658 WriteProfileStringA("windows", "device", NULL
);
659 SetLastError(MAGIC_DEAD
);
660 res
= pSetDefaultPrinterA("");
661 /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
662 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
663 "returned %ld with %ld (expected '!=0' or '0' with " \
664 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
666 WriteProfileStringA("windows", "device", NULL
);
667 SetLastError(MAGIC_DEAD
);
668 res
= pSetDefaultPrinterA(NULL
);
669 /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
670 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
671 "returned %ld with %ld (expected '!=0' or '0' with " \
672 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
674 WriteProfileStringA("windows", "device", NULL
);
675 SetLastError(MAGIC_DEAD
);
676 res
= pSetDefaultPrinterA(default_printer
);
677 ok(res
|| (!res
&& (GetLastError() == ERROR_INVALID_PRINTER_NAME
)),
678 "returned %ld with %ld (expected '!=0' or '0' with " \
679 "ERROR_INVALID_PRINTER_NAME)\n", res
, GetLastError());
681 /* restore the original value */
682 res
= pSetDefaultPrinterA(default_printer
); /* the nice way */
683 WriteProfileStringA("windows", "device", org_value
); /* the old way */
686 SetLastError(MAGIC_DEAD
);
687 res
= GetProfileStringA("windows", "device", NULL
, buffer
, size
);
688 ok(!lstrcmpA(org_value
, buffer
), "'%s' (expected '%s')\n", buffer
, org_value
);
695 hwinspool
= GetModuleHandleA("winspool.drv");
696 pGetDefaultPrinterA
= (void *) GetProcAddress(hwinspool
, "GetDefaultPrinterA");
697 pSetDefaultPrinterA
= (void *) GetProcAddress(hwinspool
, "SetDefaultPrinterA");
699 find_default_printer();
702 test_GetDefaultPrinter();
703 test_GetPrinterDriverDirectory();
705 test_SetDefaultPrinter();