2
votes

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
Do you need to start the application so that you start using it instead of the Prolog program currently running? Without additional arguments, process_create/3 is used for running a non-interactive program once. If you want to switch focus you might want to read carefully through all of the options you can pass to process_create/3. - user1812457
From my Prolog code, I am trying to start the application which is a .bat that starts a command prompt window and starts running in it. You need to pass arguments to it. Now I am able to run the application from my code using win_exec/2 and it works too(including passing parameters to it). But I wanted to run it using process_create/3 passing arguments to it by passing them in the Args list as explained in the documentation. However, it doesn't work. So I tried to open Notepad.exe and cmd.exe to see how it works, as explained above. Any idea on how to make it work? Thanks. - shujin
For the time being you might be better off using win_exec/2. I don't use Windows so the best I can do to help you is to read the documentation more carefully than you ;) but it seems that process_create/3 is not as useful as win_exec/2 for Windows. Don't take my word though. - user1812457

1 Answers

2
votes

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