使用 J-Interop 通过 Java 进行 WMI

发布于 2024-12-03 06:16:56 字数 2572 浏览 1 评论 0原文

我是 WMI 新手,并使用 J-interop lib 通过 Java 进行 COM 通信... 我需要从远程系统获取物理内存数据。

我有以下代码:它提供了一些数据,但有一些缺陷,就像我的系统中有 2 GB 内存,有两个存储卡,每个卡 1 GB,此代码仅显示 1 GB 内存:

请帮助我纠正以下代码以及 J-Interop 的任何良好参考和良好示例:

JISession dcomSession = null;
try {
dcomSession = init(domain, username, password);
JIComServer comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"), hostname, dcomSession);
IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));

Object[] params = new Object[] { new JIString(hostname), new JIString("ROOT\\CIMV2"),
        JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),
        JIVariant.OPTIONAL_PARAM(), new Integer(0), JIVariant.OPTIONAL_PARAM() };
JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);

IJIDispatch wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());

final int RETURN_IMMEDIATE = 0x10;
final int FORWARD_ONLY = 0x20;

params = new Object[] { new JIString("SELECT * FROM Win32_PhysicalMemory"), JIVariant.OPTIONAL_PARAM(),
        new JIVariant(new Integer(RETURN_IMMEDIATE)) };
JIVariant[] servicesSet = wbemServices.callMethodA("ExecQuery", params);
IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());

JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
IJIComObject enumComObject = newEnumvariant.getObjectAsComObject();
IJIEnumVariant enumVariant = (IJIEnumVariant) narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));

Object[] elements = enumVariant.next(1);
JIArray aJIArray = (JIArray) elements[0];

JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
for (JIVariant variant : array) {
    IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

    // Print object as text. Optional - comment if not needed
    JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });
    System.out.println(v1[0].getObjectAsString2());
    System.out.println(wbemObjectDispatch.get("DataWidth").getObjectAsInt());
    System.out.println(wbemObjectDispatch.get("Status").getObjectAsInt());
    System.out.println(wbemObjectDispatch.get("Speed").getObjectAsInt());
    System.out.println(wbemObjectDispatch.get("FormFactor").getObjectAsInt());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dcomSession != null) {
    try {
        JISession.destroySession(dcomSession);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

I am new to WMI and using J-interop lib for COM communication through Java...
I have a requirement to get Physical memory data from remote system.

I have following code : it provides some of the data but have some flaw in it like I have 2 GB memory in my system with two memory cards 1 GB each and this code only shows me 1 GB of memory:

Please help me in rectifying the following code and any good reference to follow for J-Interop with good examples:

JISession dcomSession = null;
try {
dcomSession = init(domain, username, password);
JIComServer comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"), hostname, dcomSession);
IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));

Object[] params = new Object[] { new JIString(hostname), new JIString("ROOT\\CIMV2"),
        JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),
        JIVariant.OPTIONAL_PARAM(), new Integer(0), JIVariant.OPTIONAL_PARAM() };
JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);

IJIDispatch wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());

final int RETURN_IMMEDIATE = 0x10;
final int FORWARD_ONLY = 0x20;

params = new Object[] { new JIString("SELECT * FROM Win32_PhysicalMemory"), JIVariant.OPTIONAL_PARAM(),
        new JIVariant(new Integer(RETURN_IMMEDIATE)) };
JIVariant[] servicesSet = wbemServices.callMethodA("ExecQuery", params);
IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());

JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
IJIComObject enumComObject = newEnumvariant.getObjectAsComObject();
IJIEnumVariant enumVariant = (IJIEnumVariant) narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));

Object[] elements = enumVariant.next(1);
JIArray aJIArray = (JIArray) elements[0];

JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
for (JIVariant variant : array) {
    IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());

    // Print object as text. Optional - comment if not needed
    JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });
    System.out.println(v1[0].getObjectAsString2());
    System.out.println(wbemObjectDispatch.get("DataWidth").getObjectAsInt());
    System.out.println(wbemObjectDispatch.get("Status").getObjectAsInt());
    System.out.println(wbemObjectDispatch.get("Speed").getObjectAsInt());
    System.out.println(wbemObjectDispatch.get("FormFactor").getObjectAsInt());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dcomSession != null) {
    try {
        JISession.destroySession(dcomSession);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2024-12-10 06:16:56

您需要获取枚举中元素的计数。查看 MSIWMI2.java 中的 j-interop 示例。这是获取枚举计数的示例。

...
results = wbemServices_dispatch.callMethodA("ExecQuery", new Object[]{new JIString("select * from Win32_OperatingSystem where Primary=True"), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),JIVariant.OPTIONAL_PARAM()});
IJIDispatch wbemObjectSet_dispatch = (IJIDispatch)JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
IJIComObject object2 = variant.getObjectAsComObject();

IJIEnumVariant enumVARIANT = (IJIEnumVariant)JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));

JIVariant Count = wbemObjectSet_dispatch.get("Count");
int count = Count.getObjectAsInt();
for (int i = 0; i < count; i++)
{
        Object[] values = enumVARIANT.next(1);
        JIArray array = (JIArray)values[0];
        Object[] arrayObj = (Object[])array.getArrayInstance();
        for (int j = 0; j < arrayObj.length; j++)
        {
            IJIDispatch wbemObject_dispatch = (IJIDispatch)JIObjectFactory.narrowObject(((JIVariant)arrayObj[j]).getObjectAsComObject());
            JIVariant variant2 = (JIVariant)(wbemObject_dispatch.callMethodA("GetObjectText_",new Object[]{new Integer(1)}))[0];
            System.out.println(variant2.getObjectAsString().getString());
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        }
}
...

此代码来自 org.jinterop.dcom.test 包中的 MSWMI2.java。

亲切的问候

比约恩

you need to get the count of the elements in the enumeration. Take a look on the j-interop example in MSIWMI2.java. Here is an example to get the count of an enumeration.

...
results = wbemServices_dispatch.callMethodA("ExecQuery", new Object[]{new JIString("select * from Win32_OperatingSystem where Primary=True"), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),JIVariant.OPTIONAL_PARAM()});
IJIDispatch wbemObjectSet_dispatch = (IJIDispatch)JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
IJIComObject object2 = variant.getObjectAsComObject();

IJIEnumVariant enumVARIANT = (IJIEnumVariant)JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));

JIVariant Count = wbemObjectSet_dispatch.get("Count");
int count = Count.getObjectAsInt();
for (int i = 0; i < count; i++)
{
        Object[] values = enumVARIANT.next(1);
        JIArray array = (JIArray)values[0];
        Object[] arrayObj = (Object[])array.getArrayInstance();
        for (int j = 0; j < arrayObj.length; j++)
        {
            IJIDispatch wbemObject_dispatch = (IJIDispatch)JIObjectFactory.narrowObject(((JIVariant)arrayObj[j]).getObjectAsComObject());
            JIVariant variant2 = (JIVariant)(wbemObject_dispatch.callMethodA("GetObjectText_",new Object[]{new Integer(1)}))[0];
            System.out.println(variant2.getObjectAsString().getString());
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        }
}
...

This code is from MSWMI2.java in the org.jinterop.dcom.test package.

Kind regards

Björn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文