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

output of executiuon of a dos command using WinExec?

Hello,
i try to read and analyse the output or the result of execution a dos coomand like ping or arp.
for exmaple when executing arp command:

WinExec('arp -a',sw_shownormal);

the output of this command is:
D:\Documents and Settings\Administrator>arp -a

Interface: 192.168.1.50 --- 0x2
  Internet Address      Physical Address      Type
  192.168.100.7         00-01-2e-08-3c-ee     dynamic
  192.168.100.12        00-e0-4c-ca-01-75     dynamic
  192.168.100.21        00-02-44-87-65-cf     dynamic
  192.168.100.35        00-a1-b0-a3-99-2b     dynamic
  192.168.100.63        00-e0-4d-44-ba-bf     dynamic
  192.168.100.67        00-a1-b0-a2-5f-4a     dynamic
  192.168.100.74        00-03-47-f2-3e-7e     dynamic
  192.168.100.81        00-e0-4c-e4-17-94     dynamic
  192.168.100.100       00-e0-4d-00-9b-59     dynamic
  192.168.100.122       00-e0-4c-a2-5f-45     dynamic
  192.168.100.147       00-30-1b-0e-49-e2     dynamic
  192.168.100.160       00-00-e8-15-36-45     dynamic
  192.168.100.162       00-a1-b0-01-0a-2d     dynamic
  192.168.100.177       00-01-2e-0a-62-24     dynamic
  192.168.100.178       20-04-12-30-31-60     dynamic
  192.168.100.199       00-00-e8-10-25-41     dynamic
  192.168.100.216       00-07-95-e3-df-8f     dynamic
  192.168.100.248       00-02-44-6c-c5-35     dynamic
  192.168.100.251       00-13-20-2a-64-d0     dynamic
  192.168.200.2         00-0e-2e-0d-a9-ad     dynamic


using dos i remember we can store a result in a text file like this:
arp -a > c:\result.txt



can't i store and deal with the output of this command directly in delphi?, is there away or function help me to store and deal with the output?

thanks alot in advance.
0
moayyad
Asked:
moayyad
3 Solutions
 
tammozCommented:
download the jedi project components delphi-jedi.org they have a component that solves all the problems of getting the results of dos commands on a memo, I can not remember the component's name but you can find it in the mega demo (the global demo of jedi components)
go to
http://sourceforge.net/project/showfiles.php?group_id=45786

best regardes
0
 
moayyadAuthor Commented:
yes it seems what i need :D.

but i'm afraid that im unable to understand how to use the it because i'm a bigginner in delphi.

ok, i'll copy and paste this code, then how can i use? .. for example for WinExec:
WinExec('arp -a', sw_shownormal) .

what is the correspendence for ur new function.?
0
Technology Partners: 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!

 
mokuleCommented:
Pust place edit box (Edit1) on a form button and memo (Memo1).
Place specified function as OnClick event of button.
Start program.
Enter command in edit, and click button
0
 
robert_marquardtCommented:
tammoz, the name is TJvCreateProcess.
0
 
Scay7Commented:
Rather use shellexecute

Place a button on the form
place a memo on the form
double click the button1.

And your entire code window should look something like this (starting with "unit Unit1;" -- ending with "end.")...

// start of pas file... ***
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, shellapi;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure ExecuteShellCommand(cmdline: string; hidden: Boolean);
const
flags: array[Boolean] of Integer = (SW_SHOWNORMAL, SW_HIDE);
var
cmdbuffer: array[0..MAX_PATH] of Char;
begin
GetEnvironmentVariable('COMSPEC', cmdBUffer, SizeOf(cmdBuffer));
  if ShellExecute(0,
                  'open',
                  cmdbuffer,
                  PChar('/c' + cmdline),
                  nil,
                  flags[hidden]) <= 32 then
                  Application.MessageBox('Couldn''t execute the ARP','Error',
                                          MB_ICONEXCLAMATION);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Clear;
ExecuteShellCommand('arp -a > c:\tempdat.txt', True);
1:
memo1.Lines.Add('Waiting for process to finish...');
sleep(1000);

if fileexists('c:\tempdat.txt') then
memo1.Lines.LoadFromFile('c:\tempdat.txt') else goto 1;

deletefile('c:\tempdat.txt');
end;

end.

// end of pas file...


{
Here is some nice information about the shell execute command...

ShellExecute(Handle, Operation, FileName, Params, Folder, ShowCmd)

    * Handle (HWND) is the window handle of the parent window, for instance the window handle of the main form of our application.
    * Operation (PChar) is a pointer to a null terminated string that contains the name of the operation to perform, which can be 'edit' (launches an editor and opens the document for editing), 'explore' (explores the specified directory), 'find' (opens the find window starting from the specified directory), 'open' (executes or opens the file with the associated application), 'print' (printes the specified file) or 'properties' (displays the file or folder's properties). This parameter can also be nil and in this case 'open' will be assumed.
    * FileName (PChar) is a pointer to a null terminated string that contains the path and the name of the application to execute, the document to open or print with its associated application, or the folder to open or explore.
    * Params (PChar) is a pointer to a null terminated string that contains the parameters that will be passed to application specified in FileName. If FileName doesn't indicate an executable file but a document, then Params should be nil.
    * Folder (PChar) is a pointer to a null terminated string that contains the path that will be taken as the application's folder by default. This parameter can be nil.
    * ShowCmd (Integer) indicates the way the application specified in FileName will be shown. There are various possible values:
      SW_HIDE
      SW_RESTORE
      SW_SHOW
      SW_SHOWNA
                    SW_SHOWNORMAL
      SW_SHOWDEFAULT
      SW_MAXIMIZE
      SW_MINIMIZE
                    SW_SHOWMAXIMIZED
      SW_SHOWMINIMIZED
      SW_SHOWMINNOACTIVE
      SW_SHOWNOACTIVATE

      The documentation states that if FileName refers to a document, ShowCmd should be 0, but if you try other values you will see they work.

Return value: If ShellExecute succeeds, it returns a value of type HINST (a LongWord) with the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application. If it fails, it returns an error code from 0 to 32.
}

Peace Scay7
0
 
moayyadAuthor Commented:
thank u Peace Scay7 (F)
0

Featured Post

Technology Partners: 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