Προς το περιεχόμενο

Εμφανιση αποτελεσματων μιας εντολης DOS σε παραθυρο windows


placid

Προτεινόμενες αναρτήσεις

<BLOCKQUOTE><font size="1" face="Verdana, Helvetica, sans-serif">code:</font><HR><pre><P>procedure ExecConsoleApp(CommandLine: AnsiString; Output: TStringList; Errors:<BR> TStringList);<BR>var<BR> sa : TSECURITYATTRIBUTES;<BR> si : TSTARTUPINFO;<BR> pi : TPROCESSINFORMATION;<BR> hPipeOutputRead : THANDLE;<BR> hPipeOutputWrite : THANDLE;<BR> hPipeErrorsRead : THANDLE;<BR> hPipeErrorsWrite : THANDLE;<BR> Res, bTest : Boolean;<BR> env : array[0..100] of Char;<BR> szBuffer : array[0..256] of Char;<BR> dwNumberOfBytesRead: DWORD;<BR> Stream : TMemoryStream;<BR>begin<BR> sa.nLength := sizeof(sa);<BR> sa.bInheritHandle := true;<BR> sa.lpSecurityDescriptor := nil;<BR> CreatePipe(hPipeOutputRead, hPipeOutputWrite, @sa, 0);<BR> CreatePipe(hPipeErrorsRead, hPipeErrorsWrite, @sa, 0);<BR> ZeroMemory(@env, SizeOf(env));<BR> ZeroMemory(@si, SizeOf(si));<BR> ZeroMemory(@pi, SizeOf(pi));<BR> si.cb := SizeOf(si);<BR> si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;<BR> si.wShowWindow := SW_HIDE;<BR> si.hStdInput := 0;<BR> si.hStdOutput := hPipeOutputWrite;<BR> si.hStdError := hPipeErrorsWrite;<P> (* Remember that if you want to execute an app with no parameters you nil the<BR> second parameter and use the first, you can also leave it as is with no<BR> problems. *)<BR> Res := CreateProcess(nil, pchar(CommandLine), nil, nil, true,<BR> CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, @env, nil, si, pi);<P> // Procedure will exit if CreateProcess fail<BR> if not Res then<BR> begin<BR> CloseHandle(hPipeOutputRead);<BR> CloseHandle(hPipeOutputWrite);<BR> CloseHandle(hPipeErrorsRead);<BR> CloseHandle(hPipeErrorsWrite);<BR> Exit;<BR> end;<BR> CloseHandle(hPipeOutputWrite);<BR> CloseHandle(hPipeErrorsWrite);<P> //Read output pipe<BR> Stream := TMemoryStream.Create;<BR> try<BR> while true do<BR> begin<BR> bTest := ReadFile(hPipeOutputRead, szBuffer, 256, dwNumberOfBytesRead, nil);<BR> if not bTest then<BR> begin<BR> break;<BR> end;<BR> Stream.Write(szBuffer, dwNumberOfBytesRead);<BR> end;<BR> Stream.Position := 0;<BR> Output.LoadFromStream(Stream);<BR> finally<BR> Stream.Free;<BR> end;<P> //Read error pipe<BR> Stream := TMemoryStream.Create;<BR> try<BR> while true do<BR> begin<BR> bTest := ReadFile(hPipeErrorsRead, szBuffer, 256, dwNumberOfBytesRead, nil);<BR> if not bTest then<BR> begin<BR> break;<BR> end;<BR> Stream.Write(szBuffer, dwNumberOfBytesRead);<BR> end;<BR> Stream.Position := 0;<BR> Errors.LoadFromStream(Stream);<BR> finally<BR> Stream.Free;<BR> end;<P> WaitForSingleObject(pi.hProcess, INFINITE);<BR> CloseHandle(pi.hProcess);<BR> CloseHandle(hPipeOutputRead);<BR> CloseHandle(hPipeErrorsRead);<BR>end;<P>procedure TForm1.Button1Click(Sender: TObject);<BR>var OutP : TStringList;<BR> ErrorP: TStringList;<BR>begin<BR> OutP := TStringList.Create;<BR> ErrorP := TstringList.Create;<P> ExecConsoleApp('ping localhost', OutP, ErrorP);<BR> Memo1.Lines.Assign(OutP);<P> OutP.Free;<BR> ErrorP.Free;<BR>end;<P><BR></pre><HR></BLOCKQUOTE><P>ή me liga logia koita gia CreateProcess API kai pipes.<P>ps: pipe einai kai i dir > 1.txt ktl

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...