Форум: "Система";
Текущий архив: 2003.09.22;
Скачать: [xml.tar.bz2];
ВнизЗапуск и останов службы Найти похожие ветки
← →
Alexkav (2003-07-09 12:12) [0]Как можно из своей программы запустить и остановить службу.
← →
wl (2003-07-09 13:08) [1]Может с помощью WinExec вызывать программу net start/stop? или тебе нужны функции WinAPI?
← →
Советчик (2003-07-09 17:15) [2]>Как можно из своей программы запустить и остановить службу
Используйте Service Control Manager.
StartService()/ControlService()
← →
Edward (2003-07-09 17:54) [3]
//Запустить
procedure TMain.acRunServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceArgVectors: LPCTSTR;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
if ServiceStatus( cs_srv_Name) = cs_srv_Paused then
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_PAUSE_CONTINUE);
ControlService( hService, SERVICE_CONTROL_CONTINUE, lpServiceStatus);
end
else
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_START);
StartService( hService, 0, lpServiceArgVectors);
end;
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
//---------------------------------------------------------------
//Остановить
procedure TMain.acStopServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_STOP);
ControlService( hService, SERVICE_CONTROL_STOP, lpServiceStatus);
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
//Состояние службы
function TMain.ServiceStatus(ServiceName: String): String;
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceStatus: _SERVICE_STATUS;
begin
Result:= cs_srv_NoSetup;
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
hService:= OpenService( hSCManager,
PChar( ServiceName),
SERVICE_QUERY_STATUS);
QueryServiceStatus( hService, lpServiceStatus);
Case lpServiceStatus.dwCurrentState of
SERVICE_RUNNING: Result:= cs_srv_Running;
SERVICE_STOPPED: Result:= cs_srv_Stopped;
SERVICE_PAUSED: Result:= cs_srv_Paused;
//Состояния ожидания
SERVICE_START_PENDING: Result:= cs_srv_StartPending;
SERVICE_STOP_PENDING: Result:= cs_srv_StopPending;
SERVICE_CONTINUE_PENDING: Result:= cs_srv_ContinuePending;
SERVICE_PAUSE_PENDING: Result:= cs_srv_PausePending;
end;
finally
CloseServiceHandle( hService);
( hSCManager)
//Запустить
, где
procedure TMain.acRunServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceArgVectors: LPCTSTR;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
if ServiceStatus( cs_srv_Name) = cs_srv_Paused then
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_PAUSE_CONTINUE);
ControlService( hService, SERVICE_CONTROL_CONTINUE, lpServiceStatus);
end
else
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_START);
StartService( hService, 0, lpServiceArgVectors);
end;
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
//---------------------------------------------------------------
//Остановить
procedure TMain.acStopServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_STOP);
ControlService( hService, SERVICE_CONTROL_STOP, lpServiceStatus);
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
//Состояние службы
function TMain.ServiceStatus(ServiceName: String): String;
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceStatus: _SERVICE_STATUS;
begin
Result:= cs_srv_NoSetup;
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
hService:= OpenService( hSCManager,
PChar( ServiceName),
SERVICE_QUERY_STATUS);
QueryServiceStatus( hService, lpServiceStatus);
Case lpServiceStatus.dwCurrentState of
SERVICE_RUNNING: Result:= cs_srv_Running;
SERVICE_STOPPED: Result:= cs_srv_Stopped;
SERVICE_PAUSED: Result:= cs_srv_Paused;
//Состояния ожидания
SERVICE_START_PENDING: Result:= cs_srv_StartPending;
SERVICE_STOP_PENDING: Result:= cs_srv_StopPending;
SERVICE_CONTINUE_PENDING: Result:= cs_srv_ContinuePending;
SERVICE_PAUSE_PENDING: Result:= cs_srv_PausePending;
end;
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
cs_srv_Name = "НазваниеСлужбы";
cs_srv_DisplayName = "Название Службы в диспетчере";
//Состояние службы
cs_srv_NoSetup = "Служба не установлена";
cs_srv_Running = "Запущена";
cs_srv_Stopped = "Остановлена";
cs_srv_Paused = "Приостановлена";
cs_srv_StartPending = "Ожидание запуска";
cs_srv_StopPending = "Ожидание останова";
cs_srv_ContinuePending = "Ожидание продолжения";
cs_srv_PausePending = "Ожидание приостанова";
← →
Edward (2003-07-09 17:58) [4]
//Запустить
procedure TMain.acRunServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceArgVectors: LPCTSTR;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
if ServiceStatus( cs_srv_Name) = cs_srv_Paused then
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_PAUSE_CONTINUE);
ControlService( hService, SERVICE_CONTROL_CONTINUE, lpServiceStatus);
end
else
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_START);
StartService( hService, 0, lpServiceArgVectors);
end;
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
//Остановить
procedure TMain.acStopServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_STOP);
ControlService( hService, SERVICE_CONTROL_STOP, lpServiceStatus);
finally
CloseServiceHandle( hService);
( hSCManager)
//Запустить
procedure TMain.acRunServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceArgVectors: LPCTSTR;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
if ServiceStatus( cs_srv_Name) = cs_srv_Paused then
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_PAUSE_CONTINUE);
ControlService( hService, SERVICE_CONTROL_CONTINUE, lpServiceStatus);
end
else
begin
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_START);
StartService( hService, 0, lpServiceArgVectors);
end;
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
//Остановить
procedure TMain.acStopServiceExecute(Sender: TObject);
var hSCManager: SC_HANDLE;
hService: SC_HANDLE;
lpServiceStatus: _SERVICE_STATUS;
begin
hSCManager:= 0;
hService := 0;
try
hSCManager:= OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS);
hService:= OpenService( hSCManager,
PChar( cs_srv_Name),
SERVICE_STOP);
ControlService( hService, SERVICE_CONTROL_STOP, lpServiceStatus);
finally
CloseServiceHandle( hService);
CloseServiceHandle( hSCManager);
end;
end;
Страницы: 1 вся ветка
Форум: "Система";
Текущий архив: 2003.09.22;
Скачать: [xml.tar.bz2];
Память: 0.47 MB
Время: 0.011 c