delphi怎样判断sqlservr.exe是否启动

delphi怎样判断sqlservr.exe是否启动,第1张

判断进程~~~~

uses TLHelp32

注意

function FindProcess(AFileName: string): boolean

var

hSnapshot: THandle//用于获得进程列表

lppe: TProcessEntry32//用于查找进程

Found: Boolean//用于判断进程遍历是否完成

begin

Result :=False

hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)//获得系统进程列表

lppe.dwSize := SizeOf(TProcessEntry32)//在调用Process32First API之前,需要初始化lppe记录的大小

Found := Process32First(hSnapshot, lppe)//将进程列表的第一个进程信息读入ppe记录中

while Found do

begin

if ((UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AFileName)) or (UpperCase(lppe.szExeFile )=UpperCase(AFileName))) then

begin

Result :=True

end

Found := Process32Next(hSnapshot, lppe)//将进程列表的下一个进程信息读入lppe记录中

end

end

例子 if FindProcess( 'mysqld-nt.exe ') then memo1.Lines.Add( '发现SQL服务! ')

1、先use TLHelp32, PsAPI,使用其中Process32First的函数和Process32Next遍历所有进程。

2、然后判断是否存在scktsrvr.exe。

3、函数代码:

function checkAppExists(appN: string): Boolean

var

  lppe: TProcessEntry32

  found : boolean

  Hand : THandle

  P:DWORD

  s:string

begin

  result := false

  Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0)

  found := Process32First(Hand,lppe)

  while found do

  begin

    s := StrPas(lppe.szExeFile)

    if lppe.th32ProcessID>0 then

      p := lppe.th32ProcessID

    else

      p := 0

    if (s = appN) then

    begin

      Result:= True

      Break

    end  

    found := Process32Next(Hand,lppe)

  end

end

4、程序示例,判断scktsrvr.exe是否存在:

procedure TForm1.btn1Click(Sender: TObject)

begin

  if checkAppExists('scktsrvr.exe') then

  begin

    ShowMessage('scktsrvr.exe在运行!')

  end

  else

  begin

    ShowMessage('scktsrvr.exe没有运行!')

  end    

end

5、效果如下:

你可以在程序运行时自动写入注册表

uses中包含 Registry

var

MyFilePath:String

begin

MyFilePath:=Application.ExeName

MyReg:=TRegistry.Create

MyReg.RootKey:= HKEY_CURRENT_USER

MyReg.OpenKey('SoftWare\Microsoft\Windows\CurrentVersion\Run',True)

MyReg.WriteString ('myfile',MyFilePath )

MyReg.Free

这样做,一般会有杀毒软件进行拦截,需要同意才行。

你还可以直接把程序放到开始菜单中的启动下。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11745641.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存