1 | /* $Id: VBoxCommon.cpp 93340 2022-01-19 10:52:09Z 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-2022 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 |
|
---|
21 | /*********************************************************************************************************************************
|
---|
22 | * Header Files *
|
---|
23 | *********************************************************************************************************************************/
|
---|
24 | #include <iprt/win/windows.h>
|
---|
25 |
|
---|
26 | #include <tchar.h>
|
---|
27 | #include <stdio.h>
|
---|
28 |
|
---|
29 | #include <msi.h>
|
---|
30 | #include <msiquery.h>
|
---|
31 |
|
---|
32 | #include <iprt/utf16.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | #if (_MSC_VER < 1400) /* Provide swprintf_s to VC < 8.0. */
|
---|
36 | int swprintf_s(WCHAR *buffer, size_t cbBuffer, const WCHAR *format, ...)
|
---|
37 | {
|
---|
38 | int ret;
|
---|
39 | va_list va;
|
---|
40 | va_start(va, format);
|
---|
41 | ret = _vsnwprintf(buffer, cbBuffer, format, va);
|
---|
42 | va_end(va);
|
---|
43 | return ret;
|
---|
44 | }
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | UINT VBoxGetMsiProp(MSIHANDLE hMsi, WCHAR *pwszName, WCHAR *pwszValue, DWORD dwSize)
|
---|
48 | {
|
---|
49 | DWORD dwBuffer = 0;
|
---|
50 | UINT uiRet = MsiGetPropertyW(hMsi, pwszName, L"", &dwBuffer);
|
---|
51 | if (uiRet == ERROR_MORE_DATA)
|
---|
52 | {
|
---|
53 | ++dwBuffer; /* On output does not include terminating null, so add 1. */
|
---|
54 |
|
---|
55 | if (dwBuffer > dwSize)
|
---|
56 | return ERROR_MORE_DATA;
|
---|
57 |
|
---|
58 | ZeroMemory(pwszValue, dwSize);
|
---|
59 | uiRet = MsiGetPropertyW(hMsi, pwszName, pwszValue, &dwBuffer);
|
---|
60 | }
|
---|
61 | return uiRet;
|
---|
62 | }
|
---|
63 |
|
---|
64 | #if 0 /* unused */
|
---|
65 | /**
|
---|
66 | * Retrieves a MSI property (in UTF-8).
|
---|
67 | *
|
---|
68 | * Convenience function for VBoxGetMsiProp().
|
---|
69 | *
|
---|
70 | * @returns VBox status code.
|
---|
71 | * @param hMsi MSI handle to use.
|
---|
72 | * @param pcszName Name of property to retrieve.
|
---|
73 | * @param ppszValue Where to store the allocated value on success.
|
---|
74 | * Must be free'd using RTStrFree() by the caller.
|
---|
75 | */
|
---|
76 | int VBoxGetMsiPropUtf8(MSIHANDLE hMsi, const char *pcszName, char **ppszValue)
|
---|
77 | {
|
---|
78 | PRTUTF16 pwszName;
|
---|
79 | int rc = RTStrToUtf16(pcszName, &pwszName);
|
---|
80 | if (RT_SUCCESS(rc))
|
---|
81 | {
|
---|
82 | WCHAR wszValue[1024]; /* 1024 should be enough for everybody (tm). */
|
---|
83 | if (VBoxGetMsiProp(hMsi, pwszName, wszValue, sizeof(wszValue)) == ERROR_SUCCESS)
|
---|
84 | {
|
---|
85 | rc = RTUtf16ToUtf8(wszValue, ppszValue);
|
---|
86 | }
|
---|
87 | else
|
---|
88 | rc = VERR_NOT_FOUND;
|
---|
89 |
|
---|
90 | RTUtf16Free(pwszName);
|
---|
91 | }
|
---|
92 |
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | UINT VBoxSetMsiProp(MSIHANDLE hMsi, WCHAR *pwszName, WCHAR *pwszValue)
|
---|
98 | {
|
---|
99 | return MsiSetPropertyW(hMsi, pwszName, pwszValue);
|
---|
100 | }
|
---|
101 |
|
---|