问题
On Windows 7, when I used process_create/3 in SWI Prolog to open an application like Notepad.exe, the notepad opens. However, it doesn't work for applications that use command prompt. As an example, when I tried to open the command prompt window, using:
main :- process_create('C:\\WINDOWS\\system32\\cmd.exe',[] ,[]).
which gives an
ERROR: Process "c:\windows\system32\cmd.exe": exit status: 1
Similarly, when trying it to open powershell.exe, it doesn't work either. the console just hangs, without displaying an error.
Any help is greatly appreciated.
回答1:
To start a separate console window:
?- process_create(path(cmd), ['/c', 'start', 'cmd'], []).
Or even shorter (but I don't know how portable this is):
?- process_create(path(cmd), ['/c', 'start'], []).
Or this way (idea taken from SWI-Prolog source code):
?- getenv('COMSPEC', CMD), process_create(CMD, ['/c', 'start'], []).
To start an external BAT command with 3 arguments:
?- process_create(
path(cmd),
['/c', 'start', 'cmd', '/c', 'c:\\test\\test.bat', 'arg1', 'arg2', 'arg3'],
[]).
Important note: theoretically you could pass all this arguments as one string, eg. '/c start cmd ...'
instead of list of strings but strange things may happen in more sophisticated cases. In my case SWI-Prolog 7.2.3 added single quote or single double quote at the end, so the last argument passed to batch script was not arg3
but arg3'
or arg3"
.
来源:https://stackoverflow.com/questions/38144451/error-while-using-command-prompt-or-shell-using-process-create-3-in-swi-prolog