Search code examples
lispautocadautolisp

Lisp To Communicate With External Process


What I would like to do is have an AutoLISP program call an executable file and get a response from that executable.

I understand that we are currently able to call applications with startapp e.g.

(startapp "notepad.exe" "acad.lsp")

but to my understanding, I don't believe that startapp can return values from the called application.

In context I would like to be able to call an application from lisp and when that application is closing, to send a status code back to the lisp that will allow it to continue execution.

in fake lisp code

(
    (startapp "myapp.exe" "args")
    (*DO UNTIL STATUS CODE == 1* or *"myapp.exe is stopped*
        *CODE*
    )
    *CONTINUE EXECUTION
)

If something of this nature is possible in LISP, or if there is a better way to see if a process has ended in LISP, any direction would be appreciated.


Solution

  • Run the external application and wait until finish process You can do like this:

    (command "_Shell" (strcat path app ) )
    

    easy to run, but don't have easy access to returned value.

    Or You can do it like this

    (defun Sleep (n / lastCmdecho ) 
        (setq lastCmdecho (getvar  "cmdecho"))
        (setvar "cmdecho" 0)
        (eval (list  'VL-CMDF "_.delay" n ) )
        (setvar "cmdecho" lastCmdecho )
    
    )
    
    (defun C:ExternalApplication (  / *error* )
        (defun *error* ( msg / ) 
            (if (not (null msg ) )  (progn (princ "\nC:ExternalApplication:*error*: " ) (princ msg ) (princ "\n")   ) )
        )
        (setq path "C:\\Windows\\")
        (setq app (strcat "Notepad.exe" ) )
        (print (strcat "Run " (strcat path app ) ) )
    
        (setq Shell (vlax-get-or-create-object "Wscript.Shell"))
        (setq AppHandle(vlax-invoke-method Shell 'Exec (strcat path app ) ))
        (while ( = (vlax-get-property AppHandle 'Status ) 0)
            (Sleep 1000)
        )`
        (vlax-release-object Shell) 
        (print "Process finished" )
    )
    

    Now if Your application returns Status, You have it.

    If Your application manipulates Acad environment You can set value by system variable (setvar) or environment variable (setenv).

    If not, You can save value to system registry and read it for example by: (getcfg )