• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

Execute Dos app from delphi

Hi all,

How do I Execute a dos application using CreateProcess?

I need some parameters too...

For example: theappname -config config.txt -m test1.
html  

The dos applcation will be in the delphi apps path so I need it pass that too so that if my delphi app is relocated I don't have to change the dos app path location.


Hope you can help

thx

0
ST3VO
Asked:
ST3VO
1 Solution
 
moorhouselondonCommented:
0
 
JohnjcesCommented:
Here are a couple functions using CreateProcess.
ShowIt will show your dos app in a window, Setting it false will hide it.
Try both and see which might work best for you.

JOhn


function ExecProcess(const CmdLine: string; ShowIt: boolean): boolean;
var
  S: TStartupInfo;
  P: TProcessInformation;
begin
  FillChar(P, SizeOf(P), #0); FillChar(S, Sizeof(S), #0);
  S.cb := Sizeof(S); S.dwFlags := STARTF_USESHOWWINDOW;
  if ShowIt then S.wShowWindow := SW_SHOWNORMAL else S.wShowWindow := SW_HIDE;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, false, CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS, nil, nil, S, P);
end;

function TForm1.ExecWaitProcess(const CmdLine: string; ShowIt: boolean): boolean;
var
  S: TStartupInfo;
  P: TProcessInformation;
  M: TMsg;
  R: DWord;
  Return: integer;
begin
  FillChar(P, SizeOf(P), #0); FillChar(S, Sizeof(S), #0);
  S.cb := Sizeof(S); S.dwFlags := STARTF_USESHOWWINDOW;
  if ShowIt then S.wShowWindow := SW_SHOWNORMAL else S.wShowWindow := SW_HIDE;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, false, CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS, nil, nil, S, P);
  if Result then begin
    //WaitforSingleObject(P.hProcess,INFINITE);
    //The following replacement better satisfies DDE requirements
    repeat
      R := MsgWaitForMultipleObjects(1, // One event to wait for
      P.hProcess, // The array of events
      false, // Wait for 1 event
      INFINITE, // Timeout value
      QS_ALLINPUT); // Any message wakes up
      if R > WAIT_OBJECT_0 then begin
        M.Message := 0;
        while PeekMessage(M, 0, 0, 0, PM_REMOVE) do begin
          TranslateMessage(M);
          DispatchMessage(M);
        end;
      end;
    until R = WAIT_OBJECT_0;
    GetExitCodeProcess(P.hProcess, DWord(Return));
    //Result := Return <> 0; //not real reliable
    CloseHandle(P.hProcess);
    CloseHandle(P.hThread);
    P.hProcess := 0;
    P.hThread := 0;
  end;
end;
0
 
ST3VOAuthor Commented:
Perfect! Thanks :o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now