// functions to support DOS shell /* * functions to support DOS shell * * Written by Mathias Mamsch, for use see 'test' below * */ XFLAGS_ &= ~AutoDeclare_ pragma runLim, 0 const string m_shell_handle = "handle" const string m_shell_cmd = "cmd" struct Shell {} //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - DxlObject DxlObjectOf(Shell p) { return (addr_ p) DxlObject } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void check_shell_res(string s) { if (!null s) error "SHELL: executing command failed with following error - " s } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void delete(Shell p) { DxlObject x = DxlObjectOf p; delete x; } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OleAutoObj ShellObject (Shell x) { DxlObject obj = DxlObjectOf x OleAutoObj result = (obj->m_shell_handle) OleAutoObj return result } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // if getOutput is called, it will return only when the command has ended. string getOutput (Shell x) { OleAutoObj obj = ShellObject x OleAutoObj stdout OleAutoArgs args = create() string result check_shell_res oleGet(obj, "StdOut", stdout) string printOut check_shell_res oleMethod (stdout, "ReadAll", args, result) delete args return result } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int getIntProperty(Shell x, string name) { OleAutoObj obj = ShellObject x int result = 0 check_shell_res oleGet(obj, name, result ) return result } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Status 0 = running Status 1 = finished Status 2 = failed int status (Shell x) { return getIntProperty(x, "Status") } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int exitCode (Shell x) { return getIntProperty(x, "ExitCode") } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int processID (Shell x) { return getIntProperty(x, "ProcessID") } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void terminate(Shell x) { OleAutoObj obj = ShellObject x check_shell_res oleMethod (obj, "Terminate") } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // if wait is true the script will wait for the process to end Shell create(string cmd, bool wait) { DxlObject x = new; x->m_shell_cmd = "" x->m_shell_handle = null OleAutoObj OleAutoObj shellObj = oleCreateAutoObject "WScript.Shell" OleAutoObj result = null OleAutoArgs args = create() put(args, cmd) check_shell_res oleMethod(shellObj, "Exec", args, result) while (true && wait) { int status check_shell_res oleGet(result, "Status", status) if (status != 0) break sleep_ 50 } int intVar string var x->m_shell_cmd = cmd x->m_shell_handle = result return addr_ x } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void test () { print "Starting NotePad" Shell npad = create ("Notepad.exe", false) print "Listing a directory ... don't close Nodepad!\n" Shell cmd = create ("cmd.exe /C dir", true) print "Output from cmd.exe:\n" (getOutput cmd) "\n" sleep_ 1000 terminate npad print "Nodepad Exit Code = " exitCode npad "\n" print "Nodepad Status = " status npad "\n" print "Doing something that will return an error:\n" cmd = create ("cmd.exe /C copy 123", true) print "Exit Code:" (exitCode cmd) "\n" print "Status :" (status cmd) "\n" } test() :" (status cmd) "\n" } test(