99#include < atlbase.h>
1010#include < shlobj.h>
1111#include < exdisp.h>
12+ #include < psapi.h> // For access to GetModuleFileNameEx
1213
1314#include " rapidassist/undef_windows_macros.h"
1415#include " rapidassist/unicode.h"
1516#include " rapidassist/errors.h"
1617#include " rapidassist/filesystem_utf8.h"
1718#include " rapidassist/environment_utf8.h"
19+ #include " rapidassist/process_utf8.h"
20+
21+ bool FindPath (const Utf8FileList& list, const std::string& path)
22+ {
23+ for (size_t i = 0 ; i < list.size (); i++)
24+ {
25+ const std::string & element = list[i];
26+ if (path == element)
27+ return true ;
28+ }
29+ return false ;
30+ }
1831
1932bool GetFileExplorerWindowPaths (Utf8FileList& files)
2033{
@@ -127,7 +140,7 @@ bool GetFileExplorerWindowPaths(Utf8FileList& files)
127140bool OpenFileExplorerWindow (const std::string& path)
128141{
129142 // Find the absolute path of explorer.exe
130- std::string explorer_path_utf8 = ra::filesystem::FindFileFromPathsUtf8 ( " explorer.exe " );
143+ std::string explorer_path_utf8 = GetFileExplorerExecPath ( );
131144 if (explorer_path_utf8.empty ())
132145 return false ;
133146 std::wstring explorer_path_wide = ra::unicode::Utf8ToUnicode (explorer_path_utf8);
@@ -223,4 +236,90 @@ void PrintPathsToString(const Utf8FileList& paths, tstring_t& str)
223236 str += " \r\n " ;
224237 #endif
225238 }
239+ }
240+
241+ std::string GetFileExplorerExecPath ()
242+ {
243+ // Find the absolute path of explorer.exe
244+ std::string explorer_path_utf8 = ra::filesystem::FindFileFromPathsUtf8 (" explorer.exe" );
245+ return explorer_path_utf8;
246+ }
247+
248+ std::string GetProcessExecPathFromProcessId (DWORD pid)
249+ {
250+ HANDLE hProcess = NULL ;
251+ TCHAR szPath[50 * MAX_PATH];
252+
253+ std::string output;
254+
255+ hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE , 1234 );
256+ if (hProcess != NULL )
257+ {
258+ if (GetModuleFileNameEx (hProcess, NULL , szPath, 50 * MAX_PATH) != 0 )
259+ {
260+ #ifdef UNICODE
261+ output = ra::unicode::UnicodeToUtf8 (szPath);
262+ #else
263+ output = ra::unicode::AnsiToUtf8 (szPath);
264+ #endif
265+ return output;
266+ }
267+ CloseHandle (hProcess);
268+ }
269+
270+ return output;
271+ }
272+
273+ ra::process::ProcessIdList GetFileExplorerProcessIds ()
274+ {
275+ ra::process::ProcessIdList pids;
276+
277+ std::string explorer_path_utf8 = GetFileExplorerExecPath ();
278+ if (explorer_path_utf8.empty ())
279+ return pids; // error.
280+
281+ ra::process::ProcessIdList system_process_ids = ra::process::GetProcesses ();
282+ if (system_process_ids.empty ())
283+ return pids; // error.
284+
285+ // For each process
286+ for (size_t i = 0 ; i < system_process_ids.size (); i++)
287+ {
288+ const ra::process::processid_t system_pid = system_process_ids[i];
289+
290+ // Does this pid match File Explorer?
291+ std::string exec_path = GetProcessExecPathFromProcessId (system_pid);
292+ if (explorer_path_utf8 == exec_path)
293+ {
294+ // Match
295+ pids.push_back (system_pid);
296+ }
297+ }
298+
299+ return pids;
300+ }
301+
302+ bool KillFileExplorerProcesses ()
303+ {
304+ std::string explorer_path_utf8 = GetFileExplorerExecPath ();
305+ if (explorer_path_utf8.empty ())
306+ return false ;
307+
308+ ra::process::ProcessIdList process_ids = GetFileExplorerProcessIds ();
309+ if (process_ids.empty ())
310+ return true ; // nothing to do
311+
312+ // For each process
313+ for (size_t i = 0 ; i < process_ids.size (); i++)
314+ {
315+ const ra::process::processid_t pid = process_ids[i];
316+ bool killed = true ; // ra::process::Kill(pid);
317+ if (!killed)
318+ return false ;
319+ }
320+
321+ // Confirm
322+ process_ids = GetFileExplorerProcessIds ();
323+ bool success = (process_ids.empty ());
324+ return success;
226325}
0 commit comments