線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3995
推到 Plurk!
推到 Facebook!

呼叫外部程式,並且等它結束!

 
zaguan
一般會員


發表:11
回覆:18
積分:10
註冊:2002-03-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-07-09 18:15:15 IP:61.221.xxx.xxx 未訂閱
看了大家相關的討論,我寫了下面的程式
while true do
begin
  hExeHandle:=ShellExecute(Application.Handle, PChar('Open'), 
  PChar('cmd.exe'), PChar(''), nil, SW_NORMAL);
  If hExeHandle <> 0 Then
  begin
    WaitForSingleObject(hExeHandle, INFINITE);
    CloseHandle(hExeHandle);
  end;
end;
結果在Run time時,只跑了一次,Delphi就丟出了一個Exception, 請問大家要怎樣修正這個程式呢? ^^""
huwk
資深會員


發表:26
回覆:340
積分:323
註冊:2002-04-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-07-10 00:51:21 IP:210.85.xxx.xxx 未訂閱
 
function ExecAndWait(const Filename, Params: string; WindowState: word): boolean;
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
begin
  { Enclose filename in quotes to take care of long filenames with spaces. }
  CmdLine := '"'   Filename   '"'   Params;      FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;      Result := CreateProcess(NIL, PChar(CmdLine), NIL, NIL, FALSE,
     CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, NIL,
     PChar(ExtractFilePath(Filename)), SUInfo, ProcInfo);
  { Wait for it to finish. }
  if Result then
  begin
     WaitForSingleObject(ProcInfo.hProcess, INFINITE);
     { Clean up the handles. }
     CloseHandle(ProcInfo.hProcess);
     CloseHandle(ProcInfo.hThread);
  end;
end;
引言: 看了大家相關的討論,我寫了下面的程式
while true do
begin
  hExeHandle:=ShellExecute(Application.Handle, PChar('Open'), 
  PChar('cmd.exe'), PChar(''), nil, SW_NORMAL);
  If hExeHandle <> 0 Then
  begin
    WaitForSingleObject(hExeHandle, INFINITE);
    CloseHandle(hExeHandle);
  end;
end;
結果在Run time時,只跑了一次,Delphi就丟出了一個Exception, 請問大家要怎樣修正這個程式呢? ^^""
------
熊的學習 http://huwk.blogspot.com
andersonhsieh
版主


發表:33
回覆:531
積分:439
註冊:2002-06-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-07-10 09:44:52 IP:211.20.xxx.xxx 未訂閱
引言: 看了大家相關的討論,我寫了下面的程式
while true do
begin
  hExeHandle:=ShellExecute(Application.Handle, PChar('Open'), 
  PChar('cmd.exe'), PChar(''), nil, SW_NORMAL);
  If hExeHandle <> 0 Then
  begin
    WaitForSingleObject(hExeHandle, INFINITE);
    CloseHandle(hExeHandle);
  end;
end;
結果在Run time時,只跑了一次,Delphi就丟出了一個Exception, 請問大家要怎樣修正這個程式呢? ^^""
procedure ShellApplication(AppName, Caption : String; Param : array of String); var execinfo : TShellExecuteInfo; ApplicationName, ParamString : String; i : integer; begin for i := Low(PARAM) to High(Param) do ParamString := ' ' ParamString ' ' Param[i]; FillChar(execinfo,SizeOf(execinfo),0); execinfo.cbSize:=sizeof(execinfo); execinfo.lpVerb:='Open'; execinfo.lpFile:=Pchar(AppName); //所要執行的外部 execinfo.lpParameters:=Pchar(ParamString); //參數 execinfo.fMask:=SEE_MASK_NOCLOSEPROCESS; execinfo.nShow:=SW_HIDE; if (not ShellExecuteEx(@execinfo)) then ShowMessage('無法執行' AppName); Application.Minimize; WaitForSingleObject(execinfo.hProcess,INFINITE); //會一直等待直到執 // 行的外部程式結束,才會把控制權交給你的應用程式。 Application.Restore; end; @@~~飛翔在天際的精靈~~@@
------
@@~~飛翔在天際的精靈~~@@
zaguan
一般會員


發表:11
回覆:18
積分:10
註冊:2002-03-31

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-07-10 16:27:13 IP:61.231.xxx.xxx 未訂閱
感謝各位熱心的大大! 我成功了~ 真是人間處處有溫情!
chiehmin
高階會員


發表:13
回覆:134
積分:134
註冊:2002-05-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2002-07-19 10:58:57 IP:61.221.xxx.xxx 未訂閱
有一些安裝程式會先進行解壓暫存後才跳到藍色漸層的安裝畫面.. 如果此時用這個procedure來執行這種安裝程式.. 解壓完後就卡住不動了.. 必須用工作管理員將安裝程式工作結束之後..藍色漸層的安裝畫面才會出現.. 該如何處理這種情況呢? 才能順利的呼叫外部安裝程式..結束後回到原程式.. 謝謝..    
引言: procedure ShellApplication(AppName, Caption : String; Param : array of String); var execinfo : TShellExecuteInfo; ApplicationName, ParamString : String; i : integer; begin for i := Low(PARAM) to High(Param) do ParamString := ' ' ParamString ' ' Param[i]; FillChar(execinfo,SizeOf(execinfo),0); execinfo.cbSize:=sizeof(execinfo); execinfo.lpVerb:='Open'; execinfo.lpFile:=Pchar(AppName); //所要執行的外部 execinfo.lpParameters:=Pchar(ParamString); //參數 execinfo.fMask:=SEE_MASK_NOCLOSEPROCESS; execinfo.nShow:=SW_HIDE; if (not ShellExecuteEx(@execinfo)) then ShowMessage('無法執行' AppName); Application.Minimize; WaitForSingleObject(execinfo.hProcess,INFINITE); //會一直等待直到執 // 行的外部程式結束,才會把控制權交給你的應用程式。 Application.Restore; end; @@~~飛翔在天際的精靈~~@@
andersonhsieh
版主


發表:33
回覆:531
積分:439
註冊:2002-06-10

發送簡訊給我
#6 引用回覆 回覆 發表時間:2002-07-19 11:15:54 IP:211.20.xxx.xxx 未訂閱
應該是你的安裝流程有問題吧... 我大致說明一下這一個procedure的使用流程 執行A->A CALL PROCEDURE執行B->B執行完回到A->A才能繼續做它的事 可否說明你的安裝流程 @@~~飛翔在天際的精靈~~@@
------
@@~~飛翔在天際的精靈~~@@
chiehmin
高階會員


發表:13
回覆:134
積分:134
註冊:2002-05-23

發送簡訊給我
#7 引用回覆 回覆 發表時間:2002-07-19 11:28:48 IP:61.221.xxx.xxx 未訂閱
A CALL PROCEDURE沒錯... 而我的B是一個安裝程式.. 如果B安裝程式是直接進入安裝畫面則沒問題 而我執行的 1. B安裝程式是先在畫面左上角進行解壓..應該是解到暫存目錄.. 然後 2. B安裝程式再去執行暫存目錄中的安裝程式.... 此時步驟2就無法順利執行.. 所以我必須再手動去結束掉步驟1..它才會繼續步驟2.. 但是結束步驟1的同時..程式執行權也回到原A程式了.....    
引言: 應該是你的安裝流程有問題吧... 我大致說明一下這一個procedure的使用流程 執行A->A CALL PROCEDURE執行B->B執行完回到A->A才能繼續做它的事 可否說明你的安裝流程 @@~~飛翔在天際的精靈~~@@
andersonhsieh
版主


發表:33
回覆:531
積分:439
註冊:2002-06-10

發送簡訊給我
#8 引用回覆 回覆 發表時間:2002-07-19 11:41:36 IP:211.20.xxx.xxx 未訂閱
引言: A CALL PROCEDURE沒錯... 而我的B是一個安裝程式.. 如果B安裝程式是直接進入安裝畫面則沒問題 而我執行的 1. B安裝程式是先在畫面左上角進行解壓..應該是解到暫存目錄.. 然後 2. B安裝程式再去執行暫存目錄中的安裝程式.... 此時步驟2就無法順利執行.. 所以我必須再手動去結束掉步驟1..它才會繼續步驟2.. 但是結束步驟1的同時..程式執行權也回到原A程式了.....
由此此看來應該是安裝程式本身的問題啊........ @@~~飛翔在天際的精靈~~@@
------
@@~~飛翔在天際的精靈~~@@
chiehmin
高階會員


發表:13
回覆:134
積分:134
註冊:2002-05-23

發送簡訊給我
#9 引用回覆 回覆 發表時間:2002-07-19 11:48:23 IP:61.221.xxx.xxx 未訂閱
那看來只好用ShellExcute來執行B安裝程式了.. 因為B安裝程式是外來程式..沒法自行修改囉~~~~ 只是如此就無法等安裝完再把執行權交回A了.... 謝謝您ㄉ幫忙~~    
引言: 由此此看來應該是安裝程式本身的問題啊........ @@~~飛翔在天際的精靈~~@@
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-10-13 15:33:48 IP:211.21.xxx.xxx 未訂閱
上面的 ExecAndWait(),執行叫另一個程式根本沒反應 而ShellApplication(),我不知怎樣丟參數進去....(array of String) 希望有人可以為我解惑 ....    下面這個也許更簡單
procedure TForm1._ForceExec(filename:string);
var execinfo:TSHELLEXECUTEINFO;
begin
   fillchar(execinfo,sizeof(execinfo),0);
   execinfo.cbsize:=sizeof(execinfo);
   execinfo.lpverb:='open';
   execinfo.lpfile:=pchar(filename);
   execinfo.lpparameters:=nil;
   execinfo.fmask:=SEE_MASK_NOCLOSEPROCESS;
   execinfo.nshow:=SW_SHOWDEFAULT;       shellexecuteex(@execinfo);       //application.minimize;
   waitforsingleobject(execinfo.hprocess,infinite);
   //application.restore;
end;  
發表人 - bruce0211 於 2003/10/13 15:57:16
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#11 引用回覆 回覆 發表時間:2003-10-24 16:47:10 IP:203.203.xxx.xxx 未訂閱
引言: 看了大家相關的討論,我寫了下面的程式
while true do
begin
  hExeHandle:=ShellExecute(Application.Handle, PChar('Open'), 
  PChar('cmd.exe'), PChar(''), nil, SW_NORMAL);
  If hExeHandle <> 0 Then
  begin
    WaitForSingleObject(hExeHandle, INFINITE);
    CloseHandle(hExeHandle);
  end;
end;
結果在Run time時,只跑了一次,Delphi就丟出了一個Exception, 請問大家要怎樣修正這個程式呢? ^^""
看大家討論一堆, 但都未說明第一個為何只跑一次就會出錯 其實第一個方法很簡單好用, 只是為何會出錯呢 ? 哈哈 其實簡單的很 請問 ShellExecute 傳回的值是 ? 嘿嘿 沒有錯, 是 hExeHandle <>0 這裡的問題 應該是 hExeHandle > 32 the resurn value of ShellExecute if succeeds return value is instance handle if fails, the return value is an error value that is less than or equal to 32.
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#12 引用回覆 回覆 發表時間:2003-10-24 17:03:05 IP:147.8.xxx.xxx 未訂閱
Handle returned by ShellExecute can only tell you the call was ok or not.....    http://delphi.ktop.com.tw/link.asp?topic_id=38858
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#13 引用回覆 回覆 發表時間:2003-10-24 20:25:02 IP:203.203.xxx.xxx 未訂閱
引言: Handle returned by ShellExecute can only tell you the call was ok or not..... http://delphi.ktop.com.tw/link.asp?topic_id=38858
Handle not jsut tell you ok or not if ok , hanlde is useless, only means call succeed if not, handle tell you why http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp see detail in Return Value
系統時間:2024-04-27 10:51:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!