Archive: Process Functions Header


Process Functions Header
I've written a header file with several process related functions I wanted to see in NSIS. These were inspired by PortableApps.com where we use NSIS to script the portable launchers. My goal was to accomplish everything using only the System plugin, so there are no external dependencies. Only one alternate function requires a 3rd party plugin. I hope some other people can find this useful as well.

Download ProcFunc.nsh

${ProcessExists}
Determines if a given process exists by name or PID, and returns the Process ID (PID).

${GetProcPath}
Determines first if a process exists, and returns the full path by name or PID.

${GetProcName}
If the process exists, returns the name by PID.

${ProcessPathSearch}
Searches all running process paths for a given search term, then passes that path and the process's PID to a user defined function for processing (works like the GetDrives macro). Use .::RETURN_ALL::. as the searchterm to return all running process paths.

${ProcessWait}
Will pause script execution until the given process exists or until timeout, by name. Will poll for the process every 500ms. Returns the process's PID, or -1 if timed out.
WARNING: this function is CPU intensive on Windows Vista. I recommend using ${ProcessWait2} if the script will be in this state for an extended period of time.

${ProcessWait2}
Same as ${ProcessWait}, but requires the FindProcDLL plugin, polls every 250ms, and produces much lower CPU load on Windows Vista.

${ProcessWaitClose}
Will pause script execution until the given process no longer exists, by name or PID. Can specify a timeout value (how long to wait), or wait indefinitely. Returns the PID of the closing process if successful, -1 if timed out.

${CloseProcess}
Closes a process by name or PID by sending the WM_CLOSE message to all windows owned by the process. This is the MS recommended method for closing a process. It will give you the opportunity, for example, to save an unsaved txt file open in notepad. This command will return immediately after sending the messages, so it's advisable to check when the process actually closes, ie with ${ProcessWaitClose}. Returns the closed process's PID.

${TerminateProcess}
Forcibly closes a process by name or PID. Returns the terminated process's PID.

${Execute}
Runs a process, supports commandline arguments. Allows you to optionally specify a working directory and returns the process's PID if successful, 0 if it fails.


Oops, quick fix in my ${ProcessWait} timeout counter.


That looks very useful and should have its own Wiki page. See the following link on creating pages on the Wiki.

http://nsis.sourceforge.net/Starting_a_new_page

It'd be worth mentioning this would only work on Windows NT and above on the Wiki page.


I've updated ProcFunc to include, among other internal changes,

${GetProcessParent}
If the process exists, returns the parent process's PID.

Download ProcFunc.nsh

Project thread on PortableApps. This page will be kept up to date more so than this one.