Skip to content

Commit d607d91

Browse files
committed
Partial working refresh_file_explorer. #115
1 parent c91c7a1 commit d607d91

File tree

7 files changed

+336
-184
lines changed

7 files changed

+336
-184
lines changed

src/refresh_file_explorer/file_explorer.cpp

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@
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

1932
bool GetFileExplorerWindowPaths(Utf8FileList& files)
2033
{
@@ -127,7 +140,7 @@ bool GetFileExplorerWindowPaths(Utf8FileList& files)
127140
bool 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
}

src/refresh_file_explorer/file_explorer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
typedef std::vector<std::string> Utf8FileList;
99

10+
/// <summary>
11+
/// Search a given path within a file list.
12+
/// </summary>
13+
/// <param name="list">The list of path to search into.</param>
14+
/// <param name="path">The value to seach for.</param>
15+
/// <returns>Returns true when given path was found. Returns false otherwise.</returns>
16+
bool FindPath(const Utf8FileList& list, const std::string& path);
17+
1018
/// <summary>
1119
/// Get the list of paths for all opened File Explorer windows.
1220
/// The paths are returned in utf8 format.
@@ -33,3 +41,15 @@ void TestOpenFolderUnicode();
3341
/// <param name="paths">The list of paths to print.</param>
3442
/// <param name="str">The output string.</param>
3543
void PrintPathsToString(const Utf8FileList& paths, tstring_t& str);
44+
45+
/// <summary>
46+
/// Get the path (utf8) of File Explorer executable.
47+
/// </summary>
48+
/// <returns>Returns a valid string when the operation is succesful. Returns an empty string on error.</returns>
49+
std::string GetFileExplorerExecPath();
50+
51+
/// <summary>
52+
/// Show a MessageBox centered on parent window.
53+
/// </summary>
54+
/// <returns>Returns true when the operation is succesful. Returns false otherwise.</returns>
55+
bool KillFileExplorerProcesses();

0 commit comments

Comments
 (0)