‘壹’ delphi的shellexecute中调用cmd.exe
你的程序不太对,照我给你的改。能用结贴给分,人的基本素质。
uses ShellAPI;
procere TForm1.Button1Click(Sender: TObject);
var
fn: string;
begin
fn := 'c:\python27\arcgis10.1\try.py';
ShellExecute(0, 'open', 'cmd.exe', PChar('/c python ' + fn), nil, SW_SHOW);
end;
另外检查一下系统路径里有没有Python:我的电脑->属性 -> 高级 -> 环境变量 -> 系统变量->选中Path->编辑 。
‘贰’ delphi中如何执行命令行程序
我很久以前的做法是 带参数调用命令并利用重定向将结果输出到一个文本文件中去读取,后来一直没涉及到这方面的东西,也没去研究过。
例如:exec('cmd.exe dir c:\ >"c:\a.txt"');再去a.txt中把结果读出来
‘叁’ delphi 运行cmd 返回
你是想得到控制台的信息吧 可以看一下例程这个涉及到管道的API
procereRunDosInMemo(constDosApp:string;AMemo:TMemo);
const
{设置ReadBuffer的大小}
ReadBuffer=2400;
var
Security:TSecurityAttributes;
ReadPipe,WritePipe:THandle;
start:TStartUpInfo;
ProcessInfo:TProcessInformation;
Buffer:PChar;
BytesRead:DWord;
Buf:string;
begin
withSecuritydo
begin
nlength:=SizeOf(TSecurityAttributes);
binherithandle:=true;
lpsecuritydescriptor:=nil;
end;
{创建一个命名管道用来捕获console程序的输出}
ifCreatepipe(ReadPipe,WritePipe,@Security,0)then
begin
Buffer:=AllocMem(ReadBuffer+1);
FillChar(Start,Sizeof(Start),#0)
{设置console程序的启动属性}
withstartdo
begin
cb:=SizeOf(start);
start.lpReserved:=nil;
lpDesktop:=nil;
lpTitle:=nil;
dwX:=0;
dwY:=0;
dwXSize:=0;
dwYSize:=0;
dwXCountChars:=0;
dwYCountChars:=0;
dwFillAttribute:=0;
cbReserved2:=0;
lpReserved2:=nil;
hStdOutput:=WritePipe;//将输出定向到我们建立的WritePipe上
hStdInput:=ReadPipe;//将输入定向到我们建立的ReadPipe上
hStdError:=WritePipe;//将错误输出定向到我们建立的WritePipe上
dwFlags:=STARTF_USESTDHANDLESorSTARTF_USESHOWWINDOW;
wShowWindow:=SW_HIDE;//设置窗口为hide
end;
try
{创建一个子进程,运行console程序}
ifCreateProcess(nil,PChar(DosApp),@Security,@Security,true,
NORMAL_PRIORITY_CLASS,
nil,nil,start,ProcessInfo)then
begin
{等待进程运行结束}
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
{关闭输出...开始没有关掉它,结果如果没有输出的话,程序死掉了。}
CloseHandle(WritePipe);
Buf:='';
{读取console程序的输出}
repeat
BytesRead:=0;
ReadFile(ReadPipe,Buffer[0],ReadBuffer,BytesRead,nil);
Buffer[BytesRead]:=#0;
OemToAnsi(Buffer,Buffer);
Buf:=Buf+string(Buffer);
until(BytesRead<ReadBuffer);
SendDebug(Buf);
{按照换行符进行分割,并在Memo中显示出来}
whilepos(#10,Buf)>0do
begin
AMemo.Lines.Add(Copy(Buf,1,pos(#10,Buf)-1));
Delete(Buf,1,pos(#10,Buf));
end;
end;
finally
FreeMem(Buffer);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
end;
end;
end;
‘肆’ Delphi调用cmd进入目录执行命令问题
winexec('c:\windows\system32\xxxx.exe',0);
‘伍’ 关于Delphi实现cmd效果的。 我找了很久啊,希望这次能实现
找了一个方法:
unit StdOutMain;
interface
uses
Windows, SysUtils, Classes, Controls, Forms, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
procere Button1Click(Sender: TObject);
private
{ Private declarations }
public
procere RunDosInMemo(const DosApp: string; AMemo: TMemo);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procere TForm1.RunDosInMemo(const DosApp: string; AMemo: TMemo);
const
{设置ReadBuffer的大小}
ReadBuffer = 2400;
var
Security: TSecurityAttributes;
ReadPipe, WritePipe: THandle;
start: TStartUpInfo;
ProcessInfo: TProcessInformation;
Buffer: PChar;
BytesRead: DWord;
Buf: string;
begin
with Security do
begin
nlength := SizeOf(TSecurityAttributes);
binherithandle := true;
lpsecuritydescriptor := nil;
end;
{创建一个命名管道用来捕获console程序的输出}
if Createpipe(ReadPipe, WritePipe, @Security, 0) then
begin
Buffer := AllocMem(ReadBuffer + 1);
FillChar(Start, Sizeof(Start), #0);
{设置console程序的启动属性}
with start do
begin
cb := SizeOf(start);
start.lpReserved := nil;
lpDesktop := nil;
lpTitle := nil;
dwX := 0;
dwY := 0;
dwXSize := 0;
dwYSize := 0;
dwXCountChars := 0;
dwYCountChars := 0;
dwFillAttribute := 0;
cbReserved2 := 0;
lpReserved2 := nil;
hStdOutput := WritePipe; //将输出定向到我们建立的WritePipe上
hStdInput := ReadPipe; //将输入定向到我们建立的ReadPipe上
hStdError := WritePipe;//将错误输出定向到我们建立的WritePipe上
dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
wShowWindow := SW_HIDE;//设置窗口为hide
end;
try
{创建一个子进程,运行console程序}
if CreateProcess(nil, PChar(DosApp), @Security, @Security, true,
NORMAL_PRIORITY_CLASS,
nil, nil, start, ProcessInfo) then
begin
{等待进程运行结束}
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
{关闭输出...开始没有关掉它,结果如果没有输出的话,程序死掉了。}
CloseHandle(WritePipe);
Buf := '';
{读取console程序的输出}
repeat
BytesRead := 0;
ReadFile(ReadPipe, Buffer[0], ReadBuffer, BytesRead, nil);
Buffer[BytesRead] := #0;
OemToAnsi(Buffer, Buffer);
Buf := Buf + string(Buffer);
until (BytesRead < ReadBuffer);
{按照换行符进行分割,并在Memo中显示出来}
while pos(#10, Buf) > 0 do
begin
AMemo.Lines.Add(Copy(Buf, 1, pos(#10, Buf) - 1));
Delete(Buf, 1, pos(#10, Buf));
end;
end;
finally
FreeMem(Buffer);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
end;
end;
end;
procere TForm1.Button1Click(Sender: TObject);
begin
RunDosInMemo(Edit1.Text,Memo1);
end;
end.
‘陆’ delphi7中怎么运行DOS命令,但是我不想让他显示DOS窗口该怎么做呢
在delphi7中有个WinExec命令,通过它可以直接运行指定的dos命令。
比如想开启tlenet 服务,就可以先定义一个字符串变量MyCmd用来存放DOS命令,然后通过WinExec来执行,执行时加上Sw_Hide参数,就能起来隐藏DOS窗口的效果。
Var
MyCmd:String;
Begin
//开启tlenet 服务
MyCmd:='Net start telnet';
WinExec(PChar(MyCmd),Sw_Hide); //执行命令
end;
‘柒’ delphi执行cmd
启动按钮:
procere TForm1.btn1Click ( Sender: TObject ) ;
var
nResult : Integer ;//定义运行结果变量
begin
nResult := WinExec ( 'c:\a.bat' , 0 ) ;//''内的是执行外部命令完整路径,0参数,表明不显示运行窗口,你可以改成1试试效果。
if nResult > 31 then //返回结果大于31表明成功运行
ShowMessage ( '成功运行!' )
else
if nResult = 0 then
ShowMessage ( '超出系统内存资源!' )
else
if nResult = ERROR_BAD_FORMAT then //
ShowMessage ( '不是合法的Win32程序' )
else
if nResult = ERROR_FILE_NOT_FOUND then
ShowMessage ( '指定的文件未找到' )
else
if nResult = ERROR_BAD_FORMAT then
ShowMessage ( '不是合法的Win32程序' )
else
ShowMessage ( '未知错误!' ) ;
end ;
结束按钮:
AFileName指为cmd.exe,bat文件是由cmd.exe执行的
先Uses TLHelp32;
procere EndProcess(AFileName: string);
const
PROCESS_TERMINATE=$0001;
var
ExeFileName: String;
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
ExeFileName := AFileName;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0);
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
edit1?不知道你要返回什么,另外目录什么的,自己再改改吧
‘捌’ Delphi 执行cmd
这回答的怎么我都看不懂啊?正确的写法应该是(win7+xE2通过测试)
ShellExecute(Application.Handle, 'open', pChar('cmd.exe'), nil, nil,
SW_SHOWNORMAL);
‘玖’ delphi 怎么调用cmd 命令 比如 执行dir
可以用
Winexe();实现
也可以用shellexecute
比如你说的dir:
command
:=
'dir
c:';
ShellExecute(Handle,nil,'cmd.exe',pchar(command),nil,SW_SHOW);
//shellapi使用
ShellExecute(Handle.nil,'你调用的程序','参数',nil,SW_SHOW);
最后一个参数是窗口初始化状态
你都自己试一下吧
‘拾’ Delphi 如何一次执行多条CMD命令
unituDOS;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls;
type
TForm1=class(TForm)
Memo1:TMemo;
Edit1:TEdit;
btnRun:TButton;
Label1:TLabel;
lblCount:TLabel;
btnSave:TButton;
SaveDialog1:TSaveDialog;
procerebtnRunClick(Sender:TObject);
procerebtnSaveClick(Sender:TObject);
private
procereRunDosCommand(Command:String;Output:TStrings);
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
{$R*.dfm}
procereTForm1.btnRunClick(Sender:TObject);
var
cm:String;
sl:TStrings;
begin
cm:=Edit1.Text;
ifcm=''thenexit;
sl:=TStringList.Create;
cm:='cmd/c'+cm;
RunDosCommand(cm,sl);
lblCount.Caption:='0';
Memo1.Lines.Clear;
Memo1.Lines.AddStrings(sl);
lblCount.Caption:=IntToStr(sl.Count)+'ÐмǼ';
sl.Free;
end;
procereTForm1.RunDosCommand(Command:String;Output:TStrings);
var
hReadPipe:THandle;
hWritePipe:THandle;
SI:TStartUpInfo;
PI:TProcessInformation;
SA:TSecurityAttributes;
BytesRead:DWORD;
Avail,ExitCode,wrResult:DWORD;
Dest:Array[0..1023]ofChar;
CmdLine:Array[0..512]ofChar;
TmpList:TStringList;
osVer:TOSVERSIONINFO;
tmpstr:AnsiString;
begin
osVer.dwOSVersionInfoSize:=Sizeof(TOSVERSIONINFO);
GetVersionEX(osVer);
ifosVer.dwPlatformId=VER_PLATFORM_WIN32_NTthenbegin
SA.nLength:=SizeOf(SA);
SA.lpSecurityDescriptor:=nil;//@SD;
SA.bInheritHandle:=True;
CreatePipe(hReadPipe,hWritePipe,@SA,0);
endelseCreatePipe(hReadPipe,hWritePipe,nil,1024);
try
Screen.Cursor:=crHourglass;
FillChar(SI,SizeOf(SI),0);
SI.cb:=SizeOf(TStartUpInfo);
SI.wShowWindow:=SW_HIDE;
SI.dwFlags:=STARTF_USESHOWWINDOW;
SI.dwFlags:=SI.dwFlagsorSTARTF_USESTDHANDLES;
SI.hStdOutput:=hWritePipe;
SI.hStdError:=hWritePipe;
StrPCopy(CmdLine,Command);
ifCreateProcess(nil,CmdLine,nil,nil,True,
NORMAL_PRIORITY_CLASS,nil,nil,SI,PI)then
begin
ExitCode:=0;
whileExitCode=0dobegin
wrResult:=WaitForSingleObject(PI.hProcess,500);
ifPeekNamedPipe(hReadPipe,@Dest[0],1024,@Avail,
nil,nil)thenbegin
ifAvail>0thenbegin
TmpList:=TStringList.Create;
try
FillChar(Dest,SizeOf(Dest),0);
ReadFile(hReadPipe,Dest[0],Avail,BytesRead,nil);
TmpStr:=Copy(Dest,0,BytesRead-1);
TmpList.Text:=TmpStr;
Output.AddStrings(TmpList);
finally
TmpList.Free;
end;
end;
end;
ifwrResult<>WAIT_TIMEOUTthenExitCode:=1;
end;
GetExitCodeProcess(PI.hProcess,ExitCode);
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
end;
finally
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
Screen.Cursor:=crDefault;
end;
end;
procereTForm1.btnSaveClick(Sender:TObject);
begin
ifMemo1.Lines.Count=0thenexit;
ifSaveDialog1.Executethenbegin
Memo1.Lines.SaveToFile(SaveDialog1.Filename);
end;
end;
end.