Thanks James,

Exactly what I needed.

Steve

-----Original Message-----
From: James Sugrue [mailto:[EMAIL PROTECTED]]
Sent: Thursday, 13 September 2001 11:06
To: Multiple recipients of list delphi
Subject: RE: [DUG]: Converting VB to Delphi - CreateProcess


Heres a function that we use. It does as you suggest. You should be able to
get some pointers (no pun intended)

function DOSExec( const ProgName   : String;
                 const Wait       : Boolean;
                 var Msg          : String ) : Boolean;
{ Executes external DOS program with Params. }
{ Visibility is
{ Returns Msg and True/False depending on Success }
{ NOTE - ProgName must include PATH }
var
   CmdLine     : String;
   StartupInfo : TStartupInfo;
   ProcessInfo : TProcessInformation;
   ResultWait  : Integer;
   ErrorCode   : Integer;
   Rslt        : LONGWORD;
begin
   Result := False;
   Msg := '';
   CmdLine := ProgName;
   FillChar( StartupInfo, sizeof( StartupInfo ), 0 );
   with StartupInfo do begin
      cb := sizeof( StartupInfo );
      dwFlags := STARTF_USESHOWWINDOW;
      wShowWindow := SW_SHOWNORMAL;
   end;
   Result := CreateProcess( Nil, PChar( CmdLine ),Nil, Nil, False,
                            NORMAL_PRIORITY_CLASS, Nil, Nil,
                            StartupInfo, ProcessInfo );
   If not Result then begin
      ErrorCode := GetLastError;
      Msg := Format( 'Create Process Error %d', [ ErrorCode ] );
      Exit;
   end;
   Application.ProcessMessages;
   if Wait then begin
      Repeat
         // Wait for External Process but give App Repaint time etc every
100ms
         ResultWait := WaitforSingleObject(ProcessInfo.hProcess,100);
         Application.ProcessMessages;
         GetExitCodeProcess(ProcessInfo.hProcess, Rslt);
      Until not ( Rslt = STILL_ACTIVE );
   end;
   CloseHandle( ProcessInfo.hProcess );
   CloseHandle( ProcessInfo.hThread );
end;

-----Original Message-----
From: Steve Aish [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, 13 September 2001 10:58
To: Multiple recipients of list delphi
Subject: [DUG]: Converting VB to Delphi - CreateProcess


Good morning everyone,

I have a little program which I have run with VB from years back that opens
a Shell and runs a program and waits for it to finish before passing control
back to the calling program.  I have been trying to convert this code to
Delphi without success.  I am sure it is not rocket science but I have a
little difficulty making the leap to the concept of pointers...

Anyway here is the code.  I was wondering if anyone could give me a couple
of ideas on how to convert this.

All the Type declarations are already in Windows.pas.

Public Sub Shell(PathName As String, Optional WindowStyle As Long)
    
    Dim proc As PROCESS_INFORMATION
    Dim Start As STARTUPINFO
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    With Start
        .cb = Len(Start)
        If Not IsMissing(WindowStyle) Then
            .dwFlags = STARTF_USESHOWWINDOW
            .wShowWindow = WindowStyle
        End If
    End With
    ' Start the shelled application:
    ret& = CreateProcessA(0&, PathName, 0&, 0&, 1&, _
            HIGH_PRIORITY_CLASS, 0&, 0&, Start, proc)
    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
End Sub

I have this so far but it gives me 'Winoldap - This program has performed an
illegal operation...'

I am guessing it is something to do with pointers or some such.

procedure RunShell(FileName : string; WindowStyle : Integer); var
  proc  : PROCESS_INFORMATION;
  Start : STARTUPINFO;
begin
  // Initialize the STARTUPINFO structure:
  with Start do begin
    cb := SizeOf(Start);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowStyle;
  end;

  // Start the shelled application:
  CreateProcessA(nil, PChar(FileName), nil, nil, True, HIGH_PRIORITY_CLASS,
nil, nil, Start, proc);
  // Wait for the shelled application to finish:
  WaitForSingleObject(proc.hProcess, INFINITE);
  CloseHandle(proc.hProcess);

end;

Thanks in advance

Steve

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to