├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── proxy_copy_handler.sln ├── proxy_copy_handler.vcxproj ├── proxy_copy_handler.vcxproj.filters └── src ├── hook.cpp ├── hook.h ├── module.cpp ├── pch.cpp ├── pch.h ├── proxy_copy_handler.def ├── proxy_copy_handler.rc ├── proxy_copy_hook.rgs └── resource.h /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019-Present CrendKing 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Proxy Copy Handler 2 | 3 | A Windows copy hook handler that proxies operations to a copy/backup software, such as [FastCopy](https://fastcopy.jp/en/). 4 | -------------------------------------------------------------------------------- /proxy_copy_handler.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29613.14 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "proxy_copy_handler", "proxy_copy_handler.vcxproj", "{A5824D80-AB78-40C4-96EC-82631799C851}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A5824D80-AB78-40C4-96EC-82631799C851}.Debug|x64.ActiveCfg = Debug|x64 17 | {A5824D80-AB78-40C4-96EC-82631799C851}.Debug|x64.Build.0 = Debug|x64 18 | {A5824D80-AB78-40C4-96EC-82631799C851}.Debug|x86.ActiveCfg = Debug|Win32 19 | {A5824D80-AB78-40C4-96EC-82631799C851}.Debug|x86.Build.0 = Debug|Win32 20 | {A5824D80-AB78-40C4-96EC-82631799C851}.Release|x64.ActiveCfg = Release|x64 21 | {A5824D80-AB78-40C4-96EC-82631799C851}.Release|x64.Build.0 = Release|x64 22 | {A5824D80-AB78-40C4-96EC-82631799C851}.Release|x86.ActiveCfg = Release|Win32 23 | {A5824D80-AB78-40C4-96EC-82631799C851}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {F086E75A-8028-4218-B2EA-CBBDAA595A70} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /proxy_copy_handler.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | Create 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | {A5824D80-AB78-40C4-96EC-82631799C851} 42 | 43 | 44 | 45 | DynamicLibrary 46 | 47 | 48 | 49 | 50 | pch.h 51 | Use 52 | pch.h 53 | _USRDLL;%(PreprocessorDefinitions) 54 | 55 | 56 | src\$(ProjectName).def 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /proxy_copy_handler.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | 29 | 30 | Header Files 31 | 32 | 33 | Header Files 34 | 35 | 36 | Header Files 37 | 38 | 39 | 40 | 41 | Resource Files 42 | 43 | 44 | 45 | 46 | Resource Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/hook.cpp: -------------------------------------------------------------------------------- 1 | #include "hook.h" 2 | 3 | 4 | static constexpr const WCHAR *REGISTRY_KEY_NAME = LR"(Software\ProxyCopyHandler)"; 5 | static constexpr const WCHAR *REGISTRY_VALUE_NAME_COPIER_PATH = L"CopierPath"; 6 | static constexpr const WCHAR *REGISTRY_VALUE_NAME_COPIER_ARGS = L"CopierArgs"; 7 | static constexpr const WCHAR *REGISTRY_VALUE_NAME_START_MIN = L"StartMinimized"; 8 | static int64_t QUEUE_SOURCES_DELAY_100_NS = -1000000; 9 | 10 | static std::array g_quotedPathBuffer {}; 11 | 12 | OBJECT_ENTRY_AUTO(__uuidof(CProxyCopyHook), CProxyCopyHook) 13 | 14 | CProxyCopyHook::CProxyCopyHook() { 15 | HKEY registryKey; 16 | 17 | if (RegCreateKeyExW(HKEY_CURRENT_USER, REGISTRY_KEY_NAME, 0, nullptr, 0, KEY_QUERY_VALUE, nullptr, ®istryKey, nullptr) != ERROR_SUCCESS) { 18 | return; 19 | } 20 | 21 | std::array strValueData; 22 | DWORD dwordValueData; 23 | DWORD regValueSize; 24 | 25 | regValueSize = static_cast(strValueData.size()); 26 | if (RegGetValueW(registryKey, nullptr, REGISTRY_VALUE_NAME_COPIER_PATH, RRF_RT_REG_SZ, nullptr, strValueData.data(), ®ValueSize) != ERROR_SUCCESS) { 27 | return; 28 | } 29 | 30 | _copierCmdline = QuotePath(strValueData.data()); 31 | 32 | regValueSize = static_cast(strValueData.size()); 33 | if (RegGetValueW(registryKey, nullptr, REGISTRY_VALUE_NAME_COPIER_ARGS, RRF_RT_REG_SZ, nullptr, strValueData.data(), ®ValueSize) != ERROR_SUCCESS) { 34 | return; 35 | } 36 | 37 | _copierCmdline.append(L" ").append(strValueData.data(), regValueSize / sizeof(WCHAR) - 1); /* remove the '\0' */ 38 | 39 | regValueSize = sizeof(dwordValueData); 40 | if (RegGetValueW(registryKey, nullptr, REGISTRY_VALUE_NAME_START_MIN, RRF_RT_REG_DWORD, nullptr, &dwordValueData, ®ValueSize) == ERROR_SUCCESS) { 41 | _startMinimized = (dwordValueData != 0); 42 | } 43 | 44 | RegCloseKey(registryKey); 45 | } 46 | 47 | CProxyCopyHook::~CProxyCopyHook() { 48 | if (_workerThread.joinable()) { 49 | _stopWorker = true; 50 | _cv.notify_all(); 51 | _workerThread.join(); 52 | } 53 | 54 | if (_waitTp != nullptr) { 55 | WaitForThreadpoolWaitCallbacks(_waitTp, FALSE); 56 | CloseThreadpoolWait(_waitTp); 57 | } 58 | 59 | if (_waitEvent != nullptr) { 60 | CloseHandle(_waitEvent); 61 | } 62 | } 63 | 64 | auto STDMETHODCALLTYPE CProxyCopyHook::CopyCallback(HWND hwnd, UINT wFunc, UINT wFlags, PCWSTR pszSrcFile, DWORD dwSrcAttribs, PCWSTR pszDestFile, DWORD dwDestAttribs) -> UINT { 65 | if (_waitEvent == nullptr || _waitTp == nullptr) { 66 | return IDYES; 67 | } 68 | 69 | if (_copierCmdline.empty()) { 70 | return IDYES; 71 | } 72 | 73 | if (wFunc != FO_MOVE && wFunc != FO_COPY && wFunc != FO_DELETE) { 74 | return IDYES; 75 | } 76 | 77 | // Windows can move file without copying within the same volume (ref. MOVEFILE_COPY_ALLOWED) 78 | // FastCopy move is always copy-then-delete 79 | if (wFunc == FO_MOVE && PathGetDriveNumberW(pszSrcFile) == PathGetDriveNumberW(pszDestFile)) { 80 | return IDYES; 81 | } 82 | 83 | { 84 | const std::unique_lock lock(_mutex); 85 | 86 | const ExecutionKey execKey(wFunc, pszDestFile); 87 | _pendingExecutions[execKey].emplace(pszSrcFile); 88 | } 89 | 90 | /* 91 | When multiple copy operations happen in quick succession (within the below DELAY constant), we want to merge them 92 | into one bigger operation and trigger the copier just once. 93 | 94 | Because SetThreadpoolWait() "can wait for only one handle", subsequent calls to it would cancel the previous registered wait. 95 | 96 | Since we never want to trigger the callback early, the event is never set. 97 | */ 98 | SetThreadpoolWait(_waitTp, _waitEvent, reinterpret_cast(&QUEUE_SOURCES_DELAY_100_NS)); 99 | 100 | return IDNO; 101 | } 102 | 103 | CProxyCopyHook::ExecutionKey::ExecutionKey(UINT func, PCWSTR dest) 104 | : operation(func) { 105 | if (dest[0] != L'\0') { 106 | std::array path {}; 107 | 108 | wcscpy_s(path.data(), path.size(), dest); 109 | PathRemoveFileSpecW(path.data()); 110 | PathAddBackslashW(path.data()); 111 | PathQuoteSpacesW(path.data()); 112 | destination = path.data(); 113 | } 114 | } 115 | 116 | auto CProxyCopyHook::ExecutionKey::Hasher::operator()(const ExecutionKey &key) const -> size_t { 117 | const size_t h1 = std::hash {}(key.operation); 118 | const size_t h2 = std::hash {}(key.destination); 119 | return h1 ^ (h2 << 1); 120 | } 121 | 122 | auto CALLBACK CProxyCopyHook::WaitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WAIT wait, TP_WAIT_RESULT waitResult) -> void { 123 | if (CProxyCopyHook *hook = static_cast(context); hook->_workerThread.joinable()) { 124 | hook->_cv.notify_one(); 125 | } else { 126 | hook->_workerThread = std::thread(&CProxyCopyHook::WorkerProc, hook); 127 | } 128 | } 129 | 130 | auto CProxyCopyHook::QuotePath(PCWSTR path) -> const WCHAR * { 131 | if (path == nullptr) { 132 | return path; 133 | } 134 | 135 | wcscpy_s(g_quotedPathBuffer.data(), g_quotedPathBuffer.size(), path); 136 | PathQuoteSpacesW(g_quotedPathBuffer.data()); 137 | return g_quotedPathBuffer.data(); 138 | } 139 | 140 | auto CProxyCopyHook::WorkerProc() -> void { 141 | while (!_stopWorker) { 142 | std::unique_lock lock(_mutex); 143 | 144 | _cv.wait(lock, [this]() -> bool { 145 | return _stopWorker || !_pendingExecutions.empty(); 146 | }); 147 | 148 | if (_stopWorker) { 149 | continue; 150 | } 151 | 152 | auto [key, sources] = *_pendingExecutions.cbegin(); 153 | _pendingExecutions.erase(_pendingExecutions.cbegin()); 154 | lock.unlock(); 155 | 156 | std::wstring sourceArg; 157 | for (const std::wstring &s : sources) { 158 | if (PathFileExistsW(s.c_str())) { 159 | sourceArg = std::format(L"{} {}", sourceArg, QuotePath(s.c_str())); 160 | } 161 | } 162 | 163 | if (sourceArg.empty()) { 164 | continue; 165 | } 166 | 167 | const WCHAR *cmdlineOperation; 168 | switch (key.operation) { 169 | case FO_MOVE: 170 | cmdlineOperation = L"move"; 171 | break; 172 | case FO_COPY: 173 | cmdlineOperation = L"diff"; 174 | break; 175 | case FO_DELETE: 176 | cmdlineOperation = L"delete"; 177 | break; 178 | default: 179 | return; 180 | } 181 | 182 | std::wstring destinationArg; 183 | if (!key.destination.empty()) { 184 | destinationArg = std::format(L" /to={}", key.destination); 185 | } 186 | 187 | std::wstring cmdline = std::format(L"{} /cmd={}{} {}", _copierCmdline, cmdlineOperation, destinationArg, sourceArg); 188 | 189 | STARTUPINFOW si { .cb = sizeof(si) }; 190 | PROCESS_INFORMATION pi; 191 | 192 | if (_startMinimized) { 193 | si.dwFlags = STARTF_USESHOWWINDOW; 194 | si.wShowWindow = SW_SHOWMINNOACTIVE; 195 | } 196 | 197 | if (CreateProcessW(nullptr, cmdline.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) { 198 | CloseHandle(pi.hThread); 199 | CloseHandle(pi.hProcess); 200 | 201 | if (key.operation == FO_DELETE) { 202 | for (const std::wstring &s : sources) { 203 | SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, s.c_str(), nullptr); 204 | } 205 | } else { 206 | SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATHW, key.destination.c_str(), nullptr); 207 | } 208 | } 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /src/hook.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | class ATL_NO_VTABLE __declspec(uuid("4EB17DC6-A98E-42DE-9F3F-93A6819B08A4")) CProxyCopyHook 5 | : public ATL::CComObjectRoot 6 | , public ATL::CComCoClass 7 | , public ICopyHookW { 8 | public: 9 | DECLARE_REGISTRY_RESOURCEID(IDS_PROXY_COPY_HOOK) 10 | 11 | DECLARE_PROTECT_FINAL_CONSTRUCT() 12 | 13 | BEGIN_COM_MAP(CProxyCopyHook) 14 | COM_INTERFACE_ENTRY(ICopyHookW) 15 | END_COM_MAP() 16 | 17 | protected: 18 | CProxyCopyHook(); 19 | ~CProxyCopyHook(); 20 | 21 | private: 22 | auto STDMETHODCALLTYPE CopyCallback(HWND hwnd, UINT wFunc, UINT wFlags, PCWSTR pszSrcFile, DWORD dwSrcAttribs, PCWSTR pszDestFile, DWORD dwDestAttribs) -> UINT override; 23 | 24 | struct ExecutionKey { 25 | UINT operation; 26 | std::wstring destination; 27 | 28 | ExecutionKey(UINT func, PCWSTR dest); 29 | constexpr auto operator==(const ExecutionKey &other) const -> bool { return operation == other.operation && destination == other.destination; } 30 | 31 | struct Hasher { 32 | auto operator()(const ExecutionKey &key) const -> size_t; 33 | }; 34 | }; 35 | 36 | static auto CALLBACK WaitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WAIT wait, TP_WAIT_RESULT waitResult) -> void; 37 | static auto QuotePath(PCWSTR path) -> const WCHAR *; 38 | 39 | auto WorkerProc() -> void; 40 | 41 | std::mutex _mutex; 42 | std::condition_variable _cv; 43 | std::thread _workerThread; 44 | HANDLE _waitEvent = CreateEventW(nullptr, FALSE, FALSE, nullptr); 45 | PTP_WAIT _waitTp = CreateThreadpoolWait(WaitCallback, this, nullptr); 46 | 47 | std::atomic _stopWorker = false; 48 | 49 | std::wstring _copierCmdline; 50 | bool _startMinimized = false; 51 | std::unordered_map, ExecutionKey::Hasher> _pendingExecutions; 52 | }; 53 | -------------------------------------------------------------------------------- /src/module.cpp: -------------------------------------------------------------------------------- 1 | class ProxyCopyHandlerModule : public ATL::CAtlDllModuleT { 2 | } g_module; 3 | 4 | extern "C" auto WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) -> BOOL { 5 | return g_module.DllMain(dwReason, lpReserved); 6 | } 7 | 8 | _Use_decl_annotations_ extern "C" auto STDAPICALLTYPE DllCanUnloadNow() -> HRESULT { 9 | return g_module.DllCanUnloadNow(); 10 | } 11 | 12 | _Use_decl_annotations_ extern "C" auto STDAPICALLTYPE DllGetClassObject(_In_ REFCLSID rclsid, _In_ REFIID riid, _Outptr_ LPVOID FAR *ppv) -> HRESULT { 13 | return g_module.DllGetClassObject(rclsid, riid, ppv); 14 | } 15 | 16 | _Use_decl_annotations_ extern "C" auto STDAPICALLTYPE DllRegisterServer() -> HRESULT { 17 | return g_module.DllRegisterServer(FALSE); 18 | } 19 | 20 | _Use_decl_annotations_ extern "C" auto STDAPICALLTYPE DllUnregisterServer() -> HRESULT { 21 | return g_module.DllUnregisterServer(FALSE); 22 | } 23 | 24 | _Use_decl_annotations_ extern "C" auto STDAPICALLTYPE DllInstall(BOOL bInstall, _In_opt_ PCWSTR pszCmdLine) -> HRESULT { 25 | HRESULT hr = E_FAIL; 26 | static const wchar_t szUserSwitch[] = L"user"; 27 | 28 | if (pszCmdLine != nullptr) { 29 | if (_wcsnicmp(pszCmdLine, szUserSwitch, _countof(szUserSwitch)) == 0) { 30 | ATL::AtlSetPerUserRegistration(true); 31 | } 32 | } 33 | 34 | if (bInstall) { 35 | hr = DllRegisterServer(); 36 | if (FAILED(hr)) { 37 | DllUnregisterServer(); 38 | } 39 | } else { 40 | hr = DllUnregisterServer(); 41 | } 42 | 43 | return hr; 44 | } 45 | -------------------------------------------------------------------------------- /src/pch.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CrendKing/proxy-copy-handler/f40a8b32399cc5749c3e0c52fca7c6b50991c645/src/pch.cpp -------------------------------------------------------------------------------- /src/pch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #pragma warning(push) 5 | #pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS) 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define WIN32_LEAN_AND_MEAN 15 | #include 16 | 17 | #define _ATL_APARTMENT_THREADED 18 | 19 | #define _ATL_NO_AUTOMATIC_NAMESPACE 20 | 21 | #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit 22 | 23 | #define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #pragma warning(pop) 32 | 33 | #include "resource.h" 34 | 35 | #pragma warning(push) 36 | #pragma warning(disable: 26812) 37 | -------------------------------------------------------------------------------- /src/proxy_copy_handler.def: -------------------------------------------------------------------------------- 1 | LIBRARY 2 | 3 | EXPORTS 4 | DllCanUnloadNow PRIVATE 5 | DllGetClassObject PRIVATE 6 | DllRegisterServer PRIVATE 7 | DllUnregisterServer PRIVATE 8 | DllInstall PRIVATE 9 | -------------------------------------------------------------------------------- /src/proxy_copy_handler.rc: -------------------------------------------------------------------------------- 1 | // Microsoft Visual C++ generated resource script. 2 | // 3 | #include "resource.h" 4 | 5 | #define APSTUDIO_READONLY_SYMBOLS 6 | ///////////////////////////////////////////////////////////////////////////// 7 | // 8 | // Generated from the TEXTINCLUDE 2 resource. 9 | // 10 | #include "winres.h" 11 | 12 | ///////////////////////////////////////////////////////////////////////////// 13 | #undef APSTUDIO_READONLY_SYMBOLS 14 | 15 | ///////////////////////////////////////////////////////////////////////////// 16 | // English (United States) resources 17 | 18 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) 19 | LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US 20 | 21 | #ifdef APSTUDIO_INVOKED 22 | ///////////////////////////////////////////////////////////////////////////// 23 | // 24 | // TEXTINCLUDE 25 | // 26 | 27 | 1 TEXTINCLUDE 28 | BEGIN 29 | "resource.h\0" 30 | END 31 | 32 | 2 TEXTINCLUDE 33 | BEGIN 34 | "#include ""winres.h""\r\n" 35 | "\0" 36 | END 37 | 38 | 3 TEXTINCLUDE 39 | BEGIN 40 | "\r\n" 41 | "\0" 42 | END 43 | 44 | #endif // APSTUDIO_INVOKED 45 | 46 | 47 | ///////////////////////////////////////////////////////////////////////////// 48 | // 49 | // REGISTRY 50 | // 51 | 52 | IDS_PROXY_COPY_HOOK REGISTRY "proxy_copy_hook.rgs" 53 | 54 | 55 | #endif // English (United States) resources 56 | ///////////////////////////////////////////////////////////////////////////// 57 | 58 | 59 | 60 | #ifndef APSTUDIO_INVOKED 61 | ///////////////////////////////////////////////////////////////////////////// 62 | // 63 | // Generated from the TEXTINCLUDE 3 resource. 64 | // 65 | 66 | 67 | ///////////////////////////////////////////////////////////////////////////// 68 | #endif // not APSTUDIO_INVOKED 69 | 70 | -------------------------------------------------------------------------------- /src/proxy_copy_hook.rgs: -------------------------------------------------------------------------------- 1 | HKCR { 2 | NoRemove CLSID { 3 | ForceRemove {4EB17DC6-A98E-42DE-9F3F-93A6819B08A4} = s 'Proxy Copy Hook Shell Extension' { 4 | InprocServer32 = s '%MODULE%' { 5 | val ThreadingModel = s 'Apartment' 6 | } 7 | } 8 | } 9 | 10 | NoRemove Directory { 11 | NoRemove ShellEx { 12 | NoRemove CopyHookHandlers { 13 | ProxyCopyHook = s '{4EB17DC6-A98E-42DE-9F3F-93A6819B08A4}' 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by proxy_copy_handler.rc 4 | // 5 | #define IDS_PROXY_COPY_HOOK 100 6 | 7 | // Next default values for new objects 8 | // 9 | #ifdef APSTUDIO_INVOKED 10 | #ifndef APSTUDIO_READONLY_SYMBOLS 11 | #define _APS_NEXT_RESOURCE_VALUE 101 12 | #define _APS_NEXT_COMMAND_VALUE 40001 13 | #define _APS_NEXT_CONTROL_VALUE 1001 14 | #define _APS_NEXT_SYMED_VALUE 101 15 | #endif 16 | #endif 17 | --------------------------------------------------------------------------------