有没有能够得到dos窗口运行的内容的api? 是这样的,dos窗口显示服务器运行的各种命令,以及执行的sql,希望能够当dos窗口运行某种命令是,将这个窗口关闭。如何得到这个dos窗口里面的内容(这个内容是不断变化的)? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://search.csdn.net/Expert/topic/466/466796.xml?temp=.9791681 用管道的例子:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TPipeForm = class(TForm) Editor: TMemo; procedure EditorKeyPress(Sender: TObject; var Key: Char); procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); procedure EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure EditorMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } CreateOk: Boolean; WPos: TPoint; hReadPipe, hWritePipe, hWriteFile, hReadFile: THandle; processinfo: PROCESS_INFORMATION; procedure SendCmdToShell(Const CmdStr: String); function GetCmdStr: string; public { Public declarations } end;var PipeForm: TPipeForm;implementation{$R *.dfm}procedure TPipeForm.EditorKeyPress(Sender: TObject; var Key: Char);var ECharPos: TPoint;begin if Key = Chr(VK_RETURN) then begin// ShowMessage(GetCmdStr); SendCmdToShell(GetCmdStr); end else if Key = Chr(VK_BACK) then begin ECharPos := Editor.CaretPos; if ECharPos.X = WPos.X + 1 then Key := #0; end;end;procedure TPipeForm.SendCmdToShell(Const CmdStr: String);var ShellCmdStr: array[0..256] of char; RBuffer: array[0..25000] of char; nByteToWrite: DWORD; nByteWritten: DWORD; nByteReaden: DWORD;begin if CreateOK then begin StrPCopy(ShellCmdStr, CmdStr); nByteToWrite := StrLen(ShellCmdStr); ShellCmdStr[nByteToWrite] := #13; ShellCmdStr[nByteToWrite+1] := #10; ShellCmdStr[nByteToWrite+2] := #0; Inc(nByteToWrite, 2); WriteFile(hWriteFile, ShellCmdStr, nByteToWrite, nByteWritten, nil); Sleep(400); Editor.Lines.Clear; FillChar(RBuffer, Sizeof(RBuffer), #0); ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil); Editor.Lines.Add(StrPas(RBuffer)); WPos.Y := Editor.Lines.Count-1; WPos.X := Length(Editor.Lines[WPos.Y])-1; end;end;procedure TPipeForm.FormDestroy(Sender: TObject);var shellexitcode: Cardinal;begin if GetExitCodeProcess(processinfo.hProcess, shellexitcode) then begin if shellexitcode = STILL_ACTIVE then TerminateProcess(processinfo.hProcess, 0); end; if hWriteFile <> 0 then CloseHandle(hWriteFile); if hReadFile <> 0 then CloseHandle(hReadFile);end;procedure TPipeForm.FormCreate(Sender: TObject);var Pipeattr: SECURITY_ATTRIBUTES; ShellStartInfo: STARTUPINFO; shellstr: array [0..256] of char; RBuffer: array[0..25000] of char; I: Integer; nByteReaden: DWORD;begin CreateOK := False; I := 0; Editor.ReadOnly := False; Wpos.X := 0; WPos.Y := 0; with Pipeattr do begin nLength := Sizeof(SECURITY_ATTRIBUTES); lpSecurityDescriptor := nil; bInheritHandle := true; end; if CreatePipe(hReadPipe, hWriteFile, @Pipeattr, 0) then Inc(i); if CreatePipe(hReadFile, hWritePipe, @pipeattr, 0) then Inc(i); GetStartupInfo(ShellStartInfo); with ShellStartInfo do begin dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; hStdInput := hReadPipe; hStdError := hWritePipe; hStdOutput := hWritePipe; wShowWindow := SW_HIDE; end; GetSystemDirectory(@Shellstr, MAX_PATH+1); StrCat(@ShellStr, Pchar('\\cmd.exe')); if CreateProcess(Shellstr, nil, nil, nil, True, 0, nil, nil, ShellStartInfo, processinfo) then begin Inc(i); end else begin MessageBox(Handle, Pchar('调用Shell错误!'), Pchar('错误'), (MB_OK or MB_ICONERROR)); end; if i = 3 then begin CreateOK := True; Editor.Lines.Clear; sleep(250); ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil); Editor.Lines.Add(StrPas(RBuffer)); WPos.Y := Editor.Lines.Count-1; WPos.X := Length(Editor.Lines[WPos.Y])-1; end;end;procedure TPipeForm.EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);var ECharPos: TPoint;begin ECharPos := Editor.CaretPos; if ECharPos.Y > WPos.Y then Editor.ReadOnly := False else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then begin Editor.ReadOnly := False; end else Editor.ReadOnly := True;end;function TPipeForm.GetCmdStr: string;var LastLine: Integer;begin LastLine := Editor.Lines.Count - 1; if LastLine > WPos.Y then begin result := Editor.Lines[LastLine]; end else if LastLine = WPos.Y then begin result := Editor.Lines[LastLine]; result := Copy(result, WPos.X+2, Length(result)); end else begin result := ' '; end;end;procedure TPipeForm.EditorMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);var ECharPos: TPoint;begin ECharPos := Editor.CaretPos; if ECharPos.Y > WPos.Y then Editor.ReadOnly := False else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then Editor.ReadOnly := False else Editor.ReadOnly := True;end;end. 其实你也可以参考2ccc.com上一个关于UPX的例子,那个就正是你需要的!!:) http://borland.mblogger.cn/lw549/posts/5220.aspx CreatePipe的API是关键Google上N多答案的例如http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=53886 很好用,但我把 cmd.exe 换成 telnet.exe 程序就在ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);卡住了,我是个初学者,能给点帮助吗? 有人说像希特勒,有人说像雪村.... 用透明窗体及透明Label做时间显示 百分之百送分:一台PC 对多个IP端的监控,请求大家给个可行的方案~ 怎样用SQL语句对同一字段查询多个值? 大家给小弟推荐两本关于delphi+sql2000的书籍吧 怎样实现在delphi开发程序中实现背景音乐?着急!!!!! 关于EditConnectionString? 大散分,有研究生免试直升面试经验的朋友请进,给你们送分!!(up者有分) 对ComboBox使用api函数GetComboBoxInfo无效 cxGrid只能选一项等相关问题 关于字符串和流的加密 请各位给我看一下,程序中哪里错误了,很郁闷
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TPipeForm = class(TForm)
Editor: TMemo;
procedure EditorKeyPress(Sender: TObject; var Key: Char);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure EditorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure EditorMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
CreateOk: Boolean;
WPos: TPoint;
hReadPipe, hWritePipe, hWriteFile, hReadFile: THandle;
processinfo: PROCESS_INFORMATION;
procedure SendCmdToShell(Const CmdStr: String);
function GetCmdStr: string;
public
{ Public declarations }
end;var
PipeForm: TPipeForm;implementation{$R *.dfm}procedure TPipeForm.EditorKeyPress(Sender: TObject; var Key: Char);
var
ECharPos: TPoint;
begin
if Key = Chr(VK_RETURN) then
begin
// ShowMessage(GetCmdStr);
SendCmdToShell(GetCmdStr);
end else if Key = Chr(VK_BACK) then
begin
ECharPos := Editor.CaretPos;
if ECharPos.X = WPos.X + 1 then
Key := #0;
end;
end;procedure TPipeForm.SendCmdToShell(Const CmdStr: String);
var
ShellCmdStr: array[0..256] of char;
RBuffer: array[0..25000] of char;
nByteToWrite: DWORD;
nByteWritten: DWORD;
nByteReaden: DWORD;
begin
if CreateOK then
begin
StrPCopy(ShellCmdStr, CmdStr);
nByteToWrite := StrLen(ShellCmdStr);
ShellCmdStr[nByteToWrite] := #13;
ShellCmdStr[nByteToWrite+1] := #10;
ShellCmdStr[nByteToWrite+2] := #0;
Inc(nByteToWrite, 2);
WriteFile(hWriteFile, ShellCmdStr, nByteToWrite, nByteWritten, nil);
Sleep(400);
Editor.Lines.Clear;
FillChar(RBuffer, Sizeof(RBuffer), #0);
ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
Editor.Lines.Add(StrPas(RBuffer));
WPos.Y := Editor.Lines.Count-1;
WPos.X := Length(Editor.Lines[WPos.Y])-1;
end;
end;procedure TPipeForm.FormDestroy(Sender: TObject);
var
shellexitcode: Cardinal;
begin
if GetExitCodeProcess(processinfo.hProcess, shellexitcode) then
begin
if shellexitcode = STILL_ACTIVE then
TerminateProcess(processinfo.hProcess, 0);
end;
if hWriteFile <> 0 then
CloseHandle(hWriteFile);
if hReadFile <> 0 then
CloseHandle(hReadFile);
end;procedure TPipeForm.FormCreate(Sender: TObject);
var
Pipeattr: SECURITY_ATTRIBUTES;
ShellStartInfo: STARTUPINFO;
shellstr: array [0..256] of char;
RBuffer: array[0..25000] of char;
I: Integer;
nByteReaden: DWORD;
begin
CreateOK := False;
I := 0;
Editor.ReadOnly := False;
Wpos.X := 0;
WPos.Y := 0;
with Pipeattr do
begin
nLength := Sizeof(SECURITY_ATTRIBUTES);
lpSecurityDescriptor := nil;
bInheritHandle := true;
end; if CreatePipe(hReadPipe, hWriteFile, @Pipeattr, 0) then
Inc(i);
if CreatePipe(hReadFile, hWritePipe, @pipeattr, 0) then
Inc(i); GetStartupInfo(ShellStartInfo);
with ShellStartInfo do
begin
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
hStdInput := hReadPipe;
hStdError := hWritePipe;
hStdOutput := hWritePipe;
wShowWindow := SW_HIDE;
end;
GetSystemDirectory(@Shellstr, MAX_PATH+1);
StrCat(@ShellStr, Pchar('\\cmd.exe'));
if CreateProcess(Shellstr, nil, nil, nil, True, 0,
nil, nil, ShellStartInfo, processinfo) then
begin
Inc(i);
end else begin
MessageBox(Handle, Pchar('调用Shell错误!'), Pchar('错误'), (MB_OK or MB_ICONERROR));
end;
if i = 3 then
begin
CreateOK := True;
Editor.Lines.Clear;
sleep(250);
ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
Editor.Lines.Add(StrPas(RBuffer));
WPos.Y := Editor.Lines.Count-1;
WPos.X := Length(Editor.Lines[WPos.Y])-1;
end;
end;procedure TPipeForm.EditorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
ECharPos: TPoint;
begin
ECharPos := Editor.CaretPos;
if ECharPos.Y > WPos.Y then
Editor.ReadOnly := False
else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then
begin
Editor.ReadOnly := False;
end else
Editor.ReadOnly := True;
end;function TPipeForm.GetCmdStr: string;
var
LastLine: Integer;
begin
LastLine := Editor.Lines.Count - 1;
if LastLine > WPos.Y then
begin
result := Editor.Lines[LastLine];
end else if LastLine = WPos.Y then
begin
result := Editor.Lines[LastLine];
result := Copy(result, WPos.X+2, Length(result));
end else
begin
result := ' ';
end;
end;procedure TPipeForm.EditorMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ECharPos: TPoint;
begin
ECharPos := Editor.CaretPos;
if ECharPos.Y > WPos.Y then
Editor.ReadOnly := False
else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then
Editor.ReadOnly := False
else
Editor.ReadOnly := True;
end;end.
Google上N多答案的
例如
http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=53886
ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
卡住了,我是个初学者,能给点帮助吗?