1 | /* $Id: VBoxCommon.cpp 106321 2024-10-15 13:06:30Z 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 | PRTUTF16 pwszName;
|
---|
133 | int rc = RTStrToUtf16(pszName, &pwszName);
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | {
|
---|
136 | char *pszTemp;
|
---|
137 | rc = VBoxMsiQueryPropUtf8(hMsi, pszName, &pszTemp);
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | {
|
---|
140 | *pdwValue = RTStrToInt32(pszTemp);
|
---|
141 | RTStrFree(pszTemp);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 | UINT VBoxMsiSetProp(MSIHANDLE hMsi, const WCHAR *pwszName, const WCHAR *pwszValue)
|
---|
149 | {
|
---|
150 | return MsiSetPropertyW(hMsi, pwszName, pwszValue);
|
---|
151 | }
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | UINT VBoxMsiSetPropDWORD(MSIHANDLE hMsi, const WCHAR *pwszName, DWORD dwVal)
|
---|
155 | {
|
---|
156 | wchar_t wszTemp[32];
|
---|
157 | RTUtf16Printf(wszTemp, RT_ELEMENTS(wszTemp), "%u", dwVal);
|
---|
158 | return VBoxMsiSetProp(hMsi, pwszName, wszTemp);
|
---|
159 | }
|
---|
160 |
|
---|