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));
}
}
}
以上代碼需引用winsvc.h檔案,為C++Builder2010編寫,基本就是兩個api,但不知為何所有的服務都列出來的,單單就是我用C++Builder2010寫的一個服務程式無法找到??