|

提醒:若下载的软件是收费的"请不要付款",可能是骗子,请立即联系本站举报,执意要付款被骗后本站概不负责。(任何交易请走第三方中介,请勿直接付款交易以免被骗!切记).
- unit untServiceMan;
- interface
- uses windows, WinSvc;
- type
- TServiceMan = class
- private
- function ServiceConnect:SC_HANDLE;
- function ServiceDisconn(hService:SC_HANDLE):SC_HANDLE;
- function ServiceOpen(hService:SC_HANDLE; AServName:PChar):SC_HANDLE;
- function ServiceClose(hService:SC_HANDLE):Boolean;
- function ServiceGetStatus(hService:SC_HANDLE; AServName:PChar ):DWord;
- function ServiceUninstalled(hService:SC_HANDLE; AServName:PChar):Boolean;
- function ServiceRunning(hService:SC_HANDLE; AServName:PChar):Boolean;
- function ServiceStopped(hService:SC_HANDLE; AServName:PChar):Boolean;
- public
- function ServiceInstall(AServName, ADispName, AServPath:PChar):SC_HANDLE;
- function ServiceUnInstall(AServName:PChar):DWORD;
- function ServiceStart(AServName:PChar):DWORD;
- function ServiceStop(AServName:PChar):DWORD;
- end;
-
- implementation
- { TServiceMan }
- {**********************************************************
- **功能:装载服务 *
- **参数1:服务名 *
- **参数2:服务描述 *
- **参数3:服务文件的地址 *
- ***********************************************************}
- function TServiceMan.ServiceInstall(AServName, ADispName, AServPath: PChar): SC_HANDLE;
- var
- hManager:SC_HANDLE;
- begin
- result := 0;
- hManager := ServiceConnect;
- try
- if ServiceUninstalled(hManager, AServName) then
- begin
- result := CreateService(hManager,
- AServName,
- ADispName,
- SERVICE_ALL_ACCESS,
- SERVICE_KERNEL_DRIVER,
- SERVICE_DEMAND_START,
- SERVICE_ERROR_NORMAL,
- AServPath,
- nil,
- nil,
- nil,
- nil,
- nil);
- ServiceClose(result); //记住关闭句柄,否则会无法卸载服务
- end;
- finally
- ServiceDisconn(hManager);
- end;
- end;
- {**********************************************************
- **功能:关闭打开的服务句柄 *
- **参数1:服务句柄 *
- ***********************************************************}
- function TServiceMan.ServiceClose(hService: SC_HANDLE): Boolean;
- begin
- if (hService <> 0) then
- result := CloseServiceHandle(hService)
- else
- result := True;
- end;
- {**********************************************************
- **功能:打开服务管理器 *
- **返回值为其句柄 *
- ***********************************************************}
- function TServiceMan.ServiceConnect: SC_HANDLE;
- begin
- result := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
- end;
- {**********************************************************
- **功能:关闭管理器句柄 *
- **参数1:服务句柄 *
- ***********************************************************}
- function TServiceMan.ServiceDisconn(hService: SC_HANDLE): SC_HANDLE;
- begin
- if (hService <> 0) then
- CloseServiceHandle(hService);
- result := 0;
- end;
- {**********************************************************
- **功能:获得一个已经加载的服务的句柄 *
- **参数1:管理器句柄 *
- **参数2:服务的名称 *
- ***********************************************************}
- function TServiceMan.ServiceOpen(hService:SC_HANDLE; AServName: PChar): SC_HANDLE;
- begin
- result := OpenService(hService, AServName, SERVICE_ALL_ACCESS);
- end;
- {**********************************************************
- **功能:卸载服务 *
- **参数1:服务的名称 *
- ***********************************************************}
- function TServiceMan.ServiceUnInstall(AServName:PChar): DWORD;
- var
- hService, hManager:SC_HANDLE;
- ServiceStatus:TServiceStatus;
- begin
- result := 0;
- hManager := ServiceConnect;
- try
- if not ServiceUninstalled(hManager, AServName) then
- begin
- hService := ServiceOpen(hManager, AServName);
- if (hService <> 0) then
- begin
- ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus); //在卸载前停止服务,否则需要停止服务才能正常卸载
- if DeleteService(hService) then
- result := 2
- else
- result := 1;
- end;
- ServiceClose(hService);
- end;
- finally
- ServiceDisconn(hManager);
- end;
- end;
- {**********************************************************
- **功能:启动服务 *
- **参数1:服务的名称 *
- **返回值: 0:驱动还未安装 *
- 1:驱动已启动 *
- 2:驱动启动成功 *
- 3:驱动启动失败 *
- ***********************************************************}
- function TServiceMan.ServiceStart(AServName: PChar): DWORD;
- var
- hService, hManager:SC_HANDLE;
- begin
- result := 0;
- hManager := ServiceConnect;
- try
- if not ServiceUninstalled(hManager, AServName) then
- begin
- if not ServiceRunning(hManager, AServName) then
- begin
- hService := ServiceOpen(hManager, AServName);
- if (hService <> 0) then
- if StartService(hService, 1, AServName) then //启动服务
- result := 2
- else
- result := 3;
- ServiceClose(hService);
- end
- else
- result := 1;
- end;
- finally
- ServiceDisconn(hManager);
- end;
- end;
- {**********************************************************
- **功能:停止服务 *
- **参数1:服务的名称 *
- **返回值: 0:驱动还未安装 *
- 1:驱动已停止 *
- 2:驱动停止成功 *
- 3:驱动停止失败 *
- ***********************************************************}
- function TServiceMan.ServiceStop(AServName: PChar): DWORD;
- var
- hService, hManager:SC_HANDLE;
- ServiceStatus:TServiceStatus;
- begin
- result := 0;
- hManager := ServiceConnect;
- try
- if not ServiceUninstalled(hManager, AServName) then
- begin
- if not ServiceStopped(hManager, AServName) then
- begin
- hService := ServiceOpen(hManager, AServName);
- if (hService <> 0) then
- if ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus) then
- result := 2
- else
- result := 3;
- ServiceClose(hService);
- end
- else
- result := 1;
- end;
- finally
- ServiceDisconn(hManager);
- end;
- end;
- {**********************************************************
- **功能:获得服务的状态 *
- **参数1:管理器的句柄 *
- **参数2:服务的名称 *
- ***********************************************************}
- function TServiceMan.ServiceGetStatus(hService:SC_HANDLE; AServName:PChar ):DWORD;
- var
- hService2:SC_HANDLE;
- queryStatus:TServiceStatus;
- dwStat:DWORD;
- begin
- dwStat := 0;
- if(hService > 0)then
- begin
- hService2 := ServiceOpen(hService, AServName);
- if(QueryServiceStatus(hService2, queryStatus))then
- begin
- dwStat := queryStatus.dwCurrentState;
- end;
-
- ServiceClose(hService2);
- end;
- Result := dwStat;
- end;
- {判断某服务是否安装,未安装返回true,已安装返回false}
- function TServiceMan.ServiceUninstalled(hService:SC_HANDLE; AServName:PChar):Boolean;
- begin
- Result := 0 = ServiceGetStatus(hService, AServName);
- end;
- {判断某服务是否启动,启动返回true,未启动返回false}
- function TServiceMan.ServiceRunning(hService:SC_HANDLE; AServName:PChar):Boolean;
- begin
- Result := SERVICE_RUNNING = ServiceGetStatus(hService, AServName);
- end;
- {判断某服务是否停止,停止返回true,未停止返回false}
- function TServiceMan.ServiceStopped(hService:SC_HANDLE; AServName:PChar):Boolean;
- begin
- Result := SERVICE_STOPPED = ServiceGetStatus(hService, AServName);
- end;
- end.
复制代码
加载驱动.rar
(1.18 KB, 下载次数: 17)
联系我时,请说是在 挂海论坛 上看到的,谢谢! |
上一篇: IOCP网络高并发服务端类【0.0.0.3版本】下一篇: Delphi 指針写内存字串一問
免责声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关。一切关于该内容及资源商业行为与www.52ghai.com无关。
2、本站提供的一切资源内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
3、本站信息来自第三方用户,非本站自制,版权归原作者享有,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
4、如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵犯你版权的,请邮件与我们联系删除(邮箱:xhzlw@foxmail.com),本站将立即改正。
|