1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2016 - TortoiseGit
4 // Copyright (C) 2003-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.
22 // Initialize GUIDs (should be done only and at-least once per DLL/EXE)
27 #include "ShellObjects.h"
28 #include "..\version.h"
31 extern ShellObjects g_shellObjects
;
33 // *********************** CShellExt *************************
34 CShellExt::CShellExt(FileState state
)
39 #if ENABLE_CRASHHANLDER
40 , m_crasher(L
"TortoiseGit", TGIT_VERMAJOR
, TGIT_VERMINOR
, TGIT_VERMICRO
, TGIT_VERBUILD
, TGIT_VERDATE
, false)
42 ,regDiffLater(L
"Software\\TortoiseGit\\DiffLater", L
"")
45 InterlockedIncrement(&g_cRefThisDll
);
48 AutoLocker
lock(g_csGlobalCOMGuard
);
49 g_shellObjects
.Insert(this);
52 INITCOMMONCONTROLSEX used
= {
53 sizeof(INITCOMMONCONTROLSEX
),
54 ICC_LISTVIEW_CLASSES
| ICC_WIN95_CLASSES
| ICC_BAR_CLASSES
| ICC_USEREX_CLASSES
56 InitCommonControlsEx(&used
);
60 CShellExt::~CShellExt()
62 AutoLocker
lock(g_csGlobalCOMGuard
);
63 InterlockedDecrement(&g_cRefThisDll
);
64 g_shellObjects
.Erase(this);
69 if (g_langid
!= g_ShellCache
.GetLangID() && (g_langTimeout
== 0 || g_langTimeout
< GetTickCount64()))
71 g_langid
= g_ShellCache
.GetLangID();
72 DWORD langId
= g_langid
;
73 TCHAR langDll
[MAX_PATH
*4] = {0};
74 HINSTANCE hInst
= nullptr;
75 TCHAR langdir
[MAX_PATH
] = {0};
76 char langdirA
[MAX_PATH
] = {0};
77 if (GetModuleFileName(g_hmodThisDll
, langdir
, _countof(langdir
))==0)
79 if (GetModuleFileNameA(g_hmodThisDll
, langdirA
, _countof(langdirA
))==0)
81 TCHAR
* dirpoint
= _tcsrchr(langdir
, '\\');
82 char * dirpointA
= strrchr(langdirA
, '\\');
87 dirpoint
= _tcsrchr(langdir
, '\\');
88 dirpointA
= strrchr(langdirA
, '\\');
93 strcat_s(langdirA
, "\\Languages");
96 IsWow64Process(GetCurrentProcess(), &bIsWow
);
101 _stprintf_s(langDll
, _T("%s\\Languages\\TortoiseProc32%lu.dll"), langdir
, langId
);
103 _stprintf_s(langDll
, _T("%s\\Languages\\TortoiseProc%lu.dll"), langdir
, langId
);
104 BOOL versionmatch
= TRUE
;
112 DWORD dwReserved
,dwBufferSize
;
113 dwBufferSize
= GetFileVersionInfoSize((LPTSTR
)langDll
,&dwReserved
);
115 if (dwBufferSize
> 0)
117 LPVOID pBuffer
= (void*) malloc(dwBufferSize
);
122 UINT nFixedLength
= 0;
123 LPSTR lpVersion
= nullptr;
124 VOID
* lpFixedPointer
;
126 if (GetFileVersionInfo((LPTSTR
)langDll
,
131 // Query the current language
132 if (VerQueryValue( pBuffer
,
133 _T("\\VarFileInfo\\Translation"),
137 auto lpTransArray
= reinterpret_cast<TRANSARRAY
*>(lpFixedPointer
);
138 TCHAR strLangProductVersion
[MAX_PATH
] = { 0 };
140 _stprintf_s(strLangProductVersion
, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
141 lpTransArray
[0].wLanguageID
, lpTransArray
[0].wCharacterSet
);
143 if (VerQueryValue(pBuffer
, (LPTSTR
)strLangProductVersion
, (LPVOID
*)&lpVersion
, &nInfoSize
))
144 versionmatch
= (_tcscmp((LPCTSTR
)lpVersion
, _T(STRPRODUCTVER
)) == 0);
150 } // if (dwBufferSize > 0)
152 versionmatch
= FALSE
;
155 hInst
= LoadLibrary(langDll
);
158 if (g_hResInst
!= g_hmodThisDll
)
159 FreeLibrary(g_hResInst
);
164 DWORD lid
= SUBLANGID(langId
);
167 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
171 } while (!hInst
&& langId
!= 0);
174 // either the dll for the selected language is not present, or
175 // it is the wrong version.
176 // fall back to English and set a timeout so we don't retry
177 // to load the language dll too often
178 if (g_hResInst
!= g_hmodThisDll
)
179 FreeLibrary(g_hResInst
);
180 g_hResInst
= g_hmodThisDll
;
182 // set a timeout of 10 seconds
183 if (g_ShellCache
.GetLangID() != 1033)
184 g_langTimeout
= GetTickCount64() + 10000;
188 } // if (g_langid != g_ShellCache.GetLangID())
191 STDMETHODIMP
CShellExt::QueryInterface(REFIID riid
, LPVOID FAR
*ppv
)
198 if (IsEqualIID(riid
, IID_IShellExtInit
) || IsEqualIID(riid
, IID_IUnknown
))
199 *ppv
= static_cast<LPSHELLEXTINIT
>(this);
200 else if (IsEqualIID(riid
, IID_IContextMenu
))
201 *ppv
= static_cast<LPCONTEXTMENU
>(this);
202 else if (IsEqualIID(riid
, IID_IContextMenu2
))
203 *ppv
= static_cast<LPCONTEXTMENU2
>(this);
204 else if (IsEqualIID(riid
, IID_IContextMenu3
))
205 *ppv
= static_cast<LPCONTEXTMENU3
>(this);
206 else if (IsEqualIID(riid
, IID_IShellIconOverlayIdentifier
))
207 *ppv
= static_cast<IShellIconOverlayIdentifier
*>(this);
208 else if (IsEqualIID(riid
, IID_IShellPropSheetExt
))
209 *ppv
= static_cast<LPSHELLPROPSHEETEXT
>(this);
210 else if (IsEqualIID(riid
, IID_IShellCopyHook
))
211 *ppv
= static_cast<ICopyHook
*>(this);
213 return E_NOINTERFACE
;
219 STDMETHODIMP_(ULONG
) CShellExt::AddRef()
221 return InterlockedIncrement(&m_cRef
);
224 STDMETHODIMP_(ULONG
) CShellExt::Release()
226 if (InterlockedDecrement(&m_cRef
))
234 // IPersistFile members
235 STDMETHODIMP
CShellExt::GetClassID(CLSID
*pclsid
)
239 *pclsid
= CLSID_Tortoisegit_UNCONTROLLED
;
243 STDMETHODIMP
CShellExt::Load(LPCOLESTR
/*pszFileName*/, DWORD
/*dwMode*/)
249 UINT __stdcall
CShellExt::CopyCallback(HWND hWnd
, UINT wFunc
, UINT wFlags
, LPCTSTR pszSrcFile
, DWORD dwSrcAttribs
, LPCTSTR pszDestFile
, DWORD dwDestAttribs
)
253 return CopyCallback_Wrap(hWnd
, wFunc
, wFlags
, pszSrcFile
, dwSrcAttribs
, pszDestFile
, dwDestAttribs
);
255 __except(CCrashReport::Instance().SendReport(GetExceptionInformation()))
261 UINT __stdcall
CShellExt::CopyCallback_Wrap(HWND
/*hWnd*/, UINT wFunc
, UINT
/*wFlags*/, LPCTSTR pszSrcFile
, DWORD
/*dwSrcAttribs*/, LPCTSTR
/*pszDestFile*/, DWORD
/*dwDestAttribs*/)
268 if (pszSrcFile
&& pszSrcFile
[0])
271 if (GitAdminDir::HasAdminDir(pszSrcFile
, &topDir
))
272 m_CachedStatus
.m_GitStatus
.ReleasePath(topDir
);
273 m_remoteCacheLink
.ReleaseLockForPath(CTGitPath(pszSrcFile
));
280 // we could now wait a little bit to give the cache time to release the handles.
281 // but the explorer/shell already retries any action for about two seconds
282 // if it first fails. So if the cache hasn't released the handle yet, the explorer
283 // will retry anyway, so we just leave here immediately.