怎么样在DELPHI里调用DOS且得到DOS的回显? 怎么样在DELPHI里调用DOS且得到DOS的回显? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把ShellExecute(0,'open','cmd','/c dir C:\ >>aaa.txt',0,0);中的dir c:\改为你的命令。代码:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses Shellapi; //注意一定要有{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin ShellExecute(0,'open','cmd','/c dir C:\ >>aaa.txt',0,0); Memo1.Lines.LoadFromFile(GetCurrentDir()+'\\aaa.txt'); end;end. 上面的代码是在win2000 delphi7.0 下通过在Win98下要把cmd 改为command :-) 用 DOS 命令的重定向功能就可以了,如楼上几位大侠所说。得到 Win2000 还是 Win98 下的命令解释器可以用环境变量来实现,查找环境变量 COMSPEC 的值,这个值存放的就是当前系统命令解释器的位置和程序名,关于环境变量的获取可以采用 GetEnvironmentVariable(Name: string): string; 函数来实现。 我要的不是這樣子,我要的結果就像黑客軟件SqlExec.exe的那種效果。 一段C++代码供参考,来自borland C++ builder 5.0自带的例子.利用管道实现: if (SourceFileSelected) { ResultsRE->Clear(); int FBreak; String TempFile; Screen->Cursor = crHourGlass; if (!Grep) SetEnvPath(); if (IsDirty) ResultsRE->Lines-> SaveToFile(OpenDlg->FileName); ViewResults(Sender); Application->ProcessMessages(); // If NT do security stuff SECURITY_DESCRIPTOR sd; SECURITY_ATTRIBUTES sa; LPSECURITY_ATTRIBUTES lpsa = NULL; if (IsWindowsNT()) { InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(&sd, true, NULL, false); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = true; sa.lpSecurityDescriptor = &sd; lpsa = &sa; } // Create the Pipe and get r/w handles HANDLE hReadPipe; HANDLE hWritePipe; assert(CreatePipe(&hReadPipe, &hWritePipe, lpsa, 2500000)); // initialize STARTUPINFO struct STARTUPINFO si; memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES; si.wShowWindow = SW_HIDE; si.hStdOutput = hWritePipe; si.hStdError = hWritePipe; PROCESS_INFORMATION pi; assert(hWritePipe); // Run the cmdLine tool ResultsRE->Lines->Add("Working....."); Application->ProcessMessages(); if( CreateProcess(NULL, CmdStrRE->Text.c_str(), NULL,//security NULL,// security TRUE,//inherits handles 0, 0, 0, &si, &pi) ){ CloseHandle(pi.hThread); WaitForSingleObject(pi.hProcess, 90000); // read from the pipe and put in richedit assert(hReadPipe); DWORD BytesRead; //unsigned long char dest[4000]; bool RdLoopDone = false; ResultsRE->Lines->Clear(); FBreak = 1; if (ExitCode) Screen->Cursor = crDefault; while (!RdLoopDone) { memset(dest, 0, 4000); assert(ReadFile(hReadPipe, &dest, sizeof(dest), &BytesRead, NULL)); ResultsRE->Lines->Add(String(dest)); if (BytesRead < 4000) RdLoopDone = true; if (FBreak > 150) RdLoopDone = true; FBreak++; } ResultsRE->Lines->Add("FINISHED!"); String RawPath; if (BCC32 || CPP32) { for (int h = 0; h < OpenDlg->Files->Count; h++) { RawPath = ""; RawPath = OpenDlg->Files->Strings[h]; TTabSheet *NewTabSheet = new TTabSheet(this); if (CPP32) NewTabSheet->Caption = StripFileExt( ExtractFileName(RawPath)) + ".i"; else NewTabSheet->Caption = ExtractFileName(OpenDlg->Files->Strings[h]); NewTabSheet->PageControl= PageCtl2; TRichEdit *NewRichEdit = new TRichEdit(this); NewRichEdit->ScrollBars = ssBoth; NewRichEdit->PlainText = true; NewRichEdit->Parent = NewTabSheet; NewRichEdit->Align = alClient; Application->ProcessMessages(); try { if (CPP32) { TempFile = ExtractFilePath(RawPath) + NewTabSheet->Caption; if (GetFileSize(TempFile) > 4000000) NewRichEdit->Lines-> Add("File size exceeds limit! Use WordPad to view!"); else NewRichEdit->Lines->LoadFromFile(TempFile); } else NewRichEdit->Lines->LoadFromFile(RawPath); } catch(...) { ShowMessage("Unable to open file " + RawPath); Screen->Cursor = crDefault; return; } } } try { if (TLib || TDump) { if (GetFileSize(DestEd) > 4000000) ResultsRE->Lines->Add("File size exceeds limit! Use WordPad to view!"); else ResultsRE->Lines->LoadFromFile(DestEd); } if (ImpDef) ResultsRE->Lines-> LoadFromFile(StripFileExt(OpenDlg->FileName) + ".def"); } catch (...) { ShowMessage("Unable to open file!"); Screen->Cursor = crDefault; return; } }//if CreateProcess else{ Screen->Cursor = crDefault; ResultsRE->Lines->Add("FINISHED!"); ShowMessage("Unable to execute program."); } // Process cleanup CloseHandle(hReadPipe); CloseHandle(hWritePipe); CloseHandle(pi.hProcess); ResultsRE->Perform(EM_SETSEL,0,0); Screen->Cursor = crDefault; IsDirty = false; BackBtn->Enabled = true; } else ShowMessage("Select a File!"); 这个方法非常之不好。有很多命令没法回显。比如net 这种问题可以看http://expert.csdn.net/Expert/topic/1230/1230681.xml上的贴,已经作了解答。回:这个方法非常之不好。有很多命令没法回显。比如net实际上像黑客軟件SqlExec.exe的那種效果就是使用binbin(BCB水王(不提倡大家向俺学习)) 所提的方法实现的。不过这种方法,不好设置。这是一个进程间通讯的问题。你可以找找进程通讯的书多看看。 zuoyexingchen(昨夜星尘):你说的进程通讯的书可以推荐几本吗?我想学学这方面的内容。-------你的回复这种问题可以看http://expert.csdn.net/Expert/topic/1230/1230681.xml上的贴,已经作了解答。回:这个方法非常之不好。有很多命令没法回显。比如net实际上像黑客軟件SqlExec.exe的那種效果就是使用binbin(BCB水王(不提倡大家向俺学习)) 所提的方法实现的。不过这种方法,不好设置。这是一个进程间通讯的问题。你可以找找进程通讯的书多看看。 这个贴子我已结了,分也给了,可不知道为什么,这里还能跟贴,不知道是怎么回事呀?份数也没显示出来??还有另外一个贴子也是这个样子,CSDN怎么了? FastReport 交叉报表是怎么做的啊? 指针相关的问题 重新装载的问题 我用了 Mune:=nil; 之后,我又如何加载回来?? 困惑不解的事... 关于TMemoryStream的文件操作的两个方式 在delphi的paradox 7 的字段设置 现在考什么证书比较好 我在做邮件接收发送程序时遇到些问题,大虾请进来看看。 线程里怎样调用如inttostr之类的函数????? 求助:如何巴activeX控件从toolbar上弄走? 请问怎么把整个硬盘的图片路径和文件名都出来
ShellExecute(0,'open','cmd','/c dir C:\ >>aaa.txt',0,0);
中的dir c:\改为你的命令。代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Shellapi; //注意一定要有{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(0,'open','cmd','/c dir C:\ >>aaa.txt',0,0); Memo1.Lines.LoadFromFile(GetCurrentDir()+'\\aaa.txt');
end;end.
win2000 delphi7.0 下通过在Win98下要把cmd 改为command
:-)
得到 Win2000 还是 Win98 下的命令解释器可以用环境变量来实现,查找环境变量 COMSPEC 的值,这个值存放的就是当前系统命令解释器的位置和程序名,关于环境变量的获取可以采用 GetEnvironmentVariable(Name: string): string; 函数来实现。
利用管道实现: if (SourceFileSelected) {
ResultsRE->Clear();
int FBreak;
String TempFile;
Screen->Cursor = crHourGlass;
if (!Grep) SetEnvPath();
if (IsDirty) ResultsRE->Lines->
SaveToFile(OpenDlg->FileName); ViewResults(Sender);
Application->ProcessMessages(); // If NT do security stuff
SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa;
LPSECURITY_ATTRIBUTES lpsa = NULL;
if (IsWindowsNT()) {
InitializeSecurityDescriptor(&sd,
SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, true, NULL, false);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = &sd;
lpsa = &sa;
} // Create the Pipe and get r/w handles
HANDLE hReadPipe;
HANDLE hWritePipe;
assert(CreatePipe(&hReadPipe,
&hWritePipe,
lpsa,
2500000));
// initialize STARTUPINFO struct
STARTUPINFO si;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hWritePipe;
si.hStdError = hWritePipe;
PROCESS_INFORMATION pi;
assert(hWritePipe);
// Run the cmdLine tool
ResultsRE->Lines->Add("Working.....");
Application->ProcessMessages(); if( CreateProcess(NULL,
CmdStrRE->Text.c_str(),
NULL,//security
NULL,// security
TRUE,//inherits handles
0,
0,
0,
&si,
&pi) ){
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, 90000); // read from the pipe and put in richedit
assert(hReadPipe);
DWORD BytesRead; //unsigned long
char dest[4000];
bool RdLoopDone = false;
ResultsRE->Lines->Clear();
FBreak = 1;
if (ExitCode) Screen->Cursor = crDefault;
while (!RdLoopDone) {
memset(dest, 0, 4000);
assert(ReadFile(hReadPipe, &dest, sizeof(dest), &BytesRead, NULL));
ResultsRE->Lines->Add(String(dest));
if (BytesRead < 4000) RdLoopDone = true;
if (FBreak > 150) RdLoopDone = true;
FBreak++;
}
ResultsRE->Lines->Add("FINISHED!");
String RawPath;
if (BCC32 || CPP32) {
for (int h = 0; h < OpenDlg->Files->Count; h++) {
RawPath = "";
RawPath = OpenDlg->Files->Strings[h];
TTabSheet *NewTabSheet = new TTabSheet(this);
if (CPP32)
NewTabSheet->Caption = StripFileExt(
ExtractFileName(RawPath)) + ".i";
else NewTabSheet->Caption =
ExtractFileName(OpenDlg->Files->Strings[h]);
NewTabSheet->PageControl= PageCtl2;
TRichEdit *NewRichEdit = new TRichEdit(this);
NewRichEdit->ScrollBars = ssBoth;
NewRichEdit->PlainText = true;
NewRichEdit->Parent = NewTabSheet;
NewRichEdit->Align = alClient;
Application->ProcessMessages();
try {
if (CPP32) {
TempFile = ExtractFilePath(RawPath) +
NewTabSheet->Caption;
if (GetFileSize(TempFile) > 4000000)
NewRichEdit->Lines->
Add("File size exceeds limit! Use WordPad to view!");
else
NewRichEdit->Lines->LoadFromFile(TempFile);
}
else NewRichEdit->Lines->LoadFromFile(RawPath);
}
catch(...) {
ShowMessage("Unable to open file " + RawPath);
Screen->Cursor = crDefault;
return;
}
}
} try {
if (TLib || TDump) {
if (GetFileSize(DestEd) > 4000000)
ResultsRE->Lines->Add("File size exceeds limit! Use WordPad to view!");
else
ResultsRE->Lines->LoadFromFile(DestEd);
}
if (ImpDef) ResultsRE->Lines->
LoadFromFile(StripFileExt(OpenDlg->FileName) +
".def");
}
catch (...) {
ShowMessage("Unable to open file!");
Screen->Cursor = crDefault;
return;
}
}//if CreateProcess
else{
Screen->Cursor = crDefault;
ResultsRE->Lines->Add("FINISHED!");
ShowMessage("Unable to execute program.");
}
// Process cleanup
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
CloseHandle(pi.hProcess);
ResultsRE->Perform(EM_SETSEL,0,0);
Screen->Cursor = crDefault;
IsDirty = false;
BackBtn->Enabled = true;
}
else ShowMessage("Select a File!");
回:这个方法非常之不好。有很多命令没法回显。比如net
实际上像黑客軟件SqlExec.exe的那種效果就是使用binbin(BCB水王(不提倡大家向俺学习)) 所提的方法实现的。不过这种方法,不好设置。这是一个进程间通讯的问题。你可以找找进程通讯的书多看看。
你说的进程通讯的书可以推荐几本吗?我想学学这方面的内容。-------你的回复
这种问题可以看http://expert.csdn.net/Expert/topic/1230/1230681.xml上的贴,已经作了解答。
回:这个方法非常之不好。有很多命令没法回显。比如net
实际上像黑客軟件SqlExec.exe的那種效果就是使用binbin(BCB水王(不提倡大家向俺学习)) 所提的方法实现的。不过这种方法,不好设置。这是一个进程间通讯的问题。你可以找找进程通讯的书多看看。