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;
40 static LPSTR find_default_printer(VOID)
42 static LPSTR default_printer = NULL;
43 static char buffer[DEFAULT_PRINTER_SIZE];
48 if ((default_printer == NULL) && (pGetDefaultPrinterA))
51 needed = sizeof(buffer);
52 res = pGetDefaultPrinterA(buffer, &needed);
53 if(res) default_printer = buffer;
54 trace("default_printer: '%s'\n", default_printer);
56 if (default_printer == NULL)
60 /* NT 3.x and above */
61 if (RegOpenKeyEx(HKEY_CURRENT_USER,
62 "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
63 0, KEY_QUERY_VALUE, &hwindows) == NO_ERROR) {
65 needed = sizeof(buffer);
66 if (RegQueryValueEx(hwindows, "device", NULL,
67 &type, (LPBYTE)buffer, &needed) == NO_ERROR) {
69 ptr = strchr(buffer, ',');
72 default_printer = buffer;
75 RegCloseKey(hwindows);
77 trace("default_printer: '%s'\n", default_printer);
79 if (default_printer == NULL)
82 needed = sizeof(buffer);
83 res = GetProfileStringA("windows", "device", "*", buffer, needed);
85 ptr = strchr(buffer, ',');
88 default_printer = buffer;
91 trace("default_printer: '%s'\n", default_printer);
93 return default_printer;
97 static void test_default_printer(void)
100 DWORD exact = DEFAULT_PRINTER_SIZE;
102 char buffer[DEFAULT_PRINTER_SIZE];
104 if (!pGetDefaultPrinterA) return;
105 /* only supported on NT like OSes starting with win2k */
107 SetLastError(ERROR_SUCCESS);
108 retval = pGetDefaultPrinterA(buffer, &exact);
109 if (!retval || !exact || !strlen(buffer) ||
110 (ERROR_SUCCESS != GetLastError())) {
111 if ((ERROR_FILE_NOT_FOUND == GetLastError()) ||
112 (ERROR_INVALID_NAME == GetLastError()))
113 trace("this test requires a default printer to be set\n");
115 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
116 "function returned %s\n"
117 "last error 0x%08lx\n"
118 "returned buffer size 0x%08lx\n"
119 "returned buffer content %s\n",
120 retval ? "true" : "false", GetLastError(), exact, buffer);
124 SetLastError(ERROR_SUCCESS);
125 retval = pGetDefaultPrinterA(NULL, NULL);
126 ok( !retval, "function result wrong! False expected\n");
127 ok( ERROR_INVALID_PARAMETER == GetLastError(),
128 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
131 SetLastError(ERROR_SUCCESS);
132 retval = pGetDefaultPrinterA(buffer, NULL);
133 ok( !retval, "function result wrong! False expected\n");
134 ok( ERROR_INVALID_PARAMETER == GetLastError(),
135 "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08lx\n",
138 SetLastError(ERROR_SUCCESS);
140 retval = pGetDefaultPrinterA(NULL, &size);
141 ok( !retval, "function result wrong! False expected\n");
142 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
143 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
145 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
148 SetLastError(ERROR_SUCCESS);
149 size = DEFAULT_PRINTER_SIZE;
150 retval = pGetDefaultPrinterA(NULL, &size);
151 ok( !retval, "function result wrong! False expected\n");
152 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
153 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
155 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
159 retval = pGetDefaultPrinterA(buffer, &size);
160 ok( !retval, "function result wrong! False expected\n");
161 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
162 "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08lx\n",
164 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
168 retval = pGetDefaultPrinterA(buffer, &size);
169 ok( retval, "function result wrong! True expected\n");
170 ok( size == exact, "Parameter size wrong! %ld expected got %ld\n",
174 static void test_printer_directory(void)
175 { LPBYTE buffer = NULL;
176 DWORD cbBuf = 0, pcbNeeded = 0;
179 SetLastError(0x00dead00);
180 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, 0, &cbBuf);
181 trace("GetPrinterDriverDirectoryA: first call returned 0x%04x, "
182 "buffer size 0x%08lx\n", res, cbBuf);
184 if((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE))
186 trace("The Service 'Spooler' is required for this test\n");
189 ok((res == 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
190 "returned %d with lasterror=%ld (expected '0' with " \
191 "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError());
194 trace("no valid buffer size returned, skipping tests\n");
198 buffer = HeapAlloc( GetProcessHeap(), 0, cbBuf*2);
199 if (buffer == NULL) return ;
201 res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded);
202 ok( res, "expected result != 0, got %d\n", res);
203 ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
206 res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
207 ok( res, "expected result != 0, got %d\n", res);
208 ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
211 SetLastError(0x00dead00);
212 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded);
213 ok( !res , "expected result == 0, got %d\n", res);
214 ok( cbBuf == pcbNeeded, "pcbNeeded set to %ld instead of %ld\n",
217 ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
218 "last error set to %ld instead of ERROR_INSUFFICIENT_BUFFER\n",
221 SetLastError(0x00dead00);
222 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
223 ok( (!res && ERROR_INVALID_USER_BUFFER == GetLastError()) ||
224 ( res && ERROR_INVALID_PARAMETER == GetLastError()) ,
225 "expected either result == 0 and "
226 "last error == ERROR_INVALID_USER_BUFFER "
227 "or result != 0 and last error == ERROR_INVALID_PARAMETER "
228 "got result %d and last error == %ld\n", res, GetLastError());
230 SetLastError(0x00dead00);
231 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf, NULL);
232 ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) || res,
233 "expected either result == 0 and "
234 "last error == RPC_X_NULL_REF_POINTER or result != 0 "
235 "got result %d and last error == %ld\n", res, GetLastError());
237 SetLastError(0x00dead00);
238 res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, NULL);
239 ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) ||
240 ( res && ERROR_INVALID_PARAMETER == GetLastError()) ,
241 "expected either result == 0 and "
242 "last error == RPC_X_NULL_REF_POINTER "
243 "or result != 0 and last error == ERROR_INVALID_PARAMETER "
244 "got result %d and last error == %ld\n", res, GetLastError());
246 /* with a valid buffer, but level is too large */
248 SetLastError(0x00dead00);
249 res = GetPrinterDriverDirectoryA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded);
251 /* Level not checked in win9x and wine:*/
252 if((res != FALSE) && buffer[0])
254 trace("invalid Level '2' not checked (valid Level is '1') => '%s'\n",
259 ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
260 "returned %d with lasterror=%ld (expected '0' with " \
261 "ERROR_INVALID_LEVEL)\n", res, GetLastError());
264 /* printing environments are case insensitive */
265 /* "Windows 4.0" is valid for win9x and NT */
267 SetLastError(0x00dead00);
268 res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1,
269 buffer, cbBuf*2, &pcbNeeded);
271 if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
273 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
274 if (buffer == NULL) return ;
276 SetLastError(0x00dead00);
277 res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1,
278 buffer, cbBuf*2, &pcbNeeded);
281 ok(res && buffer[0], "returned %d with " \
282 "lasterror=%ld and len=%d (expected '1' with 'len > 0')\n",
283 res, GetLastError(), lstrlenA((char *)buffer));
286 SetLastError(0x00dead00);
287 res = GetPrinterDriverDirectoryA(NULL, env_x86, 1,
288 buffer, cbBuf*2, &pcbNeeded);
290 if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
292 buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
293 if (buffer == NULL) return ;
296 SetLastError(0x00dead00);
297 res = GetPrinterDriverDirectoryA(NULL, env_x86, 1,
298 buffer, cbBuf*2, &pcbNeeded);
301 /* "Windows NT x86" is invalid for win9x */
302 ok( (res && buffer[0]) ||
303 (!res && (GetLastError() == ERROR_INVALID_ENVIRONMENT)),
304 "returned %d with lasterror=%ld and len=%d (expected '!= 0' with " \
305 "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
306 res, GetLastError(), lstrlenA((char *)buffer));
308 HeapFree( GetProcessHeap(), 0, buffer);
311 static void test_openprinter(void)
313 PRINTER_DEFAULTSA defaults;
315 LPSTR default_printer;
318 CHAR buffer[DEFAULT_PRINTER_SIZE];
321 SetLastError(MAGIC_DEAD);
322 res = OpenPrinter(NULL, NULL, NULL);
323 if((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE))
325 trace("The Service 'Spooler' is required for this test\n");
328 ok(!res && (GetLastError() == ERROR_INVALID_PARAMETER),
329 "returned %ld with %ld (expected '0' with ERROR_INVALID_PARAMETER)\n",
330 res, GetLastError());
333 /* Get Handle for the local Printserver (NT only)*/
334 hprinter = (HANDLE) MAGIC_DEAD;
335 SetLastError(MAGIC_DEAD);
336 res = OpenPrinter(NULL, &hprinter, NULL);
337 ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
338 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
339 res, GetLastError());
341 ClosePrinter(hprinter);
343 defaults.pDatatype=NULL;
344 defaults.pDevMode=NULL;
346 defaults.DesiredAccess=0;
347 hprinter = (HANDLE) MAGIC_DEAD;
348 SetLastError(MAGIC_DEAD);
349 res = OpenPrinter(NULL, &hprinter, &defaults);
350 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
351 if (res) ClosePrinter(hprinter);
353 defaults.DesiredAccess=-1;
354 hprinter = (HANDLE) MAGIC_DEAD;
355 SetLastError(MAGIC_DEAD);
356 res = OpenPrinter(NULL, &hprinter, &defaults);
357 ok(!res && GetLastError() == ERROR_ACCESS_DENIED,
358 "returned %ld with %ld (expected '0' with ERROR_ACCESS_DENIED)\n",
359 res, GetLastError());
360 if (res) ClosePrinter(hprinter);
364 size = sizeof(buffer) - 3 ;
370 if (GetComputerNameA(ptr, &size)) {
372 hprinter = (HANDLE) MAGIC_DEAD;
373 SetLastError(MAGIC_DEAD);
374 res = OpenPrinter(buffer, &hprinter, NULL);
376 ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
377 "returned %ld with %ld (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
378 res, GetLastError());
380 if(res) ClosePrinter(hprinter);
383 /* Invalid Printername */
384 hprinter = (HANDLE) MAGIC_DEAD;
385 SetLastError(MAGIC_DEAD);
386 res = OpenPrinter("illegal,name", &hprinter, NULL);
387 ok(!res && ((GetLastError() == ERROR_INVALID_PRINTER_NAME) ||
388 (GetLastError() == ERROR_INVALID_PARAMETER) ),
389 "returned %ld with %ld (expected '0' with: ERROR_INVALID_PARAMETER or" \
390 "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
391 if(res) ClosePrinter(hprinter);
394 /* Get Handle for the default Printer */
395 if ((default_printer = find_default_printer()))
397 hprinter = (HANDLE) MAGIC_DEAD;
398 SetLastError(MAGIC_DEAD);
399 res = OpenPrinter(default_printer, &hprinter, NULL);
400 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
401 if(res) ClosePrinter(hprinter);
403 defaults.pDatatype=NULL;
404 defaults.pDevMode=NULL;
405 defaults.DesiredAccess=0;
407 hprinter = (HANDLE) MAGIC_DEAD;
408 SetLastError(MAGIC_DEAD);
409 res = OpenPrinter(default_printer, &hprinter, &defaults);
410 ok(res, "returned %ld with %ld (expected '!=0')\n", res, GetLastError());
411 if(res) ClosePrinter(hprinter);
413 defaults.pDatatype="";
415 hprinter = (HANDLE) MAGIC_DEAD;
416 SetLastError(MAGIC_DEAD);
417 res = OpenPrinter(default_printer, &hprinter, &defaults);
418 ok(res || (GetLastError() == ERROR_INVALID_DATATYPE ||
419 GetLastError() == RPC_S_SERVER_UNAVAILABLE),
420 "returned %ld with %ld (expected '!=0' or '0' with: " \
421 "ERROR_INVALID_DATATYPE or RPC_S_SERVER_UNAVAILABLE)\n",
422 res, GetLastError());
423 if(res) ClosePrinter(hprinter);
426 defaults.pDatatype=NULL;
427 defaults.DesiredAccess=PRINTER_ACCESS_USE;
429 hprinter = (HANDLE) MAGIC_DEAD;
430 SetLastError(MAGIC_DEAD);
431 res = OpenPrinter(default_printer, &hprinter, &defaults);
432 ok(res || (GetLastError() == ERROR_ACCESS_DENIED ||
433 GetLastError() == RPC_S_SERVER_UNAVAILABLE),
434 "returned %ld with %ld (expected '!=0' or '0' with: " \
435 "ERROR_ACCESS_DENIED or RPC_S_SERVER_UNAVAILABLE)\n",
436 res, GetLastError());
437 if(res) ClosePrinter(hprinter);
440 defaults.DesiredAccess=PRINTER_ALL_ACCESS;
441 hprinter = (HANDLE) MAGIC_DEAD;
442 SetLastError(MAGIC_DEAD);
443 res = OpenPrinter(default_printer, &hprinter, &defaults);
444 ok(res || (GetLastError() == ERROR_ACCESS_DENIED ||
445 GetLastError() == RPC_S_SERVER_UNAVAILABLE),
446 "returned %ld with %ld (expected '!=0' or '0' with: " \
447 "ERROR_ACCESS_DENIED or RPC_S_SERVER_UNAVAILABLE)\n",
448 res, GetLastError());
449 if(res) ClosePrinter(hprinter);
456 hwinspool = GetModuleHandleA("winspool.drv");
457 pGetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "GetDefaultPrinterA");
459 find_default_printer();
461 test_default_printer();
462 test_printer_directory();