tools
読み取り中…
検索中…
一致する文字列を見つけられません
proc.h
[詳解]
1#pragma once
2#include <windows.h>
3#include <iostream>
4#include <vector>
5#include <boost/locale.hpp>
6
7/**
8 * @file proc.h
9 * @brief プロセス起動ユーティリティ(Windows)
10 *
11 * Windows API の CreateProcess を使用して外部プロセスを起動します。
12 * UNICODE ビルドと ANSI ビルドの両方に対応しています。
13 */
14
15/**
16 * @namespace proc
17 * @brief プロセス操作用名前空間
18 */
19namespace proc {
20#ifdef UNICODE
21 /**
22 * @brief UTF-8 文字列を wstring に変換する(UNICODE ビルド専用)
23 * @param u8 変換元の UTF-8 文字列
24 * @return 変換後の wstring
25 */
26 inline std::wstring to_wstring(const std::string& u8) {
27 return boost::locale::conv::to_utf<wchar_t>(u8, "UTF-8");
28 }
29
30 /**
31 * @brief 外部アプリケーションを起動する(UNICODE ビルド版)
32 * @param app 起動するアプリケーションのパス(UTF-8 文字列)
33 * @param arg コマンドライン引数(デフォルト: 空文字列)
34 * @param wait プロセスの終了を待つか(デフォルト: false)
35 * @param cd 起動時のカレントディレクトリ(デフォルト: 空文字列 = 親と同じ)
36 * @return wait=true の場合は終了コード、wait=false の場合は 0
37 * @throws std::runtime_error 起動失敗または終了コード取得失敗時
38 */
39 inline int start(const std::string app, std::string arg = "", const bool wait = false, const std::string& cd = "") {
40 STARTUPINFO si = { sizeof(si) };
41 PROCESS_INFORMATION pi;
42
43 if (CreateProcess(
44 to_wstring(app).c_str(), // アプリのパス
45 to_wstring(app + " " + arg).data(), // コマンドライン引数(アプリ名含めない)
46 NULL, NULL, // セキュリティ属性
47 false, // 子プロセスにハンドルを継承させない
48 0, // 作成フラグ(CREATE_NO_WINDOWとかも指定可能)
49 NULL, // 環境変数(NULLなら親プロセスと同じ)
50 (cd.empty()? NULL: to_wstring(cd).c_str()), // カレントディレクトリ(NULLなら親と同じ)
51 &si, &pi
52 )) {
53 if (wait) {
54 WaitForSingleObject(pi.hProcess, INFINITE);
55 DWORD exitCode;
56 if (GetExitCodeProcess(pi.hProcess, &exitCode)) {
57 return exitCode;
58 } else throw std::runtime_error("yy981/proc.h::start(): 返り値取得失敗");
59 }
60 CloseHandle(pi.hProcess);
61 CloseHandle(pi.hThread);
62 } else throw std::runtime_error("yy981/proc.h::start(): 起動失敗 | GetLastError()=" + std::to_string(GetLastError()));
63 return 0;
64 }
65
66#else
67 /**
68 * @brief 外部アプリケーションを起動する(ANSI ビルド版)
69 * @param app 起動するアプリケーションのパス
70 * @param arg コマンドライン引数(デフォルト: 空文字列)
71 * @param wait プロセスの終了を待つか(デフォルト: false)
72 * @param cd 起動時のカレントディレクトリ(デフォルト: 空文字列 = 親と同じ)
73 * @return wait=true の場合は終了コード、wait=false の場合は 0
74 * @throws std::runtime_error 起動失敗または終了コード取得失敗時
75 */
76 inline int start(const std::string app, std::string arg = "", const bool wait = false, const std::string& cd = "") {
77 STARTUPINFO si = { sizeof(si) };
78 PROCESS_INFORMATION pi;
79
80 if (CreateProcess(
81 app.c_str(), // アプリのパス
82 (app + " " + arg).data(), // コマンドライン引数(アプリ名含めない)
83 NULL, NULL, // セキュリティ属性
84 false, // 子プロセスにハンドルを継承させない
85 0, // 作成フラグ(CREATE_NO_WINDOWとかも指定可能)
86 NULL, // 環境変数(NULLなら親プロセスと同じ)
87 (cd.empty()? NULL: cd.c_str()), // カレントディレクトリ(NULLなら親と同じ)
88 &si, &pi
89 )) {
90 if (wait) {
91 WaitForSingleObject(pi.hProcess, INFINITE);
92 DWORD exitCode;
93 if (GetExitCodeProcess(pi.hProcess, &exitCode)) {
94 return exitCode;
95 } else throw std::runtime_error("yy981/proc.h::start(): 返り値取得失敗");
96 }
97 CloseHandle(pi.hProcess);
98 CloseHandle(pi.hThread);
99 } else throw std::runtime_error("yy981/proc.h::start(): 起動失敗 | GetLastError()=" + std::to_string(GetLastError()));
100 return 0;
101 }
102
103#endif
104}
プロセス操作用名前空間
int start(const std::string app, std::string arg="", const bool wait=false, const std::string &cd="")
外部アプリケーションを起動する(ANSI ビルド版)
Definition proc.h:76