如何得知一个在Nt下的服务的状态?1运行中,2启动中 3停止中,4 已停止?(50分)

  • 主题发起人 主题发起人 oldseven
  • 开始时间 开始时间
O

oldseven

Unregistered / Unconfirmed
GUEST, unregistred user!
另外如何控制此服务的状态?
1。停止
2。启动
3。重起
 
做hacker软件呀?我也想知道,用过win2000自动攻击探测机
它就用了这个东西,不过只能看,不能控制。你不防加作者qq问问
107060206
 
不是做hacker软件……
不是不是,别误解我。。。

我是看自己的Service的状态
 
不知道为什么,好多行业都是一代不如一代,程序员竟然也是
 
虽然自己解决了,但是总感觉有点不对,代码如下。有没有错误呀?
unit publicServ;

interface

uses
Windows,SysUtils,WinSvc;
type TServices = class
private
hscManager:SC_HANDLE;
hService:dword;
returnstatus:TServiceStatus;

function OpenEachHandle(PcharHost,ServiceName:PChar):integer;
public
function SeeServStatus(PcharHost,ServiceName:PChar):integer;
function SerStop(PcharHost,ServiceName:PChar):integer;
function SerPause(PcharHost,ServiceName:PChar):integer;
function SerContinue(PcharHost,ServiceName:PChar):integer;
function SerInterrogate(PcharHost,ServiceName:PChar):integer;
function SerShutDown(PcharHost,ServiceName:PChar):integer;
function SerStart(PcharHost,ServiceName:PChar):integer;
procedure CloseSerAll;
end;


implementation
{ TServices }


procedure TServices.CloseSerAll;
begin
CloseServiceHandle(hService);//关闭该Service
CloseServiceHandle(hscmanager);
end;

function TServices.OpenEachHandle(PcharHost,ServiceName:PChar): integer;
begin
Result:=0;
hscManager:=OpenSCManager(PCharHost,nil,SC_MANAGER_ENUMERATE_SERVICE);
if hscmanager < 0 then
begin
//无法打开control manager database
Result:=-1;
Exit;
end;
hService := OpenService(hscmanager,ServiceName,SERVICE_ALL_ACCESS);
if hService <0 then
begin
//无法打开service
Result:=-2;
Exit;
end;

end;
//------------------------------------------------------------------------------
// 函数:SeeServStatus
// 功能:查看Nt下某一服务的状态
// 返回值: <0 为错误
// SERVICE_STOPPED = $00000001;
// SERVICE_START_PENDING = $00000002;
// SERVICE_STOP_PENDING = $00000003;
// SERVICE_RUNNING = $00000004;
// SERVICE_CONTINUE_PENDING = $00000005;
// SERVICE_PAUSE_PENDING = $00000006;
// SERVICE_PAUSED = $00000007;
//------------------------------------------------------------------------------
function TServices.SeeServStatus(PcharHost,ServiceName:PChar):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1;
CloseSerAll;
Exit;
end;

QueryServiceStatus(hService,returnstatus);
Result:=returnstatus.dwCurrentState;
//SERVICE_STOPPED
end;
//------------------------------------------------------------------------------
//命令集
//------------------------------------------------------------------------------

//继续
function TServices.SerContinue(PcharHost,ServiceName:PChar):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_CONTINUE,returnstatus);
CloseSerAll;
end;
//询问
function TServices.SerInterrogate(PcharHost,ServiceName:PChar):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_INTERROGATE,returnstatus);
CloseSerAll;
end;
//暂停
function TServices.SerPause(PcharHost,ServiceName:PChar):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_PAUSE,returnstatus);
CloseSerAll;
end;
//down
function TServices.SerShutDown(PcharHost,ServiceName:PChar):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_SHUTDOWN,returnstatus);
CloseSerAll;
end;
//启动
function TServices.SerStart(PcharHost,ServiceName:PChar):integer;
var arg:PChar;
begin
Arg:=nil;
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1;
CloseSerAll;
Exit;
end;
if not StartService(hService,0,arg) then
Result:=-1;
CloseSerAll;
end;
//停止
function TServices.SerStop(PcharHost,ServiceName:PChar):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_STOP,returnstatus);
CloseSerAll;
end;

end.
 
接受答案了.
 
后退
顶部