winspool/tests: Handle the Windows bug in GetPrinterDataEx.
[wine] / dlls / winspool.drv / tests / info.c
1 /*
2  * Copyright (C) 2003, 2004 Stefan Leichter
3  * Copyright (C) 2005, 2006 Detlef Riekenberg
4  * Copyright (C) 2006 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wingdi.h"
31 #include "winnls.h"
32 #include "winuser.h"
33 #include "winreg.h"
34 #include "winspool.h"
35 #include "commdlg.h"
36 #include "wine/test.h"
37
38 #define MAGIC_DEAD  0xdeadbeef
39 #define DEFAULT_PRINTER_SIZE 1000
40
41 static CHAR defaultspooldirectory[] = "DefaultSpoolDirectory";
42 static CHAR does_not_exist_dll[]= "does_not_exist.dll";
43 static CHAR does_not_exist[]    = "does_not_exist";
44 static CHAR empty[]             = "";
45 static CHAR env_x64[]           = "Windows x64";
46 static CHAR env_x86[]           = "Windows NT x86";
47 static CHAR env_win9x_case[]    = "windowS 4.0";
48 static CHAR illegal_name[]      = "illegal,name";
49 static CHAR invalid_env[]       = "invalid_env";
50 static CHAR LocalPortA[]        = "Local Port";
51 static CHAR portname_com1[]     = "COM1:";
52 static CHAR portname_file[]     = "FILE:";
53 static CHAR portname_lpt1[]     = "LPT1:";
54 static CHAR server_does_not_exist[] = "\\\\does_not_exist";
55 static CHAR version_dll[]       = "version.dll";
56 static CHAR winetest[]          = "winetest";
57 static CHAR xcv_localport[]     = ",XcvMonitor Local Port";
58
59 static WCHAR cmd_MonitorUIW[] = {'M','o','n','i','t','o','r','U','I',0};
60 static WCHAR cmd_PortIsValidW[] = {'P','o','r','t','I','s','V','a','l','i','d',0};
61 static WCHAR emptyW[] = {0};
62
63 static WCHAR portname_com1W[] = {'C','O','M','1',':',0};
64 static WCHAR portname_com2W[] = {'C','O','M','2',':',0};
65 static WCHAR portname_fileW[] = {'F','I','L','E',':',0};
66 static WCHAR portname_lpt1W[] = {'L','P','T','1',':',0};
67 static WCHAR portname_lpt2W[] = {'L','P','T','2',':',0};
68
69 static HANDLE  hwinspool;
70 static BOOL  (WINAPI * pAddPortExA)(LPSTR, DWORD, LPBYTE, LPSTR);
71 static BOOL  (WINAPI * pEnumPrinterDriversW)(LPWSTR, LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD);
72 static BOOL  (WINAPI * pGetDefaultPrinterA)(LPSTR, LPDWORD);
73 static DWORD (WINAPI * pGetPrinterDataExA)(HANDLE, LPCSTR, LPCSTR, LPDWORD, LPBYTE, DWORD, LPDWORD);
74 static BOOL  (WINAPI * pGetPrinterDriverW)(HANDLE, LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD);
75 static BOOL  (WINAPI * pGetPrinterW)(HANDLE, DWORD, LPBYTE, DWORD, LPDWORD);
76 static BOOL  (WINAPI * pSetDefaultPrinterA)(LPCSTR);
77 static DWORD (WINAPI * pXcvDataW)(HANDLE, LPCWSTR, PBYTE, DWORD, PBYTE, DWORD, PDWORD, PDWORD);
78
79
80 /* ################################ */
81
82 struct monitor_entry {
83     LPSTR  env;
84     CHAR  dllname[32];
85 };
86
87 static LPSTR default_printer = NULL;
88 static LPSTR local_server = NULL;
89 static LPSTR tempdirA = NULL;
90 static LPSTR tempfileA = NULL;
91 static LPWSTR tempdirW = NULL;
92 static LPWSTR tempfileW = NULL;
93
94 /* ################################ */
95 /* report common behavior only once */
96 static DWORD deactivated_spooler_reported = 0;
97 #define RETURN_ON_DEACTIVATED_SPOOLER(res) \
98     if ((res == 0) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE)) \
99     { \
100         if (!deactivated_spooler_reported) { \
101             deactivated_spooler_reported++; \
102             skip("The Service 'Spooler' is required for many test\n"); \
103         } \
104         return; \
105     }
106
107 static DWORD access_denied_reported = 0;
108 #define RETURN_ON_ACCESS_DENIED(res) \
109     if ((res == 0) && (GetLastError() == ERROR_ACCESS_DENIED)) \
110     { \
111         if (!access_denied_reported) { \
112             access_denied_reported++; \
113             skip("More Access-Rights are required for many test\n"); \
114         } \
115         return; \
116     }
117
118 /* ################################ */
119
120 static BOOL on_win9x = FALSE;
121
122 static BOOL check_win9x(void)
123 {
124     if (pGetPrinterW)
125     {
126         SetLastError(0xdeadbeef);
127         pGetPrinterW(NULL, 0, NULL, 0, NULL);
128         return (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED);
129     }
130     else
131     {
132         return TRUE;
133     }
134 }
135
136 static void find_default_printer(VOID)
137 {
138     static  char    buffer[DEFAULT_PRINTER_SIZE];
139     DWORD   needed;
140     DWORD   res;
141     LPSTR   ptr;
142
143     if ((default_printer == NULL) && (pGetDefaultPrinterA))
144     {
145         /* w2k and above */
146         needed = sizeof(buffer);
147         res = pGetDefaultPrinterA(buffer, &needed);
148         if(res)  default_printer = buffer;
149         trace("default_printer: '%s'\n", default_printer);
150     }
151     if (default_printer == NULL)
152     {
153         HKEY hwindows;
154         DWORD   type;
155         /* NT 3.x and above */
156         if (RegOpenKeyEx(HKEY_CURRENT_USER, 
157                         "Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
158                         0, KEY_QUERY_VALUE, &hwindows) == NO_ERROR) {
159
160             needed = sizeof(buffer);
161             if (RegQueryValueEx(hwindows, "device", NULL, 
162                                 &type, (LPBYTE)buffer, &needed) == NO_ERROR) {
163
164                 ptr = strchr(buffer, ',');
165                 if (ptr) {
166                     ptr[0] = '\0';
167                     default_printer = buffer;
168                 }
169             }
170             RegCloseKey(hwindows);
171         }
172         trace("default_printer: '%s'\n", default_printer);
173     }
174     if (default_printer == NULL)
175     {
176         /* win9x */
177         needed = sizeof(buffer);
178         res = GetProfileStringA("windows", "device", "*", buffer, needed);
179         if(res) {
180             ptr = strchr(buffer, ',');
181             if (ptr) {
182                 ptr[0] = '\0';
183                 default_printer = buffer;
184             }
185         }
186         trace("default_printer: '%s'\n", default_printer);
187     }
188 }
189
190
191 static struct monitor_entry * find_installed_monitor(void)
192 {
193     MONITOR_INFO_2A mi2a; 
194     static struct  monitor_entry * entry = NULL;
195     DWORD   num_tests;
196     DWORD   i = 0;
197
198     static struct monitor_entry  monitor_table[] = {
199         {env_win9x_case, "localspl.dll"},
200         {env_x86,        "localspl.dll"},
201         {env_x64,        "localspl.dll"},
202         {env_win9x_case, "localmon.dll"},
203         {env_x86,        "localmon.dll"},
204         {env_win9x_case, "tcpmon.dll"},
205         {env_x86,        "tcpmon.dll"},
206         {env_win9x_case, "usbmon.dll"},
207         {env_x86,        "usbmon.dll"},
208         {env_win9x_case, "mspp32.dll"},
209         {env_x86,        "win32spl.dll"},
210         {env_x86,        "redmonnt.dll"},
211         {env_x86,        "redmon35.dll"},
212         {env_win9x_case, "redmon95.dll"},
213         {env_x86,        "pdfcmnnt.dll"},
214         {env_win9x_case, "pdfcmn95.dll"},
215     };
216
217     if (entry) return entry;
218
219     num_tests = (sizeof(monitor_table)/sizeof(struct monitor_entry));
220
221     /* cleanup */
222     DeleteMonitorA(NULL, env_x64, winetest);
223     DeleteMonitorA(NULL, env_x86, winetest);
224     DeleteMonitorA(NULL, env_win9x_case, winetest);
225
226     /* find a usable monitor from the table */
227     mi2a.pName = winetest;
228     while ((entry == NULL) && (i < num_tests)) {
229         entry = &monitor_table[i];
230         i++;
231         mi2a.pEnvironment = entry->env;
232         mi2a.pDLLName = entry->dllname;
233
234         if (AddMonitorA(NULL, 2, (LPBYTE) &mi2a)) {
235             /* we got one */
236             trace("using '%s', '%s'\n", entry->env, entry->dllname);
237             DeleteMonitorA(NULL, entry->env, winetest);
238         }
239         else
240         {
241             entry = NULL;
242         }
243     }
244     return entry;
245 }
246
247
248 /* ########################### */
249
250 static void find_local_server(VOID)
251 {
252     static  char    buffer[MAX_PATH];
253     DWORD   res;
254     DWORD   size;
255
256     size = sizeof(buffer) - 3 ;
257     buffer[0] = '\\';
258     buffer[1] = '\\';
259     buffer[2] = '\0';
260
261     SetLastError(0xdeadbeef);
262     res = GetComputerNameA(&buffer[2], &size);
263     trace("returned %d with %d and %d: '%s'\n", res, GetLastError(), size, buffer);
264
265     ok( res != 0, "returned %d with %d and %d: '%s' (expected '!= 0')\n",
266         res, GetLastError(), size, buffer);
267
268     if (res) local_server = buffer;
269 }
270
271 /* ########################### */
272
273 static void find_tempfile(VOID)
274 {
275     static CHAR buffer_dirA[MAX_PATH];
276     static CHAR buffer_fileA[MAX_PATH];
277     static WCHAR buffer_dirW[MAX_PATH];
278     static WCHAR buffer_fileW[MAX_PATH];
279     DWORD   res;
280     int     resint;
281
282     memset(buffer_dirA, 0, MAX_PATH - 1);
283     buffer_dirA[MAX_PATH - 1] = '\0';
284     SetLastError(0xdeadbeef);
285     res = GetTempPathA(MAX_PATH, buffer_dirA);
286     ok(res, "returned %u with %u and '%s' (expected '!= 0')\n", res, GetLastError(), buffer_dirA);
287     if (res == 0) return;
288
289     memset(buffer_fileA, 0, MAX_PATH - 1);
290     buffer_fileA[MAX_PATH - 1] = '\0';
291     SetLastError(0xdeadbeef);
292     res = GetTempFileNameA(buffer_dirA, winetest, 0, buffer_fileA);
293     ok(res, "returned %u with %u and '%s' (expected '!= 0')\n", res, GetLastError(), buffer_fileA);
294     if (res == 0) return;
295
296     SetLastError(0xdeadbeef);
297     resint = MultiByteToWideChar(CP_ACP, 0, buffer_dirA, -1, buffer_dirW, MAX_PATH);
298     ok(res, "returned %u with %u (expected '!= 0')\n", resint, GetLastError());
299     if (resint == 0) return;
300
301     SetLastError(0xdeadbeef);
302     resint = MultiByteToWideChar(CP_ACP, 0, buffer_fileA, -1, buffer_fileW, MAX_PATH);
303     ok(res, "returned %u with %u (expected '!= 0')\n", resint, GetLastError());
304     if (resint == 0) return;
305
306     tempdirA  = buffer_dirA;
307     tempfileA = buffer_fileA;
308     tempdirW  = buffer_dirW;
309     tempfileW = buffer_fileW;
310     trace("tempfile: '%s'\n", tempfileA);
311 }
312
313 /* ########################### */
314
315 static void test_AddMonitor(void)
316 {
317     MONITOR_INFO_2A mi2a; 
318     struct  monitor_entry * entry = NULL;
319     DWORD   res;
320
321     entry = find_installed_monitor();
322
323     SetLastError(MAGIC_DEAD);
324     res = AddMonitorA(NULL, 1, NULL);
325     ok(!res && (GetLastError() == ERROR_INVALID_LEVEL), 
326         "returned %d with %d (expected '0' with ERROR_INVALID_LEVEL)\n",
327         res, GetLastError());
328
329     SetLastError(MAGIC_DEAD);
330     res = AddMonitorA(NULL, 3, NULL);
331     ok(!res && (GetLastError() == ERROR_INVALID_LEVEL), 
332         "returned %d with %d (expected '0' with ERROR_INVALID_LEVEL)\n",
333         res, GetLastError());
334
335     if (0)
336     {
337     /* This test crash with win9x on vmware (works with win9x on qemu 0.8.1) */
338     SetLastError(MAGIC_DEAD);
339     res = AddMonitorA(NULL, 2, NULL);
340     /* NT: unchanged,  9x: ERROR_PRIVILEGE_NOT_HELD */
341     ok(!res &&
342         ((GetLastError() == MAGIC_DEAD) ||
343          (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)), 
344         "returned %d with %d (expected '0' with: MAGIC_DEAD or "
345         "ERROR_PRIVILEGE_NOT_HELD)\n", res, GetLastError());
346     }
347
348     ZeroMemory(&mi2a, sizeof(MONITOR_INFO_2A));
349     SetLastError(MAGIC_DEAD);
350     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
351     RETURN_ON_DEACTIVATED_SPOOLER(res)
352     RETURN_ON_ACCESS_DENIED(res)
353
354     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_INVALID_ENVIRONMENT */
355     ok(!res && ((GetLastError() == ERROR_INVALID_PARAMETER) ||
356                 (GetLastError() == ERROR_INVALID_ENVIRONMENT)), 
357         "returned %d with %d (expected '0' with: ERROR_INVALID_PARAMETER or "
358         "ERROR_INVALID_ENVIRONMENT)\n", res, GetLastError());
359
360     if (!entry) {
361         skip("No usable Monitor found\n");
362         return;
363     }
364
365     if (0)
366     {
367     /* The Test is deactivated, because when mi2a.pName is NULL, the subkey
368        HKLM\System\CurrentControlSet\Control\Print\Monitors\C:\WINDOWS\SYSTEM
369        or HKLM\System\CurrentControlSet\Control\Print\Monitors\ì
370        is created on win9x and we do not want to hit this bug here. */
371
372     mi2a.pEnvironment = entry->env;
373     SetLastError(MAGIC_DEAD);
374     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
375     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_PRIVILEGE_NOT_HELD */
376     }
377
378     mi2a.pEnvironment = entry->env;
379     mi2a.pName = empty;
380     SetLastError(MAGIC_DEAD);
381     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
382     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_PRIVILEGE_NOT_HELD */
383     ok( !res &&
384         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
385          (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)), 
386         "returned %d with %d (expected '0' with: ERROR_INVALID_PARAMETER or "
387         "ERROR_PRIVILEGE_NOT_HELD)\n",
388         res, GetLastError());
389
390     mi2a.pName = winetest;
391     SetLastError(MAGIC_DEAD);
392     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
393     /* NT: ERROR_INVALID_PARAMETER,  9x: ERROR_PRIVILEGE_NOT_HELD */
394     ok( !res &&
395         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
396          (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)), 
397         "returned %d with %d (expected '0' with: ERROR_INVALID_PARAMETER or "
398         "ERROR_PRIVILEGE_NOT_HELD)\n",
399         res, GetLastError());
400
401     mi2a.pDLLName = empty;
402     SetLastError(MAGIC_DEAD);
403     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
404     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
405         "returned %d with %d (expected '0' with ERROR_INVALID_PARAMETER)\n",
406         res, GetLastError());
407
408     mi2a.pDLLName = does_not_exist_dll;
409     SetLastError(MAGIC_DEAD);
410     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
411     /* NT: ERROR_MOD_NOT_FOUND,  9x: ERROR_INVALID_PARAMETER */
412     ok( !res &&
413         ((GetLastError() == ERROR_MOD_NOT_FOUND) ||
414         (GetLastError() == ERROR_INVALID_PARAMETER)),
415         "returned %d with %d (expected '0' with: ERROR_MOD_NOT_FOUND or "
416         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
417
418     mi2a.pDLLName = version_dll;
419     SetLastError(MAGIC_DEAD);
420     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
421     /* NT: ERROR_PROC_NOT_FOUND,  9x: ERROR_INVALID_PARAMETER */
422     ok( !res &&
423         ((GetLastError() == ERROR_PROC_NOT_FOUND) ||
424         (GetLastError() == ERROR_INVALID_PARAMETER)),
425         "returned %d with %d (expected '0' with: ERROR_PROC_NOT_FOUND or "
426         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
427     if (res) DeleteMonitorA(NULL, entry->env, winetest);
428
429    /* Test AddMonitor with real options */
430     mi2a.pDLLName = entry->dllname;
431     SetLastError(MAGIC_DEAD);
432     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
433     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
434
435     /* add a monitor twice */
436     SetLastError(MAGIC_DEAD);
437     res = AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
438     /* NT: ERROR_PRINT_MONITOR_ALREADY_INSTALLED (3006), 9x: ERROR_ALREADY_EXISTS (183) */
439     ok( !res &&
440         ((GetLastError() == ERROR_PRINT_MONITOR_ALREADY_INSTALLED) ||
441         (GetLastError() == ERROR_ALREADY_EXISTS)), 
442         "returned %d with %d (expected '0' with: "
443         "ERROR_PRINT_MONITOR_ALREADY_INSTALLED or ERROR_ALREADY_EXISTS)\n",
444         res, GetLastError());
445
446     DeleteMonitorA(NULL, entry->env, winetest);
447     SetLastError(MAGIC_DEAD);
448     res = AddMonitorA(empty, 2, (LPBYTE) &mi2a);
449     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
450
451     /* cleanup */
452     DeleteMonitorA(NULL, entry->env, winetest);
453
454 }
455
456 /* ########################### */
457
458 static void test_AddPort(void)
459 {
460     DWORD   res;
461
462     SetLastError(0xdeadbeef);
463     res = AddPortA(NULL, 0, NULL);
464     RETURN_ON_DEACTIVATED_SPOOLER(res)
465     /* NT: RPC_X_NULL_REF_POINTER, 9x: ERROR_INVALID_PARAMETER */
466     ok( !res && ((GetLastError() == RPC_X_NULL_REF_POINTER) || 
467                  (GetLastError() == ERROR_INVALID_PARAMETER)),
468         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
469         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
470
471
472     SetLastError(0xdeadbeef);
473     res = AddPortA(NULL, 0, empty);
474     /* Allowed only for (Printer-)Administrators */
475     RETURN_ON_ACCESS_DENIED(res)
476
477     /* XP: ERROR_NOT_SUPPORTED, NT351 and 9x: ERROR_INVALID_PARAMETER */
478     ok( !res && ((GetLastError() == ERROR_NOT_SUPPORTED) || 
479                  (GetLastError() == ERROR_INVALID_PARAMETER)),
480         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
481         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
482
483
484     SetLastError(0xdeadbeef);
485     res = AddPortA(NULL, 0, does_not_exist);
486     /* XP: ERROR_NOT_SUPPORTED, NT351 and 9x: ERROR_INVALID_PARAMETER */
487     ok( !res && ((GetLastError() == ERROR_NOT_SUPPORTED) || 
488                  (GetLastError() == ERROR_INVALID_PARAMETER)),
489         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
490         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
491
492 }
493
494 /* ########################### */
495
496 static void test_AddPortEx(void)
497 {
498     PORT_INFO_2A pi;
499     DWORD   res;
500
501
502     if (!pAddPortExA) {
503         win_skip("AddPortEx not supported\n");
504         return;
505     }
506
507     /* start test with a clean system */
508     DeletePortA(NULL, 0, tempfileA);
509
510     pi.pPortName = tempfileA;
511     SetLastError(0xdeadbeef);
512     res = pAddPortExA(NULL, 1, (LPBYTE) &pi, LocalPortA);
513     RETURN_ON_DEACTIVATED_SPOOLER(res)
514
515     /* Allowed only for (Printer-)Administrators.
516        W2K+XP: ERROR_INVALID_PARAMETER  */
517     if (!res && (GetLastError() == ERROR_INVALID_PARAMETER)) {
518         skip("ACCESS_DENIED (ERROR_INVALID_PARAMETER)\n");
519         return;
520     }
521     ok( res, "got %u with %u (expected '!= 0')\n", res, GetLastError());
522
523     /* Add a port, that already exist */
524     SetLastError(0xdeadbeef);
525     res = pAddPortExA(NULL, 1, (LPBYTE) &pi, LocalPortA);
526     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
527         "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
528         res, GetLastError());
529     DeletePortA(NULL, 0, tempfileA);
530
531
532     /* the Monitorname must match */
533     SetLastError(0xdeadbeef);
534     res = pAddPortExA(NULL, 1, (LPBYTE) &pi, NULL);
535     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
536         "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
537         res, GetLastError());
538     if (res) DeletePortA(NULL, 0, tempfileA);
539
540     SetLastError(0xdeadbeef);
541     res = pAddPortExA(NULL, 1, (LPBYTE) &pi, empty);
542     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
543         "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
544         res, GetLastError());
545     if (res) DeletePortA(NULL, 0, tempfileA);
546
547     SetLastError(0xdeadbeef);
548     res = pAddPortExA(NULL, 1, (LPBYTE) &pi, does_not_exist);
549     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
550         "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
551         res, GetLastError());
552     if (res) DeletePortA(NULL, 0, tempfileA);
553
554
555     /* We need a Portname */
556     SetLastError(0xdeadbeef);
557     res = pAddPortExA(NULL, 1, NULL, LocalPortA);
558     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
559         "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
560         res, GetLastError());
561
562     pi.pPortName = NULL;
563     SetLastError(0xdeadbeef);
564     res = pAddPortExA(NULL, 1, (LPBYTE) &pi, LocalPortA);
565     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
566         "got %u with %u (expected '0' with ERROR_INVALID_PARAMETER)\n",
567         res, GetLastError());
568     if (res) DeletePortA(NULL, 0, tempfileA);
569
570
571     /*  level 2 is documented as supported for Printmonitors,
572         but that is not supported for "Local Port" (localspl.dll) and
573         AddPortEx fails with ERROR_INVALID_LEVEL */
574
575     pi.pPortName = tempfileA;
576     pi.pMonitorName = LocalPortA;
577     pi.pDescription = winetest;
578     pi.fPortType = PORT_TYPE_WRITE;
579
580     SetLastError(0xdeadbeef);
581     res = pAddPortExA(NULL, 2, (LPBYTE) &pi, LocalPortA);
582     ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
583         "got %u with %u (expected '0' with ERROR_INVALID_LEVEL)\n",
584         res, GetLastError());
585     if (res) DeletePortA(NULL, 0, tempfileA);
586
587
588     /* invalid levels */
589     SetLastError(0xdeadbeef);
590     res = pAddPortExA(NULL, 0, (LPBYTE) &pi, LocalPortA);
591     ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
592         "got %u with %u (expected '0' with ERROR_INVALID_LEVEL)\n",
593         res, GetLastError());
594
595     SetLastError(0xdeadbeef);
596     res = pAddPortExA(NULL, 3, (LPBYTE) &pi, LocalPortA);
597     ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
598         "got %u with %u (expected '0' with ERROR_INVALID_LEVEL)\n",
599         res, GetLastError());
600
601
602     /* cleanup */
603     DeletePortA(NULL, 0, tempfileA);
604
605 }
606
607 /* ########################### */
608
609 static void test_ConfigurePort(void)
610 {
611     DWORD   res;
612
613
614     SetLastError(0xdeadbeef);
615     res = ConfigurePortA(NULL, 0, NULL);
616     RETURN_ON_DEACTIVATED_SPOOLER(res)
617     /* NT: RPC_X_NULL_REF_POINTER, 9x: ERROR_INVALID_PARAMETER */
618     ok( !res && ((GetLastError() == RPC_X_NULL_REF_POINTER) || 
619                  (GetLastError() == ERROR_INVALID_PARAMETER)),
620         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
621         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
622
623     SetLastError(0xdeadbeef);
624     res = ConfigurePortA(NULL, 0, empty);
625     /* Allowed only for (Printer-)Administrators */
626     RETURN_ON_ACCESS_DENIED(res)
627
628     /* XP: ERROR_NOT_SUPPORTED, NT351 and 9x: ERROR_INVALID_PARAMETER */
629     ok( !res && ((GetLastError() == ERROR_NOT_SUPPORTED) || 
630                  (GetLastError() == ERROR_INVALID_PARAMETER)),
631         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
632         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
633
634
635     SetLastError(0xdeadbeef);
636     res = ConfigurePortA(NULL, 0, does_not_exist);
637     /* XP: ERROR_NOT_SUPPORTED, NT351 and 9x: ERROR_INVALID_PARAMETER */
638     ok( !res && ((GetLastError() == ERROR_NOT_SUPPORTED) || 
639                  (GetLastError() == ERROR_INVALID_PARAMETER)),
640         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
641         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
642
643
644     /*  Testing-Results:
645         - Case of Portnames is ignored 
646         - Portname without ":" => NT: ERROR_NOT_SUPPORTED, 9x: Dialog comes up
647         - Empty Servername (LPT1:) => NT: ERROR_NOT_SUPPORTED, 9x: Dialog comes up
648
649         - Port not present =>  9x: ERROR_INVALID_PARAMETER, NT:ERROR_NOT_SUPPORTED
650         - "FILE:" => 9x:Success, NT:ERROR_CANCELED
651         - Cancel ("Local Port") => ERROR_CANCELED
652         - Cancel ("Redirected Port") => Success
653     */
654     if (winetest_interactive > 0) {
655         SetLastError(0xdeadbeef);
656         res = ConfigurePortA(NULL, 0, portname_com1);
657         trace("'%s' returned %d with %d\n", portname_com1, res, GetLastError());
658
659         SetLastError(0xdeadbeef);
660         res = ConfigurePortA(NULL, 0, portname_lpt1);
661         trace("'%s' returned %d with %d\n", portname_lpt1, res, GetLastError());
662
663         SetLastError(0xdeadbeef);
664         res = ConfigurePortA(NULL, 0, portname_file);
665         trace("'%s' returned %d with %d\n", portname_file, res, GetLastError());
666     }
667 }
668
669 /* ########################### */
670
671 static void test_DeleteMonitor(void)
672 {
673     MONITOR_INFO_2A         mi2a;
674     struct monitor_entry  * entry = NULL;
675     DWORD                   res;
676
677
678     entry = find_installed_monitor();
679
680     if (!entry) {
681         skip("No usable Monitor found\n");
682         return;
683     }
684
685     mi2a.pName = winetest;
686     mi2a.pEnvironment = entry->env;
687     mi2a.pDLLName = entry->dllname;
688
689     /* Testing DeleteMonitor with real options */
690     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
691
692     SetLastError(MAGIC_DEAD);
693     res = DeleteMonitorA(NULL, entry->env, winetest);
694     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
695
696     /* Delete the Monitor twice */
697     SetLastError(MAGIC_DEAD);
698     res = DeleteMonitorA(NULL, entry->env, winetest);
699     /* NT: ERROR_UNKNOWN_PRINT_MONITOR (3000), 9x: ERROR_INVALID_PARAMETER (87) */
700     ok( !res &&
701         ((GetLastError() == ERROR_UNKNOWN_PRINT_MONITOR) ||
702         (GetLastError() == ERROR_INVALID_PARAMETER)), 
703         "returned %d with %d (expected '0' with: ERROR_UNKNOWN_PRINT_MONITOR"
704         " or ERROR_INVALID_PARAMETER)\n", res, GetLastError());
705
706     /* the environment */
707     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
708     SetLastError(MAGIC_DEAD);
709     res = DeleteMonitorA(NULL, NULL, winetest);
710     ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError());
711
712     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
713     SetLastError(MAGIC_DEAD);
714     res = DeleteMonitorA(NULL, empty, winetest);
715     ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError());
716
717     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
718     SetLastError(MAGIC_DEAD);
719     res = DeleteMonitorA(NULL, invalid_env, winetest);
720     ok( res ||
721         (!res && GetLastError() == ERROR_INVALID_ENVIRONMENT) /* Vista/W2K8 */,
722         "returned %d with %d\n", res, GetLastError());
723
724     /* the monitor-name */
725     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
726     SetLastError(MAGIC_DEAD);
727     res = DeleteMonitorA(NULL, entry->env, NULL);
728     /* NT: ERROR_INVALID_PARAMETER (87),  9x: ERROR_INVALID_NAME (123)*/
729     ok( !res &&
730         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
731         (GetLastError() == ERROR_INVALID_NAME)),
732         "returned %d with %d (expected '0' with: ERROR_INVALID_PARAMETER or "
733         "ERROR_INVALID_NAME)\n", res, GetLastError());
734
735     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
736     SetLastError(MAGIC_DEAD);
737     res = DeleteMonitorA(NULL, entry->env, empty);
738     /* NT: ERROR_INVALID_PARAMETER (87),  9x: ERROR_INVALID_NAME (123)*/
739     ok( !res && 
740         ((GetLastError() == ERROR_INVALID_PARAMETER) ||
741         (GetLastError() == ERROR_INVALID_NAME)),
742         "returned %d with %d (expected '0' with: ERROR_INVALID_PARAMETER or "
743         "ERROR_INVALID_NAME)\n", res, GetLastError());
744
745     AddMonitorA(NULL, 2, (LPBYTE) &mi2a);
746     SetLastError(MAGIC_DEAD);
747     res = DeleteMonitorA(empty, entry->env, winetest);
748     ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError());
749
750     /* cleanup */
751     DeleteMonitorA(NULL, entry->env, winetest);
752 }
753
754 /* ########################### */
755
756 static void test_DeletePort(void)
757 {
758     DWORD   res;
759
760     SetLastError(0xdeadbeef);
761     res = DeletePortA(NULL, 0, NULL);
762     RETURN_ON_DEACTIVATED_SPOOLER(res)
763
764     SetLastError(0xdeadbeef);
765     res = DeletePortA(NULL, 0, empty);
766     /* Allowed only for (Printer-)Administrators */
767     RETURN_ON_ACCESS_DENIED(res)
768
769     /* XP: ERROR_NOT_SUPPORTED, NT351 and 9x: ERROR_INVALID_PARAMETER */
770     ok( !res && ((GetLastError() == ERROR_NOT_SUPPORTED) || 
771                  (GetLastError() == ERROR_INVALID_PARAMETER)),
772         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
773         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
774
775
776     SetLastError(0xdeadbeef);
777     res = DeletePortA(NULL, 0, does_not_exist);
778     /* XP: ERROR_NOT_SUPPORTED, NT351 and 9x: ERROR_INVALID_PARAMETER */
779     ok( !res && ((GetLastError() == ERROR_NOT_SUPPORTED) || 
780                  (GetLastError() == ERROR_INVALID_PARAMETER)),
781         "returned %d with %d (expected '0' with ERROR_NOT_SUPPORTED or "
782         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
783
784 }
785
786 /* ########################### */
787
788 static void test_EnumForms(LPSTR pName)
789 {
790     DWORD   res;
791     HANDLE  hprinter = 0;
792     LPBYTE  buffer;
793     DWORD   cbBuf;
794     DWORD   pcbNeeded;
795     DWORD   pcReturned;
796     DWORD   level;
797     UINT    i;
798     const char *formtype;
799     static const char * const formtypes[] = { "FORM_USER", "FORM_BUILTIN", "FORM_PRINTER", "FORM_flag_unknown" };
800 #define FORMTYPE_MAX 2
801     PFORM_INFO_1A pFI_1a;
802     PFORM_INFO_2A pFI_2a;
803
804     res = OpenPrinter(pName, &hprinter, NULL);
805     RETURN_ON_DEACTIVATED_SPOOLER(res)
806     if (!res || !hprinter)
807     {
808         /* Open the local Prinserver is not supported on win9x */
809         if (pName) skip("Failed to open '%s' (not supported on win9x)\n", pName);
810         return;
811     }
812
813     /* valid levels are 1 and 2 */
814     for(level = 0; level < 4; level++) {
815         cbBuf = 0xdeadbeef;
816         pcReturned = 0xdeadbeef;
817         SetLastError(0xdeadbeef);
818         res = EnumFormsA(hprinter, level, NULL, 0, &cbBuf, &pcReturned);
819
820         /* EnumForms is not implemented in win9x */
821         if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) continue;
822
823         /* EnumForms for the Server not implemented on all NT-Versions */
824         if (!res && (GetLastError() == ERROR_INVALID_HANDLE) && !pName) continue;
825
826         /* Level 2 for EnumForms is not supported on all systems */
827         if (!res && (GetLastError() == ERROR_INVALID_LEVEL) && (level == 2)) continue;
828
829         /* use only a short test, when we test with an invalid level */
830         if(!level || (level > 2)) {
831             ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
832                 (res && (pcReturned == 0)),
833                 "(%d) returned %d with %d and 0x%08x (expected '0' with "
834                 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
835                 level, res, GetLastError(), pcReturned);
836             continue;
837         }
838
839         ok((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
840             "(%d) returned %d with %d (expected '0' with "
841             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
842
843         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf *2);
844         if (buffer == NULL) continue;
845
846         SetLastError(0xdeadbeef);
847         res = EnumFormsA(hprinter, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
848         ok(res, "(%d) returned %d with %d (expected '!=0')\n",
849                 level, res, GetLastError());
850
851         if (winetest_debug > 1) {
852             trace("dumping %d forms level %d\n", pcReturned, level);
853             pFI_1a = (PFORM_INFO_1A)buffer;
854             pFI_2a = (PFORM_INFO_2A)buffer;
855             for (i = 0; i < pcReturned; i++)
856             {
857                 /* first part is same in FORM_INFO_1 and FORM_INFO_2 */
858                 formtype = (pFI_1a->Flags <= FORMTYPE_MAX) ? formtypes[pFI_1a->Flags] : formtypes[3];
859                 trace("%u (%s): %.03fmm x %.03fmm, %s\n", i, pFI_1a->pName,
860                       (float)pFI_1a->Size.cx/1000, (float)pFI_1a->Size.cy/1000, formtype);
861
862                 if (level == 1) pFI_1a ++;
863                 else {
864                     /* output additional FORM_INFO_2 fields */
865                     trace("\tkeyword=%s strtype=%u muidll=%s resid=%u dispname=%s langid=%u\n",
866                           pFI_2a->pKeyword, pFI_2a->StringType, pFI_2a->pMuiDll,
867                           pFI_2a->dwResourceId, pFI_2a->pDisplayName, pFI_2a->wLangId);
868
869                     /* offset pointer pFI_1a by 1*sizeof(FORM_INFO_2A) Bytes */
870                     pFI_2a ++;
871                     pFI_1a = (PFORM_INFO_1A)pFI_2a;
872                 }
873             }
874         }
875
876         SetLastError(0xdeadbeef);
877         res = EnumFormsA(hprinter, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
878         ok( res, "(%d) returned %d with %d (expected '!=0')\n",
879             level, res, GetLastError());
880
881         SetLastError(0xdeadbeef);
882         res = EnumFormsA(hprinter, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
883         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
884             "(%d) returned %d with %d (expected '0' with "
885             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
886
887
888         SetLastError(0xdeadbeef);
889         res = EnumFormsA(hprinter, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
890         ok( !res && (GetLastError() == ERROR_INVALID_USER_BUFFER) ,
891             "(%d) returned %d with %d (expected '0' with "
892             "ERROR_INVALID_USER_BUFFER)\n", level, res, GetLastError());
893
894
895         SetLastError(0xdeadbeef);
896         res = EnumFormsA(hprinter, level, buffer, cbBuf, NULL, &pcReturned);
897         ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER) ,
898             "(%d) returned %d with %d (expected '0' with "
899             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
900
901         SetLastError(0xdeadbeef);
902         res = EnumFormsA(hprinter, level, buffer, cbBuf, &pcbNeeded, NULL);
903         ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER) ,
904             "(%d) returned %d with %d (expected '0' with "
905             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
906
907         SetLastError(0xdeadbeef);
908         res = EnumFormsA(0, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
909         ok( !res && (GetLastError() == ERROR_INVALID_HANDLE) ,
910             "(%d) returned %d with %d (expected '0' with "
911             "ERROR_INVALID_HANDLE)\n", level, res, GetLastError());
912
913         HeapFree(GetProcessHeap(), 0, buffer);
914     } /* for(level ... */
915
916     ClosePrinter(hprinter);
917 }
918
919 /* ########################### */
920
921 static void test_EnumMonitors(void)
922 {
923     DWORD   res;
924     LPBYTE  buffer;
925     DWORD   cbBuf;
926     DWORD   pcbNeeded;
927     DWORD   pcReturned;
928     DWORD   level;
929
930     /* valid levels are 1 and 2 */
931     for(level = 0; level < 4; level++) {
932         cbBuf = MAGIC_DEAD;
933         pcReturned = MAGIC_DEAD;
934         SetLastError(MAGIC_DEAD);
935         res = EnumMonitorsA(NULL, level, NULL, 0, &cbBuf, &pcReturned);
936
937         RETURN_ON_DEACTIVATED_SPOOLER(res)
938
939         /* not implemented yet in wine */
940         if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) continue;
941
942
943         /* use only a short test, when we test with an invalid level */
944         if(!level || (level > 2)) {
945             ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
946                 (res && (pcReturned == 0)),
947                 "(%d) returned %d with %d and 0x%08x (expected '0' with "
948                 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
949                 level, res, GetLastError(), pcReturned);
950             continue;
951         }        
952
953         /* Level 2 is not supported on win9x */
954         if (!res && (GetLastError() == ERROR_INVALID_LEVEL)) {
955             skip("Level %d not supported\n", level);
956             continue;
957         }
958
959         ok((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
960             "(%d) returned %d with %d (expected '0' with "
961             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
962
963         if (!cbBuf) {
964             skip("no valid buffer size returned\n");
965             continue;
966         }
967
968         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf *2);
969         if (buffer == NULL) continue;
970
971         SetLastError(MAGIC_DEAD);
972         pcbNeeded = MAGIC_DEAD;
973         res = EnumMonitorsA(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
974         ok(res, "(%d) returned %d with %d (expected '!=0')\n",
975                 level, res, GetLastError());
976         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n",
977                 level, pcbNeeded, cbBuf);
978         /* We can validate the returned Data with the Registry here */
979
980
981         SetLastError(MAGIC_DEAD);
982         pcReturned = MAGIC_DEAD;
983         pcbNeeded = MAGIC_DEAD;
984         res = EnumMonitorsA(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
985         ok(res, "(%d) returned %d with %d (expected '!=0')\n", level,
986                 res, GetLastError());
987         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n", level,
988                 pcbNeeded, cbBuf);
989
990         SetLastError(MAGIC_DEAD);
991         pcbNeeded = MAGIC_DEAD;
992         res = EnumMonitorsA(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
993         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
994             "(%d) returned %d with %d (expected '0' with "
995             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
996
997         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n", level,
998                 pcbNeeded, cbBuf);
999
1000 /*
1001       Do not add the next test:
1002       w2k+:  RPC_X_NULL_REF_POINTER 
1003       NT3.5: ERROR_INVALID_USER_BUFFER
1004       win9x: crash in winspool.drv
1005
1006       res = EnumMonitorsA(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
1007 */
1008
1009         SetLastError(MAGIC_DEAD);
1010         pcbNeeded = MAGIC_DEAD;
1011         pcReturned = MAGIC_DEAD;
1012         res = EnumMonitorsA(NULL, level, buffer, cbBuf, NULL, &pcReturned);
1013         ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
1014             "(%d) returned %d with %d (expected '!=0' or '0' with "
1015             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
1016
1017         pcbNeeded = MAGIC_DEAD;
1018         pcReturned = MAGIC_DEAD;
1019         SetLastError(MAGIC_DEAD);
1020         res = EnumMonitorsA(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
1021         ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
1022             "(%d) returned %d with %d (expected '!=0' or '0' with "
1023             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
1024
1025         HeapFree(GetProcessHeap(), 0, buffer);
1026     } /* for(level ... */
1027 }
1028
1029 /* ########################### */
1030
1031 static void test_EnumPorts(void)
1032 {
1033     DWORD   res;
1034     DWORD   level;
1035     LPBYTE  buffer;
1036     DWORD   cbBuf;
1037     DWORD   pcbNeeded;
1038     DWORD   pcReturned;
1039
1040     /* valid levels are 1 and 2 */
1041     for(level = 0; level < 4; level++) {
1042
1043         cbBuf = 0xdeadbeef;
1044         pcReturned = 0xdeadbeef;
1045         SetLastError(0xdeadbeef);
1046         res = EnumPortsA(NULL, level, NULL, 0, &cbBuf, &pcReturned);
1047         RETURN_ON_DEACTIVATED_SPOOLER(res)
1048
1049         /* use only a short test, when we test with an invalid level */
1050         if(!level || (level > 2)) {
1051             /* NT: ERROR_INVALID_LEVEL, 9x: success */
1052             ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
1053                 (res && (pcReturned == 0)),
1054                 "(%d) returned %d with %d and 0x%08x (expected '0' with "
1055                 "ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
1056                 level, res, GetLastError(), pcReturned);
1057             continue;
1058         }        
1059
1060         
1061         /* Level 2 is not supported on NT 3.x */
1062         if (!res && (GetLastError() == ERROR_INVALID_LEVEL)) {
1063             skip("Level %d not supported\n", level);
1064             continue;
1065         }
1066
1067         ok((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1068             "(%d) returned %d with %d (expected '0' with "
1069             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
1070
1071         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf *2);
1072         if (buffer == NULL) continue;
1073
1074         pcbNeeded = 0xdeadbeef;
1075         SetLastError(0xdeadbeef);
1076         res = EnumPortsA(NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
1077         ok(res, "(%d) returned %d with %d (expected '!=0')\n", level, res, GetLastError());
1078         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n", level, pcbNeeded, cbBuf);
1079         /* ToDo: Compare the returned Data with the Registry / "win.ini",[Ports] here */
1080
1081         pcbNeeded = 0xdeadbeef;
1082         pcReturned = 0xdeadbeef;
1083         SetLastError(0xdeadbeef);
1084         res = EnumPortsA(NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
1085         ok(res, "(%d) returned %d with %d (expected '!=0')\n", level, res, GetLastError());
1086         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n", level, pcbNeeded, cbBuf);
1087
1088         pcbNeeded = 0xdeadbeef;
1089         SetLastError(0xdeadbeef);
1090         res = EnumPortsA(NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
1091         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1092             "(%d) returned %d with %d (expected '0' with "
1093             "ERROR_INSUFFICIENT_BUFFER)\n", level, res, GetLastError());
1094         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n", level, pcbNeeded, cbBuf);
1095
1096         /*
1097           Do not add this test:
1098           res = EnumPortsA(NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
1099           w2k+:  RPC_X_NULL_REF_POINTER 
1100           NT3.5: ERROR_INVALID_USER_BUFFER
1101           win9x: crash in winspool.drv
1102          */
1103
1104         SetLastError(0xdeadbeef);
1105         res = EnumPorts(NULL, level, buffer, cbBuf, NULL, &pcReturned);
1106         /* NT: RPC_X_NULL_REF_POINTER (1780),  9x: success */
1107         ok( (!res && (GetLastError() == RPC_X_NULL_REF_POINTER) ) ||
1108             ( res && (GetLastError() == ERROR_SUCCESS) ),
1109             "(%d) returned %d with %d (expected '0' with "
1110             "RPC_X_NULL_REF_POINTER or '!=0' with NO_ERROR)\n",
1111             level, res, GetLastError());
1112
1113
1114         SetLastError(0xdeadbeef);
1115         res = EnumPorts(NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
1116         /* NT: RPC_X_NULL_REF_POINTER (1780),  9x: success */
1117         ok( (!res && (GetLastError() == RPC_X_NULL_REF_POINTER) ) ||
1118             ( res && (GetLastError() == ERROR_SUCCESS) ),
1119             "(%d) returned %d with %d (expected '0' with "
1120             "RPC_X_NULL_REF_POINTER or '!=0' with NO_ERROR)\n",
1121             level, res, GetLastError());
1122
1123         HeapFree(GetProcessHeap(), 0, buffer);
1124     }
1125 }
1126
1127 /* ########################### */
1128
1129 static void test_EnumPrinterDrivers(void)
1130 {
1131     static char env_all[] = "all";
1132
1133     DWORD   res;
1134     LPBYTE  buffer;
1135     DWORD   cbBuf;
1136     DWORD   pcbNeeded;
1137     DWORD   pcReturned;
1138     DWORD   level;
1139
1140     /* 1-3 for w95/w98/NT4; 1-3+6 for me; 1-6 for w2k/xp/2003; 1-6+8 for vista */
1141     for(level = 0; level < 10; level++) {
1142         cbBuf = 0xdeadbeef;
1143         pcReturned = 0xdeadbeef;
1144         SetLastError(0xdeadbeef);
1145         res = EnumPrinterDriversA(NULL, NULL, level, NULL, 0, &cbBuf, &pcReturned);
1146         RETURN_ON_DEACTIVATED_SPOOLER(res)
1147
1148         /* use only a short test, when we test with an invalid level */
1149         if(!level || (level == 7) || (level > 8)) {
1150
1151             ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
1152                 (res && (pcReturned == 0)),
1153                 "(%d) got %u with %u and 0x%x "
1154                 "(expected '0' with ERROR_INVALID_LEVEL or '!=0' and 0x0)\n",
1155                 level, res, GetLastError(), pcReturned);
1156             continue;
1157         }
1158
1159         /* some level are not supported in all windows versions */
1160         if (!res && (GetLastError() == ERROR_INVALID_LEVEL)) {
1161             skip("Level %d not supported\n", level);
1162             continue;
1163         }
1164
1165         ok( ((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) ||
1166             (res && (default_printer == NULL)),
1167             "(%u) got %u with %u for %s (expected '0' with "
1168             "ERROR_INSUFFICIENT_BUFFER or '!= 0' without a printer)\n",
1169             level, res, GetLastError(), default_printer);
1170
1171         if (!cbBuf) {
1172             skip("no valid buffer size returned\n");
1173             continue;
1174         }
1175
1176         /* EnumPrinterDriversA returns the same number of bytes as EnumPrinterDriversW */
1177         if (!on_win9x && pEnumPrinterDriversW)
1178         {
1179             DWORD double_needed;
1180             DWORD double_returned;
1181             pEnumPrinterDriversW(NULL, NULL, level, NULL, 0, &double_needed, &double_returned);
1182             ok(double_needed == cbBuf, "level %d: EnumPrinterDriversA returned different size %d than EnumPrinterDriversW (%d)\n", level, cbBuf, double_needed);
1183         }
1184
1185         buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf + 4);
1186         if (buffer == NULL) continue;
1187
1188         SetLastError(0xdeadbeef);
1189         pcbNeeded = 0xdeadbeef;
1190         res = EnumPrinterDriversA(NULL, NULL, level, buffer, cbBuf, &pcbNeeded, &pcReturned);
1191         ok(res, "(%u) got %u with %u (expected '!=0')\n", level, res, GetLastError());
1192         ok(pcbNeeded == cbBuf, "(%d) returned %d (expected %d)\n", level, pcbNeeded, cbBuf);
1193
1194         /* validate the returned Data here */
1195         if (level > 1) {
1196             LPDRIVER_INFO_2A di = (LPDRIVER_INFO_2A) buffer;
1197
1198             ok( strrchr(di->pDriverPath, '\\') != NULL,
1199                 "(%u) got %s for %s (expected a full path)\n",
1200                 level, di->pDriverPath, di->pName);
1201
1202         }
1203
1204         SetLastError(0xdeadbeef);
1205         pcReturned = 0xdeadbeef;
1206         pcbNeeded = 0xdeadbeef;
1207         res = EnumPrinterDriversA(NULL, NULL, level, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
1208         ok(res, "(%u) got %u with %u (expected '!=0')\n", level, res, GetLastError());
1209         ok(pcbNeeded == cbBuf, "(%u) returned %u (expected %u)\n", level, pcbNeeded, cbBuf);
1210
1211         SetLastError(0xdeadbeef);
1212         pcbNeeded = 0xdeadbeef;
1213         res = EnumPrinterDriversA(NULL, NULL, level, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
1214         ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1215             "(%u) got %u with %u (expected '0' with ERROR_INSUFFICIENT_BUFFER)\n",
1216             level, res, GetLastError());
1217         ok(pcbNeeded == cbBuf, "(%u) returned %u (expected %u)\n", level, pcbNeeded, cbBuf);
1218
1219 /*
1220       Do not add the next test:
1221       NT: ERROR_INVALID_USER_BUFFER
1222       win9x: crash or 100% CPU
1223
1224       res = EnumPrinterDriversA(NULL, NULL, level, NULL, cbBuf, &pcbNeeded, &pcReturned);
1225 */
1226
1227         SetLastError(0xdeadbeef);
1228         pcbNeeded = 0xdeadbeef;
1229         pcReturned = 0xdeadbeef;
1230         res = EnumPrinterDriversA(NULL, NULL, level, buffer, cbBuf, NULL, &pcReturned);
1231         ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
1232             "(%u) got %u with %u (expected '!=0' or '0' with "
1233             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
1234
1235         pcbNeeded = 0xdeadbeef;
1236         pcReturned = 0xdeadbeef;
1237         SetLastError(0xdeadbeef);
1238         res = EnumPrinterDriversA(NULL, NULL, level, buffer, cbBuf, &pcbNeeded, NULL);
1239         ok( res || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ,
1240             "(%u) got %u with %u (expected '!=0' or '0' with "
1241             "RPC_X_NULL_REF_POINTER)\n", level, res, GetLastError());
1242
1243         HeapFree(GetProcessHeap(), 0, buffer);
1244     } /* for(level ... */
1245
1246     pcbNeeded = 0;
1247     pcReturned = 0;
1248     SetLastError(0xdeadbeef);
1249     res = EnumPrinterDriversA(NULL, env_all, 1, NULL, 0, &pcbNeeded, &pcReturned);
1250     if (res)
1251     {
1252         skip("no printer drivers found\n");
1253         return;
1254     }
1255     if (GetLastError() == ERROR_INVALID_ENVIRONMENT)
1256     {
1257         win_skip("NT4 and below don't support the 'all' environment value\n");
1258         return;
1259     }
1260     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "unexpected error %u\n", GetLastError());
1261
1262     buffer = HeapAlloc(GetProcessHeap(), 0, pcbNeeded);
1263     res = EnumPrinterDriversA(NULL, env_all, 1, buffer, pcbNeeded, &pcbNeeded, &pcReturned);
1264     ok(res, "EnumPrinterDriversA failed %u\n", GetLastError());
1265     if (res && pcReturned > 0)
1266     {
1267         DRIVER_INFO_1 *di_1 = (DRIVER_INFO_1 *)buffer;
1268         ok((LPBYTE) di_1->pName == NULL || (LPBYTE) di_1->pName < buffer ||
1269             (LPBYTE) di_1->pName >= (LPBYTE)(di_1 + pcReturned),
1270             "Driver Information not in sequence; pName %p, top of data %p\n",
1271             di_1->pName, di_1 + pcReturned);
1272     }
1273
1274     HeapFree(GetProcessHeap(), 0, buffer);
1275 }
1276
1277 /* ########################### */
1278
1279 static void test_EnumPrintProcessors(void)
1280 {
1281     DWORD   res;
1282     LPBYTE  buffer;
1283     DWORD   cbBuf;
1284     DWORD   pcbNeeded;
1285     DWORD   pcReturned;
1286
1287
1288     cbBuf = 0xdeadbeef;
1289     pcReturned = 0xdeadbeef;
1290     SetLastError(0xdeadbeef);
1291     res = EnumPrintProcessorsA(NULL, NULL, 1, NULL, 0, &cbBuf, &pcReturned);
1292     RETURN_ON_DEACTIVATED_SPOOLER(res)
1293
1294     if (res && !cbBuf) {
1295         skip("No Printprocessor installed\n");
1296         return;
1297     }
1298
1299     ok((!res) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1300         "got %u with %u (expected '0' with ERROR_INSUFFICIENT_BUFFER)\n",
1301         res, GetLastError());
1302
1303     buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf + 4);
1304     if (buffer == NULL)
1305         return;
1306
1307     SetLastError(0xdeadbeef);
1308     pcbNeeded = 0xdeadbeef;
1309     res = EnumPrintProcessorsA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded, &pcReturned);
1310     ok(res, "got %u with %u (expected '!=0')\n", res, GetLastError());
1311     /* validate the returned Data here. */
1312
1313
1314     SetLastError(0xdeadbeef);
1315     pcReturned = 0xdeadbeef;
1316     pcbNeeded = 0xdeadbeef;
1317     res = EnumPrintProcessorsA(NULL, NULL, 1, buffer, cbBuf+1, &pcbNeeded, &pcReturned);
1318     ok(res, "got %u with %u (expected '!=0')\n", res, GetLastError());
1319
1320     SetLastError(0xdeadbeef);
1321     pcbNeeded = 0xdeadbeef;
1322     res = EnumPrintProcessorsA(NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded, &pcReturned);
1323     ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1324         "got %u with %u (expected '0' with ERROR_INSUFFICIENT_BUFFER)\n",
1325         res, GetLastError());
1326
1327     /* only level 1 is valid */
1328     if (0) {
1329         /* both tests crash on win98se */
1330         SetLastError(0xdeadbeef);
1331         pcbNeeded = 0xdeadbeef;
1332         pcReturned = 0xdeadbeef;
1333         res = EnumPrintProcessorsA(NULL, NULL, 0, buffer, cbBuf, &pcbNeeded, &pcReturned);
1334         ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
1335             "got %u with %u (expected '0' with ERROR_INVALID_LEVEL)\n",
1336             res, GetLastError());
1337
1338         SetLastError(0xdeadbeef);
1339         pcbNeeded = 0xdeadbeef;
1340         res = EnumPrintProcessorsA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded, &pcReturned);
1341         ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
1342             "got %u with %u (expected '0' with ERROR_INVALID_LEVEL)\n",
1343             res, GetLastError());
1344     }
1345
1346     /* an empty environment is ignored */
1347     SetLastError(0xdeadbeef);
1348     pcbNeeded = 0xdeadbeef;
1349     res = EnumPrintProcessorsA(NULL, empty, 1, buffer, cbBuf, &pcbNeeded, &pcReturned);
1350     ok(res, "got %u with %u (expected '!=0')\n", res, GetLastError());
1351
1352     /* the environment is checked */
1353     SetLastError(0xdeadbeef);
1354     pcbNeeded = 0xdeadbeef;
1355     res = EnumPrintProcessorsA(NULL, invalid_env, 1, buffer, cbBuf, &pcbNeeded, &pcReturned);
1356     /* NT5: ERROR_INVALID_ENVIRONMENT, NT4: res != 0, 9x: ERROR_INVALID_PARAMETER */
1357     ok( broken(res) || /* NT4 */
1358         (GetLastError() == ERROR_INVALID_ENVIRONMENT) ||
1359         (GetLastError() == ERROR_INVALID_PARAMETER),
1360         "got %u with %u (expected '0' with ERROR_INVALID_ENVIRONMENT or "
1361         "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
1362
1363
1364     /* failure-Codes for NULL */
1365     if (0) {
1366         /* this test crash on win98se */
1367         SetLastError(0xdeadbeef);
1368         pcbNeeded = 0xdeadbeef;
1369         pcReturned = 0xdeadbeef;
1370         res = EnumPrintProcessorsA(NULL, NULL, 1, NULL, cbBuf, &pcbNeeded, &pcReturned);
1371         ok( !res && (GetLastError() == ERROR_INVALID_USER_BUFFER) ,
1372             "got %u with %u (expected '0' with ERROR_INVALID_USER_BUFFER)\n",
1373             res, GetLastError());
1374     }
1375
1376     SetLastError(0xdeadbeef);
1377     pcbNeeded = 0xdeadbeef;
1378     pcReturned = 0xdeadbeef;
1379     res = EnumPrintProcessorsA(NULL, NULL, 1, buffer, cbBuf, NULL, &pcReturned);
1380     /* the NULL is ignored on win9x */
1381     ok( broken(res) || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)),
1382         "got %u with %u (expected '0' with RPC_X_NULL_REF_POINTER)\n",
1383         res, GetLastError());
1384
1385     pcbNeeded = 0xdeadbeef;
1386     pcReturned = 0xdeadbeef;
1387     SetLastError(0xdeadbeef);
1388     res = EnumPrintProcessorsA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded, NULL);
1389     /* the NULL is ignored on win9x */
1390     ok( broken(res) || (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)),
1391         "got %u with %u (expected '0' with RPC_X_NULL_REF_POINTER)\n",
1392         res, GetLastError());
1393
1394     HeapFree(GetProcessHeap(), 0, buffer);
1395
1396 }
1397
1398 /* ########################### */
1399
1400 static void test_GetDefaultPrinter(void)
1401 {
1402     BOOL    retval;
1403     DWORD   exact = DEFAULT_PRINTER_SIZE;
1404     DWORD   size;
1405     char    buffer[DEFAULT_PRINTER_SIZE];
1406
1407     if (!pGetDefaultPrinterA)  return;
1408         /* only supported on NT like OSes starting with win2k */
1409
1410     SetLastError(ERROR_SUCCESS);
1411     retval = pGetDefaultPrinterA(buffer, &exact);
1412     if (!retval || !exact || !strlen(buffer) ||
1413         (ERROR_SUCCESS != GetLastError())) {
1414         if ((ERROR_FILE_NOT_FOUND == GetLastError()) ||
1415             (ERROR_INVALID_NAME == GetLastError()))
1416             trace("this test requires a default printer to be set\n");
1417         else {
1418                 ok( 0, "function call GetDefaultPrinterA failed unexpected!\n"
1419                 "function returned %s\n"
1420                 "last error 0x%08x\n"
1421                 "returned buffer size 0x%08x\n"
1422                 "returned buffer content %s\n",
1423                 retval ? "true" : "false", GetLastError(), exact, buffer);
1424         }
1425         return;
1426     }
1427     SetLastError(ERROR_SUCCESS);
1428     retval = pGetDefaultPrinterA(NULL, NULL); 
1429     ok( !retval, "function result wrong! False expected\n");
1430     ok( ERROR_INVALID_PARAMETER == GetLastError(),
1431         "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08x\n",
1432         GetLastError());
1433
1434     SetLastError(ERROR_SUCCESS);
1435     retval = pGetDefaultPrinterA(buffer, NULL); 
1436     ok( !retval, "function result wrong! False expected\n");
1437     ok( ERROR_INVALID_PARAMETER == GetLastError(),
1438         "Last error wrong! ERROR_INVALID_PARAMETER expected, got 0x%08x\n",
1439         GetLastError());
1440
1441     SetLastError(ERROR_SUCCESS);
1442     size = 0;
1443     retval = pGetDefaultPrinterA(NULL, &size); 
1444     ok( !retval, "function result wrong! False expected\n");
1445     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
1446         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08x\n",
1447         GetLastError());
1448     ok( size == exact, "Parameter size wrong! %d expected got %d\n",
1449         exact, size);
1450
1451     SetLastError(ERROR_SUCCESS);
1452     size = DEFAULT_PRINTER_SIZE;
1453     retval = pGetDefaultPrinterA(NULL, &size); 
1454     ok( !retval, "function result wrong! False expected\n");
1455     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
1456         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08x\n",
1457         GetLastError());
1458     ok( size == exact, "Parameter size wrong! %d expected got %d\n",
1459         exact, size);
1460
1461     size = 0;
1462     retval = pGetDefaultPrinterA(buffer, &size); 
1463     ok( !retval, "function result wrong! False expected\n");
1464     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
1465         "Last error wrong! ERROR_INSUFFICIENT_BUFFER expected, got 0x%08x\n",
1466         GetLastError());
1467     ok( size == exact, "Parameter size wrong! %d expected got %d\n",
1468         exact, size);
1469
1470     size = exact;
1471     retval = pGetDefaultPrinterA(buffer, &size); 
1472     ok( retval, "function result wrong! True expected\n");
1473     ok( size == exact, "Parameter size wrong! %d expected got %d\n",
1474         exact, size);
1475 }
1476
1477 static void test_GetPrinterDriverDirectory(void)
1478 {
1479     LPBYTE      buffer = NULL;
1480     DWORD       cbBuf = 0, pcbNeeded = 0;
1481     BOOL        res;
1482
1483
1484     SetLastError(MAGIC_DEAD);
1485     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, 0, &cbBuf);
1486     trace("first call returned 0x%04x, with %d: buffer size 0x%08x\n",
1487     res, GetLastError(), cbBuf);
1488
1489     RETURN_ON_DEACTIVATED_SPOOLER(res)
1490     ok((res == 0) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1491         "returned %d with lasterror=%d (expected '0' with "
1492         "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError());
1493
1494     if (!cbBuf) {
1495         skip("no valid buffer size returned\n");
1496         return;
1497     }
1498
1499     buffer = HeapAlloc( GetProcessHeap(), 0, cbBuf*2);
1500     if (buffer == NULL)  return ;
1501
1502     res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded);
1503     ok( res, "expected result != 0, got %d\n", res);
1504     ok( cbBuf == pcbNeeded, "pcbNeeded set to %d instead of %d\n",
1505                             pcbNeeded, cbBuf);
1506
1507     res = GetPrinterDriverDirectoryA(NULL, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
1508     ok( res, "expected result != 0, got %d\n", res);
1509     ok( cbBuf == pcbNeeded, "pcbNeeded set to %d instead of %d\n",
1510                             pcbNeeded, cbBuf);
1511  
1512     SetLastError(MAGIC_DEAD);
1513     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded);
1514     ok( !res , "expected result == 0, got %d\n", res);
1515     ok( cbBuf == pcbNeeded, "pcbNeeded set to %d instead of %d\n",
1516                             pcbNeeded, cbBuf);
1517     
1518     ok( ERROR_INSUFFICIENT_BUFFER == GetLastError(),
1519         "last error set to %d instead of ERROR_INSUFFICIENT_BUFFER\n",
1520         GetLastError());
1521
1522 /*
1523     Do not add the next test:
1524     XPsp2: crash in this app, when the spooler is not running 
1525     NT3.5: ERROR_INVALID_USER_BUFFER
1526     win9x: ERROR_INVALID_PARAMETER
1527
1528     pcbNeeded = MAGIC_DEAD;
1529     SetLastError(MAGIC_DEAD);
1530     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
1531 */
1532
1533     SetLastError(MAGIC_DEAD);
1534     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, buffer, cbBuf, NULL);
1535     ok( (!res && RPC_X_NULL_REF_POINTER == GetLastError()) || res,
1536          "expected either result == 0 and "
1537          "last error == RPC_X_NULL_REF_POINTER or result != 0 "
1538          "got result %d and last error == %d\n", res, GetLastError());
1539
1540     SetLastError(MAGIC_DEAD);
1541     res = GetPrinterDriverDirectoryA( NULL, NULL, 1, NULL, cbBuf, NULL);
1542     ok(res || (GetLastError() == RPC_X_NULL_REF_POINTER),
1543         "returned %d with %d (expected '!=0' or '0' with "
1544         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError());
1545  
1546  
1547     /* with a valid buffer, but level is too large */
1548     buffer[0] = '\0';
1549     SetLastError(MAGIC_DEAD);
1550     res = GetPrinterDriverDirectoryA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded);
1551
1552     /* Level not checked in win9x and wine:*/
1553     if((res != FALSE) && buffer[0])
1554     {
1555         trace("Level '2' not checked '%s'\n", buffer);
1556     }
1557     else
1558     {
1559         ok( !res && (GetLastError() == ERROR_INVALID_LEVEL),
1560         "returned %d with lasterror=%d (expected '0' with "
1561         "ERROR_INVALID_LEVEL)\n", res, GetLastError());
1562     }
1563
1564     /* printing environments are case insensitive */
1565     /* "Windows 4.0" is valid for win9x and NT */
1566     buffer[0] = '\0';
1567     SetLastError(MAGIC_DEAD);
1568     res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1, 
1569                                         buffer, cbBuf*2, &pcbNeeded);
1570
1571     if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
1572         cbBuf = pcbNeeded;
1573         buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
1574         if (buffer == NULL)  return ;
1575
1576         SetLastError(MAGIC_DEAD);
1577         res = GetPrinterDriverDirectoryA(NULL, env_win9x_case, 1, 
1578                                         buffer, cbBuf*2, &pcbNeeded);
1579     }
1580
1581     ok(res && buffer[0], "returned %d with "
1582         "lasterror=%d and len=%d (expected '1' with 'len > 0')\n", 
1583         res, GetLastError(), lstrlenA((char *)buffer));
1584
1585     buffer[0] = '\0';
1586     SetLastError(MAGIC_DEAD);
1587     res = GetPrinterDriverDirectoryA(NULL, env_x86, 1, 
1588                                         buffer, cbBuf*2, &pcbNeeded);
1589
1590     if(!res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) {
1591         cbBuf = pcbNeeded;
1592         buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, cbBuf*2);
1593         if (buffer == NULL)  return ;
1594
1595         buffer[0] = '\0';
1596         SetLastError(MAGIC_DEAD);
1597         res = GetPrinterDriverDirectoryA(NULL, env_x86, 1, 
1598                                         buffer, cbBuf*2, &pcbNeeded);
1599     }
1600
1601     /* "Windows NT x86" is invalid for win9x */
1602     ok( (res && buffer[0]) ||
1603         (!res && (GetLastError() == ERROR_INVALID_ENVIRONMENT)), 
1604         "returned %d with lasterror=%d and len=%d (expected '!= 0' with "
1605         "'len > 0' or '0' with ERROR_INVALID_ENVIRONMENT)\n",
1606         res, GetLastError(), lstrlenA((char *)buffer));
1607
1608     /* A setup program (PDFCreator_0.8.0) use empty strings */
1609     SetLastError(MAGIC_DEAD);
1610     res = GetPrinterDriverDirectoryA(empty, empty, 1, buffer, cbBuf*2, &pcbNeeded);
1611     ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError() );
1612
1613     SetLastError(MAGIC_DEAD);
1614     res = GetPrinterDriverDirectoryA(NULL, empty, 1, buffer, cbBuf*2, &pcbNeeded);
1615     ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError() );
1616
1617     SetLastError(MAGIC_DEAD);
1618     res = GetPrinterDriverDirectoryA(empty, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
1619     ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError() );
1620
1621     HeapFree( GetProcessHeap(), 0, buffer);
1622 }
1623
1624 /* ##### */
1625
1626 static void test_GetPrintProcessorDirectory(void)
1627 {
1628     LPBYTE      buffer = NULL;
1629     DWORD       cbBuf = 0;
1630     DWORD       pcbNeeded = 0;
1631     BOOL        res;
1632
1633
1634     SetLastError(0xdeadbeef);
1635     res = GetPrintProcessorDirectoryA(NULL, NULL, 1, NULL, 0, &cbBuf);
1636     /* The deactivated Spooler is caught here on NT3.51 */
1637     RETURN_ON_DEACTIVATED_SPOOLER(res)
1638     ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1639         "returned %d with %d (expected '0' with ERROR_INSUFFICIENT_BUFFER)\n",
1640         res, GetLastError());
1641
1642     buffer = HeapAlloc(GetProcessHeap(), 0, cbBuf*2);
1643     if(buffer == NULL)  return;
1644
1645     buffer[0] = '\0';
1646     SetLastError(0xdeadbeef);
1647     res = GetPrintProcessorDirectoryA(NULL, NULL, 1, buffer, cbBuf, &pcbNeeded);
1648     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
1649
1650     SetLastError(0xdeadbeef);
1651     buffer[0] = '\0';
1652     res = GetPrintProcessorDirectoryA(NULL, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
1653     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
1654  
1655     /* Buffer to small */
1656     buffer[0] = '\0';
1657     SetLastError(0xdeadbeef);
1658     res = GetPrintProcessorDirectoryA( NULL, NULL, 1, buffer, cbBuf-1, &pcbNeeded);
1659     ok( !res && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
1660         "returned %d with %d (expected '0' with ERROR_INSUFFICIENT_BUFFER)\n",
1661         res, GetLastError());
1662
1663     if (0)
1664     {
1665     /* XPsp2: the program will crash here, when the spooler is not running  */
1666     /*        GetPrinterDriverDirectory has the same bug */
1667     pcbNeeded = 0;
1668     SetLastError(0xdeadbeef);
1669     res = GetPrintProcessorDirectoryA( NULL, NULL, 1, NULL, cbBuf, &pcbNeeded);
1670     /* NT: ERROR_INVALID_USER_BUFFER, 9x: res != 0  */
1671     ok( (!res && (GetLastError() == ERROR_INVALID_USER_BUFFER)) ||
1672         broken(res),
1673         "returned %d with %d (expected '0' with ERROR_INVALID_USER_BUFFER)\n",
1674         res, GetLastError());
1675     }
1676
1677     buffer[0] = '\0';
1678     SetLastError(0xdeadbeef);
1679     res = GetPrintProcessorDirectoryA( NULL, NULL, 1, buffer, cbBuf, NULL);
1680     /* NT: RPC_X_NULL_REF_POINTER, 9x: res != 0  */
1681     ok( (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ||
1682         broken(res),
1683         "returned %d with %d (expected '0' with RPC_X_NULL_REF_POINTER)\n",
1684         res, GetLastError());
1685
1686     buffer[0] = '\0';
1687     SetLastError(0xdeadbeef);
1688     res = GetPrintProcessorDirectoryA( NULL, NULL, 1, NULL, cbBuf, NULL);
1689     /* NT: RPC_X_NULL_REF_POINTER, 9x: res != 0  */
1690     ok( (!res && (GetLastError() == RPC_X_NULL_REF_POINTER)) ||
1691         broken(res),
1692         "returned %d with %d (expected '0' with RPC_X_NULL_REF_POINTER)\n",
1693         res, GetLastError());
1694
1695     /* with a valid buffer, but level is invalid */
1696     buffer[0] = '\0';
1697     SetLastError(0xdeadbeef);
1698     res = GetPrintProcessorDirectoryA(NULL, NULL, 0, buffer, cbBuf, &pcbNeeded);
1699     /* Level is ignored in win9x*/
1700     ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
1701         broken(res && buffer[0]),
1702         "returned %d with %d (expected '0' with ERROR_INVALID_LEVEL)\n",
1703         res, GetLastError());
1704
1705     buffer[0] = '\0';
1706     SetLastError(0xdeadbeef);
1707     res = GetPrintProcessorDirectoryA(NULL, NULL, 2, buffer, cbBuf, &pcbNeeded);
1708     /* Level is ignored in win9x*/
1709     ok( (!res && (GetLastError() == ERROR_INVALID_LEVEL)) ||
1710         broken(res && buffer[0]),
1711         "returned %d with %d (expected '0' with ERROR_INVALID_LEVEL)\n",
1712         res, GetLastError());
1713
1714     /* Empty environment is the same as the default environment */
1715     buffer[0] = '\0';
1716     SetLastError(0xdeadbeef);
1717     res = GetPrintProcessorDirectoryA(NULL, empty, 1, buffer, cbBuf*2, &pcbNeeded);
1718     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
1719
1720     /* "Windows 4.0" is valid for win9x and NT */
1721     buffer[0] = '\0';
1722     SetLastError(0xdeadbeef);
1723     res = GetPrintProcessorDirectoryA(NULL, env_win9x_case, 1, buffer, cbBuf*2, &pcbNeeded);
1724     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
1725
1726
1727     /* "Windows NT x86" is invalid for win9x */
1728     buffer[0] = '\0';
1729     SetLastError(0xdeadbeef);
1730     res = GetPrintProcessorDirectoryA(NULL, env_x86, 1, buffer, cbBuf*2, &pcbNeeded);
1731     ok( res || (GetLastError() == ERROR_INVALID_ENVIRONMENT), 
1732         "returned %d with %d (expected '!= 0' or '0' with "
1733         "ERROR_INVALID_ENVIRONMENT)\n", res, GetLastError());
1734
1735     /* invalid on all Systems */
1736     buffer[0] = '\0';
1737     SetLastError(0xdeadbeef);
1738     res = GetPrintProcessorDirectoryA(NULL, invalid_env, 1, buffer, cbBuf*2, &pcbNeeded);
1739     ok( !res && (GetLastError() == ERROR_INVALID_ENVIRONMENT), 
1740         "returned %d with %d (expected '0' with ERROR_INVALID_ENVIRONMENT)\n",
1741         res, GetLastError());
1742
1743     /* Empty servername is the same as the local computer */
1744     buffer[0] = '\0';
1745     SetLastError(0xdeadbeef);
1746     res = GetPrintProcessorDirectoryA(empty, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
1747     ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError());
1748
1749     /* invalid on all Systems */
1750     buffer[0] = '\0';
1751     SetLastError(0xdeadbeef);
1752     res = GetPrintProcessorDirectoryA(server_does_not_exist, NULL, 1, buffer, cbBuf*2, &pcbNeeded);
1753     ok( !res, "expected failure\n");
1754     ok( GetLastError() == RPC_S_SERVER_UNAVAILABLE || /* NT */
1755         GetLastError() == ERROR_INVALID_PARAMETER ||  /* 9x */
1756         GetLastError() == RPC_S_INVALID_NET_ADDR,     /* Some Vista */
1757         "unexpected last error %d\n", GetLastError());
1758
1759     HeapFree(GetProcessHeap(), 0, buffer);
1760 }
1761
1762 /* ##### */
1763
1764 static void test_OpenPrinter(void)
1765 {
1766     PRINTER_DEFAULTSA   defaults;
1767     HANDLE              hprinter;
1768     DWORD               res;
1769
1770     SetLastError(MAGIC_DEAD);
1771     res = OpenPrinter(NULL, NULL, NULL);    
1772     /* The deactivated Spooler is caught here on NT3.51 */
1773     RETURN_ON_DEACTIVATED_SPOOLER(res)
1774     ok(!res && (GetLastError() == ERROR_INVALID_PARAMETER),
1775         "returned %d with %d (expected '0' with ERROR_INVALID_PARAMETER)\n",
1776         res, GetLastError());
1777
1778
1779     /* Get Handle for the local Printserver (NT only)*/
1780     hprinter = (HANDLE) MAGIC_DEAD;
1781     SetLastError(MAGIC_DEAD);
1782     res = OpenPrinter(NULL, &hprinter, NULL);
1783     /* The deactivated Spooler is caught here on XPsp2 */
1784     RETURN_ON_DEACTIVATED_SPOOLER(res)
1785     ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
1786         "returned %d with %d (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
1787         res, GetLastError());
1788     if(res) {
1789         ClosePrinter(hprinter);
1790
1791         defaults.pDatatype=NULL;
1792         defaults.pDevMode=NULL;
1793
1794         defaults.DesiredAccess=0;
1795         hprinter = (HANDLE) MAGIC_DEAD;
1796         SetLastError(MAGIC_DEAD);
1797         res = OpenPrinter(NULL, &hprinter, &defaults);
1798         ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError());
1799         if (res) ClosePrinter(hprinter);
1800
1801         defaults.DesiredAccess=-1;
1802         hprinter = (HANDLE) MAGIC_DEAD;
1803         SetLastError(MAGIC_DEAD);
1804         res = OpenPrinter(NULL, &hprinter, &defaults);
1805         todo_wine {
1806         ok(!res && GetLastError() == ERROR_ACCESS_DENIED,
1807             "returned %d with %d (expected '0' with ERROR_ACCESS_DENIED)\n", 
1808             res, GetLastError());
1809         }
1810         if (res) ClosePrinter(hprinter);
1811
1812     }
1813
1814
1815     if (local_server != NULL) {
1816         hprinter = (HANDLE) 0xdeadbeef;
1817         SetLastError(0xdeadbeef);
1818         res = OpenPrinter(local_server, &hprinter, NULL);
1819         ok(res || (!res && GetLastError() == ERROR_INVALID_PARAMETER),
1820             "returned %d with %d (expected '!=0' or '0' with ERROR_INVALID_PARAMETER)\n",
1821             res, GetLastError());
1822         if(res) ClosePrinter(hprinter);
1823     }
1824
1825     /* Invalid Printername */
1826     hprinter = (HANDLE) MAGIC_DEAD;
1827     SetLastError(MAGIC_DEAD);
1828     res = OpenPrinter(illegal_name, &hprinter, NULL);
1829     ok(!res && ((GetLastError() == ERROR_INVALID_PRINTER_NAME) || 
1830                 (GetLastError() == ERROR_INVALID_PARAMETER) ),
1831        "returned %d with %d (expected '0' with: ERROR_INVALID_PARAMETER or"
1832        "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1833     if(res) ClosePrinter(hprinter);
1834
1835     hprinter = (HANDLE) MAGIC_DEAD;
1836     SetLastError(MAGIC_DEAD);
1837     res = OpenPrinter(empty, &hprinter, NULL);
1838     /* NT: ERROR_INVALID_PRINTER_NAME,  9x: ERROR_INVALID_PARAMETER */
1839     ok( !res &&
1840         ((GetLastError() == ERROR_INVALID_PRINTER_NAME) || 
1841         (GetLastError() == ERROR_INVALID_PARAMETER) ),
1842         "returned %d with %d (expected '0' with: ERROR_INVALID_PRINTER_NAME"
1843         " or ERROR_INVALID_PARAMETER)\n", res, GetLastError());
1844     if(res) ClosePrinter(hprinter);
1845
1846
1847     /* Get Handle for the default Printer */
1848     if (default_printer)
1849     {
1850         hprinter = (HANDLE) MAGIC_DEAD;
1851         SetLastError(MAGIC_DEAD);
1852         res = OpenPrinter(default_printer, &hprinter, NULL);
1853         if((!res) && (GetLastError() == RPC_S_SERVER_UNAVAILABLE))
1854         {
1855             trace("The Service 'Spooler' is required for '%s'\n", default_printer);
1856             return;
1857         }
1858         ok(res, "returned %d with %d (expected '!=0')\n", res, GetLastError());
1859         if(res) ClosePrinter(hprinter);
1860
1861         SetLastError(MAGIC_DEAD);
1862         res = OpenPrinter(default_printer, NULL, NULL);
1863         /* NT: FALSE with ERROR_INVALID_PARAMETER, 9x: TRUE */
1864         ok(res || (GetLastError() == ERROR_INVALID_PARAMETER),
1865             "returned %d with %d (expected '!=0' or '0' with "
1866             "ERROR_INVALID_PARAMETER)\n", res, GetLastError());
1867
1868         defaults.pDatatype=NULL;
1869         defaults.pDevMode=NULL;
1870         defaults.DesiredAccess=0;
1871
1872         hprinter = (HANDLE) MAGIC_DEAD;
1873         SetLastError(MAGIC_DEAD);
1874         res = OpenPrinter(default_printer, &hprinter, &defaults);
1875         ok(res || GetLastError() == ERROR_ACCESS_DENIED,
1876             "returned %d with %d (expected '!=0' or '0' with "
1877             "ERROR_ACCESS_DENIED)\n", res, GetLastError());
1878         if(res) ClosePrinter(hprinter);
1879
1880         defaults.pDatatype = empty;
1881
1882         hprinter = (HANDLE) MAGIC_DEAD;
1883         SetLastError(MAGIC_DEAD);
1884         res = OpenPrinter(default_printer, &hprinter, &defaults);
1885         /* stop here, when a remote Printserver has no RPC-Service running */
1886         RETURN_ON_DEACTIVATED_SPOOLER(res)
1887         ok(res || ((GetLastError() == ERROR_INVALID_DATATYPE) ||
1888                    (GetLastError() == ERROR_ACCESS_DENIED)),
1889             "returned %d with %d (expected '!=0' or '0' with: "
1890             "ERROR_INVALID_DATATYPE or ERROR_ACCESS_DENIED)\n",
1891             res, GetLastError());
1892         if(res) ClosePrinter(hprinter);
1893
1894
1895         defaults.pDatatype=NULL;
1896         defaults.DesiredAccess=PRINTER_ACCESS_USE;
1897
1898         hprinter = (HANDLE) MAGIC_DEAD;
1899         SetLastError(MAGIC_DEAD);
1900         res = OpenPrinter(default_printer, &hprinter, &defaults);
1901         ok(res || GetLastError() == ERROR_ACCESS_DENIED,
1902             "returned %d with %d (expected '!=0' or '0' with "
1903             "ERROR_ACCESS_DENIED)\n", res, GetLastError());
1904         if(res) ClosePrinter(hprinter);
1905
1906
1907         defaults.DesiredAccess=PRINTER_ALL_ACCESS;
1908         hprinter = (HANDLE) MAGIC_DEAD;
1909         SetLastError(MAGIC_DEAD);
1910         res = OpenPrinter(default_printer, &hprinter, &defaults);
1911         ok(res || GetLastError() == ERROR_ACCESS_DENIED,
1912             "returned %d with %d (expected '!=0' or '0' with "
1913             "ERROR_ACCESS_DENIED)\n", res, GetLastError());
1914         if(res) ClosePrinter(hprinter);
1915     }
1916
1917 }
1918
1919
1920 static void test_SetDefaultPrinter(void)
1921 {
1922     DWORD   res;
1923     DWORD   size = DEFAULT_PRINTER_SIZE;
1924     CHAR    buffer[DEFAULT_PRINTER_SIZE];
1925     CHAR    org_value[DEFAULT_PRINTER_SIZE];
1926
1927
1928     if (!pSetDefaultPrinterA)  return;
1929         /* only supported on win2k and above */
1930
1931     /* backup the original value */
1932     org_value[0] = '\0';
1933     SetLastError(MAGIC_DEAD);
1934     res = GetProfileStringA("windows", "device", NULL, org_value, size);
1935
1936     /* first part: with the default Printer */
1937     SetLastError(MAGIC_DEAD);
1938     res = pSetDefaultPrinterA("no_printer_with_this_name");
1939
1940     RETURN_ON_DEACTIVATED_SPOOLER(res)
1941     /* spooler is running or we have no spooler here*/
1942
1943     /* Not implemented in wine */
1944     if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
1945         trace("SetDefaultPrinterA() not implemented yet.\n");
1946         return;
1947     }
1948
1949     ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
1950         "returned %d with %d (expected '0' with "
1951         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1952
1953     WriteProfileStringA("windows", "device", org_value);
1954     SetLastError(MAGIC_DEAD);
1955     res = pSetDefaultPrinterA("");
1956     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1957         "returned %d with %d (expected '!=0' or '0' with "
1958         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1959
1960     WriteProfileStringA("windows", "device", org_value);
1961     SetLastError(MAGIC_DEAD);
1962     res = pSetDefaultPrinterA(NULL);
1963     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1964         "returned %d with %d (expected '!=0' or '0' with "
1965         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1966
1967     WriteProfileStringA("windows", "device", org_value);
1968     SetLastError(MAGIC_DEAD);
1969     res = pSetDefaultPrinterA(default_printer);
1970     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1971         "returned %d with %d (expected '!=0' or '0' with "
1972         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1973
1974
1975     /* second part: always without a default Printer */
1976     WriteProfileStringA("windows", "device", NULL);    
1977     SetLastError(MAGIC_DEAD);
1978     res = pSetDefaultPrinterA("no_printer_with_this_name");
1979
1980     ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
1981         "returned %d with %d (expected '0' with "
1982         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1983
1984     WriteProfileStringA("windows", "device", NULL);    
1985     SetLastError(MAGIC_DEAD);
1986     res = pSetDefaultPrinterA("");
1987     /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
1988     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1989          "returned %d with %d (expected '!=0' or '0' with "
1990          "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1991
1992     WriteProfileStringA("windows", "device", NULL);    
1993     SetLastError(MAGIC_DEAD);
1994     res = pSetDefaultPrinterA(NULL);
1995     /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
1996     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1997         "returned %d with %d (expected '!=0' or '0' with "
1998         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1999
2000     WriteProfileStringA("windows", "device", NULL);    
2001     SetLastError(MAGIC_DEAD);
2002     res = pSetDefaultPrinterA(default_printer);
2003     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
2004         "returned %d with %d (expected '!=0' or '0' with "
2005         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
2006
2007     /* restore the original value */
2008     res = pSetDefaultPrinterA(default_printer);          /* the nice way */
2009     WriteProfileStringA("windows", "device", org_value); /* the old way */
2010
2011     buffer[0] = '\0';
2012     SetLastError(MAGIC_DEAD);
2013     res = GetProfileStringA("windows", "device", NULL, buffer, size);
2014     ok(!lstrcmpA(org_value, buffer), "'%s' (expected '%s')\n", buffer, org_value);
2015
2016 }
2017
2018 /* ########################### */
2019
2020 static void test_XcvDataW_MonitorUI(void)
2021 {
2022     DWORD   res;
2023     HANDLE  hXcv;
2024     BYTE    buffer[MAX_PATH + 4];
2025     DWORD   needed;
2026     DWORD   status;
2027     DWORD   len;
2028     PRINTER_DEFAULTSA pd;
2029
2030     /* api is not present before w2k */
2031     if (pXcvDataW == NULL) return;
2032
2033     pd.pDatatype = NULL;
2034     pd.pDevMode  = NULL;
2035     pd.DesiredAccess = SERVER_ACCESS_ADMINISTER;
2036
2037     hXcv = NULL;
2038     SetLastError(0xdeadbeef);
2039     res = OpenPrinter(xcv_localport, &hXcv, &pd);
2040     RETURN_ON_DEACTIVATED_SPOOLER(res)
2041     RETURN_ON_ACCESS_DENIED(res)
2042
2043     ok(res, "returned %d with %u and handle %p (expected '!= 0')\n", res, GetLastError(), hXcv);
2044     if (!res) return;
2045
2046     /* ask for needed size */
2047     needed = (DWORD) 0xdeadbeef;
2048     status = (DWORD) 0xdeadbeef;
2049     SetLastError(0xdeadbeef);
2050     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, NULL, 0, &needed, &status);
2051     ok( res && (status == ERROR_INSUFFICIENT_BUFFER) && (needed <= MAX_PATH),
2052         "returned %d with %u and %u for status %u (expected '!= 0' and "
2053         "'<= MAX_PATH' for status ERROR_INSUFFICIENT_BUFFER)\n",
2054         res, GetLastError(), needed, status);
2055
2056     if (needed > MAX_PATH) {
2057         ClosePrinter(hXcv);
2058         skip("buffer overflow (%u)\n", needed);
2059         return;
2060     }
2061     len = needed;       /* Size is in bytes */
2062
2063     /* the command is required */
2064     needed = (DWORD) 0xdeadbeef;
2065     status = (DWORD) 0xdeadbeef;
2066     SetLastError(0xdeadbeef);
2067     res = pXcvDataW(hXcv, emptyW, NULL, 0, NULL, 0, &needed, &status);
2068     ok( res && (status == ERROR_INVALID_PARAMETER),
2069         "returned %d with %u and %u for status %u (expected '!= 0' with "
2070         "ERROR_INVALID_PARAMETER)\n", res, GetLastError(), needed, status);
2071
2072     needed = (DWORD) 0xdeadbeef;
2073     status = (DWORD) 0xdeadbeef;
2074     SetLastError(0xdeadbeef);
2075     res = pXcvDataW(hXcv, NULL, NULL, 0, buffer, MAX_PATH, &needed, &status);
2076     ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER),
2077         "returned %d with %u and %u for status %u (expected '0' with "
2078         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError(), needed, status);
2079
2080     /* "PDWORD needed" is checked before RPC-Errors */
2081     needed = (DWORD) 0xdeadbeef;
2082     status = (DWORD) 0xdeadbeef;
2083     SetLastError(0xdeadbeef);
2084     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, NULL, &status);
2085     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
2086         "returned %d with %u and %u for status %u (expected '0' with "
2087         "ERROR_INVALID_PARAMETER)\n", res, GetLastError(), needed, status);
2088
2089     needed = (DWORD) 0xdeadbeef;
2090     status = (DWORD) 0xdeadbeef;
2091     SetLastError(0xdeadbeef);
2092     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, NULL, len, &needed, &status);
2093     ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER),
2094         "returned %d with %u and %u for status %u (expected '0' with "
2095         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError(), needed, status);
2096
2097     needed = (DWORD) 0xdeadbeef;
2098     status = (DWORD) 0xdeadbeef;
2099     SetLastError(0xdeadbeef);
2100     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, &needed, NULL);
2101     ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER),
2102         "returned %d with %u and %u for status %u (expected '0' with "
2103         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError(), needed, status);
2104
2105     /* off by one: larger  */
2106     needed = (DWORD) 0xdeadbeef;
2107     status = (DWORD) 0xdeadbeef;
2108     SetLastError(0xdeadbeef);
2109     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len+1, &needed, &status);
2110     ok( res && (status == ERROR_SUCCESS),
2111         "returned %d with %u and %u for status %u (expected '!= 0' for status "
2112         "ERROR_SUCCESS)\n", res, GetLastError(), needed, status);
2113
2114     /* off by one: smaller */
2115     /* the buffer is not modified for NT4, w2k, XP */
2116     needed = (DWORD) 0xdeadbeef;
2117     status = (DWORD) 0xdeadbeef;
2118     SetLastError(0xdeadbeef);
2119     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len-1, &needed, &status);
2120     ok( res && (status == ERROR_INSUFFICIENT_BUFFER),
2121         "returned %d with %u and %u for status %u (expected '!= 0' for status "
2122         "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError(), needed, status);
2123
2124
2125     /* Normal use. The DLL-Name without a Path is returned */
2126     memset(buffer, 0, len);
2127     needed = (DWORD) 0xdeadbeef;
2128     status = (DWORD) 0xdeadbeef;
2129     SetLastError(0xdeadbeef);
2130     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, &needed, &status);
2131     ok( res && (status == ERROR_SUCCESS),
2132         "returned %d with %u and %u for status %u (expected '!= 0' for status "
2133         "ERROR_SUCCESS)\n", res, GetLastError(), needed, status);
2134
2135     ClosePrinter(hXcv);
2136 }
2137
2138 /* ########################### */
2139
2140 static void test_XcvDataW_PortIsValid(void)
2141 {
2142     DWORD   res;
2143     HANDLE  hXcv;
2144     DWORD   needed;
2145     DWORD   status;
2146     PRINTER_DEFAULTSA   pd;
2147
2148     /* api is not present before w2k */
2149     if (pXcvDataW == NULL) return;
2150
2151     pd.pDatatype = NULL;
2152     pd.pDevMode  = NULL;
2153     pd.DesiredAccess = SERVER_ACCESS_ADMINISTER;
2154
2155     hXcv = NULL;
2156     SetLastError(0xdeadbeef);
2157     res = OpenPrinter(xcv_localport, &hXcv, &pd);
2158
2159     RETURN_ON_DEACTIVATED_SPOOLER(res)
2160     RETURN_ON_ACCESS_DENIED(res)
2161
2162     ok(res, "returned %d with %u and handle %p (expected '!= 0')\n", res, GetLastError(), hXcv);
2163     if (!res) return;
2164
2165
2166     /* "PDWORD needed" is always required */
2167     needed = (DWORD) 0xdeadbeef;
2168     status = (DWORD) 0xdeadbeef;
2169     SetLastError(0xdeadbeef);
2170     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, NULL, &status);
2171     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
2172         "returned %d with %u and %u for status %u (expected '!= 0' with ERROR_INVALID_PARAMETER)\n",
2173          res, GetLastError(), needed, status);
2174
2175     /* an empty name is not allowed */
2176     needed = (DWORD) 0xdeadbeef;
2177     status = (DWORD) 0xdeadbeef;
2178     SetLastError(0xdeadbeef);
2179     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) emptyW, sizeof(emptyW), NULL, 0, &needed, &status);
2180     ok( res && ((status == ERROR_FILE_NOT_FOUND) || (status == ERROR_PATH_NOT_FOUND)),
2181         "returned %d with %u and %u for status %u (expected '!= 0' for status: "
2182         "ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND)\n",
2183         res, GetLastError(), needed, status);
2184
2185     /* a directory is not allowed */
2186     needed = (DWORD) 0xdeadbeef;
2187     status = (DWORD) 0xdeadbeef;
2188     SetLastError(0xdeadbeef);
2189     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) tempdirW, (lstrlenW(tempdirW) + 1) * sizeof(WCHAR), NULL, 0, &needed, &status);
2190     /* XP: ERROR_PATH_NOT_FOUND, w2k ERROR_ACCESS_DENIED */
2191     ok( res && ((status == ERROR_PATH_NOT_FOUND) || (status == ERROR_ACCESS_DENIED)),
2192         "returned %d with %u and %u for status %u (expected '!= 0' for status: "
2193         "ERROR_PATH_NOT_FOUND or ERROR_ACCESS_DENIED)\n",
2194         res, GetLastError(), needed, status);
2195
2196     /* more valid well known Ports */
2197     needed = (DWORD) 0xdeadbeef;
2198     status = (DWORD) 0xdeadbeef;
2199     SetLastError(0xdeadbeef);
2200     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, &needed, &status);
2201     ok( res && (status == ERROR_SUCCESS),
2202         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2203         res, GetLastError(), needed, status);
2204
2205     needed = (DWORD) 0xdeadbeef;
2206     status = (DWORD) 0xdeadbeef;
2207     SetLastError(0xdeadbeef);
2208     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt2W, sizeof(portname_lpt2W), NULL, 0, &needed, &status);
2209     ok( res && (status == ERROR_SUCCESS),
2210         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2211         res, GetLastError(), needed, status);
2212
2213     needed = (DWORD) 0xdeadbeef;
2214     status = (DWORD) 0xdeadbeef;
2215     SetLastError(0xdeadbeef);
2216     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_com1W, sizeof(portname_com1W), NULL, 0, &needed, &status);
2217     ok( res && (status == ERROR_SUCCESS),
2218         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2219         res, GetLastError(), needed, status);
2220
2221     needed = (DWORD) 0xdeadbeef;
2222     status = (DWORD) 0xdeadbeef;
2223     SetLastError(0xdeadbeef);
2224     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_com2W, sizeof(portname_com2W), NULL, 0, &needed, &status);
2225     ok( res && (status == ERROR_SUCCESS),
2226         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2227         res, GetLastError(), needed, status);
2228
2229     needed = (DWORD) 0xdeadbeef;
2230     status = (DWORD) 0xdeadbeef;
2231     SetLastError(0xdeadbeef);
2232     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_fileW, sizeof(portname_fileW), NULL, 0, &needed, &status);
2233     ok( res && (status == ERROR_SUCCESS),
2234         "returned %d with %u and %u for status %u (expected '!= 0' with  ERROR_SUCCESS)\n",
2235         res, GetLastError(), needed, status);
2236
2237
2238     /* a normal, writable file is allowed */
2239     needed = (DWORD) 0xdeadbeef;
2240     status = (DWORD) 0xdeadbeef;
2241     SetLastError(0xdeadbeef);
2242     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, &needed, &status);
2243     ok( res && (status == ERROR_SUCCESS),
2244         "returned %d with %u and %u for status %u (expected '!= 0' with ERROR_SUCCESS)\n",
2245         res, GetLastError(), needed, status);
2246
2247     ClosePrinter(hXcv);
2248 }
2249
2250 /* ########################### */
2251
2252 static void test_GetPrinter(void)
2253 {
2254     HANDLE hprn;
2255     BOOL ret;
2256     BYTE *buf;
2257     INT level;
2258     DWORD needed, filled;
2259
2260     if (!default_printer)
2261     {
2262         skip("There is no default printer installed\n");
2263         return;
2264     }
2265
2266     hprn = 0;
2267     ret = OpenPrinter(default_printer, &hprn, NULL);
2268     if (!ret)
2269     {
2270         skip("Unable to open the default printer (%s)\n", default_printer);
2271         return;
2272     }
2273     ok(hprn != 0, "wrong hprn %p\n", hprn);
2274
2275     for (level = 1; level <= 9; level++)
2276     {
2277         SetLastError(0xdeadbeef);
2278         needed = (DWORD)-1;
2279         ret = GetPrinter(hprn, level, NULL, 0, &needed);
2280         if (ret)
2281         {
2282             win_skip("Level %d is not supported on Win9x/WinMe\n", level);
2283             ok(GetLastError() == ERROR_SUCCESS, "wrong error %d\n", GetLastError());
2284             ok(needed == 0,"Expected 0, got %d\n", needed);
2285             continue;
2286         }
2287         ok(!ret, "level %d: GetPrinter should fail\n", level);
2288         /* Not all levels are supported on all Windows-Versions */
2289         if (GetLastError() == ERROR_INVALID_LEVEL ||
2290             GetLastError() == ERROR_NOT_SUPPORTED /* Win9x/WinMe */)
2291         {
2292             skip("Level %d not supported\n", level);
2293             continue;
2294         }
2295         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %d\n", GetLastError());
2296         ok(needed > 0,"not expected needed buffer size %d\n", needed);
2297
2298         /* GetPrinterA returns the same number of bytes as GetPrinterW */
2299         if (!on_win9x && !ret && pGetPrinterW && level != 6 && level != 7)
2300         {
2301             DWORD double_needed;
2302             ret = pGetPrinterW(hprn, level, NULL, 0, &double_needed);
2303             ok(double_needed == needed, "level %d: GetPrinterA returned different size %d than GetPrinterW (%d)\n", level, needed, double_needed);
2304         }
2305
2306         buf = HeapAlloc(GetProcessHeap(), 0, needed);
2307
2308         SetLastError(0xdeadbeef);
2309         filled = -1;
2310         ret = GetPrinter(hprn, level, buf, needed, &filled);
2311         ok(needed == filled, "needed %d != filled %d\n", needed, filled);
2312
2313         if (level == 2)
2314         {
2315             PRINTER_INFO_2 *pi_2 = (PRINTER_INFO_2 *)buf;
2316
2317             ok(pi_2->pPrinterName!= NULL, "not expected NULL ptr\n");
2318             ok(pi_2->pDriverName!= NULL, "not expected NULL ptr\n");
2319
2320             trace("pPrinterName %s\n", pi_2->pPrinterName);
2321             trace("pDriverName %s\n", pi_2->pDriverName);
2322         }
2323
2324         HeapFree(GetProcessHeap(), 0, buf);
2325     }
2326
2327     SetLastError(0xdeadbeef);
2328     ret = ClosePrinter(hprn);
2329     ok(ret, "ClosePrinter error %d\n", GetLastError());
2330 }
2331
2332 /* ########################### */
2333
2334 static void test_GetPrinterData(void)
2335 {
2336     HANDLE hprn = 0;
2337     DWORD res;
2338     DWORD type;
2339     CHAR  buffer[MAX_PATH + 1];
2340     DWORD needed;
2341     DWORD len;
2342
2343     /* ToDo: test parameter validation, test with the default printer */
2344
2345     SetLastError(0xdeadbeef);
2346     res = OpenPrinter(NULL, &hprn, NULL);
2347     if (!res)
2348     {
2349         /* printserver not available on win9x */
2350         if (!on_win9x)
2351             win_skip("Unable to open the printserver: %d\n", GetLastError());
2352         return;
2353     }
2354
2355     memset(buffer, '#', sizeof(buffer));
2356     buffer[MAX_PATH] = 0;
2357     type = 0xdeadbeef;
2358     needed = 0xdeadbeef;
2359     SetLastError(0xdeadbeef);
2360     res = GetPrinterDataA(hprn, defaultspooldirectory, &type, (LPBYTE) buffer, sizeof(buffer), &needed);
2361
2362     len = lstrlenA(buffer) + sizeof(CHAR);
2363     /* NT4 and w2k require a buffer to save the UNICODE result also for the ANSI function */
2364     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2365         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d or %d)\n",
2366         res, type, needed, buffer, len, len * sizeof(WCHAR));
2367
2368     needed = 0xdeadbeef;
2369     SetLastError(0xdeadbeef);
2370     res = GetPrinterDataA(hprn, defaultspooldirectory, NULL, NULL, 0, &needed);
2371     ok( (res == ERROR_MORE_DATA) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2372         "got %d, needed: %d (expected ERROR_MORE_DATA and %d or %d)\n",
2373         res, needed, len, len * sizeof(WCHAR));
2374
2375     /* ToDo: test SPLREG_*  */
2376
2377     SetLastError(0xdeadbeef);
2378     res = ClosePrinter(hprn);
2379     ok(res, "ClosePrinter error %d\n", GetLastError());
2380 }
2381
2382 /* ########################### */
2383
2384 static void test_GetPrinterDataEx(void)
2385 {
2386     HANDLE hprn = 0;
2387     DWORD res;
2388     DWORD type;
2389     CHAR  buffer[MAX_PATH + 1];
2390     DWORD needed;
2391     DWORD len;
2392
2393     /* not present before w2k */
2394     if (!pGetPrinterDataExA) {
2395         win_skip("GetPrinterDataEx not found\n");
2396         return;
2397     }
2398
2399     /* ToDo: test parameter validation, test with the default printer */
2400
2401     SetLastError(0xdeadbeef);
2402     res = OpenPrinter(NULL, &hprn, NULL);
2403     if (!res)
2404     {
2405         win_skip("Unable to open the printserver: %d\n", GetLastError());
2406         return;
2407     }
2408
2409     /* keyname is ignored, when hprn is a HANDLE for a printserver */
2410     memset(buffer, '#', sizeof(buffer));
2411     buffer[MAX_PATH] = 0;
2412     type = 0xdeadbeef;
2413     needed = 0xdeadbeef;
2414     SetLastError(0xdeadbeef);
2415     res = pGetPrinterDataExA(hprn, NULL, defaultspooldirectory, &type,
2416                              (LPBYTE) buffer, sizeof(buffer), &needed);
2417
2418     len = lstrlenA(buffer) + sizeof(CHAR);
2419     /* NT4 and w2k require a buffer to save the UNICODE result also for the ANSI function */
2420     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2421         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d or %d)\n",
2422         res, type, needed, buffer, len, len * sizeof(WCHAR));
2423
2424     memset(buffer, '#', sizeof(buffer));
2425     buffer[MAX_PATH] = 0;
2426     type = 0xdeadbeef;
2427     needed = 0xdeadbeef;
2428     SetLastError(0xdeadbeef);
2429     res = pGetPrinterDataExA(hprn, "", defaultspooldirectory, &type,
2430                              (LPBYTE) buffer, sizeof(buffer), &needed);
2431     len = lstrlenA(buffer) + sizeof(CHAR);
2432     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2433         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d or %d)\n",
2434         res, type, needed, buffer, len, len * sizeof(WCHAR));
2435
2436     memset(buffer, '#', sizeof(buffer));
2437     buffer[MAX_PATH] = 0;
2438     type = 0xdeadbeef;
2439     needed = 0xdeadbeef;
2440     SetLastError(0xdeadbeef);
2441     /* Wine uses GetPrinterDataEx with "PrinterDriverData" to implement GetPrinterData */
2442     res = pGetPrinterDataExA(hprn, "PrinterDriverData", defaultspooldirectory,
2443                              &type, (LPBYTE) buffer, sizeof(buffer), &needed);
2444     len = lstrlenA(buffer) + sizeof(CHAR);
2445     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2446         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d or %d)\n",
2447         res, type, needed, buffer, len, len * sizeof(WCHAR));
2448
2449
2450     memset(buffer, '#', sizeof(buffer));
2451     buffer[MAX_PATH] = 0;
2452     type = 0xdeadbeef;
2453     needed = 0xdeadbeef;
2454     SetLastError(0xdeadbeef);
2455     res = pGetPrinterDataExA(hprn, does_not_exist, defaultspooldirectory, &type,
2456                              (LPBYTE) buffer, sizeof(buffer), &needed);
2457     len = lstrlenA(buffer) + sizeof(CHAR);
2458     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2459         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d or %d)\n",
2460         res, type, needed, buffer, len, len * sizeof(WCHAR));
2461
2462     needed = 0xdeadbeef;
2463     SetLastError(0xdeadbeef);
2464     /* vista and w2k8 have a bug in GetPrinterDataEx:
2465        the current LastError value is returned as result */
2466     res = pGetPrinterDataExA(hprn, NULL, defaultspooldirectory, NULL, NULL, 0, &needed);
2467     ok( ((res == ERROR_MORE_DATA) || broken(res == 0xdeadbeef)) &&
2468         ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2469         "got %d, needed: %d (expected ERROR_MORE_DATA and %d or %d)\n",
2470         res, needed, len, len * sizeof(WCHAR));
2471
2472     needed = 0xdeadbeef;
2473     SetLastError(0xdeaddead);
2474     res = pGetPrinterDataExA(hprn, NULL, defaultspooldirectory, NULL, NULL, 0, &needed);
2475     ok( ((res == ERROR_MORE_DATA) || broken(res == 0xdeaddead)) &&
2476         ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2477         "got %d, needed: %d (expected ERROR_MORE_DATA and %d or %d)\n",
2478         res, needed, len, len * sizeof(WCHAR));
2479
2480     SetLastError(0xdeadbeef);
2481     res = ClosePrinter(hprn);
2482     ok(res, "ClosePrinter error %d\n", GetLastError());
2483 }
2484
2485 /* ########################### */
2486
2487 static void test_GetPrinterDriver(void)
2488 {
2489     HANDLE hprn;
2490     BOOL ret;
2491     BYTE *buf;
2492     INT level;
2493     DWORD needed, filled;
2494
2495     if (!default_printer)
2496     {
2497         skip("There is no default printer installed\n");
2498         return;
2499     }
2500
2501     hprn = 0;
2502     ret = OpenPrinter(default_printer, &hprn, NULL);
2503     if (!ret)
2504     {
2505         skip("Unable to open the default printer (%s)\n", default_printer);
2506         return;
2507     }
2508     ok(hprn != 0, "wrong hprn %p\n", hprn);
2509
2510     for (level = -1; level <= 7; level++)
2511     {
2512         SetLastError(0xdeadbeef);
2513         needed = (DWORD)-1;
2514         ret = GetPrinterDriver(hprn, NULL, level, NULL, 0, &needed);
2515         ok(!ret, "level %d: GetPrinterDriver should fail\n", level);
2516         if (level >= 1 && level <= 6)
2517         {
2518             /* Not all levels are supported on all Windows-Versions */
2519             if(GetLastError() == ERROR_INVALID_LEVEL) continue;
2520             ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %d\n", GetLastError());
2521             ok(needed > 0,"not expected needed buffer size %d\n", needed);
2522         }
2523         else
2524         {
2525             /* ERROR_OUTOFMEMORY found on win9x */
2526             ok( ((GetLastError() == ERROR_INVALID_LEVEL) ||
2527                  (GetLastError() == ERROR_OUTOFMEMORY)),
2528                 "%d: returned %d with %d (expected '0' with: "
2529                 "ERROR_INVALID_LEVEL or ERROR_OUTOFMEMORY)\n",
2530                 level, ret, GetLastError());
2531             /* needed is modified in win9x. The modified Value depends on the
2532                default Printer. testing for "needed == (DWORD)-1" will fail */
2533             continue;
2534         }
2535
2536         /* GetPrinterDriverA returns the same number of bytes as GetPrinterDriverW */
2537         if (!on_win9x && !ret && pGetPrinterDriverW)
2538         {
2539             DWORD double_needed;
2540             ret = pGetPrinterDriverW(hprn, NULL, level, NULL, 0, &double_needed);
2541             ok(double_needed == needed, "GetPrinterDriverA returned different size %d than GetPrinterDriverW (%d)\n", needed, double_needed);
2542         }
2543
2544         buf = HeapAlloc(GetProcessHeap(), 0, needed);
2545
2546         SetLastError(0xdeadbeef);
2547         filled = -1;
2548         ret = GetPrinterDriver(hprn, NULL, level, buf, needed, &filled);
2549         ok(ret, "level %d: GetPrinterDriver error %d\n", level, GetLastError());
2550         ok(needed == filled, "needed %d != filled %d\n", needed, filled);
2551
2552         if (level == 2)
2553         {
2554             DRIVER_INFO_2 *di_2 = (DRIVER_INFO_2 *)buf;
2555             DWORD calculated = sizeof(*di_2);
2556             HANDLE hf;
2557
2558             /* MSDN is wrong: The Drivers on the win9x-CD's have cVersion=0x0400
2559                NT351: 1, NT4.0+w2k(Kernelmode): 2, w2k and above(Usermode): 3  */
2560             ok( (di_2->cVersion <= 3) ||
2561                 (di_2->cVersion == 0x0400), "di_2->cVersion = %d\n", di_2->cVersion);
2562             ok(di_2->pName != NULL, "not expected NULL ptr\n");
2563             ok(di_2->pEnvironment != NULL, "not expected NULL ptr\n");
2564             ok(di_2->pDriverPath != NULL, "not expected NULL ptr\n");
2565             ok(di_2->pDataFile != NULL, "not expected NULL ptr\n");
2566             ok(di_2->pConfigFile != NULL, "not expected NULL ptr\n");
2567
2568             trace("cVersion %d\n", di_2->cVersion);
2569             trace("pName %s\n", di_2->pName);
2570             calculated += strlen(di_2->pName) + 1;
2571             trace("pEnvironment %s\n", di_2->pEnvironment);
2572             calculated += strlen(di_2->pEnvironment) + 1;
2573             trace("pDriverPath %s\n", di_2->pDriverPath);
2574             calculated += strlen(di_2->pDriverPath) + 1;
2575             trace("pDataFile %s\n", di_2->pDataFile);
2576             calculated += strlen(di_2->pDataFile) + 1;
2577             trace("pConfigFile %s\n", di_2->pConfigFile);
2578             calculated += strlen(di_2->pConfigFile) + 1;
2579
2580             hf = CreateFileA(di_2->pDriverPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2581             if(hf != INVALID_HANDLE_VALUE)
2582                 CloseHandle(hf);
2583             todo_wine
2584             ok(hf != INVALID_HANDLE_VALUE, "Could not open %s\n", di_2->pDriverPath);
2585
2586             hf = CreateFileA(di_2->pDataFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2587             if(hf != INVALID_HANDLE_VALUE)
2588                 CloseHandle(hf);
2589             todo_wine
2590             ok(hf != INVALID_HANDLE_VALUE, "Could not open %s\n", di_2->pDataFile);
2591
2592             hf = CreateFileA(di_2->pConfigFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2593             if(hf != INVALID_HANDLE_VALUE)
2594                 CloseHandle(hf);
2595             todo_wine
2596             ok(hf != INVALID_HANDLE_VALUE, "Could not open %s\n", di_2->pConfigFile);
2597
2598             /* XP allocates memory for both ANSI and unicode names */
2599             ok(filled >= calculated,"calculated %d != filled %d\n", calculated, filled);
2600
2601             /* Obscure test - demonstrate that Windows zero fills the buffer, even on failure */
2602             if (di_2->pDataFile)
2603             {
2604                 ret = GetPrinterDriver(hprn, NULL, level, buf, needed - 2, &filled);
2605                 ok(!ret, "level %d: GetPrinterDriver succeeded with less buffer than it should\n", level);
2606                 ok(di_2->pDataFile == NULL ||
2607                    broken(di_2->pDataFile != NULL), /* Win9x/WinMe */
2608                    "Even on failure, GetPrinterDriver clears the buffer to zeros\n");
2609             }
2610         }
2611
2612         HeapFree(GetProcessHeap(), 0, buf);
2613     }
2614
2615     SetLastError(0xdeadbeef);
2616     ret = ClosePrinter(hprn);
2617     ok(ret, "ClosePrinter error %d\n", GetLastError());
2618 }
2619
2620 static void test_DEVMODE(const DEVMODE *dm, LONG dmSize, LPCSTR exp_prn_name)
2621 {
2622     /* On NT3.51, some fields in DEVMODE are empty/zero
2623       (dmDeviceName, dmSpecVersion, dmDriverVersion and dmDriverExtra)
2624        We skip the Tests on this Platform */
2625     if (dm->dmSpecVersion || dm->dmDriverVersion || dm->dmDriverExtra) {
2626     /* The 0-terminated Printername can be larger (MAX_PATH) than CCHDEVICENAME */
2627         ok(!strncmp(exp_prn_name, (LPCSTR)dm->dmDeviceName, CCHDEVICENAME -1) ||
2628            !strncmp(exp_prn_name, (LPCSTR)dm->dmDeviceName, CCHDEVICENAME -2), /* XP+ */
2629             "expected '%s', got '%s'\n", exp_prn_name, dm->dmDeviceName);
2630         ok(dm->dmSize + dm->dmDriverExtra == dmSize,
2631             "%u != %d\n", dm->dmSize + dm->dmDriverExtra, dmSize);
2632     }
2633     trace("dmFields %08x\n", dm->dmFields);
2634 }
2635
2636 static void test_DocumentProperties(void)
2637 {
2638     HANDLE hprn;
2639     LONG dm_size, ret;
2640     DEVMODE *dm;
2641
2642     if (!default_printer)
2643     {
2644         skip("There is no default printer installed\n");
2645         return;
2646     }
2647
2648     hprn = 0;
2649     ret = OpenPrinter(default_printer, &hprn, NULL);
2650     if (!ret)
2651     {
2652         skip("Unable to open the default printer (%s)\n", default_printer);
2653         return;
2654     }
2655     ok(hprn != 0, "wrong hprn %p\n", hprn);
2656
2657     dm_size = DocumentProperties(0, hprn, NULL, NULL, NULL, 0);
2658     trace("DEVMODE required size %d\n", dm_size);
2659     ok(dm_size >= sizeof(DEVMODE), "unexpected DocumentProperties ret value %d\n", dm_size);
2660
2661     dm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dm_size);
2662
2663     ret = DocumentProperties(0, hprn, NULL, dm, dm, DM_OUT_BUFFER);
2664     ok(ret == IDOK, "DocumentProperties ret value %d != expected IDOK\n", ret);
2665
2666     test_DEVMODE(dm, dm_size, default_printer);
2667
2668     HeapFree(GetProcessHeap(), 0, dm);
2669
2670     SetLastError(0xdeadbeef);
2671     ret = ClosePrinter(hprn);
2672     ok(ret, "ClosePrinter error %d\n", GetLastError());
2673 }
2674
2675 static void test_EnumPrinters(void)
2676 {
2677     DWORD neededA, neededW, num;
2678     DWORD ret;
2679
2680     SetLastError(0xdeadbeef);
2681     neededA = -1;
2682     ret = EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &neededA, &num);
2683     RETURN_ON_DEACTIVATED_SPOOLER(ret)
2684     if (!ret)
2685     {
2686         /* We have 1 or more printers */
2687         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "gle %d\n", GetLastError());
2688         ok(neededA > 0, "Expected neededA to show the number of needed bytes\n");
2689     }
2690     else
2691     {
2692         /* We don't have any printers defined */
2693         ok(GetLastError() == S_OK, "gle %d\n", GetLastError());
2694         ok(neededA == 0, "Expected neededA to be zero\n");
2695     }
2696     ok(num == 0, "num %d\n", num);
2697
2698     SetLastError(0xdeadbeef);
2699     neededW = -1;
2700     ret = EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &neededW, &num);
2701     /* EnumPrintersW is not supported on all platforms */
2702     if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2703     {
2704         win_skip("EnumPrintersW is not implemented\n");
2705         return;
2706     }
2707
2708     if (!ret)
2709     {
2710         /* We have 1 or more printers */
2711         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "gle %d\n", GetLastError());
2712         ok(neededW > 0, "Expected neededW to show the number of needed bytes\n");
2713     }
2714     else
2715     {
2716         /* We don't have any printers defined */
2717         ok(GetLastError() == S_OK, "gle %d\n", GetLastError());
2718         ok(neededW == 0, "Expected neededW to be zero\n");
2719     }
2720     ok(num == 0, "num %d\n", num);
2721
2722     /* Outlook2003 relies on the buffer size returned by EnumPrintersA being big enough
2723        to hold the buffer returned by EnumPrintersW */
2724     ok(neededA == neededW, "neededA %d neededW %d\n", neededA, neededW);
2725 }
2726
2727 static void test_DeviceCapabilities(void)
2728 {
2729     HANDLE hComdlg32;
2730     BOOL (WINAPI *pPrintDlgA)(PRINTDLGA *);
2731     PRINTDLGA prn_dlg;
2732     DEVMODE *dm;
2733     DEVNAMES *dn;
2734     const char *driver, *device, *port;
2735     WORD *papers;
2736     POINT *paper_size;
2737     POINTS ext;
2738     struct
2739     {
2740         char name[64];
2741     } *paper_name;
2742     INT n_papers, n_paper_size, n_paper_names, n_copies, ret;
2743     DWORD fields;
2744
2745     hComdlg32 = LoadLibrary("comdlg32.dll");
2746     assert(hComdlg32);
2747     pPrintDlgA = (void *)GetProcAddress(hComdlg32, "PrintDlgA");
2748     assert(pPrintDlgA);
2749
2750     memset(&prn_dlg, 0, sizeof(prn_dlg));
2751     prn_dlg.lStructSize = sizeof(prn_dlg);
2752     prn_dlg.Flags = PD_RETURNDEFAULT;
2753     ret = pPrintDlgA(&prn_dlg);
2754     FreeLibrary(hComdlg32);
2755     if (!ret)
2756     {
2757         skip("PrintDlg returned no default printer\n");
2758         return;
2759     }
2760     ok(prn_dlg.hDevMode != 0, "PrintDlg returned hDevMode == NULL\n");
2761     ok(prn_dlg.hDevNames != 0, "PrintDlg returned hDevNames == NULL\n");
2762
2763     dm = GlobalLock(prn_dlg.hDevMode);
2764     ok(dm != NULL, "GlobalLock(prn_dlg.hDevMode) failed\n");
2765     trace("dmDeviceName \"%s\"\n", dm->dmDeviceName);
2766
2767     dn = GlobalLock(prn_dlg.hDevNames);
2768     ok(dn != NULL, "GlobalLock(prn_dlg.hDevNames) failed\n");
2769     ok(dn->wDriverOffset, "expected not 0 wDriverOffset\n");
2770     ok(dn->wDeviceOffset, "expected not 0 wDeviceOffset\n");
2771     ok(dn->wOutputOffset, "expected not 0 wOutputOffset\n");
2772     ok(dn->wDefault == DN_DEFAULTPRN, "expected DN_DEFAULTPRN got %x\n", dn->wDefault);
2773     driver = (const char *)dn + dn->wDriverOffset;
2774     device = (const char *)dn + dn->wDeviceOffset;
2775     port = (const char *)dn + dn->wOutputOffset;
2776     trace("driver \"%s\" device \"%s\" port \"%s\"\n", driver, device, port);
2777
2778     test_DEVMODE(dm, dm->dmSize + dm->dmDriverExtra, device);
2779
2780     n_papers = DeviceCapabilities(device, port, DC_PAPERS, NULL, NULL);
2781     ok(n_papers > 0, "DeviceCapabilities DC_PAPERS failed\n");
2782     papers = HeapAlloc(GetProcessHeap(), 0, sizeof(*papers) * n_papers);
2783     ret = DeviceCapabilities(device, port, DC_PAPERS, (LPSTR)papers, NULL);
2784     ok(ret == n_papers, "expected %d, got %d\n", n_papers, ret);
2785 #ifdef VERBOSE
2786     for (ret = 0; ret < n_papers; ret++)
2787         trace("papers[%d] = %d\n", ret, papers[ret]);
2788 #endif
2789     HeapFree(GetProcessHeap(), 0, papers);
2790
2791     n_paper_size = DeviceCapabilities(device, port, DC_PAPERSIZE, NULL, NULL);
2792     ok(n_paper_size > 0, "DeviceCapabilities DC_PAPERSIZE failed\n");
2793     ok(n_paper_size == n_papers, "n_paper_size %d != n_papers %d\n", n_paper_size, n_papers);
2794     paper_size = HeapAlloc(GetProcessHeap(), 0, sizeof(*paper_size) * n_paper_size);
2795     ret = DeviceCapabilities(device, port, DC_PAPERSIZE, (LPSTR)paper_size, NULL);
2796     ok(ret == n_paper_size, "expected %d, got %d\n", n_paper_size, ret);
2797 #ifdef VERBOSE
2798     for (ret = 0; ret < n_paper_size; ret++)
2799         trace("paper_size[%d] = %d x %d\n", ret, paper_size[ret].x, paper_size[ret].y);
2800 #endif
2801     HeapFree(GetProcessHeap(), 0, paper_size);
2802
2803     n_paper_names = DeviceCapabilities(device, port, DC_PAPERNAMES, NULL, NULL);
2804     ok(n_paper_names > 0, "DeviceCapabilities DC_PAPERNAMES failed\n");
2805     ok(n_paper_names == n_papers, "n_paper_names %d != n_papers %d\n", n_paper_names, n_papers);
2806     paper_name = HeapAlloc(GetProcessHeap(), 0, sizeof(*paper_name) * n_paper_names);
2807     ret = DeviceCapabilities(device, port, DC_PAPERNAMES, (LPSTR)paper_name, NULL);
2808     ok(ret == n_paper_names, "expected %d, got %d\n", n_paper_names, ret);
2809 #ifdef VERBOSE
2810     for (ret = 0; ret < n_paper_names; ret++)
2811         trace("paper_name[%u] = %s\n", ret, paper_name[ret].name);
2812 #endif
2813     HeapFree(GetProcessHeap(), 0, paper_name);
2814
2815     n_copies = DeviceCapabilities(device, port, DC_COPIES, NULL, dm);
2816     ok(n_copies > 0, "DeviceCapabilities DC_COPIES failed\n");
2817     trace("n_copies = %d\n", n_copies);
2818
2819     /* these capabilities not available on all printer drivers */
2820     if (0)
2821     {
2822         ret = DeviceCapabilities(device, port, DC_MAXEXTENT, NULL, NULL);
2823         ok(ret != -1, "DeviceCapabilities DC_MAXEXTENT failed\n");
2824         ext = MAKEPOINTS(ret);
2825         trace("max ext = %d x %d\n", ext.x, ext.y);
2826
2827         ret = DeviceCapabilities(device, port, DC_MINEXTENT, NULL, NULL);
2828         ok(ret != -1, "DeviceCapabilities DC_MINEXTENT failed\n");
2829         ext = MAKEPOINTS(ret);
2830         trace("min ext = %d x %d\n", ext.x, ext.y);
2831     }
2832
2833     fields = DeviceCapabilities(device, port, DC_FIELDS, NULL, NULL);
2834     ok(fields != (DWORD)-1, "DeviceCapabilities DC_FIELDS failed\n");
2835     todo_wine
2836     ok(fields == (dm->dmFields | DM_FORMNAME) ||
2837         broken(fields == dm->dmFields), /* Win9x/WinMe */
2838         "fields %x != (dm->dmFields | DM_FORMNAME) %lx\n", fields, (dm->dmFields | DM_FORMNAME));
2839
2840     GlobalUnlock(prn_dlg.hDevMode);
2841     GlobalFree(prn_dlg.hDevMode);
2842     GlobalUnlock(prn_dlg.hDevNames);
2843     GlobalFree(prn_dlg.hDevNames);
2844 }
2845
2846 START_TEST(info)
2847 {
2848     hwinspool = GetModuleHandleA("winspool.drv");
2849     pAddPortExA = (void *) GetProcAddress(hwinspool, "AddPortExA");
2850     pEnumPrinterDriversW = (void *) GetProcAddress(hwinspool, "EnumPrinterDriversW");
2851     pGetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "GetDefaultPrinterA");
2852     pGetPrinterDataExA = (void *) GetProcAddress(hwinspool, "GetPrinterDataExA");
2853     pGetPrinterDriverW = (void *) GetProcAddress(hwinspool, "GetPrinterDriverW");
2854     pGetPrinterW = (void *) GetProcAddress(hwinspool, "GetPrinterW");
2855     pSetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "SetDefaultPrinterA");
2856     pXcvDataW = (void *) GetProcAddress(hwinspool, "XcvDataW");
2857
2858     on_win9x = check_win9x();
2859     if (on_win9x)
2860         win_skip("Several W-functions are not available on Win9x/WinMe\n");
2861
2862     find_default_printer();
2863     find_local_server();
2864     find_tempfile();
2865
2866     test_AddMonitor();
2867     test_AddPort();
2868     test_AddPortEx();
2869     test_ConfigurePort();
2870     test_DeleteMonitor();
2871     test_DeletePort();
2872     test_DeviceCapabilities();
2873     test_DocumentProperties();
2874     test_EnumForms(NULL);
2875     if (default_printer) test_EnumForms(default_printer);
2876     test_EnumMonitors();
2877     test_EnumPorts();
2878     test_EnumPrinterDrivers();
2879     test_EnumPrinters();
2880     test_EnumPrintProcessors();
2881     test_GetDefaultPrinter();
2882     test_GetPrinterDriverDirectory();
2883     test_GetPrintProcessorDirectory();
2884     test_OpenPrinter();
2885     test_GetPrinter();
2886     test_GetPrinterData();
2887     test_GetPrinterDataEx();
2888     test_GetPrinterDriver();
2889     test_SetDefaultPrinter();
2890     test_XcvDataW_MonitorUI();
2891     test_XcvDataW_PortIsValid();
2892
2893     /* Cleanup our temporary file */
2894     DeleteFileA(tempfileA);
2895 }