Error while using command prompt or shell using pr

2019-07-15 08:40发布

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条回答
放荡不羁爱自由
2楼-- · 2019-07-15 09:19

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".

查看更多
登录 后发表回答