Solved

Executing aplications from delphi programas

Posted on 1998-04-17
6
239 Views
Last Modified: 2010-04-06
How can I make a WinExecAndWait in Windows 95/NT ???
or something like this.
0
Comment
Question by:jpussacq
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:millerw
ID: 1334822
Try this.  It comes directly out of the Code Librarian in GExperts for Delphi 3.  

{ShellExecute spawns a process asynchronously.  It's as designed that your app
doesn't "wait on" the ShellExecute call for the called app to terminate.

Here's a 32-bit version of "WinExecAndWait":}

 function WinExecAndWait32(FileName:String; 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:String;
   StartupInfo:TStartupInfo;
   ProcessInfo:TProcessInformation;
 begin
   StrPCopy(zAppName,FileName);
   GetDir(0,WorkDir);
   StrPCopy(zCurDir,WorkDir);
   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
   StartupInfo.cb := Sizeof(StartupInfo);
   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
   StartupInfo.wShowWindow := Visibility;
   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,Result);
      end;
 end;

{OR}

 function WinExecAndWait32(FileName:String; 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;
   lpCommandLine:array[0..512] of char;
   zCurDir:array[0..255] of char;
   WorkDir:String;
   StartupInfo:TStartupInfo;
   ProcessInfo:TProcessInformation;
 begin
   StrPCopy(zAppName,'');
   StrPCopy(lpCommandLine,FileName);
   GetDir(0,WorkDir);
   StrPCopy(zCurDir,WorkDir);
   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
   StartupInfo.cb := Sizeof(StartupInfo);

   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
   StartupInfo.wShowWindow := Visibility;
   if not CreateProcess(
     nil,                           { pointer to command line string }
     lpCommandLine,
     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 Result := -1 { pointer to PROCESS_INF }
   else begin
     WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
     GetExitCodeProcess(ProcessInfo.hProcess,Result);
   end;
end;
-----------------------------------------
Scott
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1334823
For these purposes I use an excellant freeware component :

The TExecFile is a non-visual component which you place onto your form to enable easily initiated execution of other Windows or DOS applications from within your own original Delphi application.  This is the third release of the TExecFile and is designed for use with Delphi 2.0 in 32-bit applications designed to run in Windows 95/NT or Win32s environments.

TExecfile from Kevin Savko.

I can send it to you by mail, or you can find it in Torry's or Super delphi pages...

Regards, Zif.

0
 

Author Comment

by:jpussacq
ID: 1334824
millerw:

Tank you, WinExecAndWait32 solve my problem.-
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:millerw
ID: 1334825
Want me to post it an an answer?
0
 

Author Comment

by:jpussacq
ID: 1334826
Yes, millerw.-
0
 
LVL 1

Accepted Solution

by:
millerw earned 40 total points
ID: 1334827
See comment below.

Scott
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now