26. oktober 2001 - 08:26
Der er
2 kommentarer og
3 løsninger
kommunikation med service
Hvordan kommunikerer jeg med en service i nt/2000 ?
Jeg har lavet en service (TServiceApplication) og spørgsmålet er så: Hvordan styrer jeg denne service fra et andet program ? Primært start/stop.
26. oktober 2001 - 09:18
#1
Det er ikke den optimale løsning, men det virker
Dette eks. stopper/starter servicen alerter
procedure TForm1.Button1Click(Sender: TObject);
begin
// stop af service
WinExec(\'net stop alerter\', SW_SHOWNORMAL);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// start af service
WinExec(\'net start alerter\', SW_SHOWNORMAL);
end;
Stoney
26. oktober 2001 - 09:57
#2
stoney >> som du selv skriver er det en \"nu-er-jeg-ligeglad-det-her-skal-bare-virke-nu-løsning\" - MEN DET VIRKER !!!
:-)
(jeg lader lige spørgsmålet stå åben for andre forslag, men der er point på vej)
30. oktober 2001 - 15:02
#3
Hej
Som nævnt er der ingen tvivl om at stoney\'s fremgangsmåde virker. Men skal det gøres ordentligt (læs programmatisk) så er her hvordan man gør. Denne kode er \"off the top of my head\" og er ikke garanteret at virke. Slå selv de respektive funktioner efter, for yderligere information.
uses
Windows, WinSvc;
procedure ServiceKontrol(Start : boolean);
var
hSCM : SC_HANDLE;
hSvc : SC_HANDLE;
st : SERVICE_STATUS;
pArgs : pchar;
bResult : boolean;
begin
hSCM:= 0;
hSvc:= 0;
FillChar(st, SizeOf(st), 0);
pArgs:= nil;
try
// open the Service Control Manager (SCM)
hSCM:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if hSCM = 0 then begin
raise Exception.Create(\'Error opening Service Control Manager\');
end;
// open the service we want to control (in this case \'alerter\')
hSvc:= OpenService(hSCM, \'alerter\', GENERIX_EXECUTE);
if hSvc = 0 then begin
raise Exception.Create(\'Error opening service \"alerter\"\');
end;
if Start then ebgin
// start the service. pArgs can be used to send text arguments to the service
bResult:= StartService(hSvc, 0, pArgs);
if not bResult then begin
raise Exception.Create(\'Could not start service \"alerter\"\');
end;
end
else begin
// Control the service. In this example we want to stop it!
// You could use other SERVICE_CONTROL_* commands
bResult:= ControlService(hSvc, SERVICE_CONTROL_STOP, st);
if not bResult then begin
raise Exception.Create(\'Could not stop service \"alerter\"\');
end;
end;
finally
// now it is time to clean up!
if hSvc <> 0 then begin
CloseServiceHandle(hSvc);
end;
if hSCM <> 0 then begin
CloseServiceHandle(hSCM);
end;
end;
end;
31. oktober 2001 - 15:47
#4
Hej - her er et par nyttige funktioner til at styre services med :
//function to start a service running on a local or remote machine.
//aMachine is UNC path or local machine if left empty
function TForm1.ServiceStart(aMachine, aServiceName : string ) : boolean;
var
h_manager,h_svc: SC_Handle;
svc_status: TServiceStatus;
Temp: PChar;
dwCheckPoint: DWord;
intCount : integer;
begin
svc_status.dwCurrentState := 1;
h_manager := OpenSCManager(PChar(aMachine), Nil, SC_MANAGER_CONNECT);
if h_manager > 0 then
begin
h_svc := OpenService(h_manager, PChar(aServiceName),SERVICE_START or SERVICE_QUERY_STATUS);
if h_svc > 0 then
begin
temp := nil;
if (StartService(h_svc,0,temp)) then
if (QueryServiceStatus(h_svc,svc_status)) then
begin
intCount := 0;
while (SERVICE_RUNNING <> svc_status.dwCurrentState) do
begin
dwCheckPoint := svc_status.dwCheckPoint;
Sleep(2000); //(svc_status.dwWaitHint);
if (not QueryServiceStatus(h_svc,svc_status)) then
break;
if (svc_status.dwCheckPoint < dwCheckPoint) then
begin
// QueryServiceStatus didn\'t increment dwCheckPoint
break;
end;
//timeout test
inc(intCount);
if intCount > 30 then
break;
end;
end;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := SERVICE_RUNNING = svc_status.dwCurrentState;
end;
//function to stop a service running on a local or remote machine.
//aMachine is UNC path or local machine if left empty
function TForm1.ServiceStop(aMachine,aServiceName : string ) : boolean;
var
h_manager,h_svc : SC_Handle;
svc_status : TServiceStatus;
dwCheckPoint : DWord;
intCount : integer;
begin
h_manager:=OpenSCManager(PChar(aMachine),nil, SC_MANAGER_CONNECT);
if h_manager > 0 then
begin
h_svc := OpenService(h_manager,PChar(aServiceName), SERVICE_STOP or SERVICE_QUERY_STATUS);
if h_svc > 0 then
begin
if(ControlService(h_svc,SERVICE_CONTROL_STOP, svc_status))then
begin
if(QueryServiceStatus(h_svc,svc_status))then
begin
intCount := 0;
while(SERVICE_STOPPED <> svc_status.dwCurrentState)do
begin
dwCheckPoint := svc_status.dwCheckPoint;
Sleep(2000); //(svc_status.dwWaitHint);
if(not QueryServiceStatus(h_svc,svc_status))then
begin
// couldn\'t check status
break;
end;
if(svc_status.dwCheckPoint < dwCheckPoint)then
break;
inc(intCount);
if intCount > 30 then
break;
end;
end;
end;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := SERVICE_STOPPED = svc_status.dwCurrentState;
end;
//function to retrieve the status of a service running on a local or remote machine.
//aMachine is UNC path or local machine if left empty
function TForm1.ServiceGetStatus(sMachine, sService: string ): DWord;
var
h_manager, h_svc: SC_Handle;
service_status : TServiceStatus;
hStat : DWord;
begin
hStat := 1;
h_manager := OpenSCManager(PChar(sMachine) ,Nil, SC_MANAGER_CONNECT);
if h_manager > 0 then
begin
h_svc := OpenService(h_manager,PChar(sService), SERVICE_QUERY_STATUS);
if h_svc > 0 then
begin
if(QueryServiceStatus(h_svc, service_status)) then
hStat := service_status.dwCurrentState;
CloseServiceHandle(h_svc);
end;
CloseServiceHandle(h_manager);
end;
Result := hStat;
end;
31. oktober 2001 - 15:48
#5
mange tak...
har ikke lige haft tid til at teste noget endnu - vender snarligt tilbage :-)
Kurser inden for grundlæggende programmering