?
Solved

Problem with our code?

Posted on 2005-04-21
3
Medium Priority
?
242 Views
Last Modified: 2010-04-05
Hello all
We have a piece of code that allows our application to run an executable, finish it then run another. If we step through the code it works but when compiled and run it runs both exe's at once. Any ideas?
Code is

var
        test: string;
        filename, param, defaultdir: string;
begin

       
        filename := 'Purchase orderDTS.exe';
        param := '';
        defaultdir := 'C:\Program Files\Purchase order package\';
       
        shellexecute(handle, 'open', PChar(filename), PChar(param), PChar(defaultdir), SW_NORMAL);

        if iswinopen('C:\Program Files\Purchase order package\Purchase orderDTS.exe') then
        begin
               
                filename := 'sales order DTS.exe';
                defaultdir := 'C:\Program FilesSales order package\';
                shellexecute(handle, 'open', PChar(filename), PChar(param), PChar(defaultdir), SW_NORMAL);
                      end

end;




function TMainForm.iswinopen(win: string): boolean;
var
window:HWND;
begin
                window := findwindow (nil, 'C:\Program Files\Purchase order package\Purchase orderDTS.exe');
            while window <> 0 do
            begin
            window := findwindow (nil, 'C:\Program Files\Purchase order package\Purchase orderDTS.exe');
            end;
            iswinopen := true;
end;

Best regards
0
Comment
Question by:Paul-Brooks
  • 2
3 Comments
 
LVL 6

Assisted Solution

by:vadim_ti
vadim_ti earned 400 total points
ID: 13834315
I think better solution will be to use ShellExecuteEx function,
to get process handle and wait for process termination.

something like this

var
  infoblk: TShellExecuteInfo;
begin
 infoblk.cbSize := sizeof(TShellExecuteInfo);
// fill relevanted fields of the structure
if  ShellExecuteEx(@infoblk) then
  WaitForSingleObject(infoblk.hPorcess);
end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 13834473
Hi,

I'm using the function below:

//executes as well WIN and DOS application
function ExecApplication(APPName, CmdLine, CurrentDir: String; ShowMode: DWord; WaitToExit: Boolean): DWord;
  var StartInfo: TStartupInfo;
      ProcInfo: TProcessInformation;
  begin
    FillChar(StartInfo, SizeOf(StartInfo), 0);
    StartInfo.cb:=SizeOf(StartInfo);
    StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
    StartInfo.wShowWindow:=ShowMode;
    if AppName<>''
      then CreateProcess(PChar(APPName), PChar(CmdLine), nil, nil, False, 0,
                         nil, PChar(CurrentDir), StartInfo, ProcInfo)
      else CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0,
                         nil, PChar(CurrentDir), StartInfo, ProcInfo);
    if WaitToExit
      then WaitForSingleObject(ProcInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcInfo.hProcess, Result);
    CloseHandle(ProcInfo.hProcess);
    CloseHandle(ProcInfo.hThread);
  end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ExecApplication('c:\windows\notepad.exe', '', 'c:\temp', SW_SHOWNORMAL, true);
end;

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 1600 total points
ID: 13834526
The problem with your code is that immediately after ShellExecute call there is no window created yet. You need to give it some time before the new process creates its main window and you call iswinopen function successfully. iswindow called after ShellExecute returns without finding the window of interest because it hasn't been created yet.

Regards, Geo
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

862 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