A
allceo
Unregistered / Unconfirmed
GUEST, unregistred user!
我想写个服务程序监控服务器状态,可是可以正确注册成服务,但是点启动服务的时候显示,在 本地计算机 无法启动 xxx服务。 错误 1053:服务没有及时响应启动或者控制请求。不知道要怎么搞。下面是代码,哪位高手帮我看一下。
SysInfo.dpr
program SysInfo;
uses
Forms,
SysUtils,
Windows,
SvcMgr,
WinSvc,
Messages,
Monitor in 'Monitor.pas' {H_Monitor2008};
{$R *.res}
function Installing: Boolean;
begin
Result := FindCmdLineSwitch('INSTALL',['-','/','/'], True) or
FindCmdLineSwitch('UNINSTALL',['-','/','/'], True);
end;
function IfStartService: Boolean;
var
Mgr, Svc: Integer;
UserName, ServiceStartName: string;
Config: Pointer;
Size: DWord;
begin
Result := False;
Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if Mgr <> 0 then
begin
Svc := OpenService(Mgr, PChar(Server_Name), SERVICE_ALL_ACCESS);
Result := Svc <> 0;
if Result then
begin
QueryServiceConfig(Svc, nil, 0, Size);
Config := AllocMem(Size);
try
QueryServiceConfig(Svc, Config, Size, Size);
ServiceStartName := PQueryServiceConfig(Config)^.lpServiceStartName;
//MessageBox(0, PChar(Server_Name), 'Look', MB_ICONERROR);
if CompareText(ServiceStartName, 'LocalSystem') = 0 then
ServiceStartName := 'SYSTEM';
finally
Dispose(Config);
end;
CloseServiceHandle(Svc);
end;
CloseServiceHandle(Mgr);
end;
if Result then
begin
Size := 256;
SetLength(UserName, Size);
GetUserName(PChar(UserName), Size);
SetLength(UserName, StrLen(PChar(UserName)));
Result := CompareText(UserName, ServiceStartName) = 0;
end;
end;
var
Temp:string;
I:integer;
MutexHandle:Thandle;
ds: TCopyDataStruct;
hd: THandle;
begin
if paramstr(1) <>'' then
begin
if (CompareText(paramstr(1),'-k')=0) then
begin
Mutexhandle:=windows.CreateMutex(nil,False,'Hmn_SystemMonitor_20080608');
//Delay(3000);
end;
end else begin
Mutexhandle:=windows.CreateMutex(nil,False,'Hmn_SystemMonitor_20080608');
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutexhandle = 0) then
begin
Closehandle(Mutexhandle);
Halt;
Exit;
end;
end;
H_Monitor2008.ReadMe;
if Installing or IfStartService then
begin
Application.Initialize;
//SysMonitor := TSysMonitor.CreateNew(SvcMgr.Application,0);
Application.CreateForm(TH_Monitor2008, H_Monitor2008);
Application.Run;
end else begin
Application.Initialize;
Application.CreateForm(TH_Monitor2008, H_Monitor2008);
Application.Run;
end;
end.
Monitor.pas
unit Monitor;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
SvcMgr, Dialogs, WinntService, IniFiles;
type
TH_Monitor2008 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
procedure ReadMe;
end;
TSysMonitor = class(TService)
protected
procedure Start(Sender: TService; var Started: Boolean);
procedure Stop(Sender: TService; var Stopped: Boolean);
public
function GetServiceController: TServiceController; override;
constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
end;
var
H_Monitor2008: TH_Monitor2008;
SysMonitor:TSysMonitor;
Server_Name,Display_Name,Description_txt,Monitor_Interval,Server_IP,Log_File:string;
implementation
{$R *.dfm}
constructor TSysMonitor.CreateNew(AOwner: TComponent; Dummy: Integer);
begin
inherited;
AllowPause := False;
Interactive := True;
DisplayName := Display_Name;
Name := Server_Name;
OnStart := Start;
OnStop := Stop;
end;
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
SysMonitor.Controller(CtrlCode);
end;
function TSysMonitor.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TSysMonitor.Start(Sender: TService; var Started: Boolean);
begin
Started := True;
end;
procedure TSysMonitor.Stop(Sender: TService; var Stopped: Boolean);
begin
Stopped := True;
end;
//读取配置信息
procedure TH_Monitor2008.ReadMe;
var
myinifile: TIniFile;
Filename:string;
begin
//读取软件设置
Filename:=ExtractFilePath(Paramstr(0))+'setup.ini';
myinifile:=Tinifile.Create(filename);
try
Server_Name:=myinifile.ReadString('system','Server_Name','MonitorSystem');
Display_Name:=myinifile.ReadString('system','Display_Name','MS Service');
Description_txt:=myinifile.ReadString('system','Description_txt','监控服务器性能的服务。');
Monitor_Interval:=myinifile.ReadString('system','Monitor_Interval','30');
Server_IP:=myinifile.ReadString('system','Server_IP','xxxxx');
Log_File:=myinifile.ReadString('system','Log_File','Log.txt');
finally
myinifile.Free;
end;
try
DelService(Server_Name);
except
end;
Sleep(1000);
try
InstallService(Server_Name,Display_Name,PChar(ParamStr(0))+' -k',Description_txt);
except
end;
end;
end.
SysInfo.dpr
program SysInfo;
uses
Forms,
SysUtils,
Windows,
SvcMgr,
WinSvc,
Messages,
Monitor in 'Monitor.pas' {H_Monitor2008};
{$R *.res}
function Installing: Boolean;
begin
Result := FindCmdLineSwitch('INSTALL',['-','/','/'], True) or
FindCmdLineSwitch('UNINSTALL',['-','/','/'], True);
end;
function IfStartService: Boolean;
var
Mgr, Svc: Integer;
UserName, ServiceStartName: string;
Config: Pointer;
Size: DWord;
begin
Result := False;
Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if Mgr <> 0 then
begin
Svc := OpenService(Mgr, PChar(Server_Name), SERVICE_ALL_ACCESS);
Result := Svc <> 0;
if Result then
begin
QueryServiceConfig(Svc, nil, 0, Size);
Config := AllocMem(Size);
try
QueryServiceConfig(Svc, Config, Size, Size);
ServiceStartName := PQueryServiceConfig(Config)^.lpServiceStartName;
//MessageBox(0, PChar(Server_Name), 'Look', MB_ICONERROR);
if CompareText(ServiceStartName, 'LocalSystem') = 0 then
ServiceStartName := 'SYSTEM';
finally
Dispose(Config);
end;
CloseServiceHandle(Svc);
end;
CloseServiceHandle(Mgr);
end;
if Result then
begin
Size := 256;
SetLength(UserName, Size);
GetUserName(PChar(UserName), Size);
SetLength(UserName, StrLen(PChar(UserName)));
Result := CompareText(UserName, ServiceStartName) = 0;
end;
end;
var
Temp:string;
I:integer;
MutexHandle:Thandle;
ds: TCopyDataStruct;
hd: THandle;
begin
if paramstr(1) <>'' then
begin
if (CompareText(paramstr(1),'-k')=0) then
begin
Mutexhandle:=windows.CreateMutex(nil,False,'Hmn_SystemMonitor_20080608');
//Delay(3000);
end;
end else begin
Mutexhandle:=windows.CreateMutex(nil,False,'Hmn_SystemMonitor_20080608');
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutexhandle = 0) then
begin
Closehandle(Mutexhandle);
Halt;
Exit;
end;
end;
H_Monitor2008.ReadMe;
if Installing or IfStartService then
begin
Application.Initialize;
//SysMonitor := TSysMonitor.CreateNew(SvcMgr.Application,0);
Application.CreateForm(TH_Monitor2008, H_Monitor2008);
Application.Run;
end else begin
Application.Initialize;
Application.CreateForm(TH_Monitor2008, H_Monitor2008);
Application.Run;
end;
end.
Monitor.pas
unit Monitor;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
SvcMgr, Dialogs, WinntService, IniFiles;
type
TH_Monitor2008 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
procedure ReadMe;
end;
TSysMonitor = class(TService)
protected
procedure Start(Sender: TService; var Started: Boolean);
procedure Stop(Sender: TService; var Stopped: Boolean);
public
function GetServiceController: TServiceController; override;
constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
end;
var
H_Monitor2008: TH_Monitor2008;
SysMonitor:TSysMonitor;
Server_Name,Display_Name,Description_txt,Monitor_Interval,Server_IP,Log_File:string;
implementation
{$R *.dfm}
constructor TSysMonitor.CreateNew(AOwner: TComponent; Dummy: Integer);
begin
inherited;
AllowPause := False;
Interactive := True;
DisplayName := Display_Name;
Name := Server_Name;
OnStart := Start;
OnStop := Stop;
end;
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
SysMonitor.Controller(CtrlCode);
end;
function TSysMonitor.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TSysMonitor.Start(Sender: TService; var Started: Boolean);
begin
Started := True;
end;
procedure TSysMonitor.Stop(Sender: TService; var Stopped: Boolean);
begin
Stopped := True;
end;
//读取配置信息
procedure TH_Monitor2008.ReadMe;
var
myinifile: TIniFile;
Filename:string;
begin
//读取软件设置
Filename:=ExtractFilePath(Paramstr(0))+'setup.ini';
myinifile:=Tinifile.Create(filename);
try
Server_Name:=myinifile.ReadString('system','Server_Name','MonitorSystem');
Display_Name:=myinifile.ReadString('system','Display_Name','MS Service');
Description_txt:=myinifile.ReadString('system','Description_txt','监控服务器性能的服务。');
Monitor_Interval:=myinifile.ReadString('system','Monitor_Interval','30');
Server_IP:=myinifile.ReadString('system','Server_IP','xxxxx');
Log_File:=myinifile.ReadString('system','Log_File','Log.txt');
finally
myinifile.Free;
end;
try
DelService(Server_Name);
except
end;
Sleep(1000);
try
InstallService(Server_Name,Display_Name,PChar(ParamStr(0))+' -k',Description_txt);
except
end;
end;
end.