?
Solved

console app - shellexecute and wait

Posted on 2003-02-24
4
Medium Priority
?
4,899 Views
Last Modified: 2009-07-15
I use shellexecute to start a .bat file with a parameter.
But I want the app to wait for the .bat file to closed.
How do I do this?

I'm a total newbie to Delphi...
Thanks!
0
Comment
Question by:Balder1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Accepted Solution

by:
CodeBuilders earned 120 total points
ID: 8013931
The following runs a program unitl the task exits, which should run your .bat file with a parameter until it decides it's ready to close.

Give this a try. (Should be a copy and paste into Delphi)
----------------------------

function WinExecAndWait (FileName : PChar; CommandLine : PChar; Visibility : integer) : integer;
{Returns -1 if the Exec failed, otherwise returns the process' exit code when the process terminates.}
var
    zAppName : array[0..512] of char;
    zCurDir : array[0..255] of char;
    WorkDir : ShortString;
    StartupInfo : TStartupInfo;
    ProcessInfo : TProcessInformation;
    Temp : DWORD;
begin
    StrCopy (zAppName, FileName);
    StrCat (zAppName, CommandLine);
    GetDir (0, WorkDir);
    StrPCopy (zCurDir, WorkDir);
    FillChar (StartupInfo, Sizeof (StartupInfo),#0);
    with StartupInfo do begin
        cb := Sizeof (StartupInfo);
        dwFlags := STARTF_USESHOWWINDOW;
        wShowWindow := Visibility;
    end;
    if not CreateProcess (nil,
zAppName, { 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 }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo)
then { pointer to PROCESS_INF }
    Result := -1
else
begin
    WaitforSingleObject (ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess (ProcessInfo.hProcess, Temp);
    Result := integer (Temp);
end;
end;

----------------------------

This is how you would run the function:
----------------------------
WinExecAndWait(Pchar(FileString),Pchar(commandstring),SW_HIDE);
----------------------------

You can also call it like this: (make sure you have the Dialogs unit in your uses clause to do it this way).
----------------------------
Var
    Ret: Integer;
Begin
    Ret:=WinExecAndWait(Pchar(FileString),Pchar(commandstring),SW_HIDE);
    If Ret=-1 Then ShowMessage('Error');
End;
----------------------------

Have fun.
0
 
LVL 2

Expert Comment

by:BorlandMan
ID: 8013934

Use CreateProcess() This is in the Windows API department, at least I would classify it that way.

I happen to be somewhat familiar with Windows apis, so I wrote a function, which makes this a little easier.

Here it is, just paste that into a unit, declare the prototype in the interface section.

hope this helps,
J

----

{////////////////////////////////////////////////////
  CreateProcessAndWait ()

  Purpose:
     This CreateProcess waits nicely and lets
     your application 'breathe' so that repaints
     occur, yet still shuts down the application

  Example Usage:
     CreateProcessAndWait ('C:\Winnt\NotePad.exe')


  Last Update: John Scalco, June 18, 2001, slight mod.

///////////////////////////////////////////////////////}

procedure CreateProcessAndWait (AAppName: string);
var
  pi : TProcessInformation;
  si : TStartupInfo;
  lRetVal : Longint;
begin
  FillChar (si, SizeOf(si), 0);
  si.cb := SizeOf(si);

  // Standard Issue Windows API call.
  CreateProcess(PChar(AAppName), nil, nil, nil, False,
                   NORMAL_PRIORITY_CLASS,nil, nil, si, pi);

  // this allows the application to breathe
  // and still wait until
  // the other application is shut down
   Repeat
     lRetVal := WaitForSingleObject (pi.hProcess, 0);
     Application.ProcessMessages;
   until lRetVal <> 258;

   CloseHandle (pi.hProcess);
end;
0
 
LVL 2

Author Comment

by:Balder1978
ID: 8014079
Thanks alot guys!

I copying and pastethe first example, but I really dont know Delphi at all... :(
So could you please implemetns my the code I already have for me? Here is the code I have:

program MBSWSetup;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Registry,
  ShellAPI,
  Windows;

function GetEnvVar(const EnvVar: string): string;
var
BytesNeeded: DWORD;
begin

BytesNeeded := GetEnvironmentVariable(PChar(EnvVar), nil, 0);
if BytesNeeded > 0 then
begin
  SetLength(Result, BytesNeeded - 1);
  GetEnvironmentVariable(PChar(EnvVar), PChar(Result), BytesNeeded);
end
else
  Result := '';
end;

var x:TRegistry;
var Res : string;
begin
x := TRegistry.Create;
x.RootKey := HKEY_LOCAL_MACHINE;
x.OpenKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion\',false);
Res := copy(x.ReadString('SourcePath'),1,2);
SetEnvironmentVariable('INSTDRV',pchar(res));
shellexecute(0,'open',pchar(Res + '\' + paramstr(1)),pchar(Res),nil,1);
end.


Thanks again!
0
 
LVL 2

Author Comment

by:Balder1978
ID: 8020929
Thanks, I got it working now. :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question