[LOCALSPL]
[reactos.git] / reactos / win32ss / printing / base / spoolss / ports.c
1 /*
2 * PROJECT: ReactOS Spooler Router
3 * LICENSE: GNU LGPL v2.1 or any later version as published by the Free Software Foundation
4 * PURPOSE: Functions related to Ports of the Print Monitors
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 BOOL WINAPI
11 EnumPortsW(PWSTR pName, DWORD Level, PBYTE pPorts, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
12 {
13 BOOL bReturnValue;
14 DWORD cbCallBuffer;
15 DWORD cbNeeded;
16 DWORD dwReturned;
17 PBYTE pCallBuffer;
18 PSPOOLSS_PRINT_PROVIDER pPrintProvider;
19 PLIST_ENTRY pEntry;
20
21 // Sanity checks.
22 if ((cbBuf && !pPorts) || !pcbNeeded || !pcReturned)
23 {
24 SetLastError(ERROR_INVALID_PARAMETER);
25 return FALSE;
26 }
27
28 // Begin counting.
29 *pcbNeeded = 0;
30 *pcReturned = 0;
31
32 // At the beginning, we have the full buffer available.
33 cbCallBuffer = cbBuf;
34 pCallBuffer = pPorts;
35
36 // Loop through all Print Provider.
37 for (pEntry = PrintProviderList.Flink; pEntry != &PrintProviderList; pEntry = pEntry->Flink)
38 {
39 pPrintProvider = CONTAINING_RECORD(pEntry, SPOOLSS_PRINT_PROVIDER, Entry);
40
41 // Call the EnumPorts function of this Print Provider.
42 bReturnValue = pPrintProvider->PrintProvider.fpEnumPorts(pName, Level, pCallBuffer, cbCallBuffer, &cbNeeded, &dwReturned);
43
44 // Add the returned counts to the total values.
45 *pcbNeeded += cbNeeded;
46 *pcReturned += dwReturned;
47
48 // Reduce the available buffer size for the next call without risking an underflow.
49 if (cbNeeded < cbCallBuffer)
50 cbCallBuffer -= cbNeeded;
51 else
52 cbCallBuffer = 0;
53
54 // Advance the buffer if the caller provided it.
55 if (pCallBuffer)
56 pCallBuffer += cbNeeded;
57
58 // Check if we shall not ask other Print Providers.
59 if (bReturnValue == ROUTER_STOP_ROUTING)
60 break;
61 }
62
63 return bReturnValue;
64 }