There are not many replies to this one, so let me post my own routine, which
uses CreateProcess and finally GetExitCodeProcess.

Written years ago (and probably shows it) this successfully returns the exit
code from a command or another program.  It's always worked for me, and I
just tested it again in case my memory was playing tricks.  For example if
you execute a Delphi programme and set "ExitCode" you can see whatever you
set. 

{--Function to Execute another program and optionally wait until complete.--
 Cmd  - the program (and optionally path) plus parameters.
 Wait - whether to wait until the process is complete
 SWin - the type of window to use. The default is SW_Normal.

The return value is 0 if all completed OK.
Otherwise it is -1 if the process could not be created, and the exit code of

Note: If the program path is not specified, then Windows searches for the
 executable file in the following sequence:  (see HELP)
1. The directory from which the application loaded.
2. The current directory for the parent process.
3. Win95: The Windows system directory - use GetSystemDirectory to find the
path.
   WinNT: The 32-bit Windows system directory.   "    (Named SYSTEM32.)
5. The Windows directory. (GetWindowsDirectory gets the path of this
directory.)
6. The directories that are listed in the PATH environment variable.

 Integer constants to specify how to show the window (SWin).
  SW_HIDE = 0;       SW_NORMAL = 1;            SW_SHOWMINIMIZED = 2;
  SW_MAXIMIZE = 3;   SW_SHOWNOACTIVATE = 4;    SW_SHOW = 5;
  SW_MINIMIZE = 6;   SW_SHOWMINNOACTIVE = 7;   SW_SHOWNA = 8;
  SW_RESTORE = 9;    SW_SHOWDEFAULT = 10;                     }

function WExec(Cmd:string; Wait:Boolean=False;
SWin:Integer=SW_Normal):Integer;
var
  tSI : TStartupInfo;
  ProcessInfo: TProcessInformation;
  i : DWORD;
begin
  Result:=-1;                // Set return code, so we can just exit on
error
  FillChar(tSI, Sizeof(tSI), #0);
  tSI.cb:= Sizeof(tSI);
  tSI.dwFlags:= STARTF_USESHOWWINDOW;
  tSI.wShowWindow:= SWin;   { How to show the window. Default is
SW_ShowNormal}

  If CreateProcess
   (nil,                    { pointer to application name - can be nil }
    pchar(Cmd),             { 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 }
    tSI,                    { Const: TStartUpInfo - not a pointer! }
    ProcessInfo)            { pointer to PROCESS_INF }
  then begin            { OK - Process was created fine }
    Result:=0;          { Set result }
    if wait then begin { Caller wants to wait until process completes }
      If WaitforSingleObject(ProcessInfo.hProcess,INFINITE)=WAIT_OBJECT_0
then
        If GetExitCodeProcess(ProcessInfo.hProcess,i) then result := i;
    end;
    CloseHandle(ProcessInfo.hProcess);  { Must remove handles to prevent.. }
    CloseHandle(ProcessInfo.hThread);   {   ...memory creep                }
  end;
end; {--Function WExec--}



Hope that helps.....
                Brendan.


-----Original Message-----
From: delphi-boun...@elists.org [mailto:delphi-boun...@elists.org] On Behalf
Of John Dammeyer
Sent: 09 October 2009 08:58
To: 'Borland's Delphi Discussion List'
Subject: Captures information from a ShellExecute

Hi,

I'm launching a windows command line PIC programmer as follows:

    res := ShellExecute(Self.Handle, nil, 'pk2cmd.exe', '/PPIC18F2680
/FTricolourNode.hex /M /A5.00',
    'C:\Firmware', SW_SHOWNORMAL);

The shell executes and returns immediately as it probably should.  The
Command Line window stays open until the pktcmd.exe finishes.  Once the
pk2cmd.exe finishes it closes the window immediately.  If it's failed to
program th PIC microntroller I don't see the information before the window
closes.

If I use CreateProcess I can then hang around until the app is done but
then it's the same issue.  The Command Window closes when the app closes
and I can't seem to catch an error code from the pk2cmd.exe as outlined
below:

11. Return Codes
--------------------------------------------------------------------------
--
Value   Code                    Notes
-----   ----                    -----
0       OPSUCCESS               Returned if all selected operations
complete
                                successfully.
5       VOLTAGE_ERROR           A Vdd and/or Vpp voltage error was
detected.
                                This could be due to PICkit 2 being
                                improperly connected to a part, incorrect
                                part selection, or interference from other
                                circuitry on the target board.
...

39      AUTODETECT_FAILED       A part autodetect operation failed to find
                                a known part.

Is there a way to lauch a window and capture the return code of the
application as opposed to the success or failure of launching the command
line window?

GetLastError just returns 0.

I've tried >>results.txt with no luck to try and catch the text output.

Googling on this subject seems to always refer to the Windows Error Code
which GetLastError would return.

Thanks

John


Automation Artisans Inc.
http://www.autoartisans.com/ELS/
Ph. 1 250 544 4950


_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to