unit Unit1;interfaceuses
  WinSvc,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
type
  TESS = array[0..4096] of TEnumServiceStatus;
  PESS = ^TESS;
var
  pSvc: PESS;
  hSCManager: SC_HANDLE;
  nRetByte, nRetNum, nRetResume: DWORD;
  i: Integer;
begin
  hSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if hSCManager <= 0  then
    Exit;  nRetResume := 0;  New(pSvc);
  EnumServicesStatus(hSCManager, SERVICE_WIN32, SERVICE_STATE_ALL, pSvc^[0], SizeOf(pSvc^),
                    nRetByte, nRetNum, nRetResume);  for i := 0 to nRetNum - 1 do
  begin
    ListBox1.Items.Add(pSvc^[i].lpDisplayName);
  end;  Dispose(pSvc);
  CloseServiceHandle(hSCManager);
end;end.
我看到别人的代码.但是此方法是加入到listbox   我需要加入到listveiw  不光要有服务名.还要有相关的描述.状态等.希望高手给个例子.

解决方案 »

  1.   

    procedure  TForm1.Button1Click(Sender: TObject);
    type 
      TESS = array[0..4096] of TEnumServiceStatus;
      PESS = ^TESS;
    var            
      ListItem:TListItem;
      pSvc: PESS;
      hSCManager: SC_HANDLE;
      nRetByte, nRetNum, nRetResume: DWORD; 
      i: Integer; 
    begin 
      hSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
      if hSCManager <= 0  then
        Exit;   nRetResume := 0;   New(pSvc); 
      EnumServicesStatus(hSCManager, SERVICE_WIN32, SERVICE_STATE_ALL, pSvc^[0], SizeOf(pSvc^), 
                        nRetByte, nRetNum, nRetResume); 
      ListView1.Clear;
      for i := 0 to nRetNum - 1 do 
      begin
        ListItem := ListView1.Items.Add;
        ListItem.Caption := pSvc^[i].lpDisplayName;
        ListItem.SubItems.Add(pSvc^[i].lpServiceName);
        if pSvc^[i].ServiceStatus.dwCurrentState = SERVICE_STOPPED then
          ListItem.SubItems.Add('停止状态')
        else if pSvc^[i].ServiceStatus.dwCurrentState = SERVICE_RUNNING then
          ListItem.SubItems.Add('运行状态')    
        else if pSvc^[i].ServiceStatus.dwCurrentState = SERVICE_PAUSED then
          ListItem.SubItems.Add('暂停状态')
        else
          ListItem.SubItems.Add('真正转变状态');
        //ListItem.ImageIndex := 1;
      end;
      Dispose(pSvc); 
      CloseServiceHandle(hSCManager); 
    end; 
      

  2.   

    楼主这样可不行哦,看到人家加到LISTBOX里,自己改到LISTVIEW里都要发帖问,别人给你答案可能是害了你,因为独立思考对程序员来说很重要