Skip to content

Commit 086f45a

Browse files
committed
Fixed std::system opening a terminal window
1 parent d4b058e commit 086f45a

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

Sources/Overload/OvEditor/include/OvEditor/Utils/ExternalTools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace OvEditor::Utils
1919
};
2020

2121
constexpr auto ExternalTools = std::to_array<ExternalTool>({
22-
ExternalTool{ "Visual Studio Code", "code \"{}\"" },
22+
ExternalTool{ "Visual Studio Code", "code {}" },
2323
ExternalTool{ "Sublime Text", "subl {}" },
2424
ExternalTool{ "Atom", "atom {}" },
2525
});

Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ class FolderContextualMenu : public BrowserItemContextualMenu
190190
OvEditor::Settings::EditorSettings::FolderExternalToolName.Get()
191191
);
192192
}
193-
virtual void Execute() override
193+
virtual void Execute(OvUI::Plugins::EPluginExecutionContext p_context) override
194194
{
195-
BrowserItemContextualMenu::Execute();
195+
BrowserItemContextualMenu::Execute(p_context);
196196

197197
// Keep the "Open In External Tool" menu item label up to date
198198
m_openInExternalTool->name = GetOpenInExternalToolName();
@@ -214,14 +214,11 @@ class FolderContextualMenu : public BrowserItemContextualMenu
214214
std::make_format_args(filePath)
215215
);
216216

217-
const auto result = std::system(command.c_str());
218-
219-
if (result != 0)
217+
if (!OvTools::Utils::SystemCalls::ExecuteCommand(command))
220218
{
221219
OVLOG_ERROR(std::format(
222-
"Failed to open {} with error code: {}",
223-
OvEditor::Settings::EditorSettings::FolderExternalToolName.Get(),
224-
std::to_string(result)
220+
"Failed to open {}",
221+
OvEditor::Settings::EditorSettings::FolderExternalToolName.Get()
225222
));
226223
}
227224
};

Sources/Overload/OvTools/include/OvTools/Utils/SystemCalls.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ namespace OvTools::Utils
3838
*/
3939
static void EditFile(const std::string& p_file);
4040

41+
/**
42+
* Execute a custom command. Returns true if the command invocation succeeded
43+
* @param p_command
44+
*/
45+
static bool ExecuteCommand(const std::string_view p_command);
46+
4147
/**
4248
* Open the given url with the default browser
4349
* @param p_url

Sources/Overload/OvTools/src/OvTools/Utils/SystemCalls.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
* @licence: MIT
55
*/
66

7+
#include <cassert>
8+
#include <format>
9+
#include <iostream>
10+
#include <memory>
11+
712
#include "OvTools/Utils/PathParser.h"
813
#include "OvTools/Utils/SystemCalls.h"
914

10-
#include <Windows.h>
1115
#include <ShlObj.h>
12-
#include <memory>
13-
#include <assert.h>
16+
#include <Windows.h>
1417

1518
void OvTools::Utils::SystemCalls::ShowInExplorer(const std::string & p_path)
1619
{
@@ -26,9 +29,44 @@ void OvTools::Utils::SystemCalls::OpenFile(const std::string & p_file, const std
2629

2730
void OvTools::Utils::SystemCalls::EditFile(const std::string & p_file)
2831
{
29-
ShellExecuteW(NULL, NULL, std::wstring(p_file.begin(), p_file.end()).c_str(), NULL, NULL, SW_NORMAL);
32+
ShellExecuteW(NULL, NULL, std::wstring(p_file.begin(), p_file.end()).c_str(), NULL, NULL, SW_SHOWNORMAL);
3033
}
3134

35+
bool OvTools::Utils::SystemCalls::ExecuteCommand(const std::string_view p_command)
36+
{
37+
STARTUPINFO startupInfo;
38+
PROCESS_INFORMATION processInfo;
39+
40+
ZeroMemory(&startupInfo, sizeof(startupInfo));
41+
startupInfo.cb = sizeof(startupInfo);
42+
ZeroMemory(&processInfo, sizeof(processInfo));
43+
44+
std::string command = std::format("cmd.exe /c {}", p_command);
45+
46+
bool success = (CreateProcess(
47+
nullptr, // Application name (nullptr uses command line)
48+
command.data(), // Command to execute
49+
nullptr, // Process security attributes
50+
nullptr, // Thread security attributes
51+
FALSE, // Do not inherit handles
52+
CREATE_NO_WINDOW, // Run the process without a window
53+
nullptr, // Environment variables
54+
nullptr, // Current directory
55+
&startupInfo, // STARTUPINFO structure
56+
&processInfo // PROCESS_INFORMATION structure
57+
));
58+
59+
// Wait until child process exits.
60+
WaitForSingleObject(processInfo.hProcess, INFINITE);
61+
62+
// Close the process and thread handles
63+
CloseHandle(processInfo.hProcess);
64+
CloseHandle(processInfo.hThread);
65+
66+
return success;
67+
}
68+
69+
3270
void OvTools::Utils::SystemCalls::OpenURL(const std::string& p_url)
3371
{
3472
ShellExecute(0, 0, p_url.c_str(), 0, 0, SW_SHOW);

0 commit comments

Comments
 (0)