2014-07-30 19:20:55 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
#include "NCOverlayRegistrationHandler.h"
|
2015-02-04 21:54:34 +03:00
|
|
|
|
2020-08-17 21:35:39 +03:00
|
|
|
#include <windows.h>
|
2020-08-17 22:28:18 +03:00
|
|
|
#include <objbase.h>
|
2014-07-30 19:20:55 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
#define REGISTRY_OVERLAY_KEY LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers)"
|
|
|
|
#define REGISTRY_CLSID L"CLSID"
|
|
|
|
#define REGISTRY_IN_PROCESS L"InprocServer32"
|
|
|
|
#define REGISTRY_THREADING L"ThreadingModel"
|
|
|
|
#define REGISTRY_APARTMENT L"Apartment"
|
|
|
|
#define REGISTRY_VERSION L"Version"
|
|
|
|
#define REGISTRY_VERSION_NUMBER L"1.0"
|
|
|
|
|
2014-07-30 19:20:55 +04:00
|
|
|
using namespace std;
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
HRESULT NCOverlayRegistrationHandler::MakeRegistryEntries(const CLSID& clsid, PCWSTR friendlyName)
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
2017-01-05 19:28:28 +03:00
|
|
|
HRESULT hResult;
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY shellOverlayKey = nullptr;
|
2017-01-05 19:28:28 +03:00
|
|
|
// the key may not exist yet
|
2020-06-05 01:51:32 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_OVERLAY_KEY, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &shellOverlayKey, nullptr));
|
2017-01-05 19:28:28 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
hResult = RegCreateKey(HKEY_LOCAL_MACHINE, REGISTRY_OVERLAY_KEY, &shellOverlayKey);
|
|
|
|
if(!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY syncExOverlayKey = nullptr;
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(shellOverlayKey, friendlyName, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &syncExOverlayKey, nullptr));
|
2017-01-05 19:28:28 +03:00
|
|
|
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t stringCLSID[MAX_PATH];
|
2014-07-30 19:20:55 +04:00
|
|
|
StringFromGUID2(clsid, stringCLSID, ARRAYSIZE(stringCLSID));
|
2017-01-05 19:28:28 +03:00
|
|
|
LPCTSTR value = stringCLSID;
|
2020-06-05 01:51:32 +03:00
|
|
|
hResult = RegSetValueEx(syncExOverlayKey, nullptr, 0, REG_SZ, (LPBYTE)value, (DWORD)((wcslen(value)+1) * sizeof(TCHAR)));
|
2017-01-05 19:28:28 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
2014-07-30 19:20:55 +04:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
HRESULT NCOverlayRegistrationHandler::RemoveRegistryEntries(PCWSTR friendlyName)
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
2017-01-05 19:28:28 +03:00
|
|
|
HRESULT hResult;
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY shellOverlayKey = nullptr;
|
2017-01-05 19:28:28 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_OVERLAY_KEY, 0, KEY_WRITE, &shellOverlayKey));
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY syncExOverlayKey = nullptr;
|
2017-01-05 19:28:28 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegDeleteKey(shellOverlayKey, friendlyName));
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
2014-07-30 19:20:55 +04:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
HRESULT NCOverlayRegistrationHandler::RegisterCOMObject(PCWSTR modulePath, PCWSTR friendlyName, const CLSID& clsid)
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
2020-06-10 04:47:49 +03:00
|
|
|
if (!modulePath) {
|
2014-07-30 19:20:55 +04:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t stringCLSID[MAX_PATH];
|
|
|
|
StringFromGUID2(clsid, stringCLSID, ARRAYSIZE(stringCLSID));
|
2017-01-05 19:28:28 +03:00
|
|
|
HRESULT hResult;
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY hKey = nullptr;
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_CLASSES_ROOT, REGISTRY_CLSID, 0, KEY_WRITE, &hKey));
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY clsidKey = nullptr;
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(hKey, stringCLSID, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &clsidKey, nullptr));
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegSetValue(clsidKey, nullptr, REG_SZ, friendlyName, (DWORD) wcslen(friendlyName)));
|
2014-08-19 17:50:10 +04:00
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY inprocessKey = nullptr;
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(clsidKey, REGISTRY_IN_PROCESS, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &inprocessKey, nullptr));
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegSetValue(inprocessKey, nullptr, REG_SZ, modulePath, (DWORD) wcslen(modulePath)));
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegSetValueEx(inprocessKey, REGISTRY_THREADING, 0, REG_SZ, (LPBYTE)REGISTRY_APARTMENT, (DWORD)((wcslen(REGISTRY_APARTMENT)+1) * sizeof(TCHAR))));
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
HKEY versionKey = nullptr;
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(clsidKey, REGISTRY_VERSION, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &versionKey, nullptr));
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegSetValueEx(versionKey, nullptr, 0, REG_SZ, (LPBYTE)REGISTRY_VERSION_NUMBER, (DWORD)(wcslen(REGISTRY_VERSION_NUMBER)+1) * sizeof(TCHAR)));
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
2014-07-30 19:20:55 +04:00
|
|
|
|
2017-01-05 19:28:28 +03:00
|
|
|
return S_OK;
|
2014-07-30 19:20:55 +04:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:08 +03:00
|
|
|
HRESULT NCOverlayRegistrationHandler::UnregisterCOMObject(const CLSID& clsid)
|
2014-07-30 19:20:55 +04:00
|
|
|
{
|
|
|
|
wchar_t stringCLSID[MAX_PATH];
|
|
|
|
|
|
|
|
StringFromGUID2(clsid, stringCLSID, ARRAYSIZE(stringCLSID));
|
2017-01-05 19:28:28 +03:00
|
|
|
HRESULT hResult;
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY hKey = nullptr;
|
2017-01-05 19:28:28 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_CLASSES_ROOT, REGISTRY_CLSID, 0, DELETE, &hKey));
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
HKEY clsidKey = nullptr;
|
2017-01-05 19:28:28 +03:00
|
|
|
hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(hKey, stringCLSID, 0, DELETE, &clsidKey));
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegDeleteKey(clsidKey, REGISTRY_IN_PROCESS));
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegDeleteKey(clsidKey, REGISTRY_VERSION));
|
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
hResult = HRESULT_FROM_WIN32(RegDeleteKey(hKey, stringCLSID));
|
2020-08-18 20:11:08 +03:00
|
|
|
if (!SUCCEEDED(hResult)) {
|
2017-01-05 19:28:28 +03:00
|
|
|
return hResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2020-06-05 01:51:32 +03:00
|
|
|
}
|