TStringList *ListServices(int ServiceType)
{
TStringList *list = new TStringList();
SC_HANDLE schSCManager = OpenSCManager(NULL,NULL, SC_MANAGER_ALL_ACCESS);
LPBYTE lpServices;
DWORD n,nSize,npSize,nServicesReturned,nResumeHandle,dwServiceType;
if ( schSCManager > 0 )
{
nResumeHandle =0;
npSize = sizeof(ENUM_SERVICE_STATUS_PROCESS);
dwServiceType = SERVICE_WIN32 | SERVICE_DRIVER |
SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER |
SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS;
lpServices = LPBYTE(LocalAlloc(LPTR, 30720));
try
{
if (!EnumServicesStatusEx(schSCManager, SC_ENUM_PROCESS_INFO,
dwServiceType, SERVICE_STATE_ALL, lpServices, 30720,
&nSize, &nServicesReturned,&nResumeHandle, NULL))
{
// P_ShowError( '读取服务状态失败,错误: '+IntToStr(GetLastError));
return list;
}
for( n = 0;n< nServicesReturned-1;n++)
{
//Show the name of service
// ListItem := SvcListView.Items.Add;
// ListItem.Caption := (LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.lpServiceName;
// ListItem.SubItems.Add((LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.lpDisplayName);
// ListItem.SubItems.Add(IntToStr((LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.ServiceStatusProcess.dwProcessId));
// ListItem.SubItems.Add(SrvStatus((LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.ServiceStatusProcess.dwCurrentState));
String ss = (LPENUM_SERVICE_STATUS_PROCESS(lpServices))->lpServiceName ;
String ss2 = (LPENUM_SERVICE_STATUS_PROCESS(lpServices))->lpDisplayName ;
list->Add(ss2);
if (ss2 == "wwServices") {
ShowMessage(ss2);
}
lpServices += npSize;
}
return list;
}
__finally
{
// if ( lpServices != NULL)
// LocalFree(int(lpServices));
}
}
}
{
TStringList *list = new TStringList();
SC_HANDLE schSCManager = OpenSCManager(NULL,NULL, SC_MANAGER_ALL_ACCESS);
LPBYTE lpServices;
DWORD n,nSize,npSize,nServicesReturned,nResumeHandle,dwServiceType;
if ( schSCManager > 0 )
{
nResumeHandle =0;
npSize = sizeof(ENUM_SERVICE_STATUS_PROCESS);
dwServiceType = SERVICE_WIN32 | SERVICE_DRIVER |
SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER |
SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS;
lpServices = LPBYTE(LocalAlloc(LPTR, 30720));
try
{
if (!EnumServicesStatusEx(schSCManager, SC_ENUM_PROCESS_INFO,
dwServiceType, SERVICE_STATE_ALL, lpServices, 30720,
&nSize, &nServicesReturned,&nResumeHandle, NULL))
{
// P_ShowError( '读取服务状态失败,错误: '+IntToStr(GetLastError));
return list;
}
for( n = 0;n< nServicesReturned-1;n++)
{
//Show the name of service
// ListItem := SvcListView.Items.Add;
// ListItem.Caption := (LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.lpServiceName;
// ListItem.SubItems.Add((LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.lpDisplayName);
// ListItem.SubItems.Add(IntToStr((LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.ServiceStatusProcess.dwProcessId));
// ListItem.SubItems.Add(SrvStatus((LPENUM_SERVICE_STATUS_PROCESS(lpServices))^.ServiceStatusProcess.dwCurrentState));
String ss = (LPENUM_SERVICE_STATUS_PROCESS(lpServices))->lpServiceName ;
String ss2 = (LPENUM_SERVICE_STATUS_PROCESS(lpServices))->lpDisplayName ;
list->Add(ss2);
if (ss2 == "wwServices") {
ShowMessage(ss2);
}
lpServices += npSize;
}
return list;
}
__finally
{
// if ( lpServices != NULL)
// LocalFree(int(lpServices));
}
}
}
以上代码需引用winsvc.h文件,为C++Builder2010编写,基本就是两个api,但不知为何所有的服务都列出来的,单单就是我用C++Builder2010写的一个服务程序无法找到??