|
| 1 | +/* |
| 2 | + * PROJECT: NanaZip |
| 3 | + * FILE: NanaZip.Extras.ShellExtension.cpp |
| 4 | + * PURPOSE: Entry point and self registration code |
| 5 | + * |
| 6 | + * LICENSE: The MIT License |
| 7 | + * |
| 8 | + * DEVELOPER: dinhngtu (contact@tudinh.xyz) |
| 9 | + */ |
| 10 | + |
| 11 | +#define WIN32_LEAN_AND_MEAN |
| 12 | + |
| 13 | +#include <string> |
| 14 | +#include <windows.h> |
| 15 | + |
| 16 | +#include <wil/registry.h> |
| 17 | +#include <wil/win32_helpers.h> |
| 18 | +#include <wil/stl.h> |
| 19 | + |
| 20 | +#include "CNanaZipCopyHook.hpp" |
| 21 | + |
| 22 | +using namespace std::string_literals; |
| 23 | + |
| 24 | +static HINSTANCE g_hInstance = NULL; |
| 25 | + |
| 26 | +BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { |
| 27 | + UNREFERENCED_PARAMETER(lpReserved); |
| 28 | + |
| 29 | + switch (ul_reason_for_call) { |
| 30 | + case DLL_PROCESS_ATTACH: |
| 31 | + g_hInstance = hModule; |
| 32 | + break; |
| 33 | + case DLL_THREAD_ATTACH: |
| 34 | + case DLL_THREAD_DETACH: |
| 35 | + case DLL_PROCESS_DETACH: |
| 36 | + break; |
| 37 | + } |
| 38 | + return TRUE; |
| 39 | +} |
| 40 | + |
| 41 | +struct DECLSPEC_UUID("{542CE69A-6EA7-4D77-9B8F-8F56CEA2BF16}") CNanaZipLegacyContextMenuFactory |
| 42 | + : winrt::implements<CNanaZipLegacyContextMenuFactory, IClassFactory> { |
| 43 | + IFACEMETHODIMP CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject) WIN_NOEXCEPT { |
| 44 | + if (pUnkOuter) |
| 45 | + return CLASS_E_NOAGGREGATION; |
| 46 | + |
| 47 | + try { |
| 48 | + return winrt::make<CNanaZipCopyHook>()->QueryInterface(riid, ppvObject); |
| 49 | + } catch (...) { |
| 50 | + return winrt::to_hresult(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + IFACEMETHODIMP LockServer(BOOL fLock) WIN_NOEXCEPT { |
| 55 | + if (fLock) |
| 56 | + ++winrt::get_module_lock(); |
| 57 | + else |
| 58 | + --winrt::get_module_lock(); |
| 59 | + return S_OK; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +_Use_decl_annotations_ STDAPI DllCanUnloadNow() { |
| 64 | + if (winrt::get_module_lock()) |
| 65 | + return S_FALSE; |
| 66 | + winrt::clear_factory_cache(); |
| 67 | + return S_OK; |
| 68 | +} |
| 69 | + |
| 70 | +_Use_decl_annotations_ STDAPI DllGetClassObject(_In_ REFCLSID rclsid, _In_ REFIID riid, _Outptr_ LPVOID *ppv) { |
| 71 | + try { |
| 72 | + *ppv = NULL; |
| 73 | + if (rclsid == __uuidof(CNanaZipLegacyContextMenuFactory)) |
| 74 | + return winrt::make<CNanaZipLegacyContextMenuFactory>()->QueryInterface(riid, ppv); |
| 75 | + return E_INVALIDARG; |
| 76 | + } catch (...) { |
| 77 | + return winrt::to_hresult(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +_Use_decl_annotations_ STDAPI DllRegisterServer() { |
| 82 | + try { |
| 83 | + wchar_t clsid[wil::guid_string_buffer_length]; |
| 84 | + |
| 85 | + if (!StringFromGUID2(__uuidof(CNanaZipLegacyContextMenuFactory), clsid, ARRAYSIZE(clsid))) |
| 86 | + winrt::throw_hresult(E_OUTOFMEMORY); |
| 87 | + |
| 88 | + auto clsidKeyName = L"Software\\Classes\\CLSID\\"s + clsid; |
| 89 | + auto clsidKey = |
| 90 | + wil::reg::create_unique_key(HKEY_CURRENT_USER, clsidKeyName.c_str(), wil::reg::key_access::readwrite); |
| 91 | + |
| 92 | + auto serverKey = |
| 93 | + wil::reg::create_unique_key(clsidKey.get(), L"InprocServer32", wil::reg::key_access::readwrite); |
| 94 | + auto dllPath = wil::GetModuleFileNameW<std::wstring>(g_hInstance); |
| 95 | + wil::reg::set_value_string(serverKey.get(), NULL, dllPath.c_str()); |
| 96 | + wil::reg::set_value_string(serverKey.get(), L"ThreadingModel", L"Apartment"); |
| 97 | + |
| 98 | + auto handlerKeyName = L"Software\\Classes\\Directory\\shellex\\CopyHookHandlers\\"s + clsid; |
| 99 | + auto handlerKey = |
| 100 | + wil::reg::create_unique_key(HKEY_CURRENT_USER, handlerKeyName.c_str(), wil::reg::key_access::readwrite); |
| 101 | + wil::reg::set_value_string(handlerKey.get(), NULL, clsid); |
| 102 | + |
| 103 | + return S_OK; |
| 104 | + } catch (...) { |
| 105 | + return winrt::to_hresult(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +_Use_decl_annotations_ STDAPI DllUnregisterServer() { |
| 110 | + try { |
| 111 | + wchar_t clsid[wil::guid_string_buffer_length]; |
| 112 | + |
| 113 | + if (!StringFromGUID2(__uuidof(CNanaZipLegacyContextMenuFactory), clsid, ARRAYSIZE(clsid))) |
| 114 | + winrt::throw_hresult(E_OUTOFMEMORY); |
| 115 | + |
| 116 | + auto handlerKeyName = L"Software\\Classes\\Directory\\shellex\\CopyHookHandlers\\"s + clsid; |
| 117 | + RegDeleteKeyExW(HKEY_CURRENT_USER, handlerKeyName.c_str(), 0, 0); |
| 118 | + |
| 119 | + auto serverKeyName = L"Software\\Classes\\CLSID\\"s + clsid + L"\\InprocServer32"; |
| 120 | + RegDeleteKeyExW(HKEY_CURRENT_USER, serverKeyName.c_str(), 0, 0); |
| 121 | + |
| 122 | + auto clsidKeyName = L"Software\\Classes\\CLSID\\"s + clsid; |
| 123 | + RegDeleteKeyExW(HKEY_CURRENT_USER, clsidKeyName.c_str(), 0, 0); |
| 124 | + |
| 125 | + return S_OK; |
| 126 | + } catch (...) { |
| 127 | + return winrt::to_hresult(); |
| 128 | + } |
| 129 | +} |
0 commit comments