Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Система";
Текущий архив: 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
1-71583
Alek Aaz
2003-09-09 13:33
2003.09.22
Печать на принтер


14-71825
jack128
2003-09-02 10:38
2003.09.22
Именинники 2 сентября


7-71981
KILLER_ABV
2003-07-09 15:33
2003.09.22
Мыш и клава


8-71767
landy
2003-05-22 13:59
2003.09.22
text-to-speech engine


14-71895
Developerr
2003-09-02 18:37
2003.09.22
Кто-нибудь использует F1Book версии 6.0 и выше?





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский