thomh
|
|
« on: May 29, 2014, 10:04:58 AM » |
|
I am trying to use the WinExecAndWait32V2 method listed below on the server from within a TRtcFunction.OnExecute event. It basically starts an OCR application/process and waits for it to finish. However when I get to the CreateProcess call it gives me a "Read of address 00000000" exception.
Is there another preferred way to do this?
{-- WinExecAndWait32V2 ------------------------------------------------} {: Executes a program and waits for it to terminate @Param FileName contains executable + any parameters @Param Visibility is one of the ShowWindow options, e.g. SW_SHOWNORMAL @Returns -1 in case of error, otherwise the programs exit code @Desc In case of error SysErrorMessage( GetlastError ) will return an error message. The routine will process paint messages and messages send from other threads while it waits. }{ Created 27.10.2000 by P. Below -----------------------------------------------------------------------} Function WinExecAndWait32( FileName: String; Visibility: integer ): DWORD; Procedure WaitFor( processHandle: THandle ); Var msg: TMsg; ret: DWORD; Begin Repeat ret := MsgWaitForMultipleObjects( 1, { 1 handle to wait on } processHandle, { the handle } False, { wake on any event } INFINITE, { wait without timeout } QS_PAINT or { wake on paint messages } QS_SENDMESSAGE { or messages from other threads } ); If ret = WAIT_FAILED Then Exit; { can do little here } If ret = (WAIT_OBJECT_0 + 1) Then Begin { Woke on a message, process paint messages only. Calling PeekMessage gets messages send from other threads processed. } While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do DispatchMessage( msg ); End; Until ret = WAIT_OBJECT_0; End; { Waitfor } Var { V1 by Pat Ritchey, V2 by P.Below } zAppName:array[0..512] of char; StartupInfo:TStartupInfo; ProcessInfo:TProcessInformation; Begin { WinExecAndWait32V2 } StrPCopy(zAppName,FileName); FillChar(StartupInfo,Sizeof(StartupInfo),#0); StartupInfo.cb := Sizeof(StartupInfo); StartupInfo.dwFlags := STARTF_USESHOWWINDOW; StartupInfo.wShowWindow := Visibility; If not CreateProcess(nil, zAppName, { pointer to command line string } nil, { pointer to process security attributes } nil, { pointer to thread security attributes } false, { handle inheritance flag } CREATE_NEW_CONSOLE or { creation flags } NORMAL_PRIORITY_CLASS, nil, { pointer to new environment block } nil, { pointer to current directory name } StartupInfo, { pointer to STARTUPINFO } ProcessInfo) { pointer to PROCESS_INF } Then Result := DWORD(-1) { failed, GetLastError has error code } Else Begin Waitfor(ProcessInfo.hProcess); GetExitCodeProcess(ProcessInfo.hProcess, Result); CloseHandle( ProcessInfo.hProcess ); CloseHandle( ProcessInfo.hThread ); End; { Else } End; { WinExecAndWait32V2 }
|