gpt4 book ai didi

Delphi:制作ServiceDelete函数

转载 作者:行者123 更新时间:2023-12-03 15:28:34 24 4
gpt4 key购买 nike

我想做一个服务删除功能。

Function ServiceDelete(sMachine, sService: String): Boolean;
Var
schm, schs: SC_Handle;
ss: TServiceStatus;
dwChkP: dword;
Begin
Result := False;
schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
If schm > 0 Then Begin
schs := OpenService(schm, PChar(sService), SERVICE_STOP Or SERVICE_QUERY_STATUS);
If schs > 0 Then Begin
If (QueryServiceStatus(schs, ss)) Then Begin
While (SERVICE_STOPPED <> ss.dwCurrentState) Do Begin
ControlService(schs, SERVICE_CONTROL_STOP, ss);
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
If (Not QueryServiceStatus(schs, ss)) Then
Break;
If (ss.dwCheckPoint < dwChkP) Then
Break;
End;
End;
DeleteService(schs);
CloseServiceHandle(schs);
End;
CloseServiceHandle(schm);

// If service does not exist, then everything is fine.
schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
If schm > 0 Then Begin
schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS);
If schs = 0 Then Begin
If GetLastError = ERROR_SERVICE_DOES_NOT_EXIST Then
Result := True;
End Else Begin
CloseServiceHandle(schs);
End;
CloseServiceHandle(schm);
End;
End;
End;

它打开服务管理器,打开服务,执行DeleteService,但不会删除服务。函数返回 false,服务仍然存在。我做错了什么?

最佳答案

根据DeleteServiceService Security and Access Rights ,您需要添加 STANDARD_RIGHTS_REQUIRED = $F0000; 才能获取

schs := OpenService(schm, PChar(sService), STANDARD_RIGHTS_REQUIRED  or SERVICE_STOP Or SERVICE_QUERY_STATUS);

此外,以管理员权限运行程序并检查每个函数结果是否正常(如注释中所述)非常重要。

编辑:

感谢 Remy Lebeau 的评论,可以进一步缩小范围,只需要标准访问权限 Winapi.Windows._DELETE = $00010000; 即可获取

schs := OpenService(schm, PChar(sService), Winapi.Windows._DELETE or SERVICE_STOP Or SERVICE_QUERY_STATUS);

关于Delphi:制作ServiceDelete函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com