如题!
我查过一些资料,也看过MSDN,
有人说是用: 1、使用OpenSCManager得到ControlManager句柄
            2、使用EnumServicesStatus得到你需要停止的设备
            3、ControlService控制你的设备的启动与禁用有人说是用: SetupDiRemoveDevice我用第一种方法写出的程序发现设备列表并不像"设备管理器"的列表,找不到我要操纵的设备,例如显卡
但可以启动/禁止某些服务,例如 Beep, 求教高手如何实现功能类似"设备管理器"!以下是我用第一种方法写的程序,请大家指正:unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WinSVC, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function setDriver( TempFlag : Boolean ) : integer;
  end;var
  Form1: TForm1;implementation{$R *.dfm}function TForm1.setDriver(TempFlag: Boolean): integer;
var
  SC_Handle : THandle;
  EnumSrvStatus : array of TEnumServiceStatus;
  TempSrvStatus : TEnumServiceStatus;
  cbBufSize : Cardinal;
  pcbBytesNeeded : Cardinal;
  lpServicesReturned : Cardinal;
  lpResumeHandle : Cardinal;
  TempLongBool : LongBool;  TempInteger1 : integer;
  TempInteger2 : integer;
begin  SC_Handle := WinSVC.OpenSCManager( nil, nil, SC_MANAGER_ENUMERATE_SERVICE );  lpResumeHandle := 0;
  TempLongBool := WinSVC.EnumServicesStatus(
                                             SC_Handle,
                                             SERVICE_DRIVER,
                                             SERVICE_STATE_ALL,
                                             TempSrvStatus,
                                             sizeof( TEnumServiceStatus ),
                                             pcbBytesNeeded,
                                             lpServicesReturned,
                                             lpResumeHandle
                                            );  if ( ( TempLongBool = False ) and ( GetLastError() = ERROR_MORE_DATA ) ) then
  begin
    cbBufSize := pcbBytesNeeded + sizeof( TEnumServiceStatus );
    setlength( EnumSrvStatus, ( cbBufSize div sizeof( TEnumServiceStatus ) ) + 1 );    TempLongBool := WinSVC.EnumServicesStatus(
                                               SC_Handle,
                                               SERVICE_DRIVER,
                                               SERVICE_STATE_ALL,
                                               EnumSrvStatus[0],
                                               cbBufSize,
                                               pcbBytesNeeded,
                                               lpServicesReturned,
                                               lpResumeHandle
                                            );
    
    for TempInteger1 := 0 to lpServicesReturned - 1 do
    begin
      self.ListBox1.Items.Add( EnumSrvStatus[TempInteger1].lpServiceName );
    end;
      
  end;  WinSVC.CloseServiceHandle( SC_Handle );
  
  Result := 1;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  setDriver( True );
end;procedure TForm1.Button2Click(Sender: TObject);
var
  SC_Handle1 : THandle;
  SC_Handle2 : THandle;
  SrvStatus : TServiceStatus;
  lpServiceArgVectors : PAnsiChar;  TempString1 : string;
begin
  TempString1 := self.ListBox1.Items[self.ListBox1.ItemIndex];
  // TempString1 := 'Alerter';
  ShowMessage( TempString1 );  SC_Handle1 := WinSVC.OpenSCManager( nil, nil, SC_MANAGER_ALL_ACCESS );  SC_Handle2 := WinSVC.OpenService( SC_Handle1, PAnsiChar( TempString1 ), SERVICE_ALL_ACCESS );  // WinSVC.StartService( SC_Handle2, 0, lpServiceArgVectors );
  if ( WinSVC.ControlService( SC_Handle2, SERVICE_CONTROL_STOP, SrvStatus ) ) then ShowMessage( 'Succ' );  if ( GetLastError() = ERROR_ACCESS_DENIED ) then ShowMessage( '1' );
  if ( GetLastError() = ERROR_DEPENDENT_SERVICES_RUNNING ) then ShowMessage( '2' );
  if ( GetLastError() = ERROR_INVALID_HANDLE ) then ShowMessage( '3' );
  if ( GetLastError() = ERROR_INVALID_PARAMETER ) then ShowMessage( '4' );
  if ( GetLastError() = ERROR_INVALID_SERVICE_CONTROL ) then ShowMessage( '5' );
  if ( GetLastError() = ERROR_SERVICE_CANNOT_ACCEPT_CTRL ) then ShowMessage( '6' );
  if ( GetLastError() = ERROR_SERVICE_NOT_ACTIVE ) then ShowMessage( '7' );
  if ( GetLastError() = ERROR_SERVICE_REQUEST_TIMEOUT ) then ShowMessage( '8' );
  if ( GetLastError() = ERROR_SHUTDOWN_IN_PROGRESS ) then ShowMessage( '9' );  WinSVC.CloseServiceHandle( SC_Handle2 );
  WinSVC.CloseServiceHandle( SC_Handle1 );
end;procedure TForm1.Button3Click(Sender: TObject);
begin
  Windows.Beep( 1000, 1000 );
end;end.

解决方案 »

  1.   

    SetupAPI就能Ok
    具体看我在API版的FAQ内容http://lysoft.7u7.net
      

  2.   

    看看ly_liuyang写的,人家可是高手。会有帮助的
      

  3.   

    Demo代码:http://www.yeahware.com/download/eject.zip 
    链接:http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=197585http://lysoft.7u7.net
      

  4.   

    确实是使用 SetupAPI,问题已经解决,谢谢各位的帮助, 谢谢 ly_liuyang(Liu Yang) 的指点!