Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2003.09.22;
Скачать: CL | DM;

Вниз

Запуск и останов службы   Найти похожие ветки 

 
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;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.01 c
1-71628
INTAARI
2003-09-11 14:48
2003.09.22
Как изменить цвет нескольких меток пользуясь свойством Components


1-71604
GreySerg
2003-09-09 16:50
2003.09.22
Как сделать в ListView одну колонку ?


6-71787
rex
2003-07-21 13:04
2003.09.22
Ошибка при использовнии IntraWeb


4-72012
SkiF
2003-07-21 00:48
2003.09.22
Запрещение получение фокуса своему приложению


1-71667
Man
2003-09-10 16:34
2003.09.22
как програмно определить к какому винчестеру относится диск





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
Английский Французский Немецкий Итальянский Португальский Русский Испанский