Register Cheetah as a Column Handler for folders
[git-cheetah/bosko.git] / dll.c
blob48d0de52e39157af311e0344d6dfe6505c5c31d6
1 #include <shlobj.h>
2 #include <stdio.h>
3 #include "dll.h"
4 #include "menuengine.h"
5 #include "ext.h"
6 #include "factory.h"
7 #include "systeminfo.h"
8 #include "registry.h"
11 * The following is just the necessary infrastructure for having a .dll
12 * which can be registered as a COM object.
14 static HINSTANCE hInst;
16 HRESULT PASCAL DllGetClassObject(REFCLSID obj_guid, REFIID factory_guid,
17 void **factory_handle)
19 if (IsEqualCLSID(obj_guid, &CLSID_git_shell_ext) ||
20 IsEqualCLSID(obj_guid, &CLSID_git_menu))
21 return class_factory_query_interface(&factory,
22 factory_guid, factory_handle);
24 *factory_handle = 0;
25 return CLASS_E_CLASSNOTAVAILABLE;
28 HRESULT PASCAL DllCanUnloadNow(void)
30 return (object_count || lock_count) ? S_FALSE : S_OK;
33 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
35 hInst = instance;
37 if (reason == DLL_PROCESS_ATTACH) {
38 object_count = lock_count = 0;
39 DisableThreadLibraryCalls(instance);
42 return 1;
45 /* replaces a substring pattern with a string replacement within a string
46 the replacement occurs in-place, hence string must be large enough to
47 hold the result
49 the function does not handle recursive replacements, e.g.
50 strreplace ("foo", "bar", "another bar");
52 always returns *string
54 static char *strreplace(char *string, const size_t size,
55 const char *pattern, const char *replacement)
57 size_t len = strlen(string);
58 const size_t pattern_len = strlen(pattern);
59 const size_t replace_len = strlen(replacement);
61 char *found = strstr(string, pattern);
63 while (found) {
64 /* if the new len is greater than size, bail out */
65 if (len + replace_len - pattern_len >= size)
66 return string;
68 if (pattern_len != replace_len)
69 memmove(found + replace_len,
70 found + pattern_len,
71 len - (found - string) - pattern_len + 1);
72 memcpy(found, replacement, replace_len);
73 len += replace_len - pattern_len;
75 found = strstr(string, pattern);
78 return string;
82 * The following is the data for our minimal regedit engine,
83 * required for registration/unregistration of the extension
85 #define CLASS_CHEETAH CLASSES_ROOT "CLSID\\@@CLSID@@"
86 #define CONTEXTMENUHANDLER "shellex\\ContextMenuHandlers\\@@PROGRAM_NAME@@"
87 #define COLUMNHANDLER "shellex\\ColumnHandlers"
89 static const char *get_module_filename() {
90 static char module_filename[MAX_PATH] = { '\0' };
92 if (!*module_filename) {
93 DWORD module_size;
95 module_size = GetModuleFileName(hInst,
96 module_filename, MAX_PATH);
97 if (0 == module_size)
98 return NULL;
101 return module_filename;
104 /* as per "How to: Convert Between System::Guid and _GUID" */
105 static const char *get_class_id()
107 static char class_id[MAX_REGISTRY_PATH] = { '\0' };
109 if (!*class_id) {
110 GUID guid = CLSID_git_shell_ext;
111 sprintf(class_id,
112 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
113 guid.Data1, guid.Data2, guid.Data3,
114 guid.Data4[0], guid.Data4[1], guid.Data4[2],
115 guid.Data4[3], guid.Data4[4], guid.Data4[5],
116 guid.Data4[6], guid.Data4[7]);
119 return class_id;
123 * Tries to find msysGit in the following order:
124 * .. and ../.. (relative to the module)
125 * %PATH%
126 * as qgit (via InstallLocation of Git)
127 SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Git_is1\InstallLocation
129 static const reg_value registry_info[] = {
130 { CURRENT_WINDOWS APPROVED_EXT, "@@CLSID@@", "@@PROGRAM_NAME@@" },
131 { CURRENT_WINDOWS APPROVED_EXT "\\@@CLSID@@",
132 NULL, NULL },
133 { CURRENT_WINDOWS APPROVED_EXT "\\@@CLSID@@",
134 NULL,"@@PROGRAM_NAME@@" },
135 { CLASS_CHEETAH, NULL, NULL },
136 { CLASS_CHEETAH, NULL, "@@PROGRAM_NAME@@" },
137 { CLASS_CHEETAH "\\InProcServer32", NULL, NULL },
138 { CLASS_CHEETAH "\\InProcServer32", NULL, "@@PROGRAM_PATH@@"},
139 { CLASS_CHEETAH "\\InProcServer32", "ThreadingModel", "Apartment" },
140 { CLASSES_ROOT "*\\" CONTEXTMENUHANDLER, NULL, NULL },
141 { CLASSES_ROOT "*\\" CONTEXTMENUHANDLER, NULL, "@@CLSID@@" },
142 { CLASSES_ROOT "Directory\\" CONTEXTMENUHANDLER, NULL, NULL },
143 { CLASSES_ROOT "Directory\\" CONTEXTMENUHANDLER, NULL, "@@CLSID@@" },
144 { CLASSES_ROOT "Directory\\Background\\" CONTEXTMENUHANDLER,
145 NULL, NULL },
146 { CLASSES_ROOT "Directory\\Background\\" CONTEXTMENUHANDLER,
147 NULL, "@@CLSID@@" },
148 { CLASSES_ROOT "Drive\\" CONTEXTMENUHANDLER, NULL, NULL },
149 { CLASSES_ROOT "Drive\\" CONTEXTMENUHANDLER, NULL, "@@CLSID@@"},
150 { CLASSES_ROOT "Folder\\" CONTEXTMENUHANDLER, NULL, NULL },
151 { CLASSES_ROOT "Folder\\" CONTEXTMENUHANDLER, NULL, "@@CLSID@@" },
152 { CLASSES_ROOT "Folder\\" COLUMNHANDLER "\\@@CLSID@@", NULL, NULL },
153 { CLASSES_ROOT "InternetShortcut\\" CONTEXTMENUHANDLER,
154 NULL, NULL },
155 { CLASSES_ROOT "InternetShortcut\\" CONTEXTMENUHANDLER,
156 NULL, "@@CLSID@@" },
157 { GIT_CHEETAH_REG_PATH, NULL, NULL },
158 { GIT_CHEETAH_REG_PATH,
159 GIT_CHEETAH_REG_PATHTOMSYS, "@@MSYSGIT_PATH@@" },
160 { NULL, NULL, NULL }
163 static const reg_value debug_info[] = {
164 { CURRENT_WINDOWS "Explorer", "DesktopProcess", "1" },
165 { CURRENT_WINDOWS "Explorer\\AlwaysUnloadDll", NULL, NULL },
166 { CURRENT_WINDOWS "Explorer\\Advanced", "SeparateProcess", "1" },
167 { NULL, NULL, NULL }
170 static char msysgit[MAX_PATH] = { '\0' };
172 static BOOL find_msysgit_in_path()
174 char *file; /* file part of the path to git.exe */
175 DWORD dwFound; /* length of path to git.exe */
177 dwFound = SearchPath(NULL, "git.exe", NULL, MAX_PATH, msysgit, &file);
178 /* if git is not in the PATH or its path is too long */
179 if (0 == dwFound ||
180 dwFound > MAX_PATH)
181 return FALSE;
184 * git.exe is in "\bin\" from what we really need
185 * the minimal case we can handle is c:\bin\git.exe
186 * otherwise declare failure
188 if (file < msysgit + 7)
189 return FALSE;
190 if (strnicmp(file - 5, "\\bin\\", 5))
191 return FALSE;
192 file[-5] = '\0';
194 return TRUE;
197 static BOOL find_msysgit_relative(const char *path)
199 char *c;
201 strcpy(msysgit, get_module_filename());
202 c = strrchr(msysgit, '\\');
203 c[1] = '\0';
204 strcat(msysgit, path);
205 strcat(msysgit, "\\bin\\git.exe");
206 if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(msysgit)) {
207 msysgit[0] = '\0'; /* restore the original result */
208 return FALSE;
211 c[1] = '\0';
212 strcat(msysgit, path);
213 return TRUE;
216 static BOOL find_msysgit_uninstall(HKEY root)
218 HKEY key;
219 HRESULT result;
220 DWORD valuelen = MAX_PATH;
222 result = RegOpenKeyEx(root,
223 CURRENT_WINDOWS "\\Uninstall\\Git_is1",
224 0, KEY_READ, &key);
225 if (ERROR_SUCCESS != result)
226 return FALSE;
228 result = RegQueryValue(key, "InstallLocation",
229 (LPBYTE)msysgit, &valuelen);
230 return ERROR_SUCCESS == result;
233 static HKEY setup_root;
235 static const char *find_msysgit()
237 if ('\0' == msysgit[0]) {
238 if (find_msysgit_relative(".."))
239 return msysgit;
241 if (find_msysgit_relative("..\\.."))
242 return msysgit;
244 if (find_msysgit_in_path())
245 return msysgit;
247 if (setup_root)
248 find_msysgit_uninstall(setup_root);
251 return msysgit;
255 * required by registry.c
256 * supports @@PROGRAM_NAME@@, @@PROGRAM_PATH@@, @@CLSID@@ patterns
258 char *get_registry_path(const char *src, char dst[MAX_REGISTRY_PATH])
260 if (NULL == src)
261 return NULL;
263 strcpy(dst, src);
264 strreplace(dst, MAX_REGISTRY_PATH,
265 "@@PROGRAM_NAME@@", program_name);
266 strreplace(dst, MAX_REGISTRY_PATH,
267 "@@PROGRAM_PATH@@", get_module_filename());
268 strreplace(dst, MAX_REGISTRY_PATH,
269 "@@CLSID@@", get_class_id());
270 strreplace(dst, MAX_REGISTRY_PATH,
271 "@@MSYSGIT_PATH@@", find_msysgit());
273 return dst;
276 HRESULT PASCAL DllRegisterServer(void)
278 setup_root = HKEY_CURRENT_USER;
279 return create_reg_entries (setup_root, registry_info);
282 HRESULT PASCAL DllUnregisterServer(void)
284 setup_root = HKEY_CURRENT_USER;
285 return delete_reg_entries(setup_root, registry_info);
288 /* provide means to create/delete keys:
289 - described in Debugging with The Shell;
290 - machine-wide registration and debugging info
292 possible combinations of regsvr32 command line options:
293 -n (absent) (present)
295 (absent) user reg (invalid)
296 debug user reg+debug user debug
297 machine user+machine reg machine reg
298 machinedebug user+machine reg+debug machine reg+debug
300 Obviously missing option is "machine debug". To accomplish:
301 - execute "regsvr32 -n -i:machinedebug" and then
302 - regsvr32 -u -n -i:machine
304 HRESULT PASCAL DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
306 BOOL bDebug = NULL != wcsstr(pszCmdLine, L"debug");
307 HRESULT result = ERROR_SUCCESS;
309 setup_root = wcsstr(pszCmdLine, L"machine") ?
310 HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
312 if (bInstall) {
313 if (bDebug)
314 result = create_reg_entries(setup_root, debug_info);
316 /* for user-only registration, use DllRegister */
317 if (ERROR_SUCCESS == result &&
318 HKEY_LOCAL_MACHINE == setup_root)
319 result = create_reg_entries(setup_root,
320 registry_info);
321 } else { /* uninstall */
322 if (bDebug)
323 result = delete_reg_entries(setup_root, debug_info);
325 /* for user-only unregistration, use DllUnregister */
326 if (ERROR_SUCCESS == result &&
327 HKEY_LOCAL_MACHINE == setup_root)
328 result = delete_reg_entries(setup_root,
329 registry_info);
332 return result;