troubleshooting Question

getting an error code from a batch file in delphi

Avatar of scifibrian
scifibrian asked on
Delphi
5 Comments1 Solution2814 ViewsLast Modified:
I am calling a function that runs a batch file that calls an executable. when the executable crashes it sets the errorlevel to 1. I have inbedded echo commands in the batch file to show that this value is set and kept right to the end of the batch file. When I try to get this value from my deplhi function though it shows an error level of 0 (ie no error).My function is attached. Can anyone tell me why I cannot get the correct value out. Thanks. scifibrian


FUNCTION ExecuteAndWait (
  CONST asCommandLine   : STRING;
  VAR   asErrorMessage  : STRING
  ) : Boolean;

VAR
  lStartInfo   : TStartupInfo;
  lProcessInfo : TProcessInformation;
  lWaitResult  : DWORD;
  lExitCode    : DWord;

BEGIN                                                      { ExecuteAndWait }
Result := True;
asErrorMessage := '';
FillChar (lStartInfo, sizeof (TStartupInfo), 0);
lStartInfo.cb := sizeof (TStartupInfo);

IF (CreateProcess (NIL, pchar (asCommandLine), NIL, NIL, False,0, NIL, NIL, lStartInfo, lProcessInfo)) THEN
  BEGIN
  lWaitResult := WaitForSingleObject (lProcessInfo.hProcess, INFINITE);
  IF (lWaitResult = WAIT_OBJECT_0) THEN
    BEGIN
    IF GetExitCodeProcess (lProcessInfo.hProcess, lExitCode) THEN
      BEGIN
      IF lExitCode > 0 THEN
        BEGIN
        asErrorMessage := 'Process ' + asCommandLine + ' exited abnormally';
        Result := False;
        END
      END
    ELSE
      BEGIN
      asErrorMessage := 'Unable to get exit code from process ' + asCommandLine;
      Result := False;
      END
    END
  ELSE
    BEGIN
    asErrorMessage := 'Process ' + asCommandLine + ' exited prematurely.';
    Result := False;
    END
  END
ELSE
  BEGIN
  asErrorMessage := 'Could not create process ' + asCommandLine;
  Result := False;
  END;

CloseHandle (lProcessInfo.hProcess);
CloseHandle (lProcessInfo.hThread);
END;                                                       { ExecuteAndWait }
ASKER CERTIFIED SOLUTION
Russell Libby
Software Engineer, Advisory
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros