Delphi开发Windows之WMI
一、何谓WMI
Windows 该系统提供了一种管理组件的通用机制,称为 Windows 管理规范 (WMI)。它是Windows 它可以访问、配置、管理和监控几乎所有的基础设施Windows资源。英文全称Windows Management Instrumentation。
Windows Management Instrumentation - Win32 apps | Microsoft Docs
Implementing WMI - Windows drivers | Microsoft Docs
Windows Kernel-Mode WMI Library - Windows drivers | Microsoft Docs
Kernel-Mode Driver Architecture Design Guide - Windows drivers | Microsoft Docs
二、感性认识
例如,判断你的应用程序的例子是Virtual Machine虚拟机下的Windows或者直接操作Windows在操作系统下运行。有时候会得到硬件信息,可以在一些计算机上正常调用,但有时你会发现把它放进去Docker下或VM下面,报错了,那是因为在Docker下或VM在环境中,您实际上需要获取容器或虚拟机的硬件信息。
再比如,得到你的Windows计算机的BIOS版本、磁盘驱动器信息等等。当然,几乎没有别的了Windows操作系统的组件,具体我懒得写,你可以百度,我百度WMI常见问题及解决方法 - yehao's Blog - C 看完博客,可以参考问题。WMI的领域。
你可以通过这些WMI通信获取信息。
三、Delphi实施步骤和基本代码流程
3.1、UAC提权
要想读写WMI特别是在添加、配置和监控服务时,您必须有管理WMI简单地说,将您的应用程序提升为系统管理员的权限。
代码(略),详见我的博文:delphi Windows服务器UAC用户账户控制CPU获取和接口等参数TGUID_pulledup的博客-CSDN博客
3.2、如何判断你的应用运行于Docker容器下或VM虚机下
IWbemServices (wbemcli.h) - Win32 apps | Microsoft Docs
使用ExecQuery方法执行查询,检索对象:
IWbemServices::ExecQuery (wbemcli.h) - Win32 apps | Microsoft Docs
C 原始句法:
HRESULT ExecQuery( const BSTR strQueryLanguage, const BSTR strQuery, long lFlags, IWbemContext *pCtx, IEnumWbemClassObject **ppEnum );
翻译成Delphi详见单位句法Delphi10.4Update2\source\rtl\winWinapi.Wbem.pas:
{$HPPEMIT 'DECLARE_DINTERFACE_TYPE(IWbemServices);'} IWbemServices = interface(IUnknown) [SID_IWbemServices] //... function ExecQuery( strQueryLanguage: WideString; strQuery: WideString; lFlags: Integer; const pCtx: IWbemContext; out ppEnum: IEnumWbemClassObject ): HRESULT; stdcall; //... end; {$EXTERNALSYM IWbemServices}
执行ExecQuery查询,使用WMI的查询语言WQL:
Querying with WQL - Win32 apps | Microsoft Docs
wmic logicaldisk
wmic DiskDrive
wmic cpu
wmic memorychip
wmic bios
WMIC指令详解 [global switches] [global switches] The following global switches are available: /NAMESPACE Path for the namespace the alias operate against命名空间的路径. /ROLE Path for the role containing the alias definitions角色的路径包含别名定义. /NODE Servers the alias will operate against对服务器进行别名. /IMPLEVEL Client impersonation level模拟客户端级别. /AUTHLEVEL Client authentication level客户身份验证水平. /LOCALE Language id the client should use用于客户端的语言 ID. /PRIVILEGES Enable or disable all privileges所有权限启用或禁用. /TRACE Outputs debugging information to stderr输出调试信息 stderr. /RECORD Logs all input commands and output记录所有输入命令和输出. /INTERACTIVE Sets or resets the interactive mode设置或重置交互模式. /FAILFAST Sets or resets the FailFast mode设置或重置 FailFast 模式. /USER User to be used during the session会话期间使用的用户. /PASSWORD Password to be used for session login用于会话登录的密码. /OUTPUT Specifies the mode for output redirection指定重定向输出模式. /APPEND Specifies the mode for output redirection指定重定向输出模式. /AGGREGATE Sets or resets aggregate mode设置或重置聚合模式. /AUTHORITY Specifies the authority type for the connection指定连接的权限类型. /?[:BRIEF|FULL] Usage information用法信息. For more information on a specific global switch, type: switch-name /?[:BRIEF|FULL] Usage information用法信息. For more information on a specific global switch, type: switch-name 请输入更多关于特定全局开关的信息:switch-name /? The following alias/es are available in the current role:以下别名可用于当前角色: ALIAS - Access to the aliases available on the local system访问本地系统上可用的别名 BASEBOARD - Base board (also known as a motherboard or system board) management管理基板(也称为主板或系统板). BIOS - Basic input/output services (BIOS) management基本输入/输出服务 (BIOS) 管理. BOOTCONFIG - Boot configuration anagement引导配置管理.
CDROM - CD-ROM management管理CD-ROM的信息.
COMPUTERSYSTEM - Computer system management 计算机系统管理.
CPU - CPU management管理CPU的信息.
CSPRODUCT - Computer system product information from SMBIOS来自 SMBIOS 的计算机系统产品信息.
DATAFILE - DataFile Management数据文件管理.
DCOMAPP - DCOM Application management关于DCOM应用程序管理.
DESKTOP - User's Desktop management用户的桌面管理.
DESKTOPMONITOR - Desktop Monitor management桌面监视器管理.
DEVICEMEMORYADDRESS - Device memory addresses management 设备内存地址管理.
DISKDRIVE - Physical disk drive management 物理磁盘驱动器管理.
DISKQUOTA - Disk space usage for NTFS volumes卷的磁盘空间使用情况.
DMACHANNEL - Direct memory access (DMA) channel management直接内存访问 (DMA) 通道管理.
ENVIRONMENT - System environment settings management系统环境设置管理.
FSDIR - Filesystem directory entry management文件系统目录条目管理.
GROUP - Group account management组帐户管理.
IDECONTROLLER - IDE Controller management关于IDE控制器管理.
IRQ - Interrupt request line (IRQ) management某个中断请求 (IRQ) 的管理.
JOB - Provides access to the jobs scheduled using the schedule service提供对使用调度服务所调度的所有作业的访问,需要先启动调度服务才生效.
LOADORDER - Management of system services that define execution dependencies管理定义运行载入所依赖的驱动的关系的系统服务.
LOGICALDISK - Local storage device management本地逻辑存储设备管理.
LOGON - LOGON Sessions登录会话.
MEMCACHE - Cache memory management 缓存管理.
MEMORYCHIP - Memory chip information内存芯片信息.
MEMPHYSICAL - Computer system's physical memory management计算机系统的物理内存管理.
NETCLIENT - Network Client management网络客户端管理.
NETLOGIN - Network login information (of a particular user) management网络登录信息(特定用户的)管理.
NETPROTOCOL - Protocols (and their network characteristics) management协议(及其网络特性)管理.
NETUSE - Active network connection management主动网络连接管理_需要先启动对应的服务.
NIC - Network Interface Controller (NIC) management网络接口控制器 (NIC) 管理_通过字段NetConnectionID的数值识别“本地连接等”_NetConnectionStatus=2识别已连接_NetEnabled=true识别是否禁用_PhysicalAdapter=true识别物理适配器还是基于软件的逻辑适配器_ServiceName识别物理或逻辑服务名_MACAddress识别其MAC地址_GUID识别网卡的GUID_Speed的整数数值识别网速_SystemName识别器对应的操作系统的逻辑机器名.
NICCONFIG - Network adapter management网络适配器管理.
NTDOMAIN - NT Domain management关于NT 域管理.
NTEVENT - Entries in the NT Event Log管理NT 事件日志中的条目.
NTEVENTLOG - NT eventlog file managementNT 管理NT 事件日志文件.
ONBOARDDEVICE - Management of common adapter devices built into the motherboard (system board)管理内置于主板(系统板)的通用适配器设备.
OS - Installed Operating System management已安装的操作系统的管理.
PAGEFILE - Virtual memory file swapping management虚拟内存文件交换管理.
PAGEFILESET - Page file settings management页面文件设置管理.
PARTITION - Management of partitioned areas of a physical disk管理物理磁盘的分区区域.
PORT - I/O port management关于I/O 端口管理.
PORTCONNECTOR - Physical connection ports management物理连接端口管理_可能有依赖服务_测试无效.
PRINTER - Printer device management打印机设备管理.
PRINTERCONFIG - Printer device configuration management打印机设备配置管理.
PRINTJOB - Print job management打印作业管理.
PROCESS - Process management进程的管理.
PRODUCT - Installation package task management安装包任务管理.
QFE - Quick Fix Engineering快速修复工程.
QUOTASETTING - Setting information for disk quotas on a volume设置卷上磁盘配额的信息.
RDACCOUNT - Remote Desktop connection permission management远程桌面连接权限管理.
RDNIC - Remote Desktop connection management on a specific network adapter特定网络适配器上的远程桌面连接管理.
RDPERMISSIONS - Permissions to a specific Remote Desktop connection特定远程桌面连接的权限.
RDTOGGLE - Turning Remote Desktop listener on or off remotely远程打开或关闭远程桌面侦听器.
RECOVEROS - Information that will be gathered from memory when the operating system fails当操作系统出现故障时将从内存中收集的信息.
REGISTRY - Computer system registry management计算机系统注册表管理.
SCSICONTROLLER - SCSI Controller management关于SCSI 控制器管理.
SERVER - Server information management服务器信息管理.
SERVICE - Service application management服务列表中的应用程序名及其基本作用等的管理.
SHADOWCOPY - Shadow copy management卷影副本管理.
SHADOWSTORAGE - Shadow copy storage area management卷影副本存储区域管理.
SHARE - Shared resource management共享资源管理(包括共享文件及共享打印机等设备).
SOFTWAREELEMENT - Management of the elements of a software product installed on a system管理安装在系统上的软件产品的元素.
SOFTWAREFEATURE - Management of software product subsets of SoftwareElement 管理安装在系统上的软件产品元素SoftwareElement的软件产品的子集.
SOUNDDEV - Sound Device management声音设备管理.
STARTUP - Management of commands that run automatically when users log onto the computer system管理在用户登录计算机系统时自动运行的命令.
SYSACCOUNT - System account management系统帐户管理.
SYSDRIVER - Management of the system driver for a base service管理基本服务的系统驱动程序.
SYSTEMENCLOSURE - Physical system enclosure management物理系统机柜管理.
SYSTEMSLOT - Management of physical connection points including ports, slots and peripherals, and proprietary connections points管理物理连接点,包括端口、插槽和外围设备,以及专有连接点.
TAPEDRIVE - Tape drive management磁带驱动器管理.
TEMPERATURE - Data management of a temperature sensor (electronic thermometer)温度传感器(电子温度计)的数据管理.
TIMEZONE - Time zone data management时区数据管理.
UPS - Uninterruptible power supply (UPS) management不间断电源 (UPS) 管理.
USERACCOUNT - User account management用户帐户管理.
VOLTAGE - Voltage sensor (electronic voltmeter) data management电压传感器(电子电压表)数据管理.
VOLUME - Local storage volume management本地存储卷管理.
VOLUMEQUOTASETTING - Associates the disk quota setting with a specific disk volume将磁盘配额设置与特定磁盘卷相关联.
VOLUMEUSERQUOTA - Per user storage volume quota management每用户存储量配额管理.
WMISET - WMI service operational parameters management关于本WMI 服务操作参数管理_分别都在特定路径下的文件中存取_.mfl或.mof文件格式.
For more information on a specific alias, type: alias /?有关特定别名的更多信息,请键入:alias /?
CLASS - Escapes to full WMI schema转义到完整的 WMI 模式.
PATH - Escapes to full WMI object paths转义到完整的 WMI 对象路径.
CONTEXT - Displays the state of all the global switches显示所有全局开关的状态.
QUIT/EXIT - Exits the program退出程序.
For more information on CLASS/PATH/CONTEXT, type: (CLASS | PATH | CONTEXT) /?有关 CLASS/PATH/CONTEXT 的更多信息,请键入:(CLASS | PATH | CONTEXT) /?
网友做的分析很不错:
1、https://www.jb51.net/article/49987.htm