1 | /* $Id: VBoxCommon.cpp 108071 2025-02-05 14:14:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxCommon - Misc helper routines for install helper.
|
---|
4 | *
|
---|
5 | * This is used by internal/serial.cpp and VBoxInstallHelper.cpp.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008-2024 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.215389.xyz.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | /*********************************************************************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *********************************************************************************************************************************/
|
---|
34 | #include <iprt/win/windows.h>
|
---|
35 | #include <msi.h>
|
---|
36 | #include <msiquery.h>
|
---|
37 |
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/utf16.h>
|
---|
40 |
|
---|
41 | #include <VBox/GuestHost/VBoxWinDrvInst.h>
|
---|
42 | #include <VBoxWinDrvCommon.h>
|
---|
43 | #include "VBoxCommon.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Retrieves a MSI property (in UTF-16), extended version.
|
---|
48 | *
|
---|
49 | * @returns VBox status code.
|
---|
50 | * @param hMsi MSI handle to use.
|
---|
51 | * @param pwszName Name of property to retrieve.
|
---|
52 | * @param pwszVal Where to store the allocated value on success.
|
---|
53 | * @param pcwVal Input and output size (in WCHARs) of \a pwszVal.
|
---|
54 | */
|
---|
55 | int VBoxMsiQueryPropEx(MSIHANDLE hMsi, const WCHAR *pwszName, WCHAR *pwszVal, DWORD *pcwVal)
|
---|
56 | {
|
---|
57 | AssertPtrReturn(pwszName, VERR_INVALID_POINTER);
|
---|
58 | AssertPtrReturn(pwszVal, VERR_INVALID_POINTER);
|
---|
59 | AssertPtrReturn(pcwVal, VERR_INVALID_POINTER);
|
---|
60 | AssertReturn(*pcwVal, VERR_INVALID_PARAMETER);
|
---|
61 |
|
---|
62 | int rc;
|
---|
63 |
|
---|
64 | RT_BZERO(pwszVal, *pcwVal * sizeof(WCHAR));
|
---|
65 | UINT uRc = MsiGetPropertyW(hMsi, pwszName, pwszVal, pcwVal);
|
---|
66 | if (uRc == ERROR_SUCCESS)
|
---|
67 | {
|
---|
68 | if (*pcwVal > 0)
|
---|
69 | {
|
---|
70 | rc = VINF_SUCCESS;
|
---|
71 | }
|
---|
72 | else /* Indicates value not found. */
|
---|
73 | rc = VERR_NOT_FOUND;
|
---|
74 | }
|
---|
75 | else
|
---|
76 | rc = RTErrConvertFromWin32(uRc);
|
---|
77 |
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | #ifndef TESTCASE
|
---|
82 | /**
|
---|
83 | * Retrieves a MSI property (in UTF-16).
|
---|
84 | *
|
---|
85 | * @returns VBox status code.
|
---|
86 | * @param hMsi MSI handle to use.
|
---|
87 | * @param pwszName Name of property to retrieve.
|
---|
88 | * @param pwszVal Where to store the allocated value on success.
|
---|
89 | * @param cwVal Input size (in WCHARs) of \a pwszVal.
|
---|
90 | */
|
---|
91 | int VBoxMsiQueryProp(MSIHANDLE hMsi, const WCHAR *pwszName, WCHAR *pwszVal, DWORD cwVal)
|
---|
92 | {
|
---|
93 | return VBoxMsiQueryPropEx(hMsi, pwszName, pwszVal, &cwVal);
|
---|
94 | }
|
---|
95 | #endif /* !TESTCASE */
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Retrieves a MSI property (in UTF-8).
|
---|
99 | *
|
---|
100 | * Convenience function for VBoxGetMsiProp().
|
---|
101 | *
|
---|
102 | * @returns VBox status code.
|
---|
103 | * @param hMsi MSI handle to use.
|
---|
104 | * @param pszName Name of property to retrieve.
|
---|
105 | * @param ppszValue Where to store the allocated value on success.
|
---|
106 | * Must be free'd using RTStrFree() by the caller.
|
---|
107 | */
|
---|
108 | int VBoxMsiQueryPropUtf8(MSIHANDLE hMsi, const char *pszName, char **ppszValue)
|
---|
109 | {
|
---|
110 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
111 | AssertPtrReturn(ppszValue, VERR_INVALID_POINTER);
|
---|
112 |
|
---|
113 | PRTUTF16 pwszName;
|
---|
114 | int rc = RTStrToUtf16(pszName, &pwszName);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | WCHAR wszValue[1024]; /* 1024 should be enough for everybody (tm). */
|
---|
118 | rc = VBoxMsiQueryProp(hMsi, pwszName, wszValue, RT_ELEMENTS(wszValue));
|
---|
119 | if (RT_SUCCESS(rc))
|
---|
120 | rc = RTUtf16ToUtf8(wszValue, ppszValue);
|
---|
121 |
|
---|
122 | RTUtf16Free(pwszName);
|
---|
123 | }
|
---|
124 |
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 |
|
---|
128 | #ifndef TESTCASE
|
---|
129 | int VBoxMsiQueryPropInt32(MSIHANDLE hMsi, const char *pszName, DWORD *pdwValue)
|
---|
130 | {
|
---|
131 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
132 | AssertPtrReturn(pdwValue, VERR_INVALID_POINTER);
|
---|
133 |
|
---|
134 | char *pszTemp;
|
---|
135 | int rc = VBoxMsiQueryPropUtf8(hMsi, pszName, &pszTemp);
|
---|
136 | if (RT_SUCCESS(rc))
|
---|
137 | {
|
---|
138 | *pdwValue = RTStrToInt32(pszTemp);
|
---|
139 | RTStrFree(pszTemp);
|
---|
140 | }
|
---|
141 |
|
---|
142 | return rc;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Sets a MSI property.
|
---|
147 | *
|
---|
148 | * @returns UINT
|
---|
149 | * @param hMsi MSI handle to use.
|
---|
150 | * @param pwszName Name of property to set.
|
---|
151 | * @param pwszValue Value to set.
|
---|
152 | */
|
---|
153 | UINT VBoxMsiSetProp(MSIHANDLE hMsi, const WCHAR *pwszName, const WCHAR *pwszValue)
|
---|
154 | {
|
---|
155 | return MsiSetPropertyW(hMsi, pwszName, pwszValue);
|
---|
156 | }
|
---|
157 | #endif /* TESTCASE */
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Sets a MSI property (in UTF-8).
|
---|
161 | *
|
---|
162 | * Convenience function for VBoxMsiSetProp().
|
---|
163 | *
|
---|
164 | * @returns VBox status code.
|
---|
165 | * @param hMsi MSI handle to use.
|
---|
166 | * @param pszName Name of property to set.
|
---|
167 | * @param pszValue Value to set.
|
---|
168 | */
|
---|
169 | int VBoxMsiSetPropUtf8(MSIHANDLE hMsi, const char *pszName, const char *pszValue)
|
---|
170 | {
|
---|
171 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
172 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
173 |
|
---|
174 | PRTUTF16 pwszName;
|
---|
175 | int rc = RTStrToUtf16(pszName, &pwszName);
|
---|
176 | if (RT_SUCCESS(rc))
|
---|
177 | {
|
---|
178 | PRTUTF16 pwszValue;
|
---|
179 | rc = RTStrToUtf16(pszValue, &pwszValue);
|
---|
180 | if (RT_SUCCESS(rc))
|
---|
181 | {
|
---|
182 | UINT const uRc = VBoxMsiSetProp(hMsi, pwszName, pwszValue);
|
---|
183 | if (uRc != ERROR_SUCCESS)
|
---|
184 | rc = RTErrConvertFromWin32(uRc);
|
---|
185 | RTUtf16Free(pwszValue);
|
---|
186 | }
|
---|
187 |
|
---|
188 | RTUtf16Free(pwszName);
|
---|
189 | }
|
---|
190 |
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Sets a MSI property (DWORD).
|
---|
196 | *
|
---|
197 | * Convenience function for VBoxMsiSetProp().
|
---|
198 | *
|
---|
199 | * @returns UINT
|
---|
200 | * @param hMsi MSI handle to use.
|
---|
201 | * @param pwszName Name of property to set.
|
---|
202 | * @param dwVal Value to set.
|
---|
203 | */
|
---|
204 | UINT VBoxMsiSetPropDWORD(MSIHANDLE hMsi, const WCHAR *pwszName, DWORD dwVal)
|
---|
205 | {
|
---|
206 | wchar_t wszTemp[32];
|
---|
207 | RTUtf16Printf(wszTemp, RT_ELEMENTS(wszTemp), "%u", dwVal);
|
---|
208 | return VBoxMsiSetProp(hMsi, pwszName, wszTemp);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Queries a DWORD value from a Windows registry key, Unicode (wide char) version.
|
---|
213 | *
|
---|
214 | * @returns VBox status code.
|
---|
215 | * @retval VERR_FILE_NOT_FOUND if the value has not been found.
|
---|
216 | * @retval VERR_WRONG_TYPE if the type (DWORD) of the value does not match.
|
---|
217 | * @retval VERR_MISMATCH if the type sizes do not match.
|
---|
218 | * @param hMsi MSI handle to use.
|
---|
219 | * @param hKey Registry handle of key to query.
|
---|
220 | * @param pwszName Name of the value to query.
|
---|
221 | * @param pdwValue Where to return the actual value on success.
|
---|
222 | */
|
---|
223 | int VBoxMsiRegQueryDWORDW(MSIHANDLE hMsi, HKEY hKey, LPCWSTR pwszName, DWORD *pdwValue)
|
---|
224 | {
|
---|
225 | RT_NOREF(hMsi);
|
---|
226 |
|
---|
227 | return VBoxWinDrvRegQueryDWORDW(hKey, pwszName, pdwValue);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Queries a DWORD value from a Windows registry key.
|
---|
232 | *
|
---|
233 | * @returns VBox status code.
|
---|
234 | * @retval VERR_FILE_NOT_FOUND if the value has not been found.
|
---|
235 | * @retval VERR_WRONG_TYPE if the type (DWORD) of the value does not match.
|
---|
236 | * @retval VERR_MISMATCH if the type sizes do not match.
|
---|
237 | * @param hKey Registry handle of key to query.
|
---|
238 | * @param pszName Name of the value to query.
|
---|
239 | * @param pdwValue Where to return the actual value on success.
|
---|
240 | */
|
---|
241 | int VBoxMsiRegQueryDWORD(MSIHANDLE hMsi, HKEY hKey, const char *pszName, DWORD *pdwValue)
|
---|
242 | {
|
---|
243 | PRTUTF16 pwszName;
|
---|
244 | int rc = RTStrToUtf16Ex(pszName, RTSTR_MAX, &pwszName, 0, NULL);
|
---|
245 | if (RT_SUCCESS(rc))
|
---|
246 | {
|
---|
247 | rc = VBoxMsiRegQueryDWORDW(hMsi, hKey, pwszName, pdwValue);
|
---|
248 | RTUtf16Free(pwszName);
|
---|
249 | }
|
---|
250 |
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|