67483683f991c0d8d7418bd567a08915a4b24c12
[reactos.git] / reactos / win32ss / printing / providers / localspl / ports.c
1 /*
2 * PROJECT: ReactOS Local Spooler
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
11 BOOL WINAPI
12 LocalEnumPorts(PWSTR pName, DWORD Level, PBYTE pPorts, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
13 {
14 BOOL bReturnValue;
15 DWORD cbCallBuffer;
16 DWORD cbNeeded;
17 DWORD dwReturned;
18 PBYTE pCallBuffer;
19 PLOCAL_PRINT_MONITOR pPrintMonitor;
20 PLIST_ENTRY pEntry;
21
22 // Sanity checks.
23 if ((cbBuf && !pPorts) || !pcbNeeded || !pcReturned)
24 {
25 SetLastError(ERROR_INVALID_PARAMETER);
26 return FALSE;
27 }
28
29 // Begin counting.
30 *pcbNeeded = 0;
31 *pcReturned = 0;
32
33 // At the beginning, we have the full buffer available.
34 cbCallBuffer = cbBuf;
35 pCallBuffer = pPorts;
36
37 // Loop through all Print Monitors.
38 for (pEntry = PrintMonitorList.Flink; pEntry != &PrintMonitorList; pEntry = pEntry->Flink)
39 {
40 pPrintMonitor = CONTAINING_RECORD(pEntry, LOCAL_PRINT_MONITOR, Entry);
41
42 // Call the EnumPorts function of this Print Monitor.
43 if (pPrintMonitor->bIsLevel2)
44 bReturnValue = ((PMONITOR2)pPrintMonitor->pMonitor)->pfnEnumPorts(pPrintMonitor->hMonitor, pName, Level, pCallBuffer, cbCallBuffer, &cbNeeded, &dwReturned);
45 else
46 bReturnValue = ((LPMONITOREX)pPrintMonitor->pMonitor)->Monitor.pfnEnumPorts(pName, Level, pCallBuffer, cbCallBuffer, &cbNeeded, &dwReturned);
47
48 // Add the returned counts to the total values.
49 *pcbNeeded += cbNeeded;
50 *pcReturned += dwReturned;
51
52 // Reduce the available buffer size for the next call without risking an underflow.
53 if (cbNeeded < cbCallBuffer)
54 cbCallBuffer -= cbNeeded;
55 else
56 cbCallBuffer = 0;
57
58 // Advance the buffer if the caller provided it.
59 if (pCallBuffer)
60 pCallBuffer += cbNeeded;
61 }
62
63 return bReturnValue;
64 }