『壹』 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.