--------------------------------------------------------------------------------
windows 系统服务
--------------------------------------------------------------------------------
function StartService(AServName: string): Boolean; //use WinSvc
var
SCManager, hService: SC_HANDLE;
lpServiceArgVectors: PChar;
begin
SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
Result := SCManager <> 0;
if Result then
try
hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);
Result := hService <> 0;
if (hService = 0) and (GetLastError = ERROR_SERVICE_DOES_NOT_EXIST) then
Exception.Create('The specified service does not exist');
if hService <> 0 then
try
lpServiceArgVectors := nil;
Result := WinSvc.StartService(hService, 0, PChar(lpServiceArgVectors));
if not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then
Result := True;
finally
CloseServiceHandle(hService);
end;
finally
CloseServiceHandle(SCManager);
end;
end;
function StopService(AServName: string): Boolean;
var
SCManager, hService: SC_HANDLE;
SvcStatus: TServiceStatus;
begin
SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
Result := SCManager <> 0;
if Result then
try
hService := OpenService(SCManager, PChar(AServName), SERVICE_ALL_ACCESS);
Result := hService <> 0;
if Result then
try //停止并卸载服务;
Result := ControlService(hService, SERVICE_CONTROL_STOP, SvcStatus);
//删除服务,这一句可以不要;
// DeleteService(hService);
finally
CloseServiceHandle(hService);
end;
finally
CloseServiceHandle(SCManager);
end;
end;
///////////////////////////////////////////////////////////////////////////////////
总不能让用户自己启动scktsrvr.exe吧?将其安装为NT服务似乎也不是很好的办法,我想最好就是将它绑定到应用服务器了,请问各位,我该如何做呢?一定送分!
---------------------------------------------------------------
---------------------------------------------------------------
可以将你的App Server加到ScktSrvr那个程序中,或ScktSrvr加到你的程序中,那个程序在VCL目录下,不管怎么,第一次还是得安装成NT服务,不然它自动会加?
---------------------------------------------------------------
---------------------------------------------------------------
把Scktsrvr加到启动程序中(我就是这样做的);
---------------------------------------------------------------
---------------------------------------------------------------
把Scktsrvr加到启动程序中
---------------------------------------------------------------
把它加启动程序中?不好吧。
该程序是NT服务型的,只要运行一次,以后就自动运行了。所以没有必要让它加到启动组中每次再运行一次。
可以试试以下两种方法:
1.在发布应用服务器时,把Scktsrvr一同发布,在应用服务器执行的开始,先执行Scktsrvr,第一次执行后,在注册表中写一个标志,表示Scktsrvr已经运行。以后再运行时首先检查这个标志,如已运行就不再执行了。
2.在安装盘中做呀。制作安装盘的工具大多都有执行外壳命令的功能。安装应用服务器后让安装程序执行一下Scktsrvr就可以了!
---------------------------------------------------------------
---------------------------------------------------------------
Scktsrvr.exe加到启动程序中