Search code examples
delphivariablesshellexecute

How can i run shellexecute with a variable?


I try to execute a command in shell via delphi but it won't work. I use this script:

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

But i get the error:

[dcc32 Error] Unit1.pas(329): E2010 Incompatible types: 'PWideChar' and 'AnsiString'

Also if i change string to pwidechar is doesn't work. How can i fix this?


Solution

  • Try this:

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