Solved

Run a command via cmd

Posted on 2010-11-29
1
1,220 Views
Last Modified: 2016-09-29
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

0
Comment
Question by:rotem156
1 Comment
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34232195
if you are flushing the dns then should be

RunDosInMemo( 'cmd.exe /c ipconfig /flushdns', Memo1 );
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

Title # Comments Views Activity
T-SQL Debugging - Temp Object Content 2 52
Intraweb download file link ? 1 105
oracle global variables 4 63
Delphi...Split view - idea? 1 39
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

22 Experts available now in Live!

Get 1:1 Help Now