سلام
مي خوام بدونم چطور ميتونم دستورات command prompt مثل dir و ver و at و... رو در دلفي اجرا كنم
با winexec و shelexecute نشد شايدم من نتونستم
دوستان راهنماييم كنيد
Printable View
سلام
مي خوام بدونم چطور ميتونم دستورات command prompt مثل dir و ver و at و... رو در دلفي اجرا كنم
با winexec و shelexecute نشد شايدم من نتونستم
دوستان راهنماييم كنيد
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));
StrCat(cmdbuffer, ' /C ');
StrPCopy(StrEnd(cmdbuffer), cmdline);
WinExec(cmdbuffer, flags[hidden]);
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
ExecuteShellCommand('dir C:\ > c:\temp\dirlist.txt', True);
end;
آیا میشه خروجی رو هم در دلفی دید؟
مثلا خروجی ِ ping رو
بصورت زير استفاده كنيد پنجره ايي باز شده و به شما نمايش ميدهد :
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));
StrCat(cmdbuffer, ' /C ');
StrPCopy(StrEnd(cmdbuffer), cmdline);
WinExec(cmdbuffer, flags[hidden]);
end;
procedure TForm6.Button1Click(Sender: TObject);
begin
ExecuteShellCommand('Ping 192.168.1.1', false) ;
end;
راه دیگه ای هم برای دیدن نتایج حاصل وجود داره که در برنامه بتونید خروجی رو مشاهده کنید
در همین سایت قبلا گفته شده
خروجی حاصل برنامه داس
این لینکو ببین (نمونه استفاده از CreatePipe تو این راستا)
http://www.delphipages.com/threads/thread.cfm?ID=174356&G=174346
من هنوز مشكل دارم اكثر دستورا رو نميتونم اجرا كنم و اگرم بشن خروجيش ديده نميشه
چيكار بايد بكنم؟؟؟
حق با شماست، درست اجرا نشد و من هم وقت اصلاحش رو نداشتم. این رو من خودم استفاده کردم:
procedure CaptureConsoleOutput(DosApp : string;AMemo : TMemo);
const
ReadBuffer = 1048576; // 1 MB Buffer
var
Security : TSecurityAttributes;
ReadPipe,WritePipe : THandle;
start : TStartUpInfo;
ProcessInfo : TProcessInformation;
Buffer : Pchar;
TotalBytesRead,
BytesRead : DWORD;
Apprunning,n,
BytesLeftThisMessage,
TotalBytesAvail : integer;
begin
with Security do
begin
nlength := SizeOf(TSecurityAttributes);
binherithandle := true;
lpsecuritydescriptor := nil;
end;
if CreatePipe (ReadPipe, WritePipe, @Security, 0) then
begin
// Redirect In- and Output through STARTUPINFO structure
Buffer := AllocMem(ReadBuffer + 1);
FillChar(Start,Sizeof(Start),#0);
start.cb := SizeOf(start);
start.hStdOutput := WritePipe;
start.hStdInput := ReadPipe;
start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
start.wShowWindow := SW_HIDE;
// Create a Console Child Process with redirected input and output
if CreateProcess(nil ,PChar(DosApp),
@Security,@Security,
true ,CREATE_NO_WINDOW or NORMAL_PRIORITY_CLASS,
nil ,nil,
start ,ProcessInfo) then
begin
n:=0;
TotalBytesRead:=0;
repeat
// Increase counter to prevent an endless loop if the process is dead
Inc(n,1);
// wait for end of child process
Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
Application.ProcessMessages;
// it is important to read from time to time the output information
// so that the pipe is not blocked by an overflow. New information
// can be written from the console app to the pipe only if there is
// enough buffer space.
if not PeekNamedPipe(ReadPipe ,@Buffer[TotalBytesRead],
ReadBuffer ,@BytesRead,
@TotalBytesAvail,@BytesLeftThisMessage) then break
else if BytesRead > 0 then
ReadFile(ReadPipe,Buffer[TotalBytesRead],BytesRead,BytesRead,nil);
TotalBytesRead:=TotalBytesRead+BytesRead;
until (Apprunning <> WAIT_TIMEOUT) or (n > 150);
Buffer[TotalBytesRead]:= #0;
OemToChar(Buffer,Buffer);
AMemo.Text := AMemo.text + StrPas(Buffer);
end;
FreeMem(Buffer);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
CloseHandle(WritePipe);
end;
end;
برای مثال اجرا کن
CaptureConsoleOutput('cmd.exe /c dir', Memo1);
میتونی خیلی راحت تغییرش بدی که تو هر چیزی بنویسه...