1 | /* $Id: VBoxCommon.cpp 107950 2025-01-27 17:15:34Z 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.virtualbox.org.
|
---|
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 "VBoxCommon.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Retrieves a MSI property (in UTF-16), extended version.
|
---|
46 | *
|
---|
47 | * @returns VBox status code.
|
---|
48 | * @param hMsi MSI handle to use.
|
---|
49 | * @param pwszName Name of property to retrieve.
|
---|
50 | * @param pwszVal Where to store the allocated value on success.
|
---|
51 | * @param pcwVal Input and output size (in WCHARs) of \a pwszVal.
|
---|
52 | */
|
---|
53 | int VBoxMsiQueryPropEx(MSIHANDLE hMsi, const WCHAR *pwszName, WCHAR *pwszVal, DWORD *pcwVal)
|
---|
54 | {
|
---|
55 | AssertPtrReturn(pwszName, VERR_INVALID_POINTER);
|
---|
56 | AssertPtrReturn(pwszVal, VERR_INVALID_POINTER);
|
---|
57 | AssertPtrReturn(pcwVal, VERR_INVALID_POINTER);
|
---|
58 | AssertReturn(*pcwVal, VERR_INVALID_PARAMETER);
|
---|
59 |
|
---|
60 | int rc;
|
---|
61 |
|
---|
62 | RT_BZERO(pwszVal, *pcwVal * sizeof(WCHAR));
|
---|
63 | UINT uRc = MsiGetPropertyW(hMsi, pwszName, pwszVal, pcwVal);
|
---|
64 | if (uRc == ERROR_SUCCESS)
|
---|
65 | {
|
---|
66 | if (*pcwVal > 0)
|
---|
67 | {
|
---|
68 | rc = VINF_SUCCESS;
|
---|
69 | }
|
---|
70 | else /* Indicates value not found. */
|
---|
71 | rc = VERR_NOT_FOUND;
|
---|
72 | }
|
---|
73 | else
|
---|
74 | rc = RTErrConvertFromWin32(uRc);
|
---|
75 |
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | #ifndef TESTCASE
|
---|
80 | /**
|
---|
81 | * Retrieves a MSI property (in UTF-16).
|
---|
82 | *
|
---|
83 | * @returns VBox status code.
|
---|
84 | * @param hMsi MSI handle to use.
|
---|
85 | * @param pwszName Name of property to retrieve.
|
---|
86 | * @param pwszVal Where to store the allocated value on success.
|
---|
87 | * @param cwVal Input size (in WCHARs) of \a pwszVal.
|
---|
88 | */
|
---|
89 | int VBoxMsiQueryProp(MSIHANDLE hMsi, const WCHAR *pwszName, WCHAR *pwszVal, DWORD cwVal)
|
---|
90 | {
|
---|
91 | return VBoxMsiQueryPropEx(hMsi, pwszName, pwszVal, &cwVal);
|
---|
92 | }
|
---|
93 | #endif /* !TESTCASE */
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Retrieves a MSI property (in UTF-8).
|
---|
97 | *
|
---|
98 | * Convenience function for VBoxGetMsiProp().
|
---|
99 | *
|
---|
100 | * @returns VBox status code.
|
---|
101 | * @param hMsi MSI handle to use.
|
---|
102 | * @param pszName Name of property to retrieve.
|
---|
103 | * @param ppszValue Where to store the allocated value on success.
|
---|
104 | * Must be free'd using RTStrFree() by the caller.
|
---|
105 | */
|
---|
106 | int VBoxMsiQueryPropUtf8(MSIHANDLE hMsi, const char *pszName, char **ppszValue)
|
---|
107 | {
|
---|
108 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
109 | AssertPtrReturn(ppszValue, VERR_INVALID_POINTER);
|
---|
110 |
|
---|
111 | PRTUTF16 pwszName;
|
---|
112 | int rc = RTStrToUtf16(pszName, &pwszName);
|
---|
113 | if (RT_SUCCESS(rc))
|
---|
114 | {
|
---|
115 | WCHAR wszValue[1024]; /* 1024 should be enough for everybody (tm). */
|
---|
116 | rc = VBoxMsiQueryProp(hMsi, pwszName, wszValue, RT_ELEMENTS(wszValue));
|
---|
117 | if (RT_SUCCESS(rc))
|
---|
118 | rc = RTUtf16ToUtf8(wszValue, ppszValue);
|
---|
119 |
|
---|
120 | RTUtf16Free(pwszName);
|
---|
121 | }
|
---|
122 |
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | #ifndef TESTCASE
|
---|
127 | int VBoxMsiQueryPropInt32(MSIHANDLE hMsi, const char *pszName, DWORD *pdwValue)
|
---|
128 | {
|
---|
129 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
130 | AssertPtrReturn(pdwValue, VERR_INVALID_POINTER);
|
---|
131 |
|
---|
132 | char *pszTemp;
|
---|
133 | int rc = VBoxMsiQueryPropUtf8(hMsi, pszName, &pszTemp);
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | {
|
---|
136 | *pdwValue = RTStrToInt32(pszTemp);
|
---|
137 | RTStrFree(pszTemp);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return rc;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Sets a MSI property.
|
---|
145 | *
|
---|
146 | * @returns UINT
|
---|
147 | * @param hMsi MSI handle to use.
|
---|
148 | * @param pwszName Name of property to set.
|
---|
149 | * @param pwszValue Value to set.
|
---|
150 | */
|
---|
151 | UINT VBoxMsiSetProp(MSIHANDLE hMsi, const WCHAR *pwszName, const WCHAR *pwszValue)
|
---|
152 | {
|
---|
153 | return MsiSetPropertyW(hMsi, pwszName, pwszValue);
|
---|
154 | }
|
---|
155 | #endif /* TESTCASE */
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Sets a MSI property (in UTF-8).
|
---|
159 | *
|
---|
160 | * Convenience function for VBoxMsiSetProp().
|
---|
161 | *
|
---|
162 | * @returns VBox status code.
|
---|
163 | * @param hMsi MSI handle to use.
|
---|
164 | * @param pszName Name of property to set.
|
---|
165 | * @param pszValue Value to set.
|
---|
166 | */
|
---|
167 | int VBoxMsiSetPropUtf8(MSIHANDLE hMsi, const char *pszName, const char *pszValue)
|
---|
168 | {
|
---|
169 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
170 | AssertPtrReturn(pszValue, VERR_INVALID_POINTER);
|
---|
171 |
|
---|
172 | PRTUTF16 pwszName;
|
---|
173 | int rc = RTStrToUtf16(pszName, &pwszName);
|
---|
174 | if (RT_SUCCESS(rc))
|
---|
175 | {
|
---|
176 | PRTUTF16 pwszValue;
|
---|
177 | rc = RTStrToUtf16(pszValue, &pwszValue);
|
---|
178 | if (RT_SUCCESS(rc))
|
---|
179 | {
|
---|
180 | UINT const uRc = VBoxMsiSetProp(hMsi, pwszName, pwszValue);
|
---|
181 | if (uRc != ERROR_SUCCESS)
|
---|
182 | rc = RTErrConvertFromWin32(uRc);
|
---|
183 | RTUtf16Free(pwszValue);
|
---|
184 | }
|
---|
185 |
|
---|
186 | RTUtf16Free(pwszName);
|
---|
187 | }
|
---|
188 |
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Sets a MSI property (DWORD).
|
---|
194 | *
|
---|
195 | * Convenience function for VBoxMsiSetProp().
|
---|
196 | *
|
---|
197 | * @returns UINT
|
---|
198 | * @param hMsi MSI handle to use.
|
---|
199 | * @param pwszName Name of property to set.
|
---|
200 | * @param dwVal Value to set.
|
---|
201 | */
|
---|
202 | UINT VBoxMsiSetPropDWORD(MSIHANDLE hMsi, const WCHAR *pwszName, DWORD dwVal)
|
---|
203 | {
|
---|
204 | wchar_t wszTemp[32];
|
---|
205 | RTUtf16Printf(wszTemp, RT_ELEMENTS(wszTemp), "%u", dwVal);
|
---|
206 | return VBoxMsiSetProp(hMsi, pwszName, wszTemp);
|
---|
207 | }
|
---|