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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
* 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 "mozilla/UniquePtr.h"
#include "RegUtils.h"
#include <windows.h>
#include <strsafe.h>
extern std::wstring gDllPath;
const wchar_t kClsIdPrefix[] = L"CLSID\\";
const wchar_t* kExtensionSubkeys[] = {
L".zzz\\shellex\\IconHandler",
};
bool RegKey::SetStringInternal(const wchar_t* aValueName,
const wchar_t* aValueData,
DWORD aValueDataLength) {
if (!mKey) {
return false;
}
return ::RegSetValueExW(mKey, aValueName, 0, REG_SZ,
reinterpret_cast<const BYTE*>(aValueData),
aValueDataLength) == ERROR_SUCCESS;
}
RegKey::RegKey(HKEY root, const wchar_t* aSubkey) : mKey(nullptr) {
::RegCreateKeyExW(root, aSubkey, 0, nullptr, 0, KEY_ALL_ACCESS, nullptr,
&mKey, nullptr);
}
RegKey::~RegKey() {
if (mKey) {
::RegCloseKey(mKey);
}
}
bool RegKey::SetString(const wchar_t* aValueName, const wchar_t* aValueData) {
return SetStringInternal(
aValueName, aValueData,
aValueData
? static_cast<DWORD>((wcslen(aValueData) + 1) * sizeof(wchar_t))
: 0);
}
bool RegKey::SetString(const wchar_t* aValueName,
const std::wstring& aValueData) {
return SetStringInternal(
aValueName, aValueData.c_str(),
static_cast<DWORD>((aValueData.size() + 1) * sizeof(wchar_t)));
}
std::wstring RegKey::GetString(const wchar_t* aValueName) {
DWORD len = 0;
LSTATUS status = ::RegGetValueW(mKey, aValueName, nullptr, RRF_RT_REG_SZ,
nullptr, nullptr, &len);
mozilla::UniquePtr<uint8_t[]> buf = mozilla::MakeUnique<uint8_t[]>(len);
status = ::RegGetValueW(mKey, aValueName, nullptr, RRF_RT_REG_SZ, nullptr,
buf.get(), &len);
if (status != ERROR_SUCCESS) {
return L"";
}
return reinterpret_cast<wchar_t*>(buf.get());
}
ComRegisterer::ComRegisterer(const GUID& aClsId, const wchar_t* aFriendlyName)
: mClassRoot(HKEY_CURRENT_USER, L"Software\\Classes"),
mFriendlyName(aFriendlyName) {
wchar_t guidStr[64];
HRESULT hr = ::StringCbPrintfW(
guidStr, sizeof(guidStr),
L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", aClsId.Data1,
aClsId.Data2, aClsId.Data3, aClsId.Data4[0], aClsId.Data4[1],
aClsId.Data4[2], aClsId.Data4[3], aClsId.Data4[4], aClsId.Data4[5],
aClsId.Data4[6], aClsId.Data4[7]);
if (FAILED(hr)) {
return;
}
mClsId = guidStr;
}
bool ComRegisterer::UnregisterAll() {
bool isOk = true;
LSTATUS ls;
for (const wchar_t* subkey : kExtensionSubkeys) {
RegKey root(mClassRoot, subkey);
std::wstring currentHandler = root.GetString(nullptr);
if (currentHandler != mClsId) {
// If another extension is registered, don't overwrite it.
continue;
}
// Set an empty string instead of deleting the key.
if (!root.SetString(nullptr)) {
isOk = false;
}
}
std::wstring subkey(kClsIdPrefix);
subkey += mClsId;
ls = ::RegDeleteTreeW(mClassRoot, subkey.c_str());
if (ls != ERROR_SUCCESS && ls != ERROR_FILE_NOT_FOUND) {
isOk = false;
}
return isOk;
}
bool ComRegisterer::RegisterObject(const wchar_t* aThreadModel) {
std::wstring subkey(kClsIdPrefix);
subkey += mClsId;
RegKey root(mClassRoot, subkey.c_str());
if (!root || !root.SetString(nullptr, mFriendlyName)) {
return false;
}
RegKey inproc(root, L"InprocServer32");
return inproc && inproc.SetString(nullptr, gDllPath) &&
inproc.SetString(L"ThreadingModel", aThreadModel);
}
bool ComRegisterer::RegisterExtensions() {
for (const wchar_t* subkey : kExtensionSubkeys) {
RegKey root(mClassRoot, subkey);
std::wstring currentHandler = root.GetString(nullptr);
if (!currentHandler.empty()) {
// If another extension is registered, don't overwrite it.
continue;
}
if (!root.SetString(nullptr, mClsId)) {
return false;
}
}
return true;
}
|