Hi folks,
It may look like an easy one, but it isn't.
My original question was:
Can anybody tell me how to get an application to return a (DOS) Return
Code (number) ?
E.g. when a sub-application can't find a file, it should pass on the DOS
Return Code (2) back to the main-program. But also when it fails of any
sub-application specific issue, it should return its own error code
(e.g. from number 1000 onwards).
One of the reply was
Halt(x)
According to the help on Halt() it should work, but it doesn't. I've
included the code that I use to run the EXE.
I suppose it is not 100%.
Thank for any comment.
John Christenhusz.
function ProcessExecute(sCommandLine, sParams: string; vtRunMode:
tRunMode; vtStartMode : tStartMode): integer;
{Function:
Launch application by path\name using optional parameters.
This method encapsulates the call to CreateProcess()
Calling program is suspended till:
if vtRunMode = tWaitTillFinished : till after task is completed,
if vtRunMode = tReturnAfterStart : till after initialization is
completed,
with a max of 10 seconds.
Entry parameters:
sCommandLine : command line,
sParams : list of parameters,
vtRunMode : tWaitTillFinished or tReturnAfterStart.
vtStartMode : minimised or normal
Returns:
0 if successful,
DOS error code if unsuccessful }
var
vtStartUpInfo : TStartUpInfo; // help available under StartUpInfo
vtProcessInfo : TProcessInformation; // help available under
PROCESS_INFORMATION
pCommandLine : array[0..255] of char;
pParams : array[0..255] of char;
begin
if fileExists(sCommandLine) then
begin
{ Clear the vtStartUpInfo structure }
fillChar(vtStartUpInfo, SizeOf(TStartupInfo), 0);
with vtStartUpInfo do
begin
cb := SizeOf(TStartupInfo); // Specify size of structure
dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
wShowWindow := SW_SHOWDEFAULT;
case vtStartMode of
tNormal : wShowWindow := sw_ShowNormal;
tMinimised : wShowWindow := SW_ShowMinNoActive;
end;
end;
{ Create the process by calling CreateProcess(). Detailed information
is provided in the Win32 online help for the TProcessInfo structure
under PROCESS_INFORMATION.}
strPCopy(pCommandLine, sCommandLine);
strPCopy(pParams, sCommandLine + ' ' + sParams);
application.processMessages;
if createProcess(pCommandLine,
pParams,
nil,
nil,
false,
NORMAL_PRIORITY_CLASS or NORMAL_PRIORITY_CLASS,
nil,
nil,
vtStartUpInfo,
vtProcessInfo) then
with vtProcessInfo do
begin
if vtRunMode = tWaitTillFinished then
while waitForInputIdle(hProcess, INFINITE) = 0 do
application.processMessages
else
waitForInputIdle(hProcess, 10000); // wait until initialized (max 10
seconds)
if waitForInputIdle(hProcess, INFINITE) -1 then
result := 0 // set to zero, meaning successful run
else
result := getLastError; // get error number from called EXE
closeHandle(hThread); // free the hThread handle
closeHandle(hProcess); // free the hProcess handle
end
else
result := getLastError; // Set result to the error code.
end
else
result := 2; // file not found
end;{ProcessExecute}
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz