Delphi怎么才能实现关闭那些服务类的进程呢?
如 瑞星等杀毒,我用Delphi怎么都实现不了,一般的QQ.exe或者其它一应用程序都可以,网上搜索了一下,应该是没有Debug权限的问题,哪位老师,有这类的源码嘛?

解决方案 »

  1.   

    把服务停止就行了。调用下面的函数。传入服务名。注意别把服务名和服务描述搞混。function StopWinService(SvcName:string):Boolean;
    var
      SvcStatus:SERVICE_STATUS;
      dwStartTime,dwTimeout:DWORD;
      schSCManager,schService:SC_HANDLE;
      procedure CloseHandles;
      begin
        CloseServiceHandle(schService);
        CloseServiceHandle(schSCManager);
      end;
    begin
      Result:=False;
      dwStartTime:=GetTickCount();
      dwTimeout:=30000;
      schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
      if 0=schSCManager then Exit;
      schService:=OpenService(schSCManager,PAnsiChar(SvcName),SERVICE_STOP or SERVICE_QUERY_STATUS or SERVICE_ENUMERATE_DEPENDENTS);
      if schService=0 then
      begin
        CloseServiceHandle(schSCManager);
        Exit;
      end;
      if not QueryServiceStatus(schService,SvcStatus) then
      begin
        CloseHandles;
        Exit;
      end;
      if SvcStatus.dwCurrentState=SERVICE_STOPPED then
      begin
        Result:=True;
        CloseHandles;
        Exit;
      end;
      while SvcStatus.dwCurrentState=SERVICE_STOP_PENDING do
      begin
        Sleep(SvcStatus.dwWaitHint );
        if not QueryServiceStatus(schService,SvcStatus) then
        begin
          CloseHandles;
          Exit;
        end;
        if SvcStatus.dwCurrentState=SERVICE_STOPPED then
        begin
          Result:=True;
          CloseHandles;
          Exit;
        end;
        if GetTickCount()-dwStartTime>dwTimeout then
        begin
          //Service stop timed out
          CloseHandles;
          Exit;
        end;
        StopDependentServices(schSCManager,schService);
        if not ControlService(schService,SERVICE_CONTROL_STOP,SvcStatus) then
        begin
          //ControlService failed
          CloseHandles;
          Exit;
        end;
        while SvcStatus.dwCurrentState<>SERVICE_STOPPED do
        begin
          Sleep(SvcStatus.dwWaitHint);
          if not QueryServiceStatus(schService,SvcStatus) then
          begin
            //QueryServiceStatusEx failed
            CloseHandles;
            Exit;
          end;
          if SvcStatus.dwCurrentState=SERVICE_STOPPED then Break;
          if GetTickCount()-dwStartTime>dwTimeout then
          begin
            //Wait timed out
            CloseHandles;
            Exit;
          end;
        end;
        Result:=True;
      end;
    end;
      

  2.   

    如果我想在不关闭服务的情况下直接结束进程呢?是否就要用到楼上那位朋友所说的Ring0还请详解一下,谢谢!或者有源码注释也可以,