Solved

DELPHI-MSDOS

Posted on 2001-06-11
9
408 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
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6177377
Hello

 try this
WinExec('command.com /c tracert 172.16.3.39',sw_ShowNormal);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

947 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