WMI--Windows API--ExecQuery

原文来自MSDN Library for Visual Studio 2008 SP1,翻译部分仅为个人观点,想要看更多信息请看MSDN,如有版权问题请联系QQ 643166601,邮件643166601@qq.com

 

ExecQuery Method of the IWbemServices Interface
The IWbemServices::ExecQuery method executes a query to retrieve objects.
这个IWbemServices::ExecQuery方法执行一个查询取回对象。
For the valid types of queries that can be performed, see Querying with WQL.
查询有效的类型可以被执行,请看Querying with WQL。
HRESULT ExecQuery(
  [in]                 const BSTR strQueryLanguage,
  [in]                 const BSTR strQuery,
  [in]                 LONG lFlags,
  [in]                 IWbemContext* pCtx,
  [out]                IEnumWbemClassObject** ppEnum
);

Parameters
strQueryLanguage
Valid BSTR that contains one of the query languages supported by Windows Management.
有效的BSTR包含一个Windows管理支持的查询语言。
This must be "WQL", the acronym for WMI Query Language.
这必须是"WQL",WMI Query Language的首字母缩略词。
strQuery
Valid BSTR that contains the text of the query.
有效的BSTR包含查询文本。
This parameter cannot be NULL.
这个参数不能为NULL。
lFlags
The following flags affect the behavior of this method.
以下的flags影响这个方法的行为。
The suggested value for this parameter is WBEM_FLAG_RETURN_IMMEDIATELY and WBEM_FLAG_FORWARD_ONLY for best performance.
这个最好的性能这个参数建议值是WBEM_FLAG_RETURN_IMMEDIATELY 和 WBEM_FLAG_FORWARD_ONLY。
Flag Meaning
WBEM_FLAG_USE_AMENDED_QUALIFIERS
 If this flag is set, WMI retrieves the amended qualifiers stored in the localized namespace of the current connection's locale.
 如果设置这个flag,WMI返回修正限定符存储在命名空间中的当前连接区域设置。
 If not set, only the qualifiers stored in the immediate namespace are retrieved.
 如果没有设置,仅仅
WBEM_FLAG_FORWARD_ONLY
 This flag causes a forward-only enumerator to be returned.
 
 Forward-only enumerators are generally much faster and use less memory than conventional enumerators but do not allow calls to Clone or Reset.
 
WBEM_FLAG_BIDIRECTIONAL
 This flag causes Windows Management to retain pointers to objects of the enumeration until the client releases the enumerator.
 
WBEM_FLAG_RETURN_IMMEDIATELY
 This flag causes this to be a semisynchronous call.
 
 For more information, see Calling a Method.
 
WBEM_FLAG_ENSURE_LOCATABLE
 This flag ensures that any returned objects have enough information in them so that the system properties, such as __PATH, __RELPATH, and __SERVER, are non-NULL.
 
WBEM_FLAG_PROTOTYPE
 This flag is used for prototyping.
 
 It does not execute the query and instead returns an object that looks like a typical result object.
 
WBEM_FLAG_DIRECT_READ
 This flag causes direct access to the provider for the class specified without any regard to its parent class or subclasses.
 

pCtx
Typically NULL. Otherwise, this is a pointer to an IWbemContext object that can be used by the provider that is providing the requested classes or instances.

The values in the context object must be specified in the documentation for the provider in question.

For more information about this parameter, see Making Calls to WMI.

ppEnum
If no error occurs, this receives the enumerator that allows the caller to retrieve the instances in the result set of the query.

It is not an error for the query to have a result set with 0 instances. This is determined only by attempting to iterate through the instances.

This object returns with a positive reference count.

The caller must call Release when the object is no longer required.

Return Value
This method returns an HRESULT that indicates the status of the method call.

The following table lists the value contained within an HRESULT.

On failure, you can obtain any available information from the COM function GetErrorInfo.

COM-specific error codes also can be returned if network problems cause you to lose the remote connection to Windows Management.

Return code Description
WBEM_E_ACCESS_DENIED
 The current user does not have permission to view the result set.
 
WBEM_E_FAILED
 This indicates other unspecified errors.
 
WBEM_E_INVALID_PARAMETER
 An invalid parameter was specified.
 
WBEM_E_INVALID_QUERY
 The query was not syntactically valid.
 
WBEM_E_INVALID_QUERY_TYPE
 The requested query language is not supported.
 
WBEM_E_OUT_OF_MEMORY
 There was not enough memory to complete the operation.
 
WBEM_E_SHUTTING_DOWN
 Windows Management service was stopped and restarted.
 
 A new call to ConnectServer is required.
 
WBEM_E_TRANSPORT_FAILURE
 This indicates the failure of the remote procedure call (RPC) link between the current process and Windows Management.
 
WBEM_E_NOT_FOUND
 The query specifies a class that does not exist.
 
WBEM_S_NO_ERROR
 The call succeeded.
 

Remarks
The IWbemServices::ExecQuery method processes the query specified in the strQuery parameter and creates an enumerator through which the caller can access the query results.

The enumerator is a pointer to an IEnumWbemClassObject interface; the query results are instances of class objects made available through the IWbemClassObject interface.

There are limits to the number of AND and OR keywords that can be used in WQL queries.

Large numbers of WQL keywords used in a complex query can cause WMI to return the WBEM_E_QUOTA_VIOLATION error code as an HRESULT value.

The limit of WQL keywords depends on how complex the query is.

Example Code
For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.

For C++ code examples, see WMI C++ Application Examples.

Requirements
Client
 Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP4 and later, Windows Me, Windows 98, or Windows 95.
 
Server
 Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0 SP4 and later.
 
Header
 Declared in Wbemcli.h; include Wbemidl.h.
 
Library
 Use Wbemuuid.lib.
 
DLL
 Requires Fastprox.dll.

Requires Esscli.dll.

Requires Framedyn.dll.

Requires Ntevt.dll.

Requires Stdprov.dll.

Requires Viewprov.dll.

Requires Wbemcomn.dll.

Requires Wbemcore.dll.

Requires Wbemess.dll.

Requires Wbemsvc.dll.

Requires Wmipicmp.dll.

Requires Wmidcprv.dll.

Requires Wmipjobj.dll.

Requires Wmiprvsd.dll.
 

See Also
Querying with WQL
IWbemServices::ExecQueryAsync

 

Send comments about this topic to Microsoft

Build date: 8/15/2007

