Skip to content

Commit 6307435

Browse files
feat: enhance Process launch methods with LaunchOptions struct
1 parent 279d7d8 commit 6307435

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

include/blook/process.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <optional>
1313
#include <span>
1414
#include <string>
15+
#include <string_view>
1516
#include <vector>
1617

1718
namespace blook {
@@ -25,6 +26,12 @@ class Process : public std::enable_shared_from_this<Process> {
2526
public:
2627
#ifdef _WIN32
2728

29+
struct LaunchOptions {
30+
bool suspended = false;
31+
bool detached = false;
32+
DWORD extra_flags = 0;
33+
};
34+
2835
explicit Process(HANDLE h);
2936

3037
explicit Process(DWORD pid);
@@ -119,9 +126,12 @@ class Process : public std::enable_shared_from_this<Process> {
119126
void *inject(const std::string &dll_path,
120127
InjectMethod method = InjectMethod::CreateRemoteThread);)
121128

122-
static std::shared_ptr<Process> launch(const std::string &path,
123-
bool suspended = false);
124-
static std::shared_ptr<Process> launch_suspended(const std::string &path);
129+
WIN_ONLY(
130+
static std::shared_ptr<Process>
131+
launch(std::string_view path, LaunchOptions opts = {});
132+
static std::shared_ptr<Process>
133+
launch_suspended(std::string_view path,
134+
DWORD extra_flags = 0);)
125135
};
126136

127137
} // namespace blook

src/platform/windows/process.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -506,19 +506,25 @@ void Process::resume() {
506506
t.resume();
507507
}
508508

509-
std::shared_ptr<Process> Process::launch(const std::string &path,
510-
bool suspended) {
509+
std::shared_ptr<Process> Process::launch(std::string_view path,
510+
LaunchOptions opts) {
511511
STARTUPINFOA si = {sizeof(si)};
512512
PROCESS_INFORMATION pi = {};
513-
if (!CreateProcessA(path.c_str(), NULL, NULL, NULL, FALSE,
514-
suspended ? CREATE_SUSPENDED : 0, NULL, NULL, &si, &pi))
513+
DWORD flags = opts.extra_flags;
514+
if (opts.suspended)
515+
flags |= CREATE_SUSPENDED;
516+
if (opts.detached)
517+
flags |= CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB;
518+
if (!CreateProcessA(path.data(), NULL, NULL, NULL, FALSE, flags, NULL, NULL,
519+
&si, &pi))
515520
throw std::runtime_error(std::format("Failed to launch process: {}",
516521
GetLastError()));
517522
CloseHandle(pi.hThread);
518523
return attach(pi.hProcess);
519524
}
520525

521-
std::shared_ptr<Process> Process::launch_suspended(const std::string &path) {
522-
return launch(path, true);
526+
std::shared_ptr<Process> Process::launch_suspended(std::string_view path,
527+
DWORD extra_flags) {
528+
return launch(path, {.suspended = true, .extra_flags = extra_flags});
523529
}
524530
} // namespace blook

0 commit comments

Comments
 (0)