Checking the return code from an application executed

允我心安 提交于 2019-12-11 07:34:46

问题


I want to be able to check the return code from an application that is executed.

I tried implement it by the PowerBuilder function Run, but its not return the code of application which executed.

Thanks.


回答1:


In fact, Run() launches the target synchronously, so by the time the next line of PowerScript runs, your Run() app may not be finished, let alone have a return code available. You need to make the Windows API calls with appropriate parameters yourself to achieve this, instead of relying on the PowerScript wrapper, which only gives you the synchronous option.

The following is what I have to launch a DOS command and get the return value. You may or may not need to tweak the parameters to the API calls as appropriate. The API calls are assuming a Unicode version of PowerBuilder, i.e. 10.0 or later.

External Function Prototypes

FUNCTION boolean CreateProcess(string AppName, string CommLine, long l1, long l2, boolean binh, long creationflags, long l3, string dir, str_startupinfo startupinfo, ref str_processinformation pi ) library 'kernel32.dll' alias for "CreateProcessW"
FUNCTION long WaitForSingleObject ( ulong ul_Notification, long lmillisecs ) library "kernel32.dll"
FUNCTION long GetExitCodeProcess(ulong hProcess,ref ulong lpExitCode) LIBRARY "kernel32.dll"
FUNCTION boolean CloseHandle(ulong h) library 'kernel32.dll'


ObjectStructure str_startupinfo

    ulong       cb
    string      lpreserved
    string      lpdesktop
    string      lptitle
    ulong       dwx
    ulong       dwy
    ulong       dwxsize
    ulong       dwysize
    ulong       dwxcountchars
    ulong       dwycountchars
    ulong       dwfillattribute
    ulong       dwflags
    uint        wshowwindow
    uint        cbreserved2
    string      lpreserved2
    uint        hstdinput
    uint        hstdoutput
    uint        hstderror

ObjectStructure str_processinformation

    unsignedlong        hprocess
    unsignedlong        hthread
    long        dwprocessid
    long        dwthreadid

function of_runandwait (string as_command, boolean ab_Visible) returns ulong

constant long STARTF_USESHOWWINDOW = 1
constant long CREATE_NEW_CONSOLE = 16
constant long NORMAL_PRIORITY_CLASS = 32
constant long INFINITE = -1
boolean lb_Return
long ll_Null, ll_CreationFlags, ll_Return
ulong lul_ProcessReturn
string ls_CurDir, ls_Null
str_StartupInfo lstr_Start
str_Processinformation lstr_PI

SetNull(ll_Null)
SetNull(ls_Null)
SetNull(ls_CurDir)

lstr_Start.cb                = 72
lstr_Start.dwFlags       = STARTF_USESHOWWINDOW
IF ab_Visible THEN
    lstr_Start.wShowWindow   = 1
ELSE
    lstr_Start.wShowWindow   = 0
END IF

ll_CreationFlags = CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS

lb_Return = CreateProcess (ls_Null, as_Command, ll_Null, ll_Null, FALSE, ll_CreationFlags, ll_Null, ls_CurDir, lstr_Start, lstr_PI)
ll_Return = WaitForSingleObject (lstr_PI.hProcess, INFINITE)
ll_Return = GetExitCodeProcess (lstr_PI.hProcess, lul_ProcessReturn)
CloseHandle(lstr_PI.hProcess)
CloseHandle(lstr_PI.hThread)

RETURN lul_ProcessReturn

Good luck,

Terry.




回答2:


OleObject wsh
integer  li_rc

CONSTANT integer MAXIMIZED = 3
CONSTANT integer MINIMIZED = 2
CONSTANT integer NORMAL = 1
CONSTANT boolean WAIT = TRUE
CONSTANT boolean NOWAIT = FALSE

wsh = CREATE OleObject
li_rc = wsh.ConnectToNewObject( "WScript.Shell" )

li_rc = wsh.Run("Notepad" , NORMAL, WAIT)
messagebox("hello", "world")

The third parameter WAIT runs the executable and waits for the completion and li_rc will receive the exit code.



来源:https://stackoverflow.com/questions/5299345/checking-the-return-code-from-an-application-executed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!