Windows服务程序~ ( 积分: 50 )

  • 主题发起人 主题发起人 午午
  • 开始时间 开始时间

午午

Unregistered / Unconfirmed
GUEST, unregistred user!
请问一下,如果一个windows服务程序想在停止后双击它的可执行文件后又重新启动这个服务项,请问这个功能该如何实现?
 
program TrayScktServer;

uses
SvcMgr,Forms,Windows,Winsvc,Sysutils,
SocketServer in 'SocketServer.pas' {frmScktMain},
Unit1 in 'Unit1.pas' {ServiceTray: TService};

{$R *.RES}
function Installing: Boolean;
begin
Result := FindCmdLineSwitch('INSTALL',['-','/','/'], True) or
FindCmdLineSwitch('UNINSTALL',['-','/','/'], True);
end;

function StartService: 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('ServiceTray'), 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;
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;

begin
if not Installing then
begin
CreateMutex(nil, True, 'TrayScktServer');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, PChar('The Service is already running'), 'ServiceTray', MB_ICONERROR);
Halt;
end;
end;
if Installing or StartService then
begin
SvcMgr.Application.Initialize;
Application.CreateForm(TServiceTray, ServiceTray);
//SvcMgr.Application.CreateForm(TfrmScktMain, frmScktMain);
SvcMgr.Application.Run;
end
else
begin
Forms.Application.ShowMainForm := False;
Forms.Application.Initialize;
Forms.Application.CreateForm(TfrmScktMain, frmScktMain);
frmScktMain.Initialize(False);
Forms.Application.Run;
end;
end.

这是代码,当从服务里启动,出现‘服务没有及时响应启动或控制请求’错误
谁可以帮我看一下
 
来个人啊~~~
 
后退
顶部