WMI--Windows API--SWbemLocator Object

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

 

SWbemLocator Object

You can use the methods of the SWbemLocator object to obtain an SWbemServices object that represents a connection to a namespace on either a local computer or a remote host computer.
你可以使用SWbemLocator对象的方法去获得一个SWbemServices对象,它代表一个连接到一个本地计算机或远程主机计算机的命名空间上。
You can then use the methods of the SWbemServices object to access WMI.
你可以使用SWbemServices对象的方法去访问WMI。
This object can be created by the VBScript CreateObject call.
这个对象可以由VBScript创建CreateObject调用。

Methods
The SWbemLocator object defines the following method.
这个SWbemLocator对象定义以下方法

Method Description
ConnectServer
 Connects to WMI on the specified computer.
 连接到指定计算的WMI。

Properties
The SWbemLocator object has the following property.
这个SWbemLocator对象有以下属性。
Property Description
Security_
 Used to read or change the security settings.
 用于读取或改变安全设置。

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 OSR2 and later.
 
Server
 Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0 SP4 and later.
 
Type Library
 Use Wbemdisp.tlb.
 
DLL
 Requires Wbemdisp.dll.
 

See Also
Scripting API Objects

 

Send comments about this topic to Microsoft

Build date: 8/15/2007

### Java 使用 WMI 示例 为了在Java中使用WMI,可以借助一些第三方库来实现这一目标。市场上存在多个用于通过Java调用WMI的库,其中包括J-Interop、JACOB Project和J-Integra[^3]。 #### J-Interop 库介绍 特别推荐的是J-Interop这个选项,因为它仅完全免费而且是开源项目。该API实现了纯粹基于DCOM协议的桥梁功能,全部采用Java编写而成而无需依赖本地接口(JNI)。这使得开发者能够更加便捷地集成到现有的Java应用程序当中去。 下面是一个简单的例子展示怎样利用J-Interop连接至远程计算机并查询其操作系统版本: ```java import org.jinterop.dcom.common.JIException; import org.jinterop.dcom.core.*; import org.jinterop.winreg.IJIWinReg; public class WMISample { public static void main(String[] args) throws Exception{ String strComputer = "."; // 创建会话对象 JISession session = null; try { session = JISession.createSession("username", "password"); // 获取服务控制管理器句柄 IJIComObject iWbemServices = (IJIDispatch)((IJIClassFactory)new JIProgId("WbemScripting.SWbemLocator")).createInstance().queryInterface(IJIDispatch.class).invokeMethod("ConnectServer", new Object[]{strComputer, "root\\cimv2"}); // 查询OS信息 IJIComObject resultEnum = ((IJIDispatch)iWbemServices.queryInterface(IJIDispatch.class)).invokeMethod("ExecQuery",new Object[]{"SELECT * FROM Win32_OperatingSystem"}); while (((JIArray)((IJIVariant)(resultEnum.getDispatch()).getVariantValue()).toArray()[0]).getObjectAs Dispatch() != null){ System.out.println(((String)((IJIVariant)(((IJIDispatch)((JIArray)((IJIVariant)((IJIVariant)(resultEnum.getDispatch()).getVariantValue()).toArray()[0])).getObjectAsDispatch()).invokeMethod("Name").getVariant())).getValue())); break; // 只打印第一个匹配项的结果 } } catch(JIException e){ throw new RuntimeException(e.getMessage(),e); } finally { if(session!=null && !session.isClosed()){ session.close(); } } } } ``` 此代码片段展示了如何创建一个与指定主机上的WMI命名空间相连的新`SWbemLocator`实例,并执行针对特定类(`Win32_OperatingSystem`)的选择语句以检索所需数据。最后遍历返回集合中的每一个元素直到找到符合条件的对象为止,在这里只取到了名称属性作为演示目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值