dostęp do cmd z poziomu delphi

0

Witam!

Mam takie pytanie?
Jak z poziomu delphi uzyskać dostęp do windowsowego shella (cmd) ?

P0zdro

0

ale chcesz wykonywac polecenia systemowe w wlasnym programie? czy tez wywolac CMD i tam wpisywac polecenia z poziomu programu? sprecyzuj

0

chodzi mi o to zeby program był takim symulatorem cmd : czyli żeby można było w nim wpisywać polecania cmd i otrzymywać odpowiedzi :P

0

Przykład: Form1, Memo1, Edit1, Button1:

function ExecuteConsole(Executable:PChar; CommandLine:PChar; Directory:PChar):boolean;
const bufSize     = 256;
var   readPipe    :THandle;
      writePipe   :THandle;
      security    :SECURITY_ATTRIBUTES;
      info        :STARTUPINFO;
      process     :PROCESS_INFORMATION;

      buf         :array[0..bufSize-1] of char;
      bytesRead   :DWord;
      text        :string;
      line        :string;
      newLinePos  :integer;
begin
result:=FALSE;
security.nLength:=sizeof(security);
security.lpSecurityDescriptor:=nil;
security.bInheritHandle:=TRUE;
Application.ProcessMessages;
if CreatePipe(readPipe, writePipe, @security, 0) then
  begin
  ZeroMemory(@info, sizeof(info));
  with info do
    begin
    cb := sizeof( info );
    dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    wShowWindow := SW_HIDE;
    hStdInput := 0;
    hStdOutput := writePipe;
    hStdError := writePipe;
    end;
  if CreateProcess(Executable, CommandLine, nil, nil, TRUE, 0, nil, Directory, info, process) then
    begin
    CloseHandle( writePipe );
    text:='';
    while ReadFile( readPipe, buf, bufSize, bytesRead, nil) do
      begin
      text:=text+buf;
        repeat
        newLinePos:=Pos(#13, text);
        if (newLinePos=0) then break;
        line:=copy(text,1, newLinePos-1);
        delete(text, 1, newLinePos);
        Form1.Memo1.Lines.Add(line);
        Application.ProcessMessages;
        until Application.Terminated;
      ZeroMemory(@buf, bufSize);
      end;
    result:=TRUE;
    end;
  CloseHandle( readPipe );
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ExecuteConsole('c:\WINDOWS\system32\cmd.exe', PChar('/C "'+Edit1.Text+'"'), 'c:\');
Edit1.Text:='';
end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1