【问题标题】:How can i run shellexecute with a variable?如何使用变量运行 shellexecute?
【发布时间】:2014-03-04 07:53:18
【问题描述】:

我尝试通过 delphi 在 shell 中执行命令,但它不起作用。 我使用这个脚本:

var
shellexecommand:string;
begin
ShellExecute(0, nil, 'cmd.exe', '/C ' + shellexecommand + ' > output.txt', nil, SW_HIDE);
end;

但我得到了错误:

[dcc32 错误] Unit1.pas(329):E2010 不兼容类型:'PWideChar' 和'AnsiString'

另外,如果我将字符串更改为 pwidechar 也不起作用。 我该如何解决这个问题?

【问题讨论】:

  • 这里的 CreateProcess 可能会更好。特别是如果您实际上想要父进程中其他进程的输出并且不需要文本文件来忍受。

标签: delphi variables shellexecute


【解决方案1】:

试试这个:

var
  shellexecommand:string;
begin
  // shellexecommand := ....
  shellexecommand := '/C ' + shellexecommand + ' > output.txt';
  ShellExecute(0, nil, 'cmd.exe', PChar(shellexecommand), nil, SW_HIDE);
end;

【讨论】:

  • 你应该使用 PChar 而不是 PWideChar - 这将使它与 Delphi 的 Unicode 和非 Unicode 版本兼容(克里斯编辑了答案以做到这一点,而我写它:-))。而且您不需要先“计算”字符串 - 您可以使用 PChar(' /C '+ShellExecCommand+' >output.txt')
  • @HeartWare - 啊,伟大的思想相似或类似的东西! ;-) 我在您添加评论时进行了编辑 - 希望 SimaWB 不介意。
猜你喜欢
  • 1970-01-01
  • 2023-03-02
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多