4 #include "menuengine.h"
7 #include "systeminfo.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
);
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
)
37 if (reason
== DLL_PROCESS_ATTACH
) {
38 object_count
= lock_count
= 0;
39 DisableThreadLibraryCalls(instance
);
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
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
);
64 /* if the new len is greater than size, bail out */
65 if (len
+ replace_len
- pattern_len
>= size
)
68 if (pattern_len
!= replace_len
)
69 memmove(found
+ replace_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
);
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
) {
95 module_size
= GetModuleFileName(hInst
,
96 module_filename
, MAX_PATH
);
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' };
110 GUID guid
= CLSID_git_shell_ext
;
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]);
123 * Tries to find msysGit in the following order:
124 * .. and ../.. (relative to the module)
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@@",
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
,
146 { CLASSES_ROOT
"Directory\\Background\\" CONTEXTMENUHANDLER
,
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
,
155 { CLASSES_ROOT
"InternetShortcut\\" CONTEXTMENUHANDLER
,
157 { GIT_CHEETAH_REG_PATH
, NULL
, NULL
},
158 { GIT_CHEETAH_REG_PATH
,
159 GIT_CHEETAH_REG_PATHTOMSYS
, "@@MSYSGIT_PATH@@" },
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" },
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 */
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)
190 if (strnicmp(file
- 5, "\\bin\\", 5))
197 static BOOL
find_msysgit_relative(const char *path
)
201 strcpy(msysgit
, get_module_filename());
202 c
= strrchr(msysgit
, '\\');
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 */
212 strcat(msysgit
, path
);
216 static BOOL
find_msysgit_uninstall(HKEY root
)
220 DWORD valuelen
= MAX_PATH
;
222 result
= RegOpenKeyEx(root
,
223 CURRENT_WINDOWS
"\\Uninstall\\Git_is1",
225 if (ERROR_SUCCESS
!= result
)
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(".."))
241 if (find_msysgit_relative("..\\.."))
244 if (find_msysgit_in_path())
248 find_msysgit_uninstall(setup_root
);
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
])
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());
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
;
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
,
321 } else { /* uninstall */
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
,