[LOCALUI] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / win32ss / printing / providers / localspl / tools.c
1 /*
2 * PROJECT: ReactOS Local Spooler
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Various tools
5 * COPYRIGHT: Copyright 2015-2017 Colin Finck (colin@reactos.org)
6 */
7
8 #include "precomp.h"
9
10 /**
11 * @name AllocAndRegQueryWSZ
12 *
13 * Queries a REG_SZ value in the registry, allocates memory for it and returns a buffer containing the value.
14 * You have to free this buffer using DllFreeSplMem.
15 *
16 * @param hKey
17 * HKEY variable of the key opened with RegOpenKeyExW.
18 *
19 * @param pwszValueName
20 * Name of the REG_SZ value to query.
21 *
22 * @return
23 * Pointer to the buffer containing the value or NULL in case of failure.
24 */
25 PWSTR
26 AllocAndRegQueryWSZ(HKEY hKey, PCWSTR pwszValueName)
27 {
28 DWORD cbNeeded;
29 LONG lStatus;
30 PWSTR pwszValue;
31
32 // Determine the size of the required buffer.
33 lStatus = RegQueryValueExW(hKey, pwszValueName, NULL, NULL, NULL, &cbNeeded);
34 if (lStatus != ERROR_SUCCESS)
35 {
36 ERR("RegQueryValueExW failed with status %ld!\n", lStatus);
37 return NULL;
38 }
39
40 // Allocate it.
41 pwszValue = DllAllocSplMem(cbNeeded);
42 if (!pwszValue)
43 {
44 ERR("DllAllocSplMem failed!\n");
45 return NULL;
46 }
47
48 // Now get the actual value.
49 lStatus = RegQueryValueExW(hKey, pwszValueName, NULL, NULL, (PBYTE)pwszValue, &cbNeeded);
50 if (lStatus != ERROR_SUCCESS)
51 {
52 ERR("RegQueryValueExW failed with status %ld!\n", lStatus);
53 DllFreeSplMem(pwszValue);
54 return NULL;
55 }
56
57 return pwszValue;
58 }
59
60 PDEVMODEW
61 DuplicateDevMode(PDEVMODEW pInput)
62 {
63 PDEVMODEW pOutput;
64
65 // Allocate a buffer for this DevMode.
66 pOutput = DllAllocSplMem(pInput->dmSize + pInput->dmDriverExtra);
67 if (!pOutput)
68 {
69 ERR("DllAllocSplMem failed!\n");
70 return NULL;
71 }
72
73 // Copy it.
74 CopyMemory(pOutput, pInput, pInput->dmSize + pInput->dmDriverExtra);
75
76 return pOutput;
77 }