summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutthirdparty/MsiDatabase.cpp
blob: 6c667072b7fb3207da04424404695eb987e9ce3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MsiDatabase.h"

#ifdef UNICODE
#  define MSIDBOPEN_READONLY_W MSIDBOPEN_READONLY
#  define INSTALLPROPERTY_LOCALPACKAGE_W INSTALLPROPERTY_LOCALPACKAGE
#else
// MSIDBOPEN_READONLY is defined as `(LPCTSTR)0` in msiquery.h, so we need to
// cast it to LPCWSTR.
#  define MSIDBOPEN_READONLY_W reinterpret_cast<LPCWSTR>(MSIDBOPEN_READONLY)
// INSTALLPROPERTY_LOCALPACKAGE is defined as `__TEXT("LocalPackage")` in msi.h,
// so we need to define a wchar_t version.
#  define INSTALLPROPERTY_LOCALPACKAGE_W L"LocalPackage"
#endif  // UNICODE

namespace mozilla {

/*static*/
UniquePtr<wchar_t[]> MsiDatabase::GetRecordString(MSIHANDLE aRecord,
                                                  UINT aFieldIndex) {
  // The 3rd parameter of MsiRecordGetStringW must not be nullptr.
  wchar_t kEmptyString[] = L"";
  DWORD len = 0;
  UINT ret = ::MsiRecordGetStringW(aRecord, aFieldIndex, kEmptyString, &len);
  if (ret != ERROR_MORE_DATA) {
    return nullptr;
  }

  // |len| returned from MsiRecordGetStringW does not include
  // a null-character, but a length to pass to MsiRecordGetStringW
  // needs to include a null-character.
  ++len;

  auto buf = MakeUnique<wchar_t[]>(len);
  ret = ::MsiRecordGetStringW(aRecord, aFieldIndex, buf.get(), &len);
  if (ret != ERROR_SUCCESS) {
    return nullptr;
  }

  return buf;
}

MsiDatabase::MsiDatabase(const wchar_t* aDatabasePath) {
  MSIHANDLE handle = 0;
  UINT ret = ::MsiOpenDatabaseW(aDatabasePath, MSIDBOPEN_READONLY_W, &handle);
  if (ret != ERROR_SUCCESS) {
    return;
  }

  mDatabase.own(handle);
}

Maybe<MsiDatabase> MsiDatabase::FromProductId(const wchar_t* aProductId) {
  DWORD len = MAX_PATH;
  wchar_t bufStack[MAX_PATH];
  UINT ret = ::MsiGetProductInfoW(aProductId, INSTALLPROPERTY_LOCALPACKAGE_W,
                                  bufStack, &len);
  if (ret == ERROR_SUCCESS) {
    return Some(MsiDatabase(bufStack));
  }

  if (ret != ERROR_MORE_DATA) {
    return Nothing();
  }

  // |len| returned from MsiGetProductInfoW does not include
  // a null-character, but a length to pass to MsiGetProductInfoW
  // needs to include a null-character.
  ++len;

  std::unique_ptr<wchar_t[]> bufHeap(new wchar_t[len]);
  ret = ::MsiGetProductInfoW(aProductId, INSTALLPROPERTY_LOCALPACKAGE_W,
                             bufHeap.get(), &len);
  if (ret == ERROR_SUCCESS) {
    return Some(MsiDatabase(bufHeap.get()));
  }

  return Nothing();
}

MsiDatabase::operator bool() const { return !!mDatabase; }

}  // namespace mozilla