Link to home
Start Free TrialLog in
Avatar of Member_2_5194534
Member_2_5194534

asked on

Run a command via cmd

I'm trying to run cmd.exe with admin privilege and use flushdns on it, how can i do it though ? and catch the output to a TMemo, the attached code is what i'm using but it doesn't seem to flush the DNS cache, where do i put "/flushdhsn" in ?


procedure RunDosInMemo( DosApp: String; AMemo: TMemo ) ;
const
  ReadBuffer = 2400;
var
  Security : TSecurityAttributes;
  ReadPipe, WritePipe : THandle;
  si : TStartUpInfo;
  pi : TProcessInformation;
  Buffer : PAnsiChar;
  BytesRead : DWord;
  Apprunning : DWord;
begin
  With Security do
   begin
    nlength := SizeOf(TSecurityAttributes) ;
    binherithandle := true;
    lpsecuritydescriptor := nil;
   end;
  if Createpipe(ReadPipe, WritePipe, @Security, 0) then
   begin
    Buffer := AllocMem(ReadBuffer + 1);
    FillChar( si, Sizeof(si), #0 );
    si.cb := SizeOf( si );
    si.hStdOutput := WritePipe;
    si.hStdInput := ReadPipe;
    si.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
    si.wShowWindow := SW_HIDE;

    if CreateProcess( PChar(DosApp), '-flushdns', @Security, @Security,
                            TRUE, NORMAL_PRIORITY_CLASS, nil, nil, si, pi)
    then
    begin
     repeat
      Apprunning := WaitForSingleObject( pi.hProcess, 100 );
      Application.ProcessMessages;
     until (Apprunning <> WAIT_TIMEOUT);
      Repeat
        BytesRead := 0;
        ReadFile(ReadPipe, Buffer[0], ReadBuffer, BytesRead, nil);
        Buffer[BytesRead] := #0;
        OemToAnsi( Buffer, Buffer );
        AMemo.Text := AMemo.Text + String(Buffer) ;
         Application.ProcessMessages;
      until (BytesRead < ReadBuffer);
   end;
   FreeMem( Buffer ) ;
   CloseHandle( pi.hProcess ) ;
   CloseHandle( pi.hThread ) ;
   CloseHandle( ReadPipe ) ;
   CloseHandle( WritePipe ) ;
   end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial