Minor cleanup
[TortoiseGit.git] / src / GitWCRev / Register.cpp
blob678cbab5a5b4c9a978479e551989e40ac7293c85
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2017 - TortoiseGit
4 // Copyright (C) 2005-2008, 2010-2011, 2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include <objbase.h> // ATL base
22 #include <atlbase.h>
23 #include <assert.h>
25 #include "Register.h"
28 // Internal helper functions prototypes
31 // Set the given key and its value.
32 BOOL setKeyAndValue(const TCHAR* pszPath, const TCHAR* szSubkey, const TCHAR* szValue);
35 BOOL setValue(const TCHAR* szKey, const TCHAR* szEntry, const TCHAR* szValue);
38 // Convert a CLSID into a char string.
39 void CLSIDtochar(const CLSID& clsid, TCHAR* szCLSID, int length);
41 // Delete szKeyChild and all of its descendants.
42 LONG recursiveDeleteKey(HKEY hKeyParent, const TCHAR* szKeyChild);
44 ////////////////////////////////////////////////////////
46 // Constants
49 // Size of a CLSID as a string
50 const int CLSID_STRING_SIZE = 39;
52 /////////////////////////////////////////////////////////
54 // Public function implementation
58 // Register the component in the registry.
60 HRESULT RegisterServer(HMODULE hModule, // DLL module handle
61 const CLSID& clsid, // Class ID
62 const TCHAR* szFriendlyName, // Friendly Name
63 const TCHAR* szVerIndProgID, // Programmatic
64 const TCHAR* szProgID,
65 const CLSID& libid) // Type lib ID
67 // Get server location.
68 TCHAR szModule[1024] = { 0 };
69 ::GetModuleFileName(hModule, szModule, _countof(szModule) - 1);
71 wcscat_s(szModule, L" /automation");
72 // Convert the CLSID into a TCHAR.
73 TCHAR szCLSID[CLSID_STRING_SIZE];
74 CLSIDtochar(clsid, szCLSID, _countof(szCLSID));
75 TCHAR szLIBID[CLSID_STRING_SIZE];
76 CLSIDtochar(libid, szLIBID, _countof(szLIBID));
78 // Build the key CLSID\\{...}
79 TCHAR szKey[64];
80 wcscpy_s(szKey, L"CLSID\\");
81 wcscat_s(szKey, szCLSID);
83 // Add the CLSID to the registry.
84 setKeyAndValue(szKey, nullptr, szFriendlyName);
86 // Add the server filename subkey under the CLSID key.
87 setKeyAndValue(szKey, L"LocalServer32", szModule);
89 // Add the ProgID subkey under the CLSID key.
90 setKeyAndValue(szKey, L"ProgID", szProgID);
92 // Add the version-independent ProgID subkey under CLSID key.
93 setKeyAndValue(szKey, L"VersionIndependentProgID", szVerIndProgID);
95 // Add the typelib
96 setKeyAndValue(szKey, L"TypeLib", szLIBID);
99 // Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT.
100 setKeyAndValue(szVerIndProgID, nullptr, szFriendlyName);
101 setKeyAndValue(szVerIndProgID, L"CLSID", szCLSID);
102 setKeyAndValue(szVerIndProgID, L"CurVer", szProgID);
104 // Add the versioned ProgID subkey under HKEY_CLASSES_ROOT.
105 setKeyAndValue(szProgID, nullptr, szFriendlyName);
106 setKeyAndValue(szProgID, L"CLSID", szCLSID);
108 // add TypeLib keys
109 wcscpy_s(szKey, L"TypeLib\\");
110 wcscat_s(szKey, szLIBID);
112 // Add the CLSID to the registry.
113 setKeyAndValue(szKey, nullptr, nullptr);
114 wcscat_s(szKey, L"\\1.0");
115 setKeyAndValue(szKey, nullptr, szFriendlyName);
116 wcscat_s(szKey, L"\\0");
117 setKeyAndValue(szKey, nullptr, nullptr);
118 wcscat_s(szKey, L"\\win32");
119 setKeyAndValue(szKey, nullptr, szModule);
121 return S_OK;
124 void RegisterInterface(HMODULE hModule, // DLL module handle
125 const CLSID& clsid, // Class ID
126 const TCHAR* szFriendlyName, // Friendly Name
127 const CLSID& libid,
128 const IID& iid)
130 // Get server location.
131 TCHAR szModule[512] = { 0 };
132 ::GetModuleFileName(hModule, szModule, _countof(szModule) - 1);
134 // Convert the CLSID into a TCHAR.
135 TCHAR szCLSID[CLSID_STRING_SIZE];
136 CLSIDtochar(clsid, szCLSID, _countof(szCLSID));
137 TCHAR szLIBID[CLSID_STRING_SIZE];
138 CLSIDtochar(libid, szLIBID, _countof(szCLSID));
139 TCHAR szIID[CLSID_STRING_SIZE];
140 CLSIDtochar(iid, szIID, _countof(szCLSID));
142 // Build the key Interface\\{...}
143 TCHAR szKey[64];
144 wcscpy_s(szKey, L"Interface\\");
145 wcscat_s(szKey, szIID);
147 // Add the value to the registry.
148 setKeyAndValue(szKey, nullptr, szFriendlyName);
150 TCHAR szKey2[MAX_PATH] = { 0 };
151 wcscpy_s(szKey2, szKey);
152 wcscat_s(szKey2, L"\\ProxyStubClsID");
153 // Add the server filename subkey under the IID key.
154 setKeyAndValue(szKey2, nullptr, L"{00020424-0000-0000-C000-000000000046}"); //IUnknown
156 wcscpy_s(szKey2, szKey);
157 wcscat_s(szKey2, L"\\ProxyStubClsID32");
158 // Add the server filename subkey under the IID key.
159 setKeyAndValue(szKey2, nullptr, L"{00020424-0000-0000-C000-000000000046}"); //IUnknown
161 wcscpy_s(szKey2, szKey);
162 wcscat_s(szKey2, L"\\TypeLib");
163 // Add the server filename subkey under the CLSID key.
164 setKeyAndValue(szKey2, nullptr, szLIBID);
166 setValue(szKey2, L"Version", L"1.0");
169 void UnregisterInterface(const IID& iid)
171 TCHAR szIID[CLSID_STRING_SIZE];
172 CLSIDtochar(iid, szIID, _countof(szIID));
174 // Build the key Interface\\{...}
175 TCHAR szKey[64];
176 wcscpy_s(szKey, L"Interface\\");
177 wcscat_s(szKey, szIID);
179 recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey);
183 // Remove the component from the registry.
185 LONG UnregisterServer(const CLSID& clsid, // Class ID
186 const TCHAR* szVerIndProgID, // Programmatic
187 const TCHAR* szProgID,
188 const CLSID& libid) // Type lib ID
190 // Convert the CLSID into a TCHAR.
191 TCHAR szCLSID[CLSID_STRING_SIZE];
192 CLSIDtochar(clsid, szCLSID, _countof(szCLSID));
194 // Build the key CLSID\\{...}
195 TCHAR szKey[64];
196 wcscpy_s(szKey, L"CLSID\\");
197 wcscat_s(szKey, szCLSID);
199 // Delete the CLSID Key - CLSID\{...}
200 LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey);
201 assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.
203 // Delete the version-independent ProgID Key.
204 lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID);
205 assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.
207 // Delete the ProgID key.
208 lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID);
209 assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.
211 TCHAR szLIBID[CLSID_STRING_SIZE];
212 CLSIDtochar(libid, szLIBID, _countof(szLIBID));
214 wcscpy_s(szKey, L"TypeLib\\");
215 wcscat_s(szKey, szLIBID);
217 // Delete the TypeLib Key - LIBID\{...}
218 lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey);
219 assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.
221 return S_OK;
224 ///////////////////////////////////////////////////////////
226 // Internal helper functions
229 // Convert a CLSID to a TCHAR string.
230 void CLSIDtochar(const CLSID& clsid, TCHAR* szCLSID, int length)
232 assert(length >= CLSID_STRING_SIZE);
233 // Get CLSID
234 LPOLESTR wszCLSID = nullptr;
235 StringFromCLSID(clsid, &wszCLSID);
237 // Covert from wide characters to non-wide.
238 wcscpy_s(szCLSID, length, wszCLSID);
240 // Free memory.
241 CoTaskMemFree(wszCLSID);
245 // Delete a key and all of its descendants.
247 LONG recursiveDeleteKey(HKEY hKeyParent, // Parent of key to delete
248 const TCHAR* lpszKeyChild) // Key to delete
250 // Open the child.
251 HKEY hKeyChild;
252 LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0, KEY_ALL_ACCESS, &hKeyChild);
253 if (lRes != ERROR_SUCCESS)
254 return lRes;
256 // Enumerate all of the descendants of this child.
257 FILETIME time;
258 TCHAR szBuffer[256];
259 DWORD dwSize = _countof(szBuffer);
260 while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, nullptr, nullptr, nullptr, &time) == S_OK)
262 // Delete the descendants of this child.
263 lRes = recursiveDeleteKey(hKeyChild, szBuffer);
264 if (lRes != ERROR_SUCCESS)
266 // Cleanup before exiting.
267 RegCloseKey(hKeyChild);
268 return lRes;
270 dwSize = _countof(szBuffer);
273 // Close the child.
274 RegCloseKey(hKeyChild);
276 // Delete this child.
277 return RegDeleteKey(hKeyParent, lpszKeyChild);
281 // Create a key and set its value.
282 // - This helper function was borrowed and modified from
283 // Kraig Brockschmidt's book Inside OLE.
285 BOOL setKeyAndValue(const TCHAR* szKey, const TCHAR* szSubkey, const TCHAR* szValue)
287 HKEY hKey;
288 TCHAR szKeyBuf[1024];
290 // Copy key name into buffer.
291 wcscpy_s(szKeyBuf, szKey);
293 // Add subkey name to buffer.
294 if (szSubkey)
296 wcscat_s(szKeyBuf, L"\\");
297 wcscat_s(szKeyBuf, szSubkey);
300 // Create and open key and subkey.
301 long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT, szKeyBuf, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &hKey, nullptr);
302 if (lResult != ERROR_SUCCESS)
303 return FALSE;
305 // Set the Value.
306 if (szValue)
307 RegSetValueEx(hKey, nullptr, 0, REG_SZ, (BYTE*)szValue, DWORD((1 + wcslen(szValue)) * sizeof(TCHAR)));
309 RegCloseKey(hKey);
310 return TRUE;
313 BOOL setValue(const TCHAR* szKey, const TCHAR* szEntry, const TCHAR* szValue)
315 HKEY hKey;
317 // Create and open key and subkey.
318 long lResult = RegOpenKeyEx(HKEY_CLASSES_ROOT , szKey, 0, KEY_ALL_ACCESS, &hKey);
319 if (lResult != ERROR_SUCCESS)
320 return FALSE;
322 // Set the Value.
323 if (szValue)
324 RegSetValueEx(hKey, szEntry, 0, REG_SZ, (BYTE*)szValue, DWORD((1 + wcslen(szValue)) * sizeof(TCHAR)));
326 RegCloseKey(hKey);
328 return TRUE;
331 /////////////////////////////////////////////////////////////////////////////
332 // TypeLib registration
334 HRESULT LoadTypeLib(HINSTANCE hInstTypeLib, LPCOLESTR lpszIndex, BSTR* pbstrPath, ITypeLib** ppTypeLib)
336 ATLASSERT(pbstrPath && ppTypeLib);
337 if (!pbstrPath || !ppTypeLib)
338 return E_POINTER;
340 *pbstrPath = nullptr;
341 *ppTypeLib = nullptr;
343 USES_CONVERSION;
344 ATLASSERT(hInstTypeLib);
345 TCHAR szModule[MAX_PATH+10] = { 0 };
347 DWORD dwFLen = ::GetModuleFileName(hInstTypeLib, szModule, MAX_PATH);
348 if (dwFLen == 0)
349 return HRESULT_FROM_WIN32(::GetLastError());
350 else if (dwFLen == MAX_PATH)
351 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
353 // get the extension pointer in case of fail
354 LPTSTR lpszExt = ::PathFindExtension(szModule);
356 if (lpszIndex)
357 lstrcat(szModule, OLE2CT(lpszIndex));
358 LPOLESTR lpszModule = T2OLE(szModule);
359 HRESULT hr = ::LoadTypeLib(lpszModule, ppTypeLib);
360 if (FAILED(hr))
362 // typelib not in module, try <module>.tlb instead
363 lstrcpy(lpszExt, L".tlb");
364 lpszModule = T2OLE(szModule);
365 hr = ::LoadTypeLib(lpszModule, ppTypeLib);
367 if (SUCCEEDED(hr))
369 *pbstrPath = ::SysAllocString(lpszModule);
370 if (!*pbstrPath)
371 hr = E_OUTOFMEMORY;
373 return hr;
376 static inline UINT WINAPI GetDirLen(LPCOLESTR lpszPathName) throw()
378 ATLASSERT(lpszPathName);
380 // always capture the complete file name including extension (if present)
381 LPCOLESTR lpszTemp = lpszPathName;
382 for (LPCOLESTR lpsz = lpszPathName; *lpsz;)
384 LPCOLESTR lp = CharNextW(lpsz);
385 // remember last directory/drive separator
386 if (*lpsz == OLESTR('\\') || *lpsz == OLESTR('/') || *lpsz == OLESTR(':'))
387 lpszTemp = lp;
388 lpsz = lp;
391 return UINT(lpszTemp-lpszPathName);
394 HRESULT RegisterTypeLib(HINSTANCE hInstTypeLib, LPCOLESTR lpszIndex)
396 CComBSTR bstrPath;
397 CComPtr<ITypeLib> pTypeLib;
398 HRESULT hr = LoadTypeLib(hInstTypeLib, lpszIndex, &bstrPath, &pTypeLib);
399 if (SUCCEEDED(hr))
401 OLECHAR szDir[MAX_PATH] = { 0 };
402 ocscpy_s(szDir, _countof(szDir), bstrPath);
403 // If index is specified remove it from the path
404 if (lpszIndex)
406 size_t nLenPath = ocslen(szDir);
407 size_t nLenIndex = ocslen(lpszIndex);
408 if (memcmp(szDir + nLenPath - nLenIndex, lpszIndex, nLenIndex) == 0)
409 szDir[nLenPath - nLenIndex] = 0;
411 szDir[GetDirLen(szDir)] = '\0';
412 hr = ::RegisterTypeLib(pTypeLib, bstrPath, szDir);
414 return hr;
417 HRESULT UnRegisterTypeLib(HINSTANCE hInstTypeLib, LPCOLESTR lpszIndex)
419 CComBSTR bstrPath;
420 CComPtr<ITypeLib> pTypeLib;
421 HRESULT hr = LoadTypeLib(hInstTypeLib, lpszIndex, &bstrPath, &pTypeLib);
422 if (SUCCEEDED(hr))
424 TLIBATTR* ptla = 0;
425 hr = pTypeLib->GetLibAttr(&ptla);
426 if ((SUCCEEDED(hr)) && (ptla != 0))
428 hr = ::UnRegisterTypeLib(ptla->guid, ptla->wMajorVerNum, ptla->wMinorVerNum, ptla->lcid, ptla->syskind);
429 pTypeLib->ReleaseTLibAttr(ptla);
432 return hr;