有谁做过这样的程序,得到当前的系统硬件设备状态,然后控制它的
启停?
通过enum出来吗?api函数还是COM接口?

解决方案 »

  1.   

    我做过WDM驱动,我知道一个方法,就是ddk中有一个函数,它可以列出所有的设备并查找你需要的设备,如果找到就返回1否则返回0。
    你觉得有用吗?但是要装ddk
      

  2.   

    利 用Windows API 函 数 和 注 册 表 获 取 系 统 信 息 
    ---- 在 开 发 应 用 程 序 时, 增 加 一 项 显 示 计 算 机 系 统 信 息 的 功 能, 例 如 已 安 装 的 软 盘、 硬 盘、 光 驱、 网 络 驱 动 器, 硬 盘 的 容 量 和 剩 余 空 间, 显 示 器 分 辨 率、 键 盘 类 型、 鼠 标 的 键 数, 内 存 大 小、CPU 类 型,Windows 的 版 本 号、 产 品 标 识、 注 册 用 户 单 位 名 和 用 户 名、 当 前 操 作 用 户 名 等( 见 运 行 图 示), 当 然 还 有 更 多 的 信 息, 这 样 会 使 你 的 程 序 更 友 好。 其 实, 有 很 多 应 用 程 序 就 是 这 样 做 的。 ---- 通 过 调 用Windows 的API 函 数 和 访 问Windows 注 册 表, 可 以 获 取 大 量 的 系 统 信 息。Delphi 为 绝 大 多 数Windows API 函 数 提 供 了 调 用 接 口( 可 参 见DELPHI3\SOURCE\RTL\WIN\windows.pas 文 件), 并 提 供 了 一 个 功 能 全 面 的TRegistry 类, 使 我 们 可 以 方 便 的 调 用Windows API 函 数 和 访 问 注 册 表, 例 如: ---- 1、function GetDriveType(lpRootPathName: PChar): UINT; 返 回 指 定 驱 动 器 的 类 型。 ---- 2、function GetDiskFreeSpace(lpRootPathName: PChar; ---- var lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters: DWORD): BOOL; 返 回 指 定 驱 动 器 的 总 簇 数、 剩 余 簇 数 及 每 簇 扇 区 数、 每 扇 区 字 节 数, 从 而 可 以 计 算 出 总 容 量 和 剩 余 空 间。 ---- 3、function GetSystemMetrics(SM_CXSCREEN或 SM_CYSCREEN): Integer; 返 回 显 示 器 分 辨 率。 ---- 4、function GetSystemMetrics(SM_CMOUSEBUTTONS): Integer; 返 回 鼠 标 的 按 键 数 目。 ---- 5、 在windows 注 册 表 的HKEY_LOCAL_MACHINE\ software\microsoft\windows\currentversion \RegisteredOwner 主 键 下 存 放 着Windows 安 装 时 输 入 的 用 户 名, 可 用 以 下 语 句 读 取。 myreg:=Tregistry.Create; 
    //必须在程序单元的uses部分加入Registry
       myreg.RootKey:=HKEY_LOCAL_MACHINE;
       if myreg.openkey('software\microsoft
       \windows\currentversion',false) then
    memo1.lines.add(' 注册用户名: 
    '+myreg.readstring('RegisteredOwner'));
       myreg.closekey;---- 以 上 仅 举 几 例, 获 取 其 他 一 些 信 息 的 方 法 与 此 类 似, 详 见 源 程 序。 ---- 本 程 序 在Pwin95、Delphi3 下 调 试 通 过。 ---- 附: 源 程 序 清 单。 unit Unit1;
    interface
    uses
      Windows, Messages, SysUtils, Classes,
      Graphics, Controls, Forms, Dialogs,
      StdCtrls,Registry;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    {$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var i,x,y:integer;
    ch:char;
    //驱动器字符'A'~'Z'
        buffer:string;
    cpuinfo:TSYSTEMINFO;
    //存放系统信息的记录类型,在Windows.pas中查到详细内容。
    meminfo:TMemoryStatus;
     //存放系统内存信息的记录类型。
    computername,username:pchar; 
    //计算机名称、用户名
    spc,bps,nofc,tnoc:longint;
     //用于存放磁盘容量的变量
    myreg:Tregistry; 
     //用于访问注册表的TRegistry变量begin
      memo1.Lines.Clear;  for ch:='A' to 'Z' do begin
        i:=getdrivetype(pchar(ch+':\'));
        buffer:='  '+ch+': ';
        case i of
          DRIVE_UNKNOWN : buffer:=buffer+'未知类型';
          DRIVE_REMOVABLE: buffer:=buffer+'软盘';
          DRIVE_FIXED : begin
               buffer:=buffer+'硬盘';
               if getdiskfreespace(pchar(ch+':\'),
                  spc,bps,nofc,tnoc) then begin
                  buffer:=buffer+'总容量: 
                  '+inttostr((spc*bps*tnoc) div (1024*1024))+'MB';
                  buffer:=buffer+'剩余: 
                  '+inttostr((spc*bps*nofc) div (1024*1024))+'MB';
               end;
             end;
          DRIVE_REMOTE : buffer:=buffer+'网络驱动器';
          DRIVE_CDROM :buffer:=buffer+'CD-ROM驱动器';
          DRIVE_RAMDISK:buffer:=buffer+'RAM虚拟驱动器';
        end;
        if (ch >'D') and (i=1) then break;
        if i<>1 then memo1.Lines.Add(buffer);
      end;  case getkeyboardtype(0) of  //获取键盘类型
        1: buffer:='  键盘: IBM PC/XT或兼容类型(83键)';
        2: buffer:='  键盘: Olivetti "ICO"(102键)';
        3: buffer:='  键盘: IBM PC/AT(84键)';
        4: buffer:='  键盘: IBM增强型(101或102键)';
        5: buffer:='  键盘: Nokia 1050';
        6: buffer:='  键盘: Nokia 9140';
        7: buffer:='  键盘: Japanese';
      end;
      memo1.lines.add(buffer);  //获取键盘功能键数目
      memo1.lines.add(' 功能键数目:
      '+inttostr(getkeyboardtype(2)));  memo1.Lines.add('显示器分辨率:
       '+inttostr(getsystemmetrics(SM_CXSCREEN))
       +'x'+inttostr(getsystemmetrics(SM_CYSCREEN)));
      //获取鼠标按键数目
      memo1.Lines.add(' 鼠标: 
      '+inttostr(getsystemmetrics(SM_CMOUSEBUTTONS))+'键');  globalmemorystatus(meminfo); //获取系统内存数量
      memo1.lines.add('   物理内存:
      '+inttostr(meminfo.dwTotalPhys div 1024)+' KB');  i:=getsystemmetrics(SM_CLEANBOOT);
      case i of
        0:buffer:='系统启动模式:正常模式';
        1:buffer:='系统启动模式:保护模式';
        2:buffer:='系统启动模式:网络保护模式';
      end;
      memo1.lines.add(buffer);  x:=getsystemmetrics(SM_ARRANGE); 
      //获取系统最小化窗口的起始位置和排列方式
      y:=x;
      x:=x and 3;
      y:=y and 12;
      case x of
        ARW_BOTTOMLEFT :  buffer:=' 最小化窗口:自左下角';
        ARW_BOTTOMRIGHT : buffer:=' 最小化窗口:自右下角';
        ARW_TOPLEFT :  buffer:=' 最小化窗口:自左上角';
        ARW_TOPRIGHT : buffer:=' 最小化窗口:自右上角';
      end;
      case y of
        ARW_LEFT : buffer:=buffer+'横向排列';
        ARW_UP :   buffer:=buffer+'纵向排列';
        ARW_HIDE : buffer:=buffer+'隐藏';
      end;
      memo1.lines.add(buffer);  getmem(computername,255);  //获取计算机名称和用户名
      getmem(username,255);
      i:=255;
      getcomputername(computername,i);
      memo1.lines.add(' 计算机名称: '+computername);
      getusername(username,i);
      memo1.lines.add(' 用户名: '+username);
      freemem(computername);
      freemem(username);  getsysteminfo(cpuinfo);  //获取CPU类型
      case cpuinfo.dwProcessorType of
         386 : buffer:='  CPU类型: 386';
         486 : buffer:='  CPU类型: 486';
         586 : buffer:='  CPU类型: Pentium';
      end;
      memo1.Lines.add(buffer);  //从注册表中获取CPU标识,Windows版本,
     产品标识,注册单位名称及用户名
      myreg:=Tregistry.Create;
      myreg.RootKey:=HKEY_LOCAL_MACHINE;
      if myreg.OpenKey('hardware\description\
      system\centralprocessor\0',false) then
         memo1.lines.add(' CPU标识:
         '+myreg.ReadString('VendorIdentifier'));
      myreg.closekey;
      if myreg.openkey('software\microsoft\
      windows\currentversion',false) then begin
         memo1.lines.add(' windows版本: 
         '+myreg.ReadString('Version'));
         memo1.lines.add(' 版本号:
         '+myreg.ReadString('VersionNumber')+'
         '+myreg.ReadString('Subversionnumber'));
         memo1.lines.add(' 产品标识: 
         '+myreg.Readstring('ProductID'));
         memo1.lines.add('注册单位名称:
         '+myreg.readstring('RegisteredOrganization'));
         memo1.lines.add(' 注册用户名:
         '+myreg.readstring('RegisteredOwner'));
      end;
      myreg.CloseKey;
      myreg.Free;
    end;
    end. 
      

  3.   

    look up SetupDiChangeState function in MSDN, pay attention to DICS_DISABLE flag.
    Disables the device. If the device is disableable but this function cannot disable the device dynamically, this function s the device to be disabled the next time the machine reboots.
    DICS_ENABLE 
    Loads the drivers for the device and starts the device, if possible. If the function is not able to start the device, it sets the DI_NEEDREBOOT flag for the device which indicates to the initiator of the property change request that they must prompt the user to reboot the machine. 
      

  4.   

    //HOWTO: Enumerate Hardware Devices by Using SetupDi Calls Q259695#include <stdio.h>
    #include <windows.h>
    #include <setupapi.h>
    #include <devguid.h>
    #include <regstr.h>
    #pragma comment(lib,"Setupapi.lib")int main( int argc, char *argv[ ], char *envp[ ] )
    {
        HDEVINFO hDevInfo;
        SP_DEVINFO_DATA DeviceInfoData;
        DWORD i;    // Create a HDEVINFO with all present devices.
        hDevInfo = SetupDiGetClassDevs(NULL,
            0, // Enumerator
            0,
            DIGCF_PRESENT | DIGCF_ALLCLASSES );
        
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
            // Insert error handling here.
            return 1;
        }
        
        // Enumerate through all devices in Set.
        
        DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
        for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
            &DeviceInfoData);i++)
        {
            DWORD DataT;
            LPTSTR buffer = NULL;
            DWORD buffersize = 0;
            
            // 
            // Call function with null to begin with, 
            // then use the returned buffer size 
            // to Alloc the buffer. Keep calling until
            // success or an unknown failure.
            // 
            while (!SetupDiGetDeviceRegistryProperty(
                hDevInfo,
                &DeviceInfoData,
                SPDRP_DEVICEDESC,
                &DataT,
                (PBYTE)buffer,
                buffersize,
                &buffersize))
            {
                if (GetLastError() == 
                    ERROR_INSUFFICIENT_BUFFER)
                {
                    // Change the buffer size.
                    if (buffer) LocalFree(buffer);
                    buffer = (char*)LocalAlloc(LPTR,buffersize);
                }
                else
                {
                    // Insert error handling here.
                    break;
                }
            }        
            printf("SPDRP_DEVICEDESC:[%s]\n",buffer);
    if (buffer) LocalFree(buffer);
    buffer =NULL;
    buffersize=0;
            while (!SetupDiGetDeviceRegistryProperty(
                hDevInfo,
                &DeviceInfoData,
                SPDRP_LOCATION_INFORMATION,
                &DataT,
                (PBYTE)buffer,
                buffersize,
                &buffersize))
            {
                if (GetLastError() == 
                    ERROR_INSUFFICIENT_BUFFER)
                {
                    // Change the buffer size.
                    if (buffer) LocalFree(buffer);
                    buffer = (char*)LocalAlloc(LPTR,buffersize);
                }
                else
                {
                    // Insert error handling here.
                    break;
                }
            }        
            printf("SPDRP_LOCATION_INFORMATION:[%s]\n",buffer);        
            if (buffer) LocalFree(buffer);
        }
        
        
        if ( GetLastError()!=NO_ERROR &&
             GetLastError()!=ERROR_NO_MORE_ITEMS )
        {
            // Insert error handling here.
            return 1;
        }
        
        //  Cleanup
        SetupDiDestroyDeviceInfoList(hDevInfo);
        
        return 0;
    }