您好,欢迎来到好走旅游网。
搜索
您的当前位置:首页delphi操作服务代码

delphi操作服务代码

来源:好走旅游网


delphi操作服务代码

该内容来自网络,上传此文的目的:1、为了获取分数;2、我认为它能对编程爱好者提供一点点帮助。

unit Unit_Service;

interface

uses Windows, Messages, SysUtils,Winsvc,Dialogs;

function StartServices(Const SvrName: String): Boolean;

function StopServices(Const SvrName: String): Boolean;

function QueryServiceStatu(Const SvrName: String): String;

function CreateServices(Const SvrName,FilePath: String): Boolean;

function DeleteServices(Const SvrName: String): Boolean;

implementation

//开启服务

function StartServices(Const SvrName: String): Boolean;

var

a,b: SC_HANDLE;

c: PChar;

begin

Result := False;

a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);

if a <= 0 then Exit;

b := OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);

if b <= 0 then Exit;

try

Result := StartService(b,0,c);

CloseServiceHandle(b);

CloseServiceHandle(a);

except

CloseServiceHandle(b);

CloseServiceHandle(a);

Exit;

end;

end;

//停止服务

function StopServices(Const var

a,b: SC_HANDLE;

d: TServiceStatus;

begin

Result := False;

SvrName: String): Boolean;

a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);

if a <= 0 then Exit;

b := OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);

if b <= 0 then Exit;

try

Result := ControlService(b,SERVICE_CONTROL_STOP,d);

CloseServiceHandle(a);

CloseServiceHandle(b);

except

CloseServiceHandle(a);

CloseServiceHandle(b);

Exit;

end;

end;

//查询当前服务的状态

function QueryServiceStatu(Const SvrName: String): String;

var

a,b: SC_HANDLE;

d: TServiceStatus;

begin

Result := '未安装 ';

a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);

if a <= 0 then Exit;

b := OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);

if b <= 0 then Exit;

try

QueryServiceStatus(b,d);

if d.dwCurrentState = SERVICE_RUNNING then

Result := '启动 ' //Run

else else then

else then

else else if d.dwCurrentState Result := 'Wait ' if d.dwCurrentState Result := 'Wait ' if d.dwCurrentState Result := '停止 ' if d.dwCurrentState Result := '暂停 ' if d.dwCurrentState SERVICE_RUNNING then

//Runing

= SERVICE_START_PENDING //Pause

= SERVICE_STOP_PENDING //Pause

SERVICE_PAUSED then

//Pause

SERVICE_STOPPED then

= = =

Result := '停止 ' //Stop

else if d.dwCurrentState =

SERVICE_CONTINUE_PENDING then

Result := 'Wait ' //Pause

else if d.dwCurrentState then

Result := 'Wait '; CloseServiceHandle(a);

CloseServiceHandle(b);

except

CloseServiceHandle(a);

CloseServiceHandle(b);

Exit;

end;

= SERVICE_PAUSE_PENDING //Pause

end;

{建立服务}

function CreateServices(Const SvrName,FilePath: String): Boolean;

var

a,b: SC_HANDLE;

begin

Result := False;

if FilePath = ' ' then Exit;

a := OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);

if a <= 0 then Exit;

try

b := CreateService(a,PChar(SvrName),

PChar(SvrName),

SERVICE_ALL_ACCESS,

SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS,

SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,

PChar(FilePath),nil,nil,nil,nil,nil); if b <= 0 then begin

ShowMessage( SysErrorMessage( GetlastError Exit;

end;

CloseServiceHandle(a);

CloseServiceHandle(b);

Result := True;

));

except

CloseServiceHandle(a);

CloseServiceHandle(b);

Exit;

end;

end;

{卸载服务}

function DeleteServices(Const SvrName: String): Boolean;

var

a,b: SC_HANDLE;

begin

Result := False;

a := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);

if a <= 0 then Exit;

b := OpenService(a,PChar(SvrName),STANDARD_RIGHTS_REQUIRED);

if b <= 0 then Exit;

try

Result := DeleteService(b);

if not Result then

ShowMessage( SysErrorMessage(

CloseServiceHandle(b);

CloseServiceHandle(a);

except

CloseServiceHandle(b);

CloseServiceHandle(a);

GetlastError ));

Exit;

end;

end;

end.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- haog.cn 版权所有 赣ICP备2024042798号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务