Страница: 1 |
Страница: 1 |
Вопрос: Список служб
Добавлено: 10.08.07 10:24
Автор вопроса: Hunter2003 | ICQ: 287314254
Народ, подскажите плиз, как можно получить список служб... Или подскажите в каком направлении копать...
Ответы
Всего ответов: 7
Номер ответа: 1
Автор ответа:
VisualPaul
ICQ: 402519798
Вопросов: 12
Ответов: 111
Профиль | | #1
Добавлено: 10.08.07 12:33
What servises?!!!!!!!!!!!!!!!!!!!
Номер ответа: 2
Автор ответа:
Administrator
ICQ: ------------------------------
Вопросов: 6
Ответов: 98
Web-сайт:
Профиль | | #2
Добавлено: 10.08.07 13:20
Каких именно служб: работающих, запущенных, отключенных или всех?
Номер ответа: 3
Автор ответа:
shuffle
Администратор
ICQ: 201502381
Вопросов: 15
Ответов: 737
Профиль | | #3
Добавлено: 10.08.07 14:24
Номер ответа: 4
Автор ответа:
Hunter2003
ICQ: 287314254
Вопросов: 14
Ответов: 72
Профиль | | #4
Добавлено: 10.08.07 20:58
Нужен список всех служб...
Номер ответа: 5
Автор ответа:
Hunter2003
ICQ: 287314254
Вопросов: 14
Ответов: 72
Профиль | | #5
Добавлено: 10.08.07 21:03
Через реестр не годится.., в той ветке кроме зарегистрированных служб полно всякой прочей грязи, которую не понятно как фильтровать...
Номер ответа: 6
Автор ответа:
Sharp
Лидер форума
ICQ: 216865379
Вопросов: 106
Ответов: 9979
Web-сайт:
Профиль | | #6
Добавлено: 15.08.07 03:15
#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];
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;
}
Номер ответа: 7
Автор ответа:
Mr.Smile
ICQ: 427682013
Вопросов: 14
Ответов: 464
Профиль | | #7
Добавлено: 15.08.07 17:49
На VB6:
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2
Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS + SERVICE_WIN32_SHARE_PROCESS
Private Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type
Private Type ENUM_SERVICE_STATUS
lpServiceName As Long
lpDisplayName As Long
ServiceStatus As SERVICE_STATUS
End Type
Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function EnumServicesStatus Lib "advapi32.dll" Alias "EnumServicesStatusA" (ByVal hSCManager As Long, ByVal dwServiceType As Long, ByVal dwServiceState As Long, lpServices As Any, ByVal cbBufSize As Long, pcbBytesNeeded As Long, lpServicesReturned As Long, lpResumeHandle As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (szDest As String, szcSource As Long) As Long
Private Sub Form_Load()
Dim hSCM As Long, lpEnumServiceStatus() As ENUM_SERVICE_STATUS, lngServiceStatusInfoBuffer As Long
Dim strServiceName As String * 250, lngBytesNeeded As Long, lngServicesReturned As Long
Dim hNextUnreadEntry As Long, lngStructsNeeded As Long, lngResult As Long, i As Long
hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ENUMERATE_SERVICE)
If hSCM = 0 Then
MsgBox "OpenSCManager failed. LastDllError = " & CStr(Err.LastDllError)
Exit Sub
End If
hNextUnreadEntry = 0
lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, ByVal &H0, &H0, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
If Not Err.LastDllError = ERROR_MORE_DATA Then
MsgBox "LastDLLError = " & CStr(Err.LastDllError)
Exit Sub
End If
lngStructsNeeded = lngBytesNeeded / Len(lpEnumServiceStatus(0)) + 1
ReDim lpEnumServiceStatus(lngStructsNeeded - 1)
lngServiceStatusInfoBuffer = lngStructsNeeded * Len(lpEnumServiceStatus(0))
hNextUnreadEntry = 0
lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, lpEnumServiceStatus(0), lngServiceStatusInfoBuffer, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
If lngResult = 0 Then
MsgBox "EnumServicesStatus failed. LastDllError = " & CStr(Err.LastDllError)
Exit Sub
End If
Me.AutoRedraw = True
Me.Print "All registered services:" + vbCrLf
For i = 0 To lngServicesReturned - 1
lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpServiceName)
Me.Print StripTerminator(strServiceName) + " - ";
lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpDisplayName)
Me.Print StripTerminator(strServiceName)
Next i
CloseServiceHandle (hSCM)
End Sub
Function StripTerminator(sInput As String) As String
Dim ZeroPos As Integer
ZeroPos = InStr(1, sInput, Chr$(0))
If ZeroPos > 0 Then
StripTerminator = Left$(sInput, ZeroPos - 1)
Else
StripTerminator = sInput
End If
End Function