Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateProcess, SECURITY_ATTRIBUTES, GetExitCodeProcess question, help!

Status
Not open for further replies.

6volt

Programmer
Jun 4, 2003
74
US
I have a Delphi program, A, which uses CreateProcess to call another Delphi program, B.

If B bombs or is terminated to an error I RAISE, A does not see this.

First, the Boolean CreateProcess is of no use because this just reports the success of CreateProcess.

Now, GetExitCodeProcess seems to be the way to do this, however it always brings back the same code, 126. One possible reason is a "Permission Problem."

Studying things a bit leads to the using "nil" definitions for process and thread security in the CreateProcess call: perhaps this default is not "permitting."

So it looks like I need the PROCESS_QUERY_INFORMATION securtiy level in SECURITY_ATTRIBUTES.

Unfortunatly, it appears that you simply can't put "PROCESS_QUERY_INFORMATION" in lieu of the "nil" since this is an integer and pSecurityAttributes type is required.

I've been spending a couple hours trying to figure out how this is done and my "eyes are spinning..."

Thanks in advance,
Tom
 
I suppose you want to have something to see if the app you started has ended?? use this :

Code:
function ExecuteAndWait(const strCommandLine : String; intVisibility: Integer = SW_SHOW) : Cardinal;
var
  StartupInfo : TStartupInfo;
  ProcessInformation : TProcessInformation;
  intWaitState : DWORD;
begin
  Result := 0;
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb := SizeOf(TStartupInfo);
  StartupInfo.wShowWindow := intVisibility;

  if (CreateProcess(nil, PChar(strCommandLine), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInformation)) then
  begin
    intWaitState := WaitForSingleObject(ProcessInformation.hProcess, INFINITE);

    if (intWaitState = WAIT_OBJECT_0) then
      if (GetExitCodeProcess(ProcessInformation.hProcess, intWaitState)) then
        Result := intWaitState;

    CloseHandle(ProcessInformation.hProcess);
    CloseHandle(ProcessInformation.hThread);
  end;
end;


--------------------------------------
What You See Is What You Get
 
What you have is basically what I originally wrote.

The only problem is that GetExitCodeProcess seems to bring back the same value no matter how the process terminates - even if it errors out from not finding a file.

I was able to run with Security_Attributes set to Process_All_Access and it still behaved the same.

What I was thinking is that since in Program B, I RAISE my own errors, might it be possible to assign an exitcode that I could "get" from Program A after B completes?

FYI, the way I'm handling it now is I write a "start" file with TIME @ start, then an "Error" file with Time @ RAISED error all in Prog B. Then in A, I check to see if Error Time is greater than Start Time.

Yeah, I know, Stone Knives and Bearskins, ya?

Baffled,
Tom
 
to get the exitcode off your delphi app just use
this in your exception block before you terminate the app :

Code:
  ExitCode := 8; // or whatever value

if you want to show a dialog with the exitcode hen app has finished :

Code:
proc X;
var
  i : Integer;
begin
  // Set up an error address so that halt shows a termination dialog
  ErrorAddr := Addr(i);

  // Set the program exit code
  ExitCode := 8; // or whatever value
end;

--------------------------------------
What You See Is What You Get
 
Ha!

You mean it is THAT SIMPLE???

It never occurred to me that it simply was a system variable that you can assign. I thought it would have been some crazy type with some obscure function call to set it.

Good grief!

I can't wait to try it out "tomorrow." (is 2am now)

Thanks
Tom
 
you can also use Halt(X) - where X is the exitcode - to stop your app but take into account to no object destructors are called, only the finalization sections of your units will be called. so use halt with care...

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top