Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - Общий форум

Страница: 1 |

 

  Вопрос: службы в Windows XP Добавлено: 25.10.06 00:41  

Автор вопроса:  ZoomerSD | ICQ: 148640473 
Доброго всем дня!
У меня возник вопрос относительно служб в ХР.
как можно программно прочитать список всех служб, и остановить/запустить какую либо из них?

Ответить

  Ответы Всего ответов: 4  

Номер ответа: 1
Автор ответа:
 HACKER


 

Разработчик Offline Client

Вопросов: 236
Ответов: 8362
 Профиль | | #1 Добавлено: 25.10.06 01:24
Shell "net start имя_службы"
Shell "net stop имя_службы"

Можно через WMI
Можно через API

Можно сходить в гугл...

Ответить

Номер ответа: 2
Автор ответа:
 vito



Разработчик Offline Client

Вопросов: 23
Ответов: 879
 Web-сайт: softvito.narod2.ru
 Профиль | | #2
Добавлено: 25.10.06 01:31
Можно через .NET.:)

Класс ServiceController. Он все это умеет.

Ответить

Номер ответа: 3
Автор ответа:
 Sharp


Лидер форума

ICQ: 216865379 

Вопросов: 106
Ответов: 9979
 Web-сайт: sharpc.livejournal.com
 Профиль | | #3
Добавлено: 25.10.06 22:26
#include <windows.h>
#include <cstdio>
#include <vector>
#include <string>
#include <conio.h>

using namespace std;


class ServiceController {
private:
SC_HANDLE hSC;

public:
ServiceController(LPCTSTR MachineName = NULL, DWORD DesiredAccess = SC_MANAGER_ALL_ACCESS){
hSC = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSC == NULL){
SCHandleTest(GetLastError());
}
}

~ServiceController(){
CloseServiceHandle(hSC);
}

vector<ENUM_SERVICE_STATUS> EnumServices(DWORD SvcState = SERVICE_STATE_ALL){
vector<ENUM_SERVICE_STATUS> res;
DWORD svcRet, Bytes;

EnumServicesStatus(hSC, SERVICE_DRIVER | SERVICE_WIN32, SvcState, NULL, 0, &Bytes, &svcRet, 0);
ENUM_SERVICE_STATUS *pESS;
pESS = new ENUM_SERVICE_STATUS [Bytes / sizeof(ENUM_SERVICE_STATUS)];

EnumServicesStatus(hSC, SERVICE_DRIVER | SERVICE_WIN32, SvcState, pESS, Bytes, &Bytes, &svcRet, 0);

for(size_t i = 0; i < svcRet; i++){
res.push_back(pESS[i];);
}

return res;
}


