1 /*
2  * PROJECT:     ReactOS Print Spooler DLL API Tests
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Tests for GetPrinterA/GetPrinterW
5  * COPYRIGHT:   Copyright 2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include <apitest.h>
9 
10 #define WIN32_NO_STATUS
11 #include <windef.h>
12 #include <winbase.h>
13 #include <wingdi.h>
14 #include <winspool.h>
15 
16 /* From printing/include/spoolss.h */
17 #define MAX_PRINTER_NAME        220
18 
19 START_TEST(GetPrinter)
20 {
21     DWORD cbNeeded;
22     DWORD cbTemp;
23     DWORD cchDefaultPrinter;
24     DWORD Level;
25     HANDLE hPrinter;
26     PVOID pMem;
27     WCHAR wszDefaultPrinter[MAX_PRINTER_NAME + 1];
28 
29     // Open a handle to the default printer.
30     cchDefaultPrinter = _countof(wszDefaultPrinter);
31     ok(GetDefaultPrinterW(wszDefaultPrinter, &cchDefaultPrinter), "GetDefaultPrinterW returns FALSE and requires %lu characters!\n", cchDefaultPrinter);
32     if (!OpenPrinterW(wszDefaultPrinter, &hPrinter, NULL))
33     {
34         skip("Could not retrieve a handle to the default printer!\n");
35         return;
36     }
37 
38     // Try for all levels. Level 0 is valid here and returns the PRINTER_INFO_STRESS structure (documented in MS-RPRN).
39     for (Level = 0; Level <= 9; Level++)
40     {
41         // Try with no valid arguments at all.
42         SetLastError(0xDEADBEEF);
43         ok(!GetPrinterW(NULL, Level, NULL, 0, NULL), "GetPrinterW returns TRUE for Level %lu!\n", Level);
44         ok(GetLastError() == ERROR_INVALID_HANDLE, "GetPrinterW returns error %lu for Level %lu!\n", GetLastError(), Level);
45 
46         // Now supply at least a handle.
47         SetLastError(0xDEADBEEF);
48         ok(!GetPrinterW(hPrinter, Level, NULL, 0, NULL), "GetPrinterW returns TRUE for Level %lu!\n", Level);
49         ok(GetLastError() == RPC_X_NULL_REF_POINTER, "GetPrinterW returns error %lu for Level %lu!\n", GetLastError(), Level);
50 
51         // Now get the buffer size.
52         SetLastError(0xDEADBEEF);
53         ok(!GetPrinterW(hPrinter, Level, NULL, 0, &cbNeeded), "GetPrinterW returns TRUE for Level %lu!\n", Level);
54         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetPrinterW returns error %lu for Level %lu!\n", GetLastError(), Level);
55 
56         // Finally use the function as intended and aim for success!
57         pMem = HeapAlloc(GetProcessHeap(), 0, cbNeeded);
58         SetLastError(0xDEADBEEF);
59         ok(GetPrinterW(hPrinter, Level, pMem, cbNeeded, &cbTemp), "GetPrinterW returns FALSE for Level %lu!\n", Level);
60         ok(cbNeeded == cbTemp, "cbNeeded is %lu, reference size is %lu for Level %lu!\n", cbNeeded, cbTemp, Level);
61         HeapFree(GetProcessHeap(), 0, pMem);
62     }
63 
64     ClosePrinter(hPrinter);
65 }
66