HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
processthreadsapi.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8#include "win32_error_intf.hpp"
9#include <expected>
10#include <string>
11#include <system_error>
12
13hi_export_module(hikogui.win32 : processthreadsapi);
14
15hi_export namespace hi {
16inline namespace v1 {
17
18[[nodiscard]] inline std::expected<uint32_t, win32_error> win32_GetExitCodeProcess(HANDLE process_handle) noexcept
19{
20 DWORD exit_code = 0;
21 if (GetExitCodeProcess(process_handle, &exit_code)) {
22 if (exit_code == STILL_ACTIVE) {
23 return std::unexpected{win32_error::status_pending};
24 } else {
25 return exit_code;
26 }
27 } else {
28 return std::unexpected{win32_GetLastError()};
29 }
30}
31
32template<typename StartupInfo>
33[[nodiscard]] inline std::expected<PROCESS_INFORMATION, win32_error> win32_CreateProcess(
34 std::optional<std::string> application_name,
35 std::optional<std::string> command_line,
36 SECURITY_ATTRIBUTES const *process_attributes,
37 SECURITY_ATTRIBUTES const *thread_attributes,
38 bool inherit_handles,
39 uint32_t creation_flags,
40 void const *environment,
41 std::optional<std::string> current_directory,
42 StartupInfo const &startup_info)
43{
44 auto application_name_wstr = std::wstring{};
45 wchar_t const *application_name_cstr = nullptr;
46 if (application_name) {
47 if (auto application_name_wstr_ = win32_MultiByteToWideChar(*application_name)) {
48 application_name_wstr = *application_name_wstr_;
49 application_name_cstr = application_name_wstr.c_str();
50 } else {
51 return std::unexpected{application_name_wstr_.error()};
52 }
53 }
54
55 auto command_line_wstr = std::wstring{};
56 wchar_t *command_line_cstr = nullptr;
57 if (command_line) {
58 if (auto command_line_wstr_ = win32_MultiByteToWideChar(*command_line)) {
59 command_line_wstr = *command_line_wstr_;
60 command_line_cstr = command_line_wstr.data();
61 } else {
62 return std::unexpected{command_line_wstr_.error()};
63 }
64 }
65
66 auto current_directory_wstr = std::wstring{};
67 wchar_t *current_directory_cstr = nullptr;
68 if (current_directory) {
69 if (auto current_directory_wstr_ = win32_MultiByteToWideChar(*current_directory)) {
70 current_directory_wstr = *current_directory_wstr_;
71 current_directory_cstr = current_directory_wstr.data();
72 } else {
73 return std::unexpected{current_directory_wstr_.error()};
74 }
75 }
76
77 auto r = PROCESS_INFORMATION{};
78
79 if (not CreateProcessW(
80 application_name_cstr,
81 command_line_cstr,
82 const_cast<SECURITY_ATTRIBUTES *>(process_attributes),
83 const_cast<SECURITY_ATTRIBUTES *>(thread_attributes),
84 inherit_handles,
85 static_cast<DWORD>(creation_flags),
86 const_cast<void *>(environment),
87 current_directory_cstr,
88 const_cast<STARTUPINFOW *>(reinterpret_cast<STARTUPINFOW const *>(&startup_info)),
89 &r)) {
90 return std::unexpected{win32_GetLastError()};
91 }
92
93 return r;
94}
95
96} // namespace v1
97}
Rules for working with win32 headers.
The HikoGUI namespace.
Definition array_generic.hpp:20
std::expected< std::wstring, win32_error > win32_MultiByteToWideChar(std::string_view s, unsigned int code_page=CP_UTF8, uint32_t flags=0) noexcept
Convert a win32-API compatible std::wstring to a multi-byte std::string.
Definition stringapiset.hpp:58
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T c_str(T... args)
T data(T... args)
T unexpected(T... args)