3

In CAD application (ZWCAD) I start my application by AutoLISP.

(startapp "C://[path]//Application.exe")

so the application runs each time new file is created. Works OK.

Now I want to limit instances of application just to one. So how can I check if the application is already running?

CAD Developer
  • 1,532
  • 2
  • 21
  • 27

2 Answers2

2

You could use a function to query the Win32_Process WMI class for a process with name matching your given application.

Such a function might be written in the following way:

;; Win32 Process-p  -  Lee Mac
;; Returns T if a process exists with the supplied name

(defun LM:win32process-p ( pro / qry rtn srv wmi )
    (if (setq wmi (vlax-create-object "wbemscripting.swbemlocator"))
        (progn
            (setq rtn
                (vl-catch-all-apply
                   '(lambda ( )
                        (setq srv (vlax-invoke wmi 'connectserver)
                              qry (vlax-invoke srv 'execquery (strcat "select * from win32_process where name = '" pro "'"))
                        )
                        (< 0 (vla-get-count qry))
                    )
                )
            )
            (foreach obj (list qry srv wmi)
                (if (= 'vla-object (type obj)) (vlax-release-object obj))
            )
            (and (not (vl-catch-all-error-p rtn)) rtn)
        )
    )
)

Which could be called in the following way:

_$ (LM:win32process-p "notepad.exe")
T
Lee Mac
  • 15,615
  • 6
  • 32
  • 80
2

Today I found easier way. Ready to use LISP function

(dos_processes)

Which returns list of all running processes.

CAD Developer
  • 1,532
  • 2
  • 21
  • 27
  • But note that this requires the DOSLib library to be installed - (dos_processes) is not part of the standard LISP API in AutoCAD. – Lee Mac Mar 27 '20 at 13:31
  • 1
    So it's one of the differences between AutoCAD and ZWCAD, because in ZWCAD this function is standard. – CAD Developer Mar 30 '20 at 03:08