delphi 调用外部程序获取程序ID,并能关闭该程序


本文整理自网络,侵删。

 
hApp: Cardinal;

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 DestroyProcess(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;

procedure TForm1.Button3Click(Sender: TObject);
begin
  hApp := StartApp('open', 'd:\Workers.pdf', '', '', SW_SHOWNORMAL);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if hApp > 0 then
    DestroyProcess(hApp);
end;

相关阅读 >>

Delphi winapi: setwindowtext - 设置窗口标题

Delphi xe 泛型数组和splitstring处理数据

Delphi消息发送字符串

Delphi 学习使用资源文件 - 字符串资源

Delphi 实现简易语音发音(基于tts方式)

pos、ansipos、fillchar在Delphi2010中unicode的问题

Delphi windows 编程[6] - 学习窗体生成的过程六: 最终的代码!

Delphi判断exe文件是否正在运行的函数

Delphi tms web core twebsocketclient

Delphi实现文件防删除

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...