我怎么能用变量运行shellexecute?

Jan*_*tze 2 delphi variables shellexecute

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

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

但我得到错误:

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

此外,如果我将字符串更改为pwidechar是不起作用.我怎样才能解决这个问题?

Sim*_*aWB 5

试试这个:

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

  • 而不是PWideChar,你应该使用PChar - 这将使它兼容Unicode和非Unicode版本的Delphi(Chris编写答案,在我写它的时候这样做:-)).而且你不需要先"计算"字符串 - 你可以使用PChar('/ C'+ ShellExecCommand +'> output.txt') (2认同)