(Gast)
n/a Beiträge
|
Re: Dienst installieren + unter anderen Benutzerdaten starte
13. Jul 2007, 09:01
Ich habe schon seit einer Weile das gleiche Problem. Installieren, starten und stoppen mache ich mit folgenden Funktionen:
Delphi-Quellcode:
procedure ServiceInstall;
var
SvcMgr, Svc: HWND;
begin
SvcMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
try
Svc := CreateService(SvcMgr, APPLICATION_NAME, PChar(APPLICATION_NAME), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, PChar(WorkDir + FILE_SERVICE), '', nil, '', nil, '');
finally
CloseServiceHandle(Svc);
end;
CloseServiceHandle(SvcMgr);
end;
function ServiceStart(Machine, ServiceName: String): Boolean;
//******************************************************************************
//* Parameter:
//* Machine: Computername
//* Service: Name des Dienstes
//******************************************************************************
var
h_manager, h_svc: SC_Handle;
ServiceStatus: TServiceStatus;
dwCheckPoint: DWORD;
ServiceArgVectors: PChar;
begin
h_manager := OpenSCManager(PChar(Machine), nil, SC_MANAGER_CONNECT);
if h_manager > 0 then
begin
h_svc := OpenService(h_manager, PChar(ServiceName), SERVICE_START or SERVICE_QUERY_STATUS or SC_MANAGER_ALL_ACCESS);
if h_svc > 0 then
begin
if (StartService(h_svc, 0, ServiceArgVectors)) then { succeeded }
begin
if (QueryServiceStatus(h_svc, ServiceStatus)) then
begin
while (SERVICE_RUNNING <> ServiceStatus.dwCurrentState) do
begin
dwCheckPoint := ServiceStatus.dwCheckPoint;
Sleep(ServiceStatus.dwWaitHint);
if (not QueryServiceStatus(h_svc, ServiceStatus)) then
// couldn't check status
break;
if (ServiceStatus.dwCheckPoint < dwCheckPoint) then
break;
end;
end;
end;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := (SERVICE_RUNNING = ServiceStatus.dwCurrentState);
end;
function ServiceStop(Machine, ServiceName: String): Boolean;
//******************************************************************************
//* Parameter:
//* Machine: Computername
//* Service: Name des Dienstes
//******************************************************************************
var
h_manager, h_svc: SC_Handle;
ServiceStatus: TServiceStatus;
dwCheckPoint: DWORD;
begin
h_manager := OpenSCManager(PChar(Machine), nil, SC_MANAGER_CONNECT);
if h_manager > 0 then
begin
h_svc := OpenService(h_manager, PChar(ServiceName), SERVICE_STOP or SERVICE_QUERY_STATUS);
if h_svc > 0 then
begin
if (ControlService(h_svc, SERVICE_CONTROL_STOP, ServiceStatus)) then
begin
if (QueryServiceStatus(h_svc, ServiceStatus)) then
begin
while (SERVICE_STOPPED <> ServiceStatus.dwCurrentState) do
begin
dwCheckPoint := ServiceStatus.dwCheckPoint;
Sleep(ServiceStatus.dwWaitHint);
if (not QueryServiceStatus(h_svc, ServiceStatus)) then
// couldn't check status
break;
if (ServiceStatus.dwCheckPoint < dwCheckPoint) then
break;
end;
end;
end;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := (SERVICE_STOPPED = ServiceStatus.dwCurrentState);
end;
Bloss, wie ich dem Dienst vor dem Starten einen anderen Benutzer zuweisen muss, habe ich noch nicht heraus gefunden. Ich werde diesen Thread aber noch im Auge behalten.
|
|
Zitat
|