91bb80ea9131027e5fca33d76b38ff041f9e5225
[reactos.git] / reactos / win32ss / printing / base / winspool / monitors.c
1 /*
2 * PROJECT: ReactOS Spooler API
3 * LICENSE: GNU LGPL v2.1 or any later version as published by the Free Software Foundation
4 * PURPOSE: Functions related to Print Monitors
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 static void
11 _MarshallUpMonitorInfo(PBYTE pMonitorInfo, DWORD Level)
12 {
13 PMONITOR_INFO_2W pMonitorInfo2 = (PMONITOR_INFO_2W)pMonitorInfo; // MONITOR_INFO_1W is a subset of MONITOR_INFO_2W
14
15 // Replace relative offset addresses in the output by absolute pointers.
16 pMonitorInfo2->pName = (PWSTR)((ULONG_PTR)pMonitorInfo2->pName + (ULONG_PTR)pMonitorInfo2);
17
18 if (Level == 2)
19 {
20 pMonitorInfo2->pDLLName = (PWSTR)((ULONG_PTR)pMonitorInfo2->pDLLName + (ULONG_PTR)pMonitorInfo2);
21 pMonitorInfo2->pEnvironment = (PWSTR)((ULONG_PTR)pMonitorInfo2->pEnvironment + (ULONG_PTR)pMonitorInfo2);
22 }
23 }
24
25 BOOL WINAPI
26 EnumMonitorsW(PWSTR pName, DWORD Level, PBYTE pMonitors, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
27 {
28 DWORD dwErrorCode;
29 DWORD i;
30 PBYTE p = pMonitors;
31
32 // Do the RPC call
33 RpcTryExcept
34 {
35 dwErrorCode = _RpcEnumMonitors(pName, Level, pMonitors, cbBuf, pcbNeeded, pcReturned);
36 }
37 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
38 {
39 dwErrorCode = RpcExceptionCode();
40 ERR("_RpcEnumPorts failed with exception code %lu!\n", dwErrorCode);
41 }
42 RpcEndExcept;
43
44 if (dwErrorCode == ERROR_SUCCESS)
45 {
46 // Replace relative offset addresses in the output by absolute pointers.
47 for (i = 0; i < *pcReturned; i++)
48 {
49 _MarshallUpMonitorInfo(p, Level);
50
51 if (Level == 1)
52 p += sizeof(MONITOR_INFO_1W);
53 else if (Level == 2)
54 p += sizeof(MONITOR_INFO_2W);
55 }
56 }
57
58 SetLastError(dwErrorCode);
59 return (dwErrorCode == ERROR_SUCCESS);
60 }