;============================================================ ; iron_process_net.hsp — プロセス管理 (.NET版) ; ; System.Diagnostics.Process を使用。 ; 外部コマンド実行、標準入出力パイプ、プロセス監視に対応。 ; hsp3net 専用。 ; ; API: ; procn_exec "cmd", var_stdout, var_exitcode 実行して stdout と終了コードを取得 ; procn_exec_async "cmd" 非同期実行 (stat=pid) ; procn_exec_stdin "cmd", "input", var_stdout stdin パイプ付き実行 ; procn_kill pid プロセス終了 ; procn_is_running pid 実行中チェック (stat: 1/0) ; procn_set_env "key", "value" 環境変数設定 ; procn_set_workdir "path" 作業ディレクトリ設定 ;============================================================ #ifndef __iron_process_net_hsp__ #define __iron_process_net_hsp__ #module iron_process_net dim _procn_cs_loaded, 1 #deffunc _procn_load_cs if _procn_cs_loaded : return sdim _cs, 16384 _cs = {" using System; using System.IO; using System.Diagnostics; using System.Collections.Generic; public class HspProcessNet { static Dictionary envVars = new Dictionary(); static string workDir = ""; public static string SetEnv(string key, string val) { envVars[key] = val; return "ok"; } public static string SetWorkDir(string path) { workDir = path; return "ok"; } static ProcessStartInfo MakePsi(string cmd) { var psi = new ProcessStartInfo(); psi.FileName = "cmd.exe"; psi.Arguments = "/c " + cmd; psi.UseShellExecute = false; psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.RedirectStandardInput = true; psi.StandardOutputEncoding = System.Text.Encoding.UTF8; psi.StandardErrorEncoding = System.Text.Encoding.UTF8; if (!string.IsNullOrEmpty(workDir)) psi.WorkingDirectory = workDir; foreach (var kv in envVars) { psi.EnvironmentVariables[kv.Key] = kv.Value; } return psi; } public static string Exec(string cmd) { try { var psi = MakePsi(cmd); var proc = Process.Start(psi); string stdout = proc.StandardOutput.ReadToEnd(); string stderr = proc.StandardError.ReadToEnd(); proc.WaitForExit(); int exitCode = proc.ExitCode; proc.Dispose(); return exitCode + "\t" + stdout; } catch (Exception e) { return "-1\tERROR:" + e.Message; } } public static string ExecAsync(string cmd) { try { var psi = MakePsi(cmd); psi.RedirectStandardOutput = false; psi.RedirectStandardError = false; psi.RedirectStandardInput = false; var proc = Process.Start(psi); return proc.Id.ToString(); } catch (Exception e) { return "-1\t" + e.Message; } } public static string ExecStdin(string cmd, string input) { try { var psi = MakePsi(cmd); var proc = Process.Start(psi); proc.StandardInput.Write(input); proc.StandardInput.Close(); string stdout = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); int exitCode = proc.ExitCode; proc.Dispose(); return exitCode + "\t" + stdout; } catch (Exception e) { return "-1\tERROR:" + e.Message; } } public static string Kill(int pid) { try { var proc = Process.GetProcessById(pid); proc.Kill(); proc.Dispose(); return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string IsRunning(int pid) { try { var proc = Process.GetProcessById(pid); bool running = !proc.HasExited; proc.Dispose(); return running ? "1" : "0"; } catch { return "0"; } } } "} loadnet _cs, 3, "" _procn_cs_loaded = 1 return ;------------------------------------------------------------ ; procn_exec "cmd", var_stdout, var_exitcode ;------------------------------------------------------------ #deffunc procn_exec str cmd, var v_stdout, var v_exitcode, \ local _h, local _r, local _s, local _tab _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "Exec", _r, cmd _s = "" + _r _tab = instr(_s, 0, "\t") if _tab < 0 { v_stdout = _s v_exitcode = -1 return 1 } v_exitcode = int(strmid(_s, 0, _tab)) v_stdout = strmid(_s, _tab + 1, strlen(_s) - _tab - 1) return 0 ;------------------------------------------------------------ ; procn_exec_async "cmd" — stat=pid ;------------------------------------------------------------ #deffunc procn_exec_async str cmd, \ local _h, local _r, local _s _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "ExecAsync", _r, cmd _s = "" + _r if instr(_s, 0, "\t") >= 0 : return -1 return int(_s) ;------------------------------------------------------------ ; procn_exec_stdin "cmd", "input", var_stdout ;------------------------------------------------------------ #deffunc procn_exec_stdin str cmd, str input, var v_stdout, \ local _h, local _r, local _s, local _tab _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "ExecStdin", _r, cmd, input _s = "" + _r _tab = instr(_s, 0, "\t") if _tab < 0 { v_stdout = _s return 1 } v_stdout = strmid(_s, _tab + 1, strlen(_s) - _tab - 1) return int(strmid(_s, 0, _tab)) ;------------------------------------------------------------ ; procn_kill pid ;------------------------------------------------------------ #deffunc procn_kill int pid, \ local _h, local _r _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "Kill", _r, pid if instr("" + _r, 0, "ERROR") >= 0 : return 1 return 0 ;------------------------------------------------------------ ; procn_is_running pid — stat: 1=実行中, 0=終了済 ;------------------------------------------------------------ #deffunc procn_is_running int pid, \ local _h, local _r _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "IsRunning", _r, pid return int("" + _r) ;------------------------------------------------------------ ; procn_set_env "key", "value" ;------------------------------------------------------------ #deffunc procn_set_env str key, str val, \ local _h, local _r _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "SetEnv", _r, key, val return 0 ;------------------------------------------------------------ ; procn_set_workdir "path" ;------------------------------------------------------------ #deffunc procn_set_workdir str path, \ local _h, local _r _procn_load_cs newnet _h, "", "HspProcessNet", 0 mcall _h, "SetWorkDir", _r, path return 0 #global #endif