Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

DELPHI-MSDOS

HOW CAN I CALL A MSDOS PROGRAM FROM DELPHI, FOR EXAMPLE I WANT TO EXECUTE TRACERT FROM DELPHI,
HOW CAN I DO IT?????
0
Iamme
Asked:
Iamme
  • 3
  • 2
  • 2
  • +2
1 Solution
 
ITugayCommented:
Hi Iamme,
somesthing like this
ShellExecute(handle,'open','command','/c c:\tr\tracert.exe ',nil,SW_SHOW);
0
 
ITugayCommented:
Hi Iamme,
do not forget include ShellApi unit in uses clause.
0
 
IammeAuthor Commented:
tnks.......I'll try it................wait me, and I have you an answer..if you have more examples please send it to me
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mohammed NasmanSoftware DeveloperCommented:
Hello

 try this
WinExec('command.com /c tracert 172.16.3.39',sw_ShowNormal);
0
 
djadjaCommented:
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
 
geobulCommented:
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
 
geobulCommented:
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
 
djadjaCommented:
>> 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
 
geobulCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now