string FmtEnum(ENUM_SERVICE_STATUS ess){
string res = "";
char buff[1024];
/*
SERVICE_FILE_SYSTEM_DRIVER The service is a file system driver.
SERVICE_KERNEL_DRIVER The service is a device driver.
SERVICE_WIN32_OWN_PROCESS The service runs in its own process.
SERVICE_WIN32_SHARE_PROCESS The service shares a process with other services.
If the service type is either SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS,
and the service is running in the context of the LocalSystem account,
the following type may also be specified.
SERVICE_INTERACTIVE_PROCESS The service can interact with the desktop.
*/

/*
SERVICE_CONTINUE_PENDING The service continue is pending.
SERVICE_PAUSE_PENDING The service pause is pending.
SERVICE_PAUSED The service is paused.
SERVICE_RUNNING The service is running.
SERVICE_START_PENDING The service is starting.
SERVICE_STOP_PENDING The service is stopping.
SERVICE_STOPPED The service is not running.
*/

/*
SERVICE_ACCEPT_NETBINDCHANGE The service is a network component that can accept changes in its binding without being stopped and restarted.
This control code allows the service to receive SERVICE_CONTROL_NETBINDADD, SERVICE_CONTROL_NETBINDREMOVE, SERVICE_CONTROL_NETBINDENABLE, and SERVICE_CONTROL_NETBINDDISABLE notifications.

Windows NT:  This value is not supported.
SERVICE_ACCEPT_PARAMCHANGE The service can reread its startup parameters without being stopped and restarted.
This control code allows the service to receive SERVICE_CONTROL_PARAMCHANGE notifications.

Windows NT:  This value is not supported.
SERVICE_ACCEPT_PAUSE_CONTINUE The service can be paused and continued.
This control code allows the service to receive SERVICE_CONTROL_PAUSE and SERVICE_CONTROL_CONTINUE notifications.

SERVICE_ACCEPT_SHUTDOWN The service is notified when system shutdown occurs.
This control code allows the service to receive SERVICE_CONTROL_SHUTDOWN notifications. Note that ControlService cannot send this notification; only the system can send it.

SERVICE_ACCEPT_STOP The service can be stopped.
This control code allows the service to receive SERVICE_CONTROL_STOP notifications.
This value can also contain the following extended control codes, which are supported only by HandlerEx.

Control code Meaning
SERVICE_ACCEPT_HARDWAREPROFILECHANGE The service is notified when the computer's hardware profile has changed. This enables the system to send SERVICE_CONTROL_HARDWAREPROFILECHANGE notifications to the service.
Windows NT:  This value is not supported.
SERVICE_ACCEPT_POWEREVENT The service is notified when the computer's power status has changed. This enables the system to send SERVICE_CONTROL_POWEREVENT notifications to the service.
Windows NT:  This value is not supported.
SERVICE_ACCEPT_SESSIONCHANGE The service is notified when the computer's session status has changed. This enables the system to send SERVICE_CONTROL_SESSIONCHANGE notifications to the service.
Windows 2000/NT:  This value is not supported.
*/

sprintf(buff, "[%s] [%s] SvcType: %d; CurrState: %d; CtrlsAccepted: %d; WaitHint: %d ms",
ess.lpDisplayName, ess.lpServiceName,
ess.ServiceStatus.dwServiceType, ess.ServiceStatus.dwCurrentState,
ess.ServiceStatus.dwControlsAccepted, ess.ServiceStatus.dwWaitHint);

res.append(buff);

return res;
}


private:
void SCHandleTest(long retCode){
if(retCode == ERROR_ACCESS_DENIED){
throw "[ServiceController::OpenSCManager] Доступ запрещен";
} else if(retCode == ERROR_DATABASE_DOES_NOT_EXIST){
throw "[ServiceController::OpenSCManager] Запрошенная база данных не существует";
} else if(retCode == ERROR_INVALID_PARAMETER){
throw "[ServiceController::OpenSCManager] Неверно указаны параметры";
} else{
throw "[ServiceController::OpenSCManager] Неизвестная ошибка";
}
}
};


int main(){
ServiceController sc;
vector<ENUM_SERVICE_STATUS> res = sc.EnumServices();
for(size_t i = 0; i < res.size(); i++){
printf("%s\n", (sc.FmtEnum(res[i];)).c_str());
}

getch();
return 0;
}

Ответить

Номер ответа: 4
Автор ответа:
 Bombardier



ICQ: 42305746 

Вопросов: 2
Ответов: 67
 Web-сайт: alexander.tsioka.ru
 Профиль | | #4
Добавлено: 26.10.06 11:25
Public Function ServiceControl(Name As String, Action As Byte, Optional Computer As String = "", Optional StartMode As String = "Manual";) As Long
Dim wbemMService As SWbemObject
Dim wbemObject As SWbemObject
Dim wbemService As SWbemServices
Dim wbemLocator As SWbemLocator
    APP_NameSpace = "root\cimv2"
    APP_UserName = ""
    APP_Password = ""
    Set wbemLocator = CreateObject("WbemScripting.SWbemLocator";)
    Set wbemService = wbemLocator.ConnectServer(Computer, APP_NameSpace, APP_UserName, APP_Password)
    wbemService.Security_.ImpersonationLevel = 3
    Set wbemMService = wbemService.Get("Win32_Service.Name='" & Name & "'", 0)
    Select Case Action
        Case 1
            wbemMService.StartService
        Case 2
            wbemMService.StopService
        Case 3
            wbemMService.PauseService
        Case 4
            wbemMService.ResumeService
        Case 5
            wbemMService.ChangeStartMode (StartMode)
    End Select
    Set wbemObject = Nothing
    Set wbemMService = Nothing
    Set wbemService = Nothing
    Set wbemLocator = Nothing
End Function

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам