如何執行WinExec完後,再繼續執行程式… |
答題得分者是:william
|
channel
尊榮會員 發表:67 回覆:707 積分:854 註冊:2002-05-02 發送簡訊給我 |
各位先進:
如何等待WinExec執行完後,再繼續執行程式?感激不盡…
以下是小弟寫的程式碼,是將資料壓縮,但是資料還未壓縮完,就跑到ShowShowMessage('成功!');這一行了…
(如果可以的話,請將欠缺的程式補上…)
procedure TForm1.Button1Click(Sender: TObject); begin WinExec('D:\WinRAR\Rar.exe a -r E:\ShareFile.rar E:\ShareFile', SW_SHOWNORMAL); ShowMessage('成功!'); end;
------
~小弟淺見,參考看看~ |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
channel
尊榮會員 發表:67 回覆:707 積分:854 註冊:2002-05-02 發送簡訊給我 |
william、hagar兄:
感謝兩位前輩這麼快就回應了,這兩篇文大致上已看過,若小弟堅持要用WinExec或ShellExecute的話,應該如何做?
小弟有在KTop上查詢以前的文章,將之前的做法改為如下,可是還是不行,可否直接指正小弟的錯誤在那裏,因為相關文章都已看過了…感激不盡…
procedure TForm1.Button1Click(Sender: TObject); var aHandle: Hwnd; begin //WinExec('D:\WinRAR\Rar.exe a -r E:\ShareFile.rar E:\ShareFile', SW_SHOWNORMAL); aHandle := ShellExecute(Self.Handle, 'Open', 'rar.exe', ' a -r E:\ShareFile.rar E:\ShareFile', 'D:\WinRAR\', SW_SHOWNORMAL); WaitForSingleObject(aHandle, INFINITE); ShowMessage('成功!') end;
------
~小弟淺見,參考看看~ |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
how about ShellExecuteEx?
var ExitCode: cardinal; ExecInfo: TShellExecuteInfo; begin ZeroMemory(@ExecInfo,SizeOf(ExecInfo)); with ExecInfo do begin cbSize := SizeOf(ExecInfo); fMask := SEE_MASK_NOCLOSEPROCESS; lpVerb := 'open'; lpFile := 'cmd.exe'; Wnd := self.Handle; nShow := SW_SHOWNORMAL; end; ShellExecuteEx(@ExecInfo); GetExitCodeProcess(ExecInfo.hProcess,ExitCode); while ExitCode=STILL_ACTIVE do begin GetExitCodeProcess(ExecInfo.hProcess,ExitCode); sleep(10); Application.ProcessMessages; end; ShowMessage('成功!') end; |
channel
尊榮會員 發表:67 回覆:707 積分:854 註冊:2002-05-02 發送簡訊給我 |
引言: how about ShellExecuteEx?william兄: 感謝您的再度回應,小弟用您的方法已試出來了,小弟是怎麼寫的(多了紅色那一行…):var ExitCode: cardinal; ExecInfo: TShellExecuteInfo; begin ZeroMemory(@ExecInfo,SizeOf(ExecInfo)); with ExecInfo do begin cbSize := SizeOf(ExecInfo); fMask := SEE_MASK_NOCLOSEPROCESS; lpVerb := 'open'; lpFile := 'cmd.exe'; Wnd := self.Handle; nShow := SW_SHOWNORMAL; end; ShellExecuteEx(@ExecInfo); GetExitCodeProcess(ExecInfo.hProcess,ExitCode); while ExitCode=STILL_ACTIVE do begin GetExitCodeProcess(ExecInfo.hProcess,ExitCode); sleep(10); Application.ProcessMessages; end; ShowMessage('成功!') end; var ExitCode: cardinal; ExecInfo: TShellExecuteInfo; begin ZeroMemory(@ExecInfo,SizeOf(ExecInfo)); with ExecInfo do begin cbSize := SizeOf(ExecInfo); fMask := SEE_MASK_NOCLOSEPROCESS; lpVerb := 'open'; lpFile := 'D:\WinRAR\Rar.exe'; lpParameters := ' a -r E:\ShareFile.rar E:\ShareFile'; Wnd := self.Handle; nShow := SW_SHOWNORMAL; end; ShellExecuteEx(@ExecInfo); GetExitCodeProcess(ExecInfo.hProcess,ExitCode); while ExitCode=STILL_ACTIVE do begin GetExitCodeProcess(ExecInfo.hProcess,ExitCode); sleep(10); Application.ProcessMessages; end; ShowMessage('成功!') end;非常的感謝william兄的大力幫忙…感恩… 不過小弟仍有一事不解: 在這篇文章當中http://delphi.ktop.com.tw/topic.php?TOPIC_ID=19897,lcsboy兄:說了這一段話: 我要等程式作完再繼續往下作 HANDLE hProg=ShellExecute(.........); WaitForSingleObject(hProg); 可是小弟照lcsboy兄的方法去做,為什麼就是無法等程式作完再繼續往下作? var aHandle: Hwnd; begin aHandle := ShellExecute(Self.Handle, 'Open', 'rar.exe', ' a -r E:\ShareFile.rar E:\ShareFile', 'D:\WinRAR\', SW_SHOWNORMAL); WaitForSingleObject(aHandle, INFINITE); ShowMessage('成功!') end;可否幫小弟糾正一下錯誤…感激不盡…
------
~小弟淺見,參考看看~ |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp
引言:...... Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise. The following table lists the error values. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an int and compare it with the value 32 or one of the error codes below. ...... |
channel
尊榮會員 發表:67 回覆:707 積分:854 註冊:2002-05-02 發送簡訊給我 |
引言: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.aspwilliam兄: 小弟的英文能力不是很好,您的思意是指 HANDLE hProg=ShellExecute(.........); WaitForSingleObject(hProg); 這一段程式無法在Windows 2000上跑嗎?還是有其他的原因… 除了您提供的方法之外,用ShellExecute的方法是行不通的嗎? 煩請解答…感激不盡…引言:...... Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise. The following table lists the error values. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an int and compare it with the value 32 or one of the error codes below. ......
------
~小弟淺見,參考看看~ |
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: william兄: 小弟的英文能力不是很好,您的思意是指 HANDLE hProg=ShellExecute(.........); WaitForSingleObject(hProg); 這一段程式無法在Windows 2000上跑嗎?還是有其他的原因… 除了您提供的方法之外,用ShellExecute的方法是行不通的嗎? 煩請解答…感激不盡…To summarize, the article stated that the value returned by ShellExecute is a fake handle. |
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
用这个函数就可以判断这个程式是否在运行了
[转贴]
function IsFileInUse(FileName: TFileName): Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if not FileExists(FileName) then Exit;
HFileRes := CreateFile(PChar(FileName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end; try~~
|
channel
尊榮會員 發表:67 回覆:707 積分:854 註冊:2002-05-02 發送簡訊給我 |
引言: 用这个函数就可以判断这个程式是否在运行了 [转贴] function IsFileInUse(FileName: TFileName): Boolean; var HFileRes: HFILE; begin Result := False; if not FileExists(FileName) then Exit; HFileRes := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Result := (HFileRes = INVALID_HANDLE_VALUE); if not Result then CloseHandle(HFileRes); end; try~~ >>< face="Verdana, Arial, Helvetica"> 感謝sos_admin的回覆,您寶貴的意見,小弟會Try… 小弟採用william的用法…感恩…
------
~小弟淺見,參考看看~ |
fatmoon1
初階會員 發表:3 回覆:29 積分:36 註冊:2004-05-24 發送簡訊給我 |
引言:
引言: procedure TForm1.Button1Click(Sender: TObject); var aHandle: Hwnd; begin //WinExec('D:\WinRAR\Rar.exe a -r E:\ShareFile.rar E:\ShareFile', SW_SHOWNORMAL); aHandle := ShellExecute(Self.Handle, 'Open', 'rar.exe', ' a -r E:\ShareFile.rar E:\ShareFile', 'D:\WinRAR\', SW_SHOWNORMAL); WaitForSingleObject(aHandle, INFINITE); ShowMessage('成功!') end;雖然已經有正確解答了,但我針對上述方法會失敗的原因來回應 上述方法會失敗的原因是因為 aHandle:=ShellExecute(Self.Handle, 'Open', 'rar.exe', ' a -r E:\ShareFile.rar E:\ShareFile', 'D:\WinRAR\', SW_SHOWNORMAL); 如此的話aHandle只是存入ShellExecute的回傳值(成功的話回傳值會大於32) 所以WaitForSingleObject(aHandle, INFINITE);此行根本沒等到正確的Handle值 而WaitForSingleObject(hHandle: THandle; dwMilliseconds: DWORD); 這個函式的作用是 等候 hHandle(執行程式的Handle值) dwMilliseconds(ms) 所以經過dwMilliseconds(ms)後仍會執行下一行 所以要改寫成 var Result: Boolean; ShellExInfo: TShellExecuteInfo; begin FillChar(ShellExInfo, SizeOf(ShellExInfo), 0); with ShellExInfo do begin cbSize := SizeOf(ShellExInfo); fMask := see_Mask_NoCloseProcess; Wnd := Application.Handle; lpFile := 'D:\WinRAR\Rar.exe'; lpDirectory := 'D:\WinRAR\'; lpParameters := ' a -r E:\ShareFile.rar E:\ShareFile'; nShow := SW_SHOWNORMAL; end; //上述程式碼與william兄是一樣的,不一樣的在下方 Result := ShellExecuteEx(@ShellExInfo); if Result then while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do begin Application.ProcessMessages; if Application.Terminated then Break; end; end; ========================= fat eat moon,fat eat moon
------
才疏學淺 |
hdflove
一般會員 發表:4 回覆:3 積分:1 註冊:2004-10-14 發送簡訊給我 |
我这样使用
var Result: Boolean;
ShellExInfo: TShellExecuteInfo;
begin
FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
with ShellExInfo do begin
cbSize := SizeOf(ShellExInfo);
fMask := see_Mask_NoCloseProcess;
Wnd := Application.Handle;
lpFile := 'D:\Microsoft Office\Office\WINWORD.EXE';
lpDirectory := 'D:\Microsoft Office\Office\';
lpParameters := 'd:\wlm.doc';
nShow := SW_SHOWNORMAL;
end;
Result := ShellExecuteEx(@ShellExInfo);
if Result then
while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do
begin
Application.ProcessMessages;
if Application.Terminated then
Break;
end; end; 求助
但为什么我先打开一个其它WORD文件就直接Break了,没有打开其它WORD文件就正常。
|
hdflove
一般會員 發表:4 回覆:3 積分:1 註冊:2004-10-14 發送簡訊給我 |
var Result: Boolean;
ShellExInfo: TShellExecuteInfo;
begin
FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
with ShellExInfo do begin
cbSize := SizeOf(ShellExInfo);
fMask := see_Mask_NoCloseProcess;
Wnd := Application.Handle;
lpFile := 'D:\Microsoft Office\Office\WINWORD.EXE';
lpDirectory := 'D:\Microsoft Office\Office\';
lpParameters := 'd:\wlm.doc';
nShow := SW_SHOWNORMAL;
end;
Result := ShellExecuteEx(@ShellExInfo);
if Result then
while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do
begin
Application.ProcessMessages;
if Application.Terminated then
Break;
end; end; 为什么先打开其它WORD文件执行上面语句就会直接Break
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |