zoukankan      html  css  js  c++  java
  • Delphi服务程序注册与卸载

    Delphi服务程序注册与卸载

    uses winsvc;
    function InstallService(ServiceName, DisplayName, FileName: string): boolean;
    var
    SCManager,Service: THandle;
    Args: pchar;
    begin
    Result := False;
    SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    if SCManager = 0 then Exit;
    try
       Service := CreateService(SCManager, //句柄
                       PChar(ServiceName), //服务名称
                       PChar(DisplayName), //显示服务名
                       SERVICE_ALL_ACCESS, //服务访问类型
                       SERVICE_WIN32_OWN_PROCESS, //服务类型 or SERVICE_INTERACTIVE_PROCESS
                       SERVICE_AUTO_START, //自动启动服务
                       SERVICE_ERROR_IGNORE, //忽略错误
                       PChar(FileName), //启动的文件名
                       nil, //name of load ordering group (载入组名) 'LocalSystem'
                       nil, //标签标识符
                       nil, //相关性数组名
                       nil, //帐户(当前)
                       nil); //密码(当前)

       Args := nil;
       StartService(Service, 0, Args);
       CloseServiceHandle(Service);
    finally
       CloseServiceHandle(SCManager);
    end;
    Result := True;
    end;

    procedure UninstallService(ServiceName: string);
    var
    SCManager,Service: THandle;
    ServiceStatus: SERVICE_STATUS;
    begin
    SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    if SCManager = 0 then Exit;
    try
       Service := OpenService(SCManager, PChar(ServiceName), SERVICE_ALL_ACCESS);
       ControlService(Service, SERVICE_CONTROL_STOP, ServiceStatus);
       DeleteService(Service);
       CloseServiceHandle(Service);
    finally
       CloseServiceHandle(SCManager);
    end;
    end;

    procedure ServiceCtrlHandler(Control: dword); stdcall;
    begin
    case Control of
       SERVICE_CONTROL_STOP:
       begin
         Stopped := True;
         Status.dwCurrentState := SERVICE_STOPPED;
       end;
       SERVICE_CONTROL_PAUSE:
       begin
         Paused := True;
         Status.dwcurrentstate := SERVICE_PAUSED;
       end;
       SERVICE_CONTROL_CONTINUE:
       begin
         Paused := False;
         Status.dwCurrentState := SERVICE_RUNNING;
       end;
       SERVICE_CONTROL_INTERROGATE: ;
       SERVICE_CONTROL_SHUTDOWN: Stopped := True;
    end;
    SetServiceStatus(StatusHandle, Status);
    end;

  • 相关阅读:
    eclipse里面自动添加get和set方法
    初探内联方式的 onload="doSomething()"为何要加"()"?而js代码的 onload="doSomething" 和 addEventListener 为何不加"()"?
    ubuntu下安装git,sublime,nodejs
    ajax学习计划
    ajax学习笔记
    滑动窗口思路精髓总结
    Java给定一个字符串,分割字符串使得每个子字符串都是回文串,求最少分割次数
    顺时针打印
    戳气球最少需要几下一样的题
    ip覆盖算法
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940879.html
Copyright © 2011-2022 走看看