1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2010, 2012 - TortoiseSVN
4 // Copyright (C) 2008-2012,2014 - TortoiseGit
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.
23 #include "ShellExtClassFactory.h"
24 #include "ShellObjects.h"
26 volatile LONG g_cRefThisDll
= 0; ///< reference count of this DLL.
27 HINSTANCE g_hmodThisDll
= NULL
; ///< handle to this DLL itself.
28 ShellCache g_ShellCache
; ///< caching of registry entries, ...
30 DWORD g_langTimeout
= 0;
31 HINSTANCE g_hResInst
= NULL
;
33 git_wc_status_kind g_filestatus
= git_wc_status_none
; ///< holds the corresponding status to the file/dir above
34 bool g_readonlyoverlay
= false;
35 bool g_lockedoverlay
= false;
37 bool g_normalovlloaded
= false;
38 bool g_modifiedovlloaded
= false;
39 bool g_conflictedovlloaded
= false;
40 bool g_readonlyovlloaded
= false;
41 bool g_deletedovlloaded
= false;
42 bool g_lockedovlloaded
= false;
43 bool g_addedovlloaded
= false;
44 bool g_ignoredovlloaded
= false;
45 bool g_unversionedovlloaded
= false;
46 CComCriticalSection g_csGlobalCOMGuard
;
48 LPCTSTR g_MenuIDString
= _T("TortoiseGit");
50 ShellObjects g_shellObjects
;
52 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
54 extern "C" int APIENTRY
55 DllMain(HINSTANCE hInstance
, DWORD dwReason
, LPVOID
/* lpReserved */)
58 // if no debugger is present, then don't load the dll.
59 // this prevents other apps from loading the dll and locking
62 bool bInShellTest
= false;
63 TCHAR buf
[MAX_PATH
+ 1] = {0}; // MAX_PATH ok, the test really is for debugging anyway.
64 DWORD pathLength
= GetModuleFileName(NULL
, buf
, MAX_PATH
);
67 if (pathLength
>= 24 && _tcsicmp(&buf
[pathLength
- 24], _T("\\TortoiseGitExplorer.exe")) == 0)
71 if ((_tcsicmp(&buf
[pathLength
-14], _T("\\ShellTest.exe"))) == 0)
75 if ((_tcsicmp(&buf
[pathLength
-13], _T("\\verclsid.exe"))) == 0)
81 if (!::IsDebuggerPresent() && !bInShellTest
)
83 ATLTRACE("In debug load preventer\n");
88 // NOTE: Do *NOT* call apr_initialize() or apr_terminate() here in DllMain(),
89 // because those functions call LoadLibrary() indirectly through malloc().
90 // And LoadLibrary() inside DllMain() is not allowed and can lead to unexpected
91 // behavior and even may create dependency loops in the dll load order.
92 if (dwReason
== DLL_PROCESS_ATTACH
)
94 if (g_hmodThisDll
== NULL
)
96 g_csGlobalCOMGuard
.Init();
99 // Extension DLL one-time initialization
100 g_hmodThisDll
= hInstance
;
102 else if (dwReason
== DLL_PROCESS_DETACH
)
104 // do not clean up memory here:
105 // if an application doesn't release all COM objects
106 // but still unloads the dll, cleaning up ourselves
107 // will lead to crashes.
108 // better to leak some memory than to crash other apps.
109 // sometimes an application doesn't release all COM objects
110 // but still unloads the dll.
111 // in that case, we do it ourselves
112 g_csGlobalCOMGuard
.Term();
117 STDAPI
DllCanUnloadNow(void)
119 return (g_cRefThisDll
== 0 ? S_OK
: S_FALSE
);
122 STDAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppvOut
)
128 FileState state
= FileStateInvalid
;
129 if (IsEqualIID(rclsid
, CLSID_Tortoisegit_UPTODATE
))
130 state
= FileStateVersioned
;
131 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_MODIFIED
))
132 state
= FileStateModified
;
133 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_CONFLICTING
))
134 state
= FileStateConflict
;
135 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_UNCONTROLLED
))
136 state
= FileStateUncontrolled
;
137 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_DROPHANDLER
))
138 state
= FileStateDropHandler
;
139 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_DELETED
))
140 state
= FileStateDeleted
;
141 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_READONLY
))
142 state
= FileStateReadOnly
;
143 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_LOCKED
))
144 state
= FileStateLockedOverlay
;
145 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_ADDED
))
146 state
= FileStateAddedOverlay
;
147 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_IGNORED
))
148 state
= FileStateIgnoredOverlay
;
149 else if (IsEqualIID(rclsid
, CLSID_Tortoisegit_UNVERSIONED
))
150 state
= FileStateUnversionedOverlay
;
152 if (state
!= FileStateInvalid
)
154 CShellExtClassFactory
*pcf
= new (std::nothrow
) CShellExtClassFactory(state
);
156 return E_OUTOFMEMORY
;
157 // refcount currently set to 0
158 const HRESULT hr
= pcf
->QueryInterface(riid
, ppvOut
);
164 return CLASS_E_CLASSNOTAVAILABLE
;