首页 > 系统相关 >检测指定软件是否在运行(windows和linux)

检测指定软件是否在运行(windows和linux)

时间:2022-12-17 08:44:06浏览次数:43  
标签:end ExeName IFDEF windows ENDIF FProcessEntry32 Result linux 软件

检测指定软件是否在运行,以下代码适用于windows和linux

unit uappisrunning;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils
  {$IFDEF WINDOWS}, Windows, JwaTlHelp32{$ENDIF}
  {$IFDEF LINUX}, process{$ENDIF};
// JwaTlHelp32 is in fpc\packages\winunits-jedi\src\jwatlhelp32.pas

// Returns TRUE if EXEName is running under Windows or Linux
// Don't pass an .exe extension to Linux!
function AppIsRunning(const ExeName: string): boolean;

implementation

// These functions return Zero if app is NOT running
// Override them if you have a better implementation

{$IFDEF WINDOWS}
function WindowsAppIsRunning(const ExeName: string): integer;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;

begin
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  Result := 0;

  while integer(ContinueLoop) <> 0 do
  begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      UpperCase(ExeName)) or (UpperCase(FProcessEntry32.szExeFile) =
      UpperCase(ExeName))) then
    begin
      Inc(Result);
      // SendMessage(Exit-Message) possible?
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;

  CloseHandle(FSnapshotHandle);
end;

{$ENDIF}

{$IFDEF LINUX}
function LinuxAppIsRunning(const ExeName: string): integer;
var
  t: TProcess;
  s: TStringList;

begin
  Result := 0;
  t := tprocess.Create(nil);
  t.CommandLine := 'ps -C ' + ExeName;
  t.Options := [poUsePipes, poWaitonexit];

  try
    t.Execute;
    s := TStringList.Create;
    try
      s.LoadFromStream(t.Output);
      Result := Pos(ExeName, s.Text);
    finally
      s.Free;
    end;
  finally
    t.Free;
  end;

end;

{$ENDIF}

function AppIsRunning(const ExeName: string): boolean;
begin

{$IFDEF WINDOWS}
  Result := (WindowsAppIsRunning(ExeName) > 0);
{$ENDIF}

{$IFDEF LINUX}
  Result := (LinuxAppIsRunning(ExeName) > 0);
{$ENDIF}

end;

end.

 

标签:end,ExeName,IFDEF,windows,ENDIF,FProcessEntry32,Result,linux,软件
From: https://www.cnblogs.com/qiufeng2014/p/16988600.html

相关文章

  • Windows for Docker 搭建LNMP开发环境
    转载至《Docker-Windows上搭建LNMP环境》1windows安装DockerDesktop下载地址注:请仔细查看安装环境要求,否则容易做无用功Hyper-v如果开启的话电脑有装VMware的话会V......
  • hualinux 进阶 vue 5.0:vue UI组件介绍及Element UI
    目录​​ 一、vue流行的UI介绍​​​​1.1un-app所有前端框架​​​​1.2pc浏览器​​​​1.3 小程序mpvue​​​​1.4移动端​​​​1.5 桌面端(客户端)Electron ​​......
  • Ubuntu助手 — 一键自动安装软件,一键进行系统配置
    ......
  • 【java-02】Nginx以及前后端分离项目在linux上的部署
    目录写在开头Nginx重点目录/文件配置文件结构server块反向代理负载均衡写在开头本篇主要介绍了nginx的简单使用,包括目录结构、配置文件结构、反向代理和负载均衡等。以及......
  • 开源软件中 几种经典的Hash算法的实现
     哈希算法将任意长度的二进制值映射为固定长度的较小二进制值,这个小的二进制值称为哈希值。哈希值是一段数据唯一且极其紧凑的数值表示形式。如果散列一段明文而且哪怕只更......
  • Linux下突破限制实现高并发量服务器
    1、修改用户进程可打开文件数限制在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文......
  • 调试+linux+网络问题+wiki
    SimpleNetworkTroubleshooting​​http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch04_:_Simple_Network_Troubleshooting​​​​http://www.linuxh......
  • Windows 10 G 神州网信政府版
    ​神州网信政府版2018版:Win10CMGE_V0-H.1020.000.iso校验码:9484e568c6505f9c4ad5b9fcf7ec8d83588eebfb38089f53e3301112076fb7f2下载地址:https://download.cmgos.com/a......
  • 【Linux】Linux的buff/cache占用内存过高解决方法
    参考资料​​Varnish简介,原理,配置缓存-腾讯云开发者社区-腾讯云​​​​Varnish简介,原理,配置缓存-腾讯云开发者社区-腾讯云​​​​关于varnish缓存|缓存​​ ......
  • A_A01_001 KEIL4-KEIL5软件安装
    @目录一、软件下载二、交流学习三、防止电脑误删文件操作步骤四、KEIL4安装五、KEIL5安装六、注意事项一、软件下载KEIL4/KEIL5网盘链接戳它跳转提取码:omni二、交流......