【发布时间】:2012-07-25 18:45:33
【问题描述】:
在Ubuntu 下,我在OCaml 中编写了一个非常简单的ml.ml:
let () = print_string "hello world, in OCaml\n"
还有一个简单的c.cin C:
#include <stdio.h>
main() {
printf("hello world, in C\n");
return 0; }
然后我通过ocamlc -o mlexe.exe ml.ml 和gcc -o cexe.exe c.c 编译它。在Ubuntu 的终端下启动mlexe.exe 或cexe.exe 会返回字符串。
现在我想从 VBA 代码中调用它。我启动 Windows,打开 Microsoft Excel 文件和 VBA 编辑器,然后输入:
Sub run()
Dim ProcID As Integer
ProcID = Shell("C:\Windows\system32\calc.exe", 1)
Dim Result As Variant
Result = Shell("C:\test\cexe.exe",1)
'Result = Shell("C:\test\mlexe.exe",1)
'Result = Shell("C:\test\cexe.exe")
'Result = Shell("C:\test\mlexe.exe")
End Sub
我希望 Result 得到字符串 hello world...(或者在不太好的情况下是退出代码),运行宏确实会启动计算器,但给我一个错误 Run-time error '5': Invalid procedure call or argument,对于其他 4 个 @987654335 @ 与我自己的可执行文件。
目的只是从VBA代码中调用我自己用另一种语言编译的可执行文件。
谁能告诉我怎么了?
【问题讨论】:
-
如果你使用原生编译器
ocamlopt而不是字节码编译器ocamlc来生成程序会发生什么?我想那可能会更简单。否则,由于hello.exe实际上是一个字节码文件,你可以尝试运行ocamlrun.exe hello.exe -
ocaml 程序是真的return 字符串还是只是print?我猜是后者。
-
@PascalCuoq :我试过
ocamlopt,得到了同样的错误。我试过Result = Shell("""ocamlrun.exe"" C:\test\hello.exe"),它返回File not found。 -
@phimuemue :你说得对,但我试过
let () = "hello world",编译显示Error: This expression has type string but an expression was expected of type unit,是否可以让可执行文件返回除单位以外的其他内容? -
不确定,但我认为返回值通常是整数。也许 OCaml 的
exit函数可以帮助...
标签: c vba excel ocaml executable