在我的应用程序(Delphi XE10)中,我想用用户的默认编辑器(MSword 或 OpenOffice writter 或其他)打开一个文件(RTF),并能够从我的应用程序中关闭这个编辑器。我尝试使用下面的代码,但没有成功关闭编辑器
procedure TForm1.Button1Click(Sender: TObject);
var hApp : Cardinal;
begin
hApp := StartApp('open','temp.rtf','','',sw_normal);
anotherForm.showmodal;
KillProcess(hApp);
end;
在哪里:
function StartApp(apchOperation, apchFileName, apchParameters, apchDirectory: PChar;awrdShowCmd: Word): Cardinal;
var
lseiInfo: TShellExecuteInfo;
begin
Result := 0;
FillChar(lseiInfo, SizeOf(lseiInfo), Chr(0));
lseiInfo.cbSize := SizeOf(lseiInfo);
lseiInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
lseiInfo.lpVerb := apchOperation;
lseiInfo.lpFile := apchFileName;
lseiInfo.lpParameters := apchParameters;
lseiInfo.lpDirectory := apchDirectory;
lseiInfo.nShow := awrdShowCmd;
if Boolean(ShellExecuteEx(@lseiInfo)) then
Result := lseiInfo.hProcess;
end;
procedure KillProcess(hProcess: Cardinal);
Var
ovExitCode: LongWord;
begin
try
if hProcess <> 0 then begin
GetExitCodeProcess(hProcess, ovExitCode);
if (ovExitCode = STILL_ACTIVE) or (ovExitCode <> WAIT_OBJECT_0) then
TerminateProcess(hProcess, ovExitCode);
CloseHandle(hProcess);
end;
except
end;
end;
编辑器打开,但是当它必须用 KillProcess 关闭时,ovExitCode 始终为零,因此 TerminateProcess 不会执行。我也尝试在任何情况下执行 TerminateProcess,但编辑器(在这种情况下为 MSword)没有关闭。
你能帮我吗 ?提前致谢