Solved

DELPHI-MSDOS

Posted on 2001-06-11
9
411 Views
Last Modified: 2010-04-06
HOW CAN I CALL A MSDOS PROGRAM FROM DELPHI, FOR EXAMPLE I WANT TO EXECUTE TRACERT FROM DELPHI,
HOW CAN I DO IT?????
0
Comment
Question by:Iamme
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6177341
Hi Iamme,
somesthing like this
ShellExecute(handle,'open','command','/c c:\tr\tracert.exe ',nil,SW_SHOW);
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6177344
Hi Iamme,
do not forget include ShellApi unit in uses clause.
0
 

Author Comment

by:Iamme
ID: 6177354
tnks.......I'll try it................wait me, and I have you an answer..if you have more examples please send it to me
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6177377
Hello

 try this
WinExec('command.com /c tracert 172.16.3.39',sw_ShowNormal);
0
 

Accepted Solution

by:
djadja earned 100 total points
ID: 6180702
Naughty, naughty - hard coding the command processor! It's really not needed...
Also WinExec is a backwards compatibility function that shouldn't be used according to the help...

However, the following cut down should work fine:

uses ShellAPI;

ShellExecute(Application.Handle,PChar('open'),PChar('c:\tr\tracert.exe'),nil,nil,SW_SHOWNORMAL);
0
 
LVL 17

Expert Comment

by:geobul
ID: 6180893
djadja: the same applies to your answer. PChar is not necessary here because Delphi does it for you. Also, when the app is an exe file, specifying 'open', 'print' or nothing does the same - executes the app.

Here is more complex but flexible way which allows you to wait for the completion of the new app.

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

Usage:
ExecApplication('','c:\tr\tracert.exe',SW_SHOWNORMAL,false);

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 6180946
I would like to mention that it is always better to post comments only because:
- proposing an answer locks the question and removes it from the queue;
- the question becomes out of sight for other experts and the questioner cannot receive different ideas from other people - he has that right, doesn't he?;
- every one problem always has more than one possible solution - let the questioner to decide which one best fits his needs.

Regards, Geo
0
 

Expert Comment

by:djadja
ID: 6181282
>> the same applies to your answer
What do you mean? - I haven't used WinExec or command.com?

Sorry about posting as an answer - I've been picked up on that on another post!

Yep - Delphi will convert constant strings to it's PChar equivalent. It's just I ALWAYS use PChar around the string parameters to show its a DLL function as opposed to a pascal 'local' function.

PS. I think it's missing a WaitForInputIdle...

Taken from Delphi Win32 help..."The calling thread can use the WaitForInputIdle function to wait until the new process has finished its initialization and is waiting for user input with no input pending. This can be useful for synchronization between parent and child processes, because CreateProcess returns without waiting for the new process to finish its initialization. For example, the creating process would use WaitForInputIdle before trying to find a window associated with the new process."
0
 
LVL 17

Expert Comment

by:geobul
ID: 6181530
I mean that every one piece of code can be rewritten another way and it's not a reason to post an answer. As far as you didn't want to, I'm sorry too, forget it.

About your PS: CreateProcess returns the handle of the newly created process which is enough for waiting for its end. I'm not going to find its window handle or to communicate with it. The calling app becomes frozen and inaccesible until the called process is working. This is when the last parameter is 'true' only.

Regards, Geo
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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