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