<think> 首先,用户的问题是关于WMIWMIC和Windows API的介绍、使用方法以及相关技术信息。我需要基于提供的引用内容来回答,这些引用来自站内引用:[^1]、[^2]、[^3]、[^4]。 回顾引用内容: - [^1]:介绍了WMIC作为WMI的命令行接口,简化了WMI管理,无需编程知识。 - [^2]:讨论了使用WMI获取系统版本信息的替代方法,提到了GetVersionEx API将被废弃。 - [^3]:描述了使用WMI的SNMP类查询网卡信息,并提到WMIC用于查看和更改系统属性。 - [^4]:简要提及使用WMI技术获取系统信息的一个示例。 用户的上一个问题似乎是关于WMIWMIC和Windows API的概述和使用。系统指令要求: - 所有行内数学表达式使用$...$格式(但在这个主题中可能不涉及数学,所以可能不需要)。 - 独立公式使用$$...$$格式并单独成段(同样,可能不适用)。 - LaTeX语法正确(如果涉及公式)。 - 使用中文回答。 - 生成相关问题(在回答末尾)。 - 回答中引用的段落末尾添加引用标识,例如[^1]。 - 示例中展示了回答结构和代码块。 用户可见层指令: - 回答结构清晰,帮助用户逐步解决问题。 - 保证回答真实可靠。 回答结构: 1. **介绍WMIWMIC和Windows API**:给出定义和基本概念。 2. **比较和关联**:解释三者之间的关系和区别。 3. **使用方法**:提供具体的使用示例,基于引用内容。 4. **相关技术信息**:包括优缺点、应用场景等。 5. **生成相关问题**:在末尾添加相关问题板块。 引用添加:在回答中引用特定内容时,末尾添加[^n],例如提到WMIC简化管理时引用[^1]。 现在,整理关键点: - **WMI (Windows Management Instrumentation)**:基于Web-Based Enterprise Management (WBEM)标准,用于管理Windows系统和设备。它提供API来查询和控制系统信息。 - **WMIC (WMI Command-line)**:是WMI的命令行工具,允许通过命令提示符执行WMI操作,无需编写脚本或使用GUI工具[^1]。 - **Windows API**:是Windows操作系统提供的一套函数库,用于应用程序开发。它包括各种API如Win32 API,用于文件系统、网络、硬件访问等。引用[^2]提到GetVersionEx API将被废弃,表明API会过时。 基于引用: - 引用[^1]:WMIC扩展了WMI,提供命令行接口,简化管理。 - 引用[^2]:WMI可用于获取系统版本信息,替代过时的API- 引用[^3]:WMIC用于查询系统属性,如网卡信息。 - 引用[^4]:WMI用于获取系统信息,如进程信息。 使用方法示例: - 使用WMI:通过PowerShell或VBScript查询数据。 - 使用WMIC:在命令提示符运行命令,如`wmic os get Caption, Version`获取OS信息。 - Windows API:通过编程语言调用函数。 回答步骤: 1. **介绍**:定义WMIWMIC和Windows API。 2. **关系**:WMIWindows API的一部分,WMIC是WMI的工具。 3. **使用方法**:给出命令行和编程示例。 4. **优缺点**:基于引用,WMIC易于使用但可能不如脚本灵活。 5. **相关技术**:提到替代方法如注册表查询。 最后,生成相关问题。 使用中文回答。 大纲: - **开头**:概述主题。 - **WMI介绍**:定义、作用、基于引用[^1]。 - **WMIC介绍**:作为命令行工具,优点[^1]。 - **Windows API介绍**:定义、包括WMI[^2]。 - **使用方法和示例**: - WMIC命令示例:如获取系统信息[^2][^3]。 - WMI编程示例:如在PowerShell查询。 - API示例:获取版本信息[^2]。 - **比较和总结**:区别和适用场景。 - **相关问题**:在末尾添加板块。 引用标识:在相关句子末尾添加[^n]。 例如:"WMIC提供了从命令行接口执行系统管理的支持[^1]。" 确保代码块正确格式化:如果有代码,像示例中一样使用```代码块```。 可能的代码示例: - WMIC命令:`wmic os get Caption, Version` - PowerShell WMI查询:`Get-WmiObject Win32_OperatingSystem | Select Caption, Version` 基于引用[^4],有一个示例:运行notepad.exe并检查进程。 最终回答结构: 1. 介绍WMIWMIC和Windows API。 2. 详细解释每个部分。 3. 使用方法。 4. 总结。 5. §§相关问题§§ </think> ### WMIWMIC和Windows API介绍及使用方法 WMIWindows Management Instrumentation)、WMIC(WMI Command-line)和Windows APIWindows操作系统中用于系统管理和开发的核心技术。它们共同提供了访问和控制系统资源的能力,但各有侧重和应用场景。以下我将基于您的查询和提供的引用内容,逐步介绍它们的定义、关系、使用方法及相关技术信息。回答参考了站内引用[^1][^2][^3][^4],确保信息真实可靠。 #### 1. WMIWMIC和Windows API的基本介绍 - **WMIWindows Management Instrumentation)**: WMIWindows内置的管理框架,基于Web-Based Enterprise Management (WBEM)标准。它允许管理员和开发者通过统一的接口查询和控制系统硬件、软件、网络等资源,例如监控进程、获取系统版本或管理设备。WMI的核心是通过“命名空间”(namespace)组织数据,并支持编程语言(如C++、VBScript)访问。引用[^1]提到,WMI简化了系统管理,但传统上需要脚本或工具支持[^1]。引用[^4]展示了WMI用于获取进程信息(如notepad.exe的运行状态)[^4]。 - **WMIC(WMI Command-line)**: WMIC是WMI的命令行工具,提供无需编程即可执行WMI操作的能力。管理员通过命令提示符直接运行WMIC命令,查询或修改系统设置(如系统属性、网络配置)。引用[^1]强调,WMIC降低了使用门槛,无需熟悉WMI名称空间或脚本语言[^1]。引用[^3]指出,WMIC可用于查询网卡信息或远程管理计算机[^3]。 - **Windows API**: Windows API(应用程序编程接口)是Windows操作系统提供的一套函数库,供开发者构建应用程序。它包括核心API如Win32 API,用于文件操作、内存管理、用户界面等。引用[^2]提到,部分API(如`GetVersionEx`)因过时而不再推荐使用,WMI成为替代方案[^2]。Windows API是底层接口,WMI实际上构建在其之上(例如通过COM组件调用API)。 **三者关系总结**: - Windows API是底层基础,WMI基于其上提供高级管理功能。 - WMIC是WMI的简化命令行接口,面向管理员而非开发者。 - 在实际应用中,WMIWMIC常用于自动化管理,而Windows API更适用于应用程序开发。 #### 2. 使用方法和示例 以下是具体的使用场景和示例代码,基于引用内容。WMIC和WMI操作通常在命令提示符或PowerShell中执行,而Windows API需要通过编程语言(如C++或Python)调用。 - **WMIC使用示例(命令行操作)**: WMIC命令格式为`wmic <alias> <command>`,其中`alias`代表WMI类(如`os`表示操作系统)。 - 获取系统版本信息(引用[^2]提到,替代过时的API如`GetVersionEx`)[^2]: ```bash wmic os get Caption, Version, BuildNumber ``` 输出示例: ``` Caption Version BuildNumber Microsoft Windows 11 10.0.22000 22000 ``` - 查询网卡信息(引用[^3]提到使用WMI的SNMP类)[^3]: ```bash wmic nic get Name, MACAddress, IPAddress ``` 输出示例: ``` Name MACAddress IPAddress Ethernet Adapter 00-11-22-33-44-55 {192.168.1.100} ``` - 远程管理计算机(引用[^3]描述,需管理员权限)[^3]: ```bash wmic /node:192.168.1.2 /user:admin process call create "notepad.exe" ``` 此命令在远程计算机上启动记事本进程。 - **WMI使用示例(编程接口)**: WMI可通过PowerShell、VBScript或.NET访问。引用[^4]展示了简单的进程查询。 - PowerShell脚本获取进程信息(类似引用[^4]示例)[^4]: ```powershell # 查询运行中的记事本进程 $processes = Get-WmiObject Win32_Process -Filter "Name='notepad.exe'" foreach ($process in $processes) { Write-Host "Process: $($process.Name), CommandLine: $($process.CommandLine)" } ``` 输出示例: ``` Process: notepad.exe, CommandLine: "C:\WINDOWS\system32\NOTEPAD.EXE" C:\Users\admin\Desktop\tcp.txt ``` - VBScript示例(引用[^1]提到的传统方法)[^1]: ```vb Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem") For Each objItem in colItems WScript.Echo "OS Caption: " & objItem.Caption Next ``` - **Windows API使用示例(开发层面)**: Windows API直接调用函数,通常使用C++或Python(通过pywin32库)。引用[^2]说明,API如`GetVersionEx`已被废弃,推荐改用WMI[^2]。 - Python示例使用WMI替代过时API(获取系统版本): ```python import wmi # 需要安装pywin32库(pip install pywin32) c = wmi.WMI() for os in c.Win32_OperatingSystem(): print(f"OS: {os.Caption}, Version: {os.Version}") ``` 输出示例: ``` OS: Microsoft Windows 11, Version: 10.0.22000 ``` - 直接调用Win32 API(C++示例,不推荐用于版本获取): ```cpp #include <windows.h> #include <iostream> int main() { OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); // 此API已过时(reference [^2]) std::cout << "Major Version: " << osvi.dwMajorVersion << std::endl; return 0; } ``` #### 3. 相关技术信息 - **优缺点比较**: - **WMI**:优点包括跨平台兼容性(支持远程管理)和标准化查询语言(WQL);缺点是性能开销大,且需处理命名空间复杂性[^1][^4]。 - **WMIC**:优点为易于使用(命令行即可操作),适合快速诊断;缺点是不支持复杂逻辑,且Windows 10后微软已弃用WMIC(但仍可用),推荐改用PowerShell[^1][^3]。 - **Windows API**:优点提供底层控制和高性能;缺点是需要编程知识,部分API过时(如`GetVersionEx`),维护难度高[^2]。 - **应用场景**: - WMIC:日常系统维护,如查询硬件状态或远程重启。 - WMI:自动化脚本(如监控系统健康),与引用[^4]的进程管理类似[^4]。 - Windows API:开发驱动程序或高性能应用。 - **最佳实践**: - 优先使用PowerShell替代WMIC(命令如`Get-CimInstance`),以兼容新Windows版本[^1][^2]。 - 查询系统信息时,避免过时API,改用WMI类(如`Win32_OperatingSystem`)[^2]。 - 安全考虑:WMI操作需管理员权限,远程访问时启用防火墙规则。 #### 总结 WMIWMIC和Windows API构成了Windows管理生态:WMI提供高级接口,WMIC是其命令行简化版,而Windows API是底层基础。在现代管理中,PowerShell结合WMI取代了WMIC的大部分功能。如果您是开发者,优先学习Windows APIWMI编程;如果是管理员,则掌握WMIC或PowerShell命令更高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值