如何在 TCL 中获取 C exe 文件的 return 值 (Windows)

How to get a return value of a C exe file in TCL (Windows)

我一直在尝试使用 catch 和 exec 来 运行 编译后的 C 程序,return 最后是一个 int,但到目前为止我只能得到 return 0 和 1 的值,0 returning 当 c 程序 returns 0 和 1 用于其他任何东西。 有没有办法从 C 程序中获取任何 return 值(例如 5)?

是的。使用 catch 命令的第三个参数来检索 return 选项:

 set returnvalue 0
 if { [catch { exec ./myprogram } result retopts] } {
   lassign [dict get $retopts -errorcode] class pid retcode
   set returnvalue 1
   if { $class eq "CHILDSTATUS" } {
     set returnvalue $retcode
   }
 }

也可以使用try / on error块:

try {
  exec ./myprogram
  set returnvalue 0
} on error {result retopts} {
  lassign [dict get $retopts -errorcode] class pid retcode
  set returnvalue 1
  if { $class eq "CHILDSTATUS" } {
    set returnvalue $retcode
  }
}

编辑: try / trap 示例:

set returnvalue 1
try {
  exec ./myprogram
  set returnvalue 0
} trap {CHILDSTATUS} {result retopts} {
  lassign [dict get $retopts -errorcode] class pid retcode
  set returnvalue $retcode
}

参考文献:catch errorCode try