include/msvcrt: Define more CPU control word flags.
[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     if (!default_printer)
1928     {
1929         skip("There is no default printer installed\n");
1930         return;
1931     }
1932
1933     if (!pSetDefaultPrinterA)  return;
1934         /* only supported on win2k and above */
1935
1936     /* backup the original value */
1937     org_value[0] = '\0';
1938     SetLastError(MAGIC_DEAD);
1939     res = GetProfileStringA("windows", "device", NULL, org_value, size);
1940
1941     /* first part: with the default Printer */
1942     SetLastError(MAGIC_DEAD);
1943     res = pSetDefaultPrinterA("no_printer_with_this_name");
1944
1945     RETURN_ON_DEACTIVATED_SPOOLER(res)
1946     /* spooler is running or we have no spooler here*/
1947
1948     /* Not implemented in wine */
1949     if (!res && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
1950         trace("SetDefaultPrinterA() not implemented yet.\n");
1951         return;
1952     }
1953
1954     ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
1955         "returned %d with %d (expected '0' with "
1956         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1957
1958     WriteProfileStringA("windows", "device", org_value);
1959     SetLastError(MAGIC_DEAD);
1960     res = pSetDefaultPrinterA("");
1961     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1962         "returned %d with %d (expected '!=0' or '0' with "
1963         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1964
1965     WriteProfileStringA("windows", "device", org_value);
1966     SetLastError(MAGIC_DEAD);
1967     res = pSetDefaultPrinterA(NULL);
1968     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1969         "returned %d with %d (expected '!=0' or '0' with "
1970         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1971
1972     WriteProfileStringA("windows", "device", org_value);
1973     SetLastError(MAGIC_DEAD);
1974     res = pSetDefaultPrinterA(default_printer);
1975     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1976         "returned %d with %d (expected '!=0' or '0' with "
1977         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1978
1979
1980     /* second part: always without a default Printer */
1981     WriteProfileStringA("windows", "device", NULL);    
1982     SetLastError(MAGIC_DEAD);
1983     res = pSetDefaultPrinterA("no_printer_with_this_name");
1984
1985     ok(!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME),
1986         "returned %d with %d (expected '0' with "
1987         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1988
1989     WriteProfileStringA("windows", "device", NULL);    
1990     SetLastError(MAGIC_DEAD);
1991     res = pSetDefaultPrinterA("");
1992     /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
1993     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
1994          "returned %d with %d (expected '!=0' or '0' with "
1995          "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
1996
1997     WriteProfileStringA("windows", "device", NULL);    
1998     SetLastError(MAGIC_DEAD);
1999     res = pSetDefaultPrinterA(NULL);
2000     /* we get ERROR_INVALID_PRINTER_NAME when no printer is installed */
2001     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
2002         "returned %d with %d (expected '!=0' or '0' with "
2003         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
2004
2005     WriteProfileStringA("windows", "device", NULL);    
2006     SetLastError(MAGIC_DEAD);
2007     res = pSetDefaultPrinterA(default_printer);
2008     ok(res || (!res && (GetLastError() == ERROR_INVALID_PRINTER_NAME)),
2009         "returned %d with %d (expected '!=0' or '0' with "
2010         "ERROR_INVALID_PRINTER_NAME)\n", res, GetLastError());
2011
2012     /* restore the original value */
2013     res = pSetDefaultPrinterA(default_printer);          /* the nice way */
2014     WriteProfileStringA("windows", "device", org_value); /* the old way */
2015
2016     buffer[0] = '\0';
2017     SetLastError(MAGIC_DEAD);
2018     res = GetProfileStringA("windows", "device", NULL, buffer, size);
2019     ok(!lstrcmpA(org_value, buffer), "'%s' (expected '%s')\n", buffer, org_value);
2020
2021 }
2022
2023 /* ########################### */
2024
2025 static void test_XcvDataW_MonitorUI(void)
2026 {
2027     DWORD   res;
2028     HANDLE  hXcv;
2029     BYTE    buffer[MAX_PATH + 4];
2030     DWORD   needed;
2031     DWORD   status;
2032     DWORD   len;
2033     PRINTER_DEFAULTSA pd;
2034
2035     /* api is not present before w2k */
2036     if (pXcvDataW == NULL) return;
2037
2038     pd.pDatatype = NULL;
2039     pd.pDevMode  = NULL;
2040     pd.DesiredAccess = SERVER_ACCESS_ADMINISTER;
2041
2042     hXcv = NULL;
2043     SetLastError(0xdeadbeef);
2044     res = OpenPrinter(xcv_localport, &hXcv, &pd);
2045     RETURN_ON_DEACTIVATED_SPOOLER(res)
2046     RETURN_ON_ACCESS_DENIED(res)
2047
2048     ok(res, "returned %d with %u and handle %p (expected '!= 0')\n", res, GetLastError(), hXcv);
2049     if (!res) return;
2050
2051     /* ask for needed size */
2052     needed = (DWORD) 0xdeadbeef;
2053     status = (DWORD) 0xdeadbeef;
2054     SetLastError(0xdeadbeef);
2055     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, NULL, 0, &needed, &status);
2056     ok( res && (status == ERROR_INSUFFICIENT_BUFFER) && (needed <= MAX_PATH),
2057         "returned %d with %u and %u for status %u (expected '!= 0' and "
2058         "'<= MAX_PATH' for status ERROR_INSUFFICIENT_BUFFER)\n",
2059         res, GetLastError(), needed, status);
2060
2061     if (needed > MAX_PATH) {
2062         ClosePrinter(hXcv);
2063         skip("buffer overflow (%u)\n", needed);
2064         return;
2065     }
2066     len = needed;       /* Size is in bytes */
2067
2068     /* the command is required */
2069     needed = (DWORD) 0xdeadbeef;
2070     status = (DWORD) 0xdeadbeef;
2071     SetLastError(0xdeadbeef);
2072     res = pXcvDataW(hXcv, emptyW, NULL, 0, NULL, 0, &needed, &status);
2073     ok( res && (status == ERROR_INVALID_PARAMETER),
2074         "returned %d with %u and %u for status %u (expected '!= 0' with "
2075         "ERROR_INVALID_PARAMETER)\n", res, GetLastError(), needed, status);
2076
2077     needed = (DWORD) 0xdeadbeef;
2078     status = (DWORD) 0xdeadbeef;
2079     SetLastError(0xdeadbeef);
2080     res = pXcvDataW(hXcv, NULL, NULL, 0, buffer, MAX_PATH, &needed, &status);
2081     ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER),
2082         "returned %d with %u and %u for status %u (expected '0' with "
2083         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError(), needed, status);
2084
2085     /* "PDWORD needed" is checked before RPC-Errors */
2086     needed = (DWORD) 0xdeadbeef;
2087     status = (DWORD) 0xdeadbeef;
2088     SetLastError(0xdeadbeef);
2089     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, NULL, &status);
2090     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
2091         "returned %d with %u and %u for status %u (expected '0' with "
2092         "ERROR_INVALID_PARAMETER)\n", res, GetLastError(), needed, status);
2093
2094     needed = (DWORD) 0xdeadbeef;
2095     status = (DWORD) 0xdeadbeef;
2096     SetLastError(0xdeadbeef);
2097     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, NULL, len, &needed, &status);
2098     ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER),
2099         "returned %d with %u and %u for status %u (expected '0' with "
2100         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError(), needed, status);
2101
2102     needed = (DWORD) 0xdeadbeef;
2103     status = (DWORD) 0xdeadbeef;
2104     SetLastError(0xdeadbeef);
2105     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, &needed, NULL);
2106     ok( !res && (GetLastError() == RPC_X_NULL_REF_POINTER),
2107         "returned %d with %u and %u for status %u (expected '0' with "
2108         "RPC_X_NULL_REF_POINTER)\n", res, GetLastError(), needed, status);
2109
2110     /* off by one: larger  */
2111     needed = (DWORD) 0xdeadbeef;
2112     status = (DWORD) 0xdeadbeef;
2113     SetLastError(0xdeadbeef);
2114     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len+1, &needed, &status);
2115     ok( res && (status == ERROR_SUCCESS),
2116         "returned %d with %u and %u for status %u (expected '!= 0' for status "
2117         "ERROR_SUCCESS)\n", res, GetLastError(), needed, status);
2118
2119     /* off by one: smaller */
2120     /* the buffer is not modified for NT4, w2k, XP */
2121     needed = (DWORD) 0xdeadbeef;
2122     status = (DWORD) 0xdeadbeef;
2123     SetLastError(0xdeadbeef);
2124     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len-1, &needed, &status);
2125     ok( res && (status == ERROR_INSUFFICIENT_BUFFER),
2126         "returned %d with %u and %u for status %u (expected '!= 0' for status "
2127         "ERROR_INSUFFICIENT_BUFFER)\n", res, GetLastError(), needed, status);
2128
2129
2130     /* Normal use. The DLL-Name without a Path is returned */
2131     memset(buffer, 0, len);
2132     needed = (DWORD) 0xdeadbeef;
2133     status = (DWORD) 0xdeadbeef;
2134     SetLastError(0xdeadbeef);
2135     res = pXcvDataW(hXcv, cmd_MonitorUIW, NULL, 0, buffer, len, &needed, &status);
2136     ok( res && (status == ERROR_SUCCESS),
2137         "returned %d with %u and %u for status %u (expected '!= 0' for status "
2138         "ERROR_SUCCESS)\n", res, GetLastError(), needed, status);
2139
2140     ClosePrinter(hXcv);
2141 }
2142
2143 /* ########################### */
2144
2145 static void test_XcvDataW_PortIsValid(void)
2146 {
2147     DWORD   res;
2148     HANDLE  hXcv;
2149     DWORD   needed;
2150     DWORD   status;
2151     PRINTER_DEFAULTSA   pd;
2152
2153     /* api is not present before w2k */
2154     if (pXcvDataW == NULL) return;
2155
2156     pd.pDatatype = NULL;
2157     pd.pDevMode  = NULL;
2158     pd.DesiredAccess = SERVER_ACCESS_ADMINISTER;
2159
2160     hXcv = NULL;
2161     SetLastError(0xdeadbeef);
2162     res = OpenPrinter(xcv_localport, &hXcv, &pd);
2163
2164     RETURN_ON_DEACTIVATED_SPOOLER(res)
2165     RETURN_ON_ACCESS_DENIED(res)
2166
2167     ok(res, "returned %d with %u and handle %p (expected '!= 0')\n", res, GetLastError(), hXcv);
2168     if (!res) return;
2169
2170
2171     /* "PDWORD needed" is always required */
2172     needed = (DWORD) 0xdeadbeef;
2173     status = (DWORD) 0xdeadbeef;
2174     SetLastError(0xdeadbeef);
2175     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, NULL, &status);
2176     ok( !res && (GetLastError() == ERROR_INVALID_PARAMETER),
2177         "returned %d with %u and %u for status %u (expected '!= 0' with ERROR_INVALID_PARAMETER)\n",
2178          res, GetLastError(), needed, status);
2179
2180     /* an empty name is not allowed */
2181     needed = (DWORD) 0xdeadbeef;
2182     status = (DWORD) 0xdeadbeef;
2183     SetLastError(0xdeadbeef);
2184     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) emptyW, sizeof(emptyW), NULL, 0, &needed, &status);
2185     ok( res && ((status == ERROR_FILE_NOT_FOUND) || (status == ERROR_PATH_NOT_FOUND)),
2186         "returned %d with %u and %u for status %u (expected '!= 0' for status: "
2187         "ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND)\n",
2188         res, GetLastError(), needed, status);
2189
2190     /* a directory is not allowed */
2191     needed = (DWORD) 0xdeadbeef;
2192     status = (DWORD) 0xdeadbeef;
2193     SetLastError(0xdeadbeef);
2194     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) tempdirW, (lstrlenW(tempdirW) + 1) * sizeof(WCHAR), NULL, 0, &needed, &status);
2195     /* XP: ERROR_PATH_NOT_FOUND, w2k ERROR_ACCESS_DENIED */
2196     ok( res && ((status == ERROR_PATH_NOT_FOUND) || (status == ERROR_ACCESS_DENIED)),
2197         "returned %d with %u and %u for status %u (expected '!= 0' for status: "
2198         "ERROR_PATH_NOT_FOUND or ERROR_ACCESS_DENIED)\n",
2199         res, GetLastError(), needed, status);
2200
2201     /* more valid well known Ports */
2202     needed = (DWORD) 0xdeadbeef;
2203     status = (DWORD) 0xdeadbeef;
2204     SetLastError(0xdeadbeef);
2205     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt1W, sizeof(portname_lpt1W), NULL, 0, &needed, &status);
2206     ok( res && (status == ERROR_SUCCESS),
2207         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2208         res, GetLastError(), needed, status);
2209
2210     needed = (DWORD) 0xdeadbeef;
2211     status = (DWORD) 0xdeadbeef;
2212     SetLastError(0xdeadbeef);
2213     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_lpt2W, sizeof(portname_lpt2W), NULL, 0, &needed, &status);
2214     ok( res && (status == ERROR_SUCCESS),
2215         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2216         res, GetLastError(), needed, status);
2217
2218     needed = (DWORD) 0xdeadbeef;
2219     status = (DWORD) 0xdeadbeef;
2220     SetLastError(0xdeadbeef);
2221     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_com1W, sizeof(portname_com1W), NULL, 0, &needed, &status);
2222     ok( res && (status == ERROR_SUCCESS),
2223         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2224         res, GetLastError(), needed, status);
2225
2226     needed = (DWORD) 0xdeadbeef;
2227     status = (DWORD) 0xdeadbeef;
2228     SetLastError(0xdeadbeef);
2229     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_com2W, sizeof(portname_com2W), NULL, 0, &needed, &status);
2230     ok( res && (status == ERROR_SUCCESS),
2231         "returned %d with %u and %u for status %u (expected '!= 0' for ERROR_SUCCESS)\n",
2232         res, GetLastError(), needed, status);
2233
2234     needed = (DWORD) 0xdeadbeef;
2235     status = (DWORD) 0xdeadbeef;
2236     SetLastError(0xdeadbeef);
2237     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) portname_fileW, sizeof(portname_fileW), NULL, 0, &needed, &status);
2238     ok( res && (status == ERROR_SUCCESS),
2239         "returned %d with %u and %u for status %u (expected '!= 0' with  ERROR_SUCCESS)\n",
2240         res, GetLastError(), needed, status);
2241
2242
2243     /* a normal, writable file is allowed */
2244     needed = (DWORD) 0xdeadbeef;
2245     status = (DWORD) 0xdeadbeef;
2246     SetLastError(0xdeadbeef);
2247     res = pXcvDataW(hXcv, cmd_PortIsValidW, (PBYTE) tempfileW, (lstrlenW(tempfileW) + 1) * sizeof(WCHAR), NULL, 0, &needed, &status);
2248     ok( res && (status == ERROR_SUCCESS),
2249         "returned %d with %u and %u for status %u (expected '!= 0' with ERROR_SUCCESS)\n",
2250         res, GetLastError(), needed, status);
2251
2252     ClosePrinter(hXcv);
2253 }
2254
2255 /* ########################### */
2256
2257 static void test_GetPrinter(void)
2258 {
2259     HANDLE hprn;
2260     BOOL ret;
2261     BYTE *buf;
2262     INT level;
2263     DWORD needed, filled;
2264
2265     if (!default_printer)
2266     {
2267         skip("There is no default printer installed\n");
2268         return;
2269     }
2270
2271     hprn = 0;
2272     ret = OpenPrinter(default_printer, &hprn, NULL);
2273     if (!ret)
2274     {
2275         skip("Unable to open the default printer (%s)\n", default_printer);
2276         return;
2277     }
2278     ok(hprn != 0, "wrong hprn %p\n", hprn);
2279
2280     for (level = 1; level <= 9; level++)
2281     {
2282         SetLastError(0xdeadbeef);
2283         needed = (DWORD)-1;
2284         ret = GetPrinter(hprn, level, NULL, 0, &needed);
2285         if (ret)
2286         {
2287             win_skip("Level %d is not supported on Win9x/WinMe\n", level);
2288             ok(GetLastError() == ERROR_SUCCESS, "wrong error %d\n", GetLastError());
2289             ok(needed == 0,"Expected 0, got %d\n", needed);
2290             continue;
2291         }
2292         ok(!ret, "level %d: GetPrinter should fail\n", level);
2293         /* Not all levels are supported on all Windows-Versions */
2294         if (GetLastError() == ERROR_INVALID_LEVEL ||
2295             GetLastError() == ERROR_NOT_SUPPORTED /* Win9x/WinMe */)
2296         {
2297             skip("Level %d not supported\n", level);
2298             continue;
2299         }
2300         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %d\n", GetLastError());
2301         ok(needed > 0,"not expected needed buffer size %d\n", needed);
2302
2303         /* GetPrinterA returns the same number of bytes as GetPrinterW */
2304         if (!on_win9x && !ret && pGetPrinterW && level != 6 && level != 7)
2305         {
2306             DWORD double_needed;
2307             ret = pGetPrinterW(hprn, level, NULL, 0, &double_needed);
2308             ok(double_needed == needed, "level %d: GetPrinterA returned different size %d than GetPrinterW (%d)\n", level, needed, double_needed);
2309         }
2310
2311         buf = HeapAlloc(GetProcessHeap(), 0, needed);
2312
2313         SetLastError(0xdeadbeef);
2314         filled = -1;
2315         ret = GetPrinter(hprn, level, buf, needed, &filled);
2316         ok(needed == filled, "needed %d != filled %d\n", needed, filled);
2317
2318         if (level == 2)
2319         {
2320             PRINTER_INFO_2 *pi_2 = (PRINTER_INFO_2 *)buf;
2321
2322             ok(pi_2->pPrinterName!= NULL, "not expected NULL ptr\n");
2323             ok(pi_2->pDriverName!= NULL, "not expected NULL ptr\n");
2324
2325             trace("pPrinterName %s\n", pi_2->pPrinterName);
2326             trace("pDriverName %s\n", pi_2->pDriverName);
2327         }
2328
2329         HeapFree(GetProcessHeap(), 0, buf);
2330     }
2331
2332     SetLastError(0xdeadbeef);
2333     ret = ClosePrinter(hprn);
2334     ok(ret, "ClosePrinter error %d\n", GetLastError());
2335 }
2336
2337 /* ########################### */
2338
2339 static void test_GetPrinterData(void)
2340 {
2341     HANDLE hprn = 0;
2342     DWORD res;
2343     DWORD type;
2344     CHAR  buffer[MAX_PATH + 1];
2345     DWORD needed;
2346     DWORD len;
2347
2348     /* ToDo: test parameter validation, test with the default printer */
2349
2350     SetLastError(0xdeadbeef);
2351     res = OpenPrinter(NULL, &hprn, NULL);
2352     if (!res)
2353     {
2354         /* printserver not available on win9x */
2355         if (!on_win9x)
2356             win_skip("Unable to open the printserver: %d\n", GetLastError());
2357         return;
2358     }
2359
2360     memset(buffer, '#', sizeof(buffer));
2361     buffer[MAX_PATH] = 0;
2362     type = 0xdeadbeef;
2363     needed = 0xdeadbeef;
2364     SetLastError(0xdeadbeef);
2365     res = GetPrinterDataA(hprn, defaultspooldirectory, &type, (LPBYTE) buffer, sizeof(buffer), &needed);
2366
2367     len = lstrlenA(buffer) + sizeof(CHAR);
2368     /* NT4 and w2k require a buffer to save the UNICODE result also for the ANSI function */
2369     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2370         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d)\n",
2371         res, type, needed, buffer, len);
2372
2373     needed = 0xdeadbeef;
2374     SetLastError(0xdeadbeef);
2375     res = GetPrinterDataA(hprn, defaultspooldirectory, NULL, NULL, 0, &needed);
2376     ok( (res == ERROR_MORE_DATA) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2377         "got %d, needed: %d (expected ERROR_MORE_DATA and %d)\n", res, needed, len);
2378
2379     /* ToDo: test SPLREG_*  */
2380
2381     SetLastError(0xdeadbeef);
2382     res = ClosePrinter(hprn);
2383     ok(res, "ClosePrinter error %d\n", GetLastError());
2384 }
2385
2386 /* ########################### */
2387
2388 static void test_GetPrinterDataEx(void)
2389 {
2390     HANDLE hprn = 0;
2391     DWORD res;
2392     DWORD type;
2393     CHAR  buffer[MAX_PATH + 1];
2394     DWORD needed;
2395     DWORD len;
2396
2397     /* not present before w2k */
2398     if (!pGetPrinterDataExA) {
2399         win_skip("GetPrinterDataEx not found\n");
2400         return;
2401     }
2402
2403     /* ToDo: test parameter validation, test with the default printer */
2404
2405     SetLastError(0xdeadbeef);
2406     res = OpenPrinter(NULL, &hprn, NULL);
2407     if (!res)
2408     {
2409         win_skip("Unable to open the printserver: %d\n", GetLastError());
2410         return;
2411     }
2412
2413     /* keyname is ignored, when hprn is a HANDLE for a printserver */
2414     memset(buffer, '#', sizeof(buffer));
2415     buffer[MAX_PATH] = 0;
2416     type = 0xdeadbeef;
2417     needed = 0xdeadbeef;
2418     SetLastError(0xdeadbeef);
2419     res = pGetPrinterDataExA(hprn, NULL, defaultspooldirectory, &type,
2420                              (LPBYTE) buffer, sizeof(buffer), &needed);
2421
2422     len = lstrlenA(buffer) + sizeof(CHAR);
2423     /* NT4 and w2k require a buffer to save the UNICODE result also for the ANSI function */
2424     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2425         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d)\n",
2426         res, type, needed, buffer, len);
2427
2428     memset(buffer, '#', sizeof(buffer));
2429     buffer[MAX_PATH] = 0;
2430     type = 0xdeadbeef;
2431     needed = 0xdeadbeef;
2432     SetLastError(0xdeadbeef);
2433     res = pGetPrinterDataExA(hprn, "", defaultspooldirectory, &type,
2434                              (LPBYTE) buffer, sizeof(buffer), &needed);
2435     len = lstrlenA(buffer) + sizeof(CHAR);
2436     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2437         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d)\n",
2438         res, type, needed, buffer, len);
2439
2440     memset(buffer, '#', sizeof(buffer));
2441     buffer[MAX_PATH] = 0;
2442     type = 0xdeadbeef;
2443     needed = 0xdeadbeef;
2444     SetLastError(0xdeadbeef);
2445     /* Wine uses GetPrinterDataEx with "PrinterDriverData" to implement GetPrinterData */
2446     res = pGetPrinterDataExA(hprn, "PrinterDriverData", defaultspooldirectory,
2447                              &type, (LPBYTE) buffer, sizeof(buffer), &needed);
2448     len = lstrlenA(buffer) + sizeof(CHAR);
2449     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2450         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d)\n",
2451         res, type, needed, buffer, len);
2452
2453
2454     memset(buffer, '#', sizeof(buffer));
2455     buffer[MAX_PATH] = 0;
2456     type = 0xdeadbeef;
2457     needed = 0xdeadbeef;
2458     SetLastError(0xdeadbeef);
2459     res = pGetPrinterDataExA(hprn, does_not_exist, defaultspooldirectory, &type,
2460                              (LPBYTE) buffer, sizeof(buffer), &needed);
2461     len = lstrlenA(buffer) + sizeof(CHAR);
2462     ok( !res && (type == REG_SZ) && ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2463         "got %d, type %d, needed: %d and '%s' (expected ERROR_SUCCESS, REG_SZ and %d)\n",
2464         res, type, needed, buffer, len);
2465
2466     needed = 0xdeadbeef;
2467     SetLastError(0xdeadbeef);
2468     /* vista and w2k8 have a bug in GetPrinterDataEx:
2469        the current LastError value is returned as result */
2470     res = pGetPrinterDataExA(hprn, NULL, defaultspooldirectory, NULL, NULL, 0, &needed);
2471     ok( ((res == ERROR_MORE_DATA) || broken(res == 0xdeadbeef)) &&
2472         ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2473         "got %d, needed: %d (expected ERROR_MORE_DATA and %d)\n", res, needed, len);
2474
2475     needed = 0xdeadbeef;
2476     SetLastError(0xdeaddead);
2477     res = pGetPrinterDataExA(hprn, NULL, defaultspooldirectory, NULL, NULL, 0, &needed);
2478     ok( ((res == ERROR_MORE_DATA) || broken(res == 0xdeaddead)) &&
2479         ((needed == len) || (needed == (len * sizeof(WCHAR)))),
2480         "got %d, needed: %d (expected ERROR_MORE_DATA and %d)\n", res, needed, len);
2481
2482     SetLastError(0xdeadbeef);
2483     res = ClosePrinter(hprn);
2484     ok(res, "ClosePrinter error %d\n", GetLastError());
2485 }
2486
2487 /* ########################### */
2488
2489 static void test_GetPrinterDriver(void)
2490 {
2491     HANDLE hprn;
2492     BOOL ret;
2493     BYTE *buf;
2494     INT level;
2495     DWORD needed, filled;
2496
2497     if (!default_printer)
2498     {
2499         skip("There is no default printer installed\n");
2500         return;
2501     }
2502
2503     hprn = 0;
2504     ret = OpenPrinter(default_printer, &hprn, NULL);
2505     if (!ret)
2506     {
2507         skip("Unable to open the default printer (%s)\n", default_printer);
2508         return;
2509     }
2510     ok(hprn != 0, "wrong hprn %p\n", hprn);
2511
2512     for (level = -1; level <= 7; level++)
2513     {
2514         SetLastError(0xdeadbeef);
2515         needed = (DWORD)-1;
2516         ret = GetPrinterDriver(hprn, NULL, level, NULL, 0, &needed);
2517         ok(!ret, "level %d: GetPrinterDriver should fail\n", level);
2518         if (level >= 1 && level <= 6)
2519         {
2520             /* Not all levels are supported on all Windows-Versions */
2521             if(GetLastError() == ERROR_INVALID_LEVEL) continue;
2522             ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "wrong error %d\n", GetLastError());
2523             ok(needed > 0,"not expected needed buffer size %d\n", needed);
2524         }
2525         else
2526         {
2527             /* ERROR_OUTOFMEMORY found on win9x */
2528             ok( ((GetLastError() == ERROR_INVALID_LEVEL) ||
2529                  (GetLastError() == ERROR_OUTOFMEMORY)),
2530                 "%d: returned %d with %d (expected '0' with: "
2531                 "ERROR_INVALID_LEVEL or ERROR_OUTOFMEMORY)\n",
2532                 level, ret, GetLastError());
2533             /* needed is modified in win9x. The modified Value depends on the
2534                default Printer. testing for "needed == (DWORD)-1" will fail */
2535             continue;
2536         }
2537
2538         /* GetPrinterDriverA returns the same number of bytes as GetPrinterDriverW */
2539         if (!on_win9x && !ret && pGetPrinterDriverW)
2540         {
2541             DWORD double_needed;
2542             ret = pGetPrinterDriverW(hprn, NULL, level, NULL, 0, &double_needed);
2543             ok(double_needed == needed, "GetPrinterDriverA returned different size %d than GetPrinterDriverW (%d)\n", needed, double_needed);
2544         }
2545
2546         buf = HeapAlloc(GetProcessHeap(), 0, needed);
2547
2548         SetLastError(0xdeadbeef);
2549         filled = -1;
2550         ret = GetPrinterDriver(hprn, NULL, level, buf, needed, &filled);
2551         ok(ret, "level %d: GetPrinterDriver error %d\n", level, GetLastError());
2552         ok(needed == filled, "needed %d != filled %d\n", needed, filled);
2553
2554         if (level == 2)
2555         {
2556             DRIVER_INFO_2 *di_2 = (DRIVER_INFO_2 *)buf;
2557             DWORD calculated = sizeof(*di_2);
2558             HANDLE hf;
2559
2560             /* MSDN is wrong: The Drivers on the win9x-CD's have cVersion=0x0400
2561                NT351: 1, NT4.0+w2k(Kernelmode): 2, w2k and above(Usermode): 3  */
2562             ok( (di_2->cVersion <= 3) ||
2563                 (di_2->cVersion == 0x0400), "di_2->cVersion = %d\n", di_2->cVersion);
2564             ok(di_2->pName != NULL, "not expected NULL ptr\n");
2565             ok(di_2->pEnvironment != NULL, "not expected NULL ptr\n");
2566             ok(di_2->pDriverPath != NULL, "not expected NULL ptr\n");
2567             ok(di_2->pDataFile != NULL, "not expected NULL ptr\n");
2568             ok(di_2->pConfigFile != NULL, "not expected NULL ptr\n");
2569
2570             trace("cVersion %d\n", di_2->cVersion);
2571             trace("pName %s\n", di_2->pName);
2572             calculated += strlen(di_2->pName) + 1;
2573             trace("pEnvironment %s\n", di_2->pEnvironment);
2574             calculated += strlen(di_2->pEnvironment) + 1;
2575             trace("pDriverPath %s\n", di_2->pDriverPath);
2576             calculated += strlen(di_2->pDriverPath) + 1;
2577             trace("pDataFile %s\n", di_2->pDataFile);
2578             calculated += strlen(di_2->pDataFile) + 1;
2579             trace("pConfigFile %s\n", di_2->pConfigFile);
2580             calculated += strlen(di_2->pConfigFile) + 1;
2581
2582             hf = CreateFileA(di_2->pDriverPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2583             if(hf != INVALID_HANDLE_VALUE)
2584                 CloseHandle(hf);
2585             todo_wine
2586             ok(hf != INVALID_HANDLE_VALUE, "Could not open %s\n", di_2->pDriverPath);
2587
2588             hf = CreateFileA(di_2->pDataFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2589             if(hf != INVALID_HANDLE_VALUE)
2590                 CloseHandle(hf);
2591             todo_wine
2592             ok(hf != INVALID_HANDLE_VALUE, "Could not open %s\n", di_2->pDataFile);
2593
2594             hf = CreateFileA(di_2->pConfigFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2595             if(hf != INVALID_HANDLE_VALUE)
2596                 CloseHandle(hf);
2597             todo_wine
2598             ok(hf != INVALID_HANDLE_VALUE, "Could not open %s\n", di_2->pConfigFile);
2599
2600             /* XP allocates memory for both ANSI and unicode names */
2601             ok(filled >= calculated,"calculated %d != filled %d\n", calculated, filled);
2602
2603             /* Obscure test - demonstrate that Windows zero fills the buffer, even on failure */
2604             if (di_2->pDataFile)
2605             {
2606                 ret = GetPrinterDriver(hprn, NULL, level, buf, needed - 2, &filled);
2607                 ok(!ret, "level %d: GetPrinterDriver succeeded with less buffer than it should\n", level);
2608                 ok(di_2->pDataFile == NULL ||
2609                    broken(di_2->pDataFile != NULL), /* Win9x/WinMe */
2610                    "Even on failure, GetPrinterDriver clears the buffer to zeros\n");
2611             }
2612         }
2613
2614         HeapFree(GetProcessHeap(), 0, buf);
2615     }
2616
2617     SetLastError(0xdeadbeef);
2618     ret = ClosePrinter(hprn);
2619     ok(ret, "ClosePrinter error %d\n", GetLastError());
2620 }
2621
2622 static void test_DEVMODE(const DEVMODE *dm, LONG dmSize, LPCSTR exp_prn_name)
2623 {
2624     /* On NT3.51, some fields in DEVMODE are empty/zero
2625       (dmDeviceName, dmSpecVersion, dmDriverVersion and dmDriverExtra)
2626        We skip the Tests on this Platform */
2627     if (dm->dmSpecVersion || dm->dmDriverVersion || dm->dmDriverExtra) {
2628     /* The 0-terminated Printername can be larger (MAX_PATH) than CCHDEVICENAME */
2629         ok(!strncmp(exp_prn_name, (LPCSTR)dm->dmDeviceName, CCHDEVICENAME -1) ||
2630            !strncmp(exp_prn_name, (LPCSTR)dm->dmDeviceName, CCHDEVICENAME -2), /* XP+ */
2631             "expected '%s', got '%s'\n", exp_prn_name, dm->dmDeviceName);
2632         ok(dm->dmSize + dm->dmDriverExtra == dmSize,
2633             "%u != %d\n", dm->dmSize + dm->dmDriverExtra, dmSize);
2634     }
2635     trace("dmFields %08x\n", dm->dmFields);
2636 }
2637
2638 static void test_DocumentProperties(void)
2639 {
2640     HANDLE hprn;
2641     LONG dm_size, ret;
2642     DEVMODE *dm;
2643
2644     if (!default_printer)
2645     {
2646         skip("There is no default printer installed\n");
2647         return;
2648     }
2649
2650     hprn = 0;
2651     ret = OpenPrinter(default_printer, &hprn, NULL);
2652     if (!ret)
2653     {
2654         skip("Unable to open the default printer (%s)\n", default_printer);
2655         return;
2656     }
2657     ok(hprn != 0, "wrong hprn %p\n", hprn);
2658
2659     dm_size = DocumentProperties(0, hprn, NULL, NULL, NULL, 0);
2660     trace("DEVMODE required size %d\n", dm_size);
2661     ok(dm_size >= sizeof(DEVMODE), "unexpected DocumentProperties ret value %d\n", dm_size);
2662
2663     dm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dm_size);
2664
2665     ret = DocumentProperties(0, hprn, NULL, dm, dm, DM_OUT_BUFFER);
2666     ok(ret == IDOK, "DocumentProperties ret value %d != expected IDOK\n", ret);
2667
2668     test_DEVMODE(dm, dm_size, default_printer);
2669
2670     HeapFree(GetProcessHeap(), 0, dm);
2671
2672     SetLastError(0xdeadbeef);
2673     ret = ClosePrinter(hprn);
2674     ok(ret, "ClosePrinter error %d\n", GetLastError());
2675 }
2676
2677 static void test_EnumPrinters(void)
2678 {
2679     DWORD neededA, neededW, num;
2680     DWORD ret;
2681
2682     SetLastError(0xdeadbeef);
2683     neededA = -1;
2684     ret = EnumPrintersA(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &neededA, &num);
2685     RETURN_ON_DEACTIVATED_SPOOLER(ret)
2686     if (!ret)
2687     {
2688         /* We have 1 or more printers */
2689         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "gle %d\n", GetLastError());
2690         ok(neededA > 0, "Expected neededA to show the number of needed bytes\n");
2691     }
2692     else
2693     {
2694         /* We don't have any printers defined */
2695         ok(GetLastError() == S_OK, "gle %d\n", GetLastError());
2696         ok(neededA == 0, "Expected neededA to be zero\n");
2697     }
2698     ok(num == 0, "num %d\n", num);
2699
2700     SetLastError(0xdeadbeef);
2701     neededW = -1;
2702     ret = EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &neededW, &num);
2703     /* EnumPrintersW is not supported on all platforms */
2704     if (!ret && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2705     {
2706         win_skip("EnumPrintersW is not implemented\n");
2707         return;
2708     }
2709
2710     if (!ret)
2711     {
2712         /* We have 1 or more printers */
2713         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "gle %d\n", GetLastError());
2714         ok(neededW > 0, "Expected neededW to show the number of needed bytes\n");
2715     }
2716     else
2717     {
2718         /* We don't have any printers defined */
2719         ok(GetLastError() == S_OK, "gle %d\n", GetLastError());
2720         ok(neededW == 0, "Expected neededW to be zero\n");
2721     }
2722     ok(num == 0, "num %d\n", num);
2723
2724     /* Outlook2003 relies on the buffer size returned by EnumPrintersA being big enough
2725        to hold the buffer returned by EnumPrintersW */
2726     ok(neededA == neededW, "neededA %d neededW %d\n", neededA, neededW);
2727 }
2728
2729 static void test_DeviceCapabilities(void)
2730 {
2731     HANDLE hComdlg32;
2732     BOOL (WINAPI *pPrintDlgA)(PRINTDLGA *);
2733     PRINTDLGA prn_dlg;
2734     DEVMODE *dm;
2735     DEVNAMES *dn;
2736     const char *driver, *device, *port;
2737     WORD *papers;
2738     POINT *paper_size;
2739     POINTS ext;
2740     struct
2741     {
2742         char name[64];
2743     } *paper_name;
2744     INT n_papers, n_paper_size, n_paper_names, n_copies, ret;
2745     DWORD fields;
2746
2747     hComdlg32 = LoadLibrary("comdlg32.dll");
2748     assert(hComdlg32);
2749     pPrintDlgA = (void *)GetProcAddress(hComdlg32, "PrintDlgA");
2750     assert(pPrintDlgA);
2751
2752     memset(&prn_dlg, 0, sizeof(prn_dlg));
2753     prn_dlg.lStructSize = sizeof(prn_dlg);
2754     prn_dlg.Flags = PD_RETURNDEFAULT;
2755     ret = pPrintDlgA(&prn_dlg);
2756     FreeLibrary(hComdlg32);
2757     if (!ret)
2758     {
2759         skip("PrintDlg returned no default printer\n");
2760         return;
2761     }
2762     ok(prn_dlg.hDevMode != 0, "PrintDlg returned hDevMode == NULL\n");
2763     ok(prn_dlg.hDevNames != 0, "PrintDlg returned hDevNames == NULL\n");
2764
2765     dm = GlobalLock(prn_dlg.hDevMode);
2766     ok(dm != NULL, "GlobalLock(prn_dlg.hDevMode) failed\n");
2767     trace("dmDeviceName \"%s\"\n", dm->dmDeviceName);
2768
2769     dn = GlobalLock(prn_dlg.hDevNames);
2770     ok(dn != NULL, "GlobalLock(prn_dlg.hDevNames) failed\n");
2771     ok(dn->wDriverOffset, "expected not 0 wDriverOffset\n");
2772     ok(dn->wDeviceOffset, "expected not 0 wDeviceOffset\n");
2773     ok(dn->wOutputOffset, "expected not 0 wOutputOffset\n");
2774     ok(dn->wDefault == DN_DEFAULTPRN, "expected DN_DEFAULTPRN got %x\n", dn->wDefault);
2775     driver = (const char *)dn + dn->wDriverOffset;
2776     device = (const char *)dn + dn->wDeviceOffset;
2777     port = (const char *)dn + dn->wOutputOffset;
2778     trace("driver \"%s\" device \"%s\" port \"%s\"\n", driver, device, port);
2779
2780     test_DEVMODE(dm, dm->dmSize + dm->dmDriverExtra, device);
2781
2782     n_papers = DeviceCapabilities(device, port, DC_PAPERS, NULL, NULL);
2783     ok(n_papers > 0, "DeviceCapabilities DC_PAPERS failed\n");
2784     papers = HeapAlloc(GetProcessHeap(), 0, sizeof(*papers) * n_papers);
2785     ret = DeviceCapabilities(device, port, DC_PAPERS, (LPSTR)papers, NULL);
2786     ok(ret == n_papers, "expected %d, got %d\n", n_papers, ret);
2787 #ifdef VERBOSE
2788     for (ret = 0; ret < n_papers; ret++)
2789         trace("papers[%d] = %d\n", ret, papers[ret]);
2790 #endif
2791     HeapFree(GetProcessHeap(), 0, papers);
2792
2793     n_paper_size = DeviceCapabilities(device, port, DC_PAPERSIZE, NULL, NULL);
2794     ok(n_paper_size > 0, "DeviceCapabilities DC_PAPERSIZE failed\n");
2795     ok(n_paper_size == n_papers, "n_paper_size %d != n_papers %d\n", n_paper_size, n_papers);
2796     paper_size = HeapAlloc(GetProcessHeap(), 0, sizeof(*paper_size) * n_paper_size);
2797     ret = DeviceCapabilities(device, port, DC_PAPERSIZE, (LPSTR)paper_size, NULL);
2798     ok(ret == n_paper_size, "expected %d, got %d\n", n_paper_size, ret);
2799 #ifdef VERBOSE
2800     for (ret = 0; ret < n_paper_size; ret++)
2801         trace("paper_size[%d] = %d x %d\n", ret, paper_size[ret].x, paper_size[ret].y);
2802 #endif
2803     HeapFree(GetProcessHeap(), 0, paper_size);
2804
2805     n_paper_names = DeviceCapabilities(device, port, DC_PAPERNAMES, NULL, NULL);
2806     ok(n_paper_names > 0, "DeviceCapabilities DC_PAPERNAMES failed\n");
2807     ok(n_paper_names == n_papers, "n_paper_names %d != n_papers %d\n", n_paper_names, n_papers);
2808     paper_name = HeapAlloc(GetProcessHeap(), 0, sizeof(*paper_name) * n_paper_names);
2809     ret = DeviceCapabilities(device, port, DC_PAPERNAMES, (LPSTR)paper_name, NULL);
2810     ok(ret == n_paper_names, "expected %d, got %d\n", n_paper_names, ret);
2811 #ifdef VERBOSE
2812     for (ret = 0; ret < n_paper_names; ret++)
2813         trace("paper_name[%u] = %s\n", ret, paper_name[ret].name);
2814 #endif
2815     HeapFree(GetProcessHeap(), 0, paper_name);
2816
2817     n_copies = DeviceCapabilities(device, port, DC_COPIES, NULL, dm);
2818     ok(n_copies > 0, "DeviceCapabilities DC_COPIES failed\n");
2819     trace("n_copies = %d\n", n_copies);
2820
2821     /* these capabilities not available on all printer drivers */
2822     if (0)
2823     {
2824         ret = DeviceCapabilities(device, port, DC_MAXEXTENT, NULL, NULL);
2825         ok(ret != -1, "DeviceCapabilities DC_MAXEXTENT failed\n");
2826         ext = MAKEPOINTS(ret);
2827         trace("max ext = %d x %d\n", ext.x, ext.y);
2828
2829         ret = DeviceCapabilities(device, port, DC_MINEXTENT, NULL, NULL);
2830         ok(ret != -1, "DeviceCapabilities DC_MINEXTENT failed\n");
2831         ext = MAKEPOINTS(ret);
2832         trace("min ext = %d x %d\n", ext.x, ext.y);
2833     }
2834
2835     fields = DeviceCapabilities(device, port, DC_FIELDS, NULL, NULL);
2836     ok(fields != (DWORD)-1, "DeviceCapabilities DC_FIELDS failed\n");
2837     todo_wine
2838     ok(fields == (dm->dmFields | DM_FORMNAME) ||
2839         broken(fields == dm->dmFields), /* Win9x/WinMe */
2840         "fields %x != (dm->dmFields | DM_FORMNAME) %lx\n", fields, (dm->dmFields | DM_FORMNAME));
2841
2842     GlobalUnlock(prn_dlg.hDevMode);
2843     GlobalFree(prn_dlg.hDevMode);
2844     GlobalUnlock(prn_dlg.hDevNames);
2845     GlobalFree(prn_dlg.hDevNames);
2846 }
2847
2848 START_TEST(info)
2849 {
2850     hwinspool = GetModuleHandleA("winspool.drv");
2851     pAddPortExA = (void *) GetProcAddress(hwinspool, "AddPortExA");
2852     pEnumPrinterDriversW = (void *) GetProcAddress(hwinspool, "EnumPrinterDriversW");
2853     pGetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "GetDefaultPrinterA");
2854     pGetPrinterDataExA = (void *) GetProcAddress(hwinspool, "GetPrinterDataExA");
2855     pGetPrinterDriverW = (void *) GetProcAddress(hwinspool, "GetPrinterDriverW");
2856     pGetPrinterW = (void *) GetProcAddress(hwinspool, "GetPrinterW");
2857     pSetDefaultPrinterA = (void *) GetProcAddress(hwinspool, "SetDefaultPrinterA");
2858     pXcvDataW = (void *) GetProcAddress(hwinspool, "XcvDataW");
2859
2860     on_win9x = check_win9x();
2861     if (on_win9x)
2862         win_skip("Several W-functions are not available on Win9x/WinMe\n");
2863
2864     find_default_printer();
2865     find_local_server();
2866     find_tempfile();
2867
2868     test_AddMonitor();
2869     test_AddPort();
2870     test_AddPortEx();
2871     test_ConfigurePort();
2872     test_DeleteMonitor();
2873     test_DeletePort();
2874     test_DeviceCapabilities();
2875     test_DocumentProperties();
2876     test_EnumForms(NULL);
2877     if (default_printer) test_EnumForms(default_printer);
2878     test_EnumMonitors();
2879     test_EnumPorts();
2880     test_EnumPrinterDrivers();
2881     test_EnumPrinters();
2882     test_EnumPrintProcessors();
2883     test_GetDefaultPrinter();
2884     test_GetPrinterDriverDirectory();
2885     test_GetPrintProcessorDirectory();
2886     test_OpenPrinter();
2887     test_GetPrinter();
2888     test_GetPrinterData();
2889     test_GetPrinterDataEx();
2890     test_GetPrinterDriver();
2891     test_SetDefaultPrinter();
2892     test_XcvDataW_MonitorUI();
2893     test_XcvDataW_PortIsValid();
2894
2895     /* Cleanup our temporary file */
2896     DeleteFileA(tempfileA);
2897 }