windows下PAI执行带命令行参数的外部命令

本文介绍了一个使用ShellExecuteExW函数执行外部命令的C++代码示例。该函数用于启动另一个应用程序并控制其运行方式,如隐藏窗口、等待进程结束等。通过这个示例,读者可以了解如何在Windows环境下利用API进行进程调用。
bool CFileBase::ExcuteCmdW(const wchar_t * CommandLine, const wchar_t * ExeName)
{
	do
	{
	    SHELLEXECUTEINFOW shell;
		memset(&shell, 0, sizeof(SHELLEXECUTEINFOW));
		shell.cbSize = sizeof(SHELLEXECUTEINFOW);
		shell.fMask = SEE_MASK_NOCLOSEPROCESS;
		shell.lpVerb = L"Open";
		shell.nShow = SW_HIDE;
		shell.lpParameters = CommandLine;
		shell.lpFile = ExeName;

		int ret = ShellExecuteExW(&shell);//ÕâÀïÈç¹ûÖ´ÐÐʧ°ÜÔõô°ìÄØ
		if (ret != 0)
		{
			if (shell.hProcess == NULL)
				break;
			WaitForSingleObject(shell.hProcess, INFINITE);
			return true;
		}
	} while (false);
	return false;
}

 

public class KataGoEngine : MonoBehaviour { public static KataGoEngine instance; private Process _kataGoProcess; private StreamWriter _inputStream; private AutoResetEvent _responseEvent = new AutoResetEvent(false); private string _lastResponse = ""; public bool isOpenAI = false; private void Awake() { DontDestroyOnLoad(gameObject); instance = this; StartEngine(); } private void Start() { } public void StartEngine() { if (isOpenAI) { return; } isOpenAI = true; // 使用@禁止转义 string katagoexe = Path.Combine( Application.dataPath,@"KataGo\katago.exe"); string default_gtp = Path.Combine(Application.dataPath, @"KataGo\default_gtp.cfg"); string modelPath = Path.Combine(Application.dataPath, @"KataGo\kata1-b28c512nbt-s10904468224-d5317014586.bin.gz"); var startInfo = new ProcessStartInfo { FileName = katagoexe, Arguments = $"gtp -config \"{default_gtp}\" -model \"{modelPath}\"", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, CreateNoWindow = true }; _kataGoProcess = new Process { StartInfo = startInfo }; _kataGoProcess.OutputDataReceived += (sender, e) => { UnityEngine.Debug.Log("打印Katago所有回调 " + e.Data); if (e.Data != null && !e.Data.Equals("")) { _lastResponse = e.Data; _responseEvent.Set(); // 通知收到响应 } }; _kataGoProcess.Start(); _inputStream = _kataGoProcess.StandardInput; _kataGoProcess.BeginOutputReadLine(); // 开始异步读取输出 } // 发送GTP命令 public string SendCommand(string command) { _lastResponse = ""; _inputStream.WriteLine(command); // _responseEvent.WaitOne(1000); // 等待5秒超时 return _lastResponse; } public void StartSetKatagoQipan() { UnityEngine.Debug.Log("Katago回复 " + SendCommand("boardsize 19")); UnityEngine.Debug.Log("Katago回复 " + SendCommand("komi 7.5")); UnityEngine.Debug.Log("Katago回复 " + SendCommand("clear_board")); } public void TongbuQipan(WeizhiBean qiwz,QiziLeix qizileix) { char[] gtpLetters = "ABCDEFGHJKLMNOPQRRT".ToCharArray(); // GTP列标识(跳过I) switch (qizileix) { case QiziLeix.heiqi: var luodian = gtpLetters[qiwz.pai - 1]+"" + (qiwz.ge); UnityEngine.Debug.Log("落点黑 " + luodian); UnityEngine.Debug.Log("Katago回复黑棋 " + SendCommand($"play B " + luodian)); break; case QiziLeix.baiqi: var luodianbai = gtpLetters[qiwz.pai - 1] + "" + (qiwz.ge ); UnityEngine.Debug.Log("落点白 " + luodianbai); UnityEngine.Debug.Log("Katago回复白棋 " + SendCommand($"play W " + luodianbai)); break; default: break; } } public void QingqiuAiLuozi() { switch (QipanManager.instance.currLeix) { case QiziLeix.heiqi: string response = SendCommand("genmove w"); UnityEngine.Debug.Log($"KataGo白棋建议: {response}"); break; case QiziLeix.baiqi: string response2 = SendCommand("genmove b"); UnityEngine.Debug.Log($"KataGo黑棋建议: {response2}"); break; default: break; } } // 关闭引擎 public void Shutdown() { SendCommand("quit"); _kataGoProcess?.WaitForExit(1000); _kataGoProcess?.Close(); } private void OnApplicationQuit() { Shutdown(); } } 这是我的Katago代码现在我的Katago只会返回"="和空字符如何解决
最新发布
10-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值