#include "Reg.h" #include #pragma region Registry Helper Functions HRESULT SetRegistryKeyAndValue(HKEY hRootKey, PCWSTR pszSubKey, PCWSTR pszValueName, PCWSTR pszData) { HRESULT hr; HKEY hKey = NULL; hr = HRESULT_FROM_WIN32(RegCreateKeyEx(hRootKey, pszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL)); if (SUCCEEDED(hr)) { if (pszData != NULL) { DWORD cbData = lstrlen(pszData) * sizeof(*pszData); hr = HRESULT_FROM_WIN32(RegSetValueEx(hKey, pszValueName, 0, REG_SZ, reinterpret_cast(pszData), cbData)); } RegCloseKey(hKey); } return hr; } HRESULT GetRegistryKeyAndValue(HKEY hRootKey, PCWSTR pszSubKey, PCWSTR pszValueName, PWSTR pszData, DWORD cbData) { HRESULT hr; HKEY hKey = NULL; hr = HRESULT_FROM_WIN32(RegOpenKeyEx(hRootKey, pszSubKey, 0, KEY_READ, &hKey)); if (SUCCEEDED(hr)) { hr = HRESULT_FROM_WIN32(RegQueryValueEx(hKey, pszValueName, NULL, NULL, reinterpret_cast(pszData), &cbData)); RegCloseKey(hKey); } return hr; } HRESULT SetHKCRRegistryKeyAndValue(PCWSTR pszSubKey, PCWSTR pszValueName, PCWSTR pszData) { return SetRegistryKeyAndValue(HKEY_CLASSES_ROOT, pszSubKey, pszValueName, pszData); } HRESULT GetHKCRRegistryKeyAndValue(PCWSTR pszSubKey, PCWSTR pszValueName, PWSTR pszData, DWORD cbData) { return GetRegistryKeyAndValue(HKEY_CLASSES_ROOT, pszSubKey, pszValueName, pszData, cbData); } #pragma endregion HRESULT RegisterInprocServer(PCWSTR pszModule, const CLSID& clsid, PCWSTR pszFriendlyName, PCWSTR pszThreadModel) { if (pszModule == NULL || pszThreadModel == NULL) return E_INVALIDARG; HRESULT hr; wchar_t szCLSID[MAX_PATH]; StringFromGUID2(clsid, szCLSID, ARRAYSIZE(szCLSID)); wchar_t szSubkey[MAX_PATH]; hr = StringCchPrintf(szSubkey, ARRAYSIZE(szSubkey), L"CLSID\\%s", szCLSID); if (SUCCEEDED(hr)) { hr = SetHKCRRegistryKeyAndValue(szSubkey, NULL, pszFriendlyName); if (SUCCEEDED(hr)) { hr = StringCchPrintf(szSubkey, ARRAYSIZE(szSubkey), L"CLSID\\%s\\InprocServer32", szCLSID); if (SUCCEEDED(hr)) { hr = SetHKCRRegistryKeyAndValue(szSubkey, NULL, pszModule); if (SUCCEEDED(hr)) { hr = SetHKCRRegistryKeyAndValue(szSubkey, L"ThreadingModel", pszThreadModel); } } } } return hr; } HRESULT UnregisterInprocServer(const CLSID& clsid) { HRESULT hr = S_OK; wchar_t szCLSID[MAX_PATH]; StringFromGUID2(clsid, szCLSID, ARRAYSIZE(szCLSID)); wchar_t szSubkey[MAX_PATH]; hr = StringCchPrintf(szSubkey, ARRAYSIZE(szSubkey), L"CLSID\\%s", szCLSID); if (SUCCEEDED(hr)) { hr = HRESULT_FROM_WIN32(RegDeleteTree(HKEY_CLASSES_ROOT, szSubkey)); } return hr; } HRESULT RegisterShellExtContextMenuHandler(PCWSTR pszFileType, const CLSID& clsid, PCWSTR pszFriendlyName) { if (pszFileType == NULL) return E_INVALIDARG; HRESULT hr; wchar_t szCLSID[MAX_PATH]; StringFromGUID2(clsid, szCLSID, ARRAYSIZE(szCLSID)); wchar_t szSubkey[MAX_PATH]; if (*pszFileType == L'.') { wchar_t szDefaultVal[260]; hr = GetHKCRRegistryKeyAndValue(pszFileType, NULL, szDefaultVal, sizeof(szDefaultVal)); if (SUCCEEDED(hr) && szDefaultVal[0] != L'\0') pszFileType = szDefaultVal; } hr = StringCchPrintf(szSubkey, ARRAYSIZE(szSubkey), L"%s\\shellex\\ContextMenuHandlers\\%s", pszFileType, pszFriendlyName); if (SUCCEEDED(hr)) hr = SetHKCRRegistryKeyAndValue(szSubkey, NULL, szCLSID); return hr; } HRESULT UnregisterShellExtContextMenuHandler(PCWSTR pszFileType, PCWSTR pszFriendlyName) { if (pszFileType == NULL) return E_INVALIDARG; HRESULT hr; wchar_t szSubkey[MAX_PATH]; if (*pszFileType == L'.') { wchar_t szDefaultVal[260]; hr = GetHKCRRegistryKeyAndValue(pszFileType, NULL, szDefaultVal, sizeof(szDefaultVal)); if (SUCCEEDED(hr) && szDefaultVal[0] != L'\0') pszFileType = szDefaultVal; } hr = StringCchPrintf(szSubkey, ARRAYSIZE(szSubkey), L"%s\\shellex\\ContextMenuHandlers\\%s", pszFileType, pszFriendlyName); if (SUCCEEDED(hr)) hr = HRESULT_FROM_WIN32(RegDeleteTree(HKEY_CLASSES_ROOT, szSubkey)); return hr; }