Delphi 服务程序注册与卸载
来源:互联网 发布:手机可视化编程 编辑:程序博客网 时间:2024/06/18 12:24
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;
取得當前系統已安裝的NtService
相關Api: EnumServicesStatus
The EnumServicesStatus function enumerates services in the specified service control manager database. The name and status of each service are provided.
This function has been superseded by the EnumServicesStatusEx function. It returns the same information EnumServicesStatus returns, plus the process identifier and additional information for the service. In addition, EnumServicesStatusEx enables you to enumerate services that belong to a specified group.
BOOL EnumServicesStatus(
SC_HANDLE hSCManager,
DWORD dwServiceType,
DWORD dwServiceState,
LPENUM_SERVICE_STATUS lpServices,
DWORD cbBufSize,
LPDWORD pcbBytesNeeded,
LPDWORD lpServicesReturned,
LPDWORD lpResumeHandle
);
sample:
uses Windows,WinSvc;
function ServiceGetList(sMachine: string; dwServiceType,
dwServiceState: DWord; slServicesList: TStrings): boolean;
var
j: integer;
schm: SC_Handle;
nBytesNeeded, nServices, nResumeHandle: DWord;
ServiceStatusRecs: array[0..511] of TEnumServiceStatus;
begin
Result := false;
schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_ALL_ACCESS);
try
if (schm = 0) then Exit;
nResumeHandle := 0;
while True do
begin
EnumServicesStatus(schm, dwServiceType, dwServiceState, @ServiceStatusRecs[0], sizeof(ServiceStatusRecs),
@nBytesNeeded, @nServices, @nResumeHandle);
for j := 0 to nServices - 1 do
begin
slServicesList.Add(ServiceStatusRecs[j].lpDisplayName + '---' + ServiceStatusRecs[j].lpServiceName);
end;
if nBytesNeeded = 0 then Break;
end;
Result := true;
finally
if schm > 0 then
CloseServiceHandle(schm);
end;
end;
procedure TForm1.btnServiceGetListClick(Sender: TObject);
begin
ServiceGetList('', SERVICE_WIN32, SERVICE_STATE_ALL, lbServices.Items);
end;
- Delphi服务程序注册与卸载
- Delphi 服务程序注册与卸载
- Delphi服务程序注册与卸载
- ATL 服务注册与卸载
- tomcat 注册卸载服务
- 注册卸载SVN服务
- 注册、卸载系统服务
- 引擎服务程序安装,启动与卸载
- delphi 安装卸载服务批处理
- 创建服务工程、注册卸载服务
- iphone 下的 lighttpd 服务程序安装与卸载
- C#操作注册服务卸载服务启动服务停止服务..
- C#操作注册服务卸载服务启动服务停止服务.. .
- MFC注册服务程序
- Delphi 服务程序编写
- Delphi创建服务程序
- com组件注册与卸载
- 注册和卸载windows服务进程
- 服务器维护常用命令
- 基本插入法排序
- php二维数组排序实现
- 数据库 行转列 列转行详解
- Dom4J解析XML文件
- Delphi 服务程序注册与卸载
- 养成编程时及时关闭资源的好习惯
- PT2313 音量控制IC 开发经验
- jquery 里的each使用方法详解
- gcc详解
- svn 打tag 命令
- codeblock添加头文件路径和静态库路径
- C语言栈实现行编辑器
- 批处理修改IP和DNS