有人知道如何使用 C 而不是 C++ 来使用 WMI 吗?
我不愿意为此重新学习 C++! 任何库/URL 都很棒,是的,Google 在这里没有提供太多帮助:-(
这是针对即将到来的项目,在该项目中,我的产品(基于 Java)将为 Microsoft 的 Hyper-V 虚拟化平台提供支持。与 VMware 不同,VMware 提供作为一个 Web 服务,Hyper-V API 只是 WMI 的扩展,我不喜欢使用 J-Integra 等商业工具来将 Java 集成到 COM/WMI 中,而且我发现少数开源工具已经过时了
。宁愿使用 JNI 和 C,而不是 C++。有人知道在哪里可以找到使用 C 进行 WMI 操作的库吗?(是的,我知道 C 不是 OOP 语言:D) )。
提前致谢。
I'd hate to have to relearn C++ just for this! Any libraries/URLs would be great and yes, Google didn't help much here :-(
This is for an upcoming project in which my product (Java based) would provide support for Microsoft's Hyper-V virtualization platform. Unlike VMware, which provides a Web-service, the Hyper-V APIs are mere extensions of WMI. I'd prefer not to use commercial tools such as J-Integra for Java integration into COM/WMI and the few open source tools I found are way outdated.
I would rather use JNI with C than C++. Anybody know where I can find libraries et cetera for using C for WMI operations? In the same vein as Python clients can be used? (And yes, I know C is not an OOP language :D ).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WMI 是通过 COM 访问的吗?
虽然它更冗长且更容易出错(很容易意外地为 vtable 和“this”参数使用不同的指针),但您也可以使用 C 语言中的 COM。
您也可以使用 C++,但将其视为“带有语言扩展的 C,以便更轻松地使用 COM”。
WMI is acessed via COM right?
While it is more verbose and more error-prone (it is easy to accidentally use different pointers for the vtable and the "this" parameter), you can also use COM from the C language.
You also could use C++ but treat it as "C with language extensions to make using COM easier".
JNI 接口本身是 COM 的派生物,如果您使用足够多的 C++ 将接口视为由 C++ 类实现,您会发现这些方法和 WMI 接口的方法更容易使用。
另一件有用的事情是,您将能够使用 COM 接口指针和引用计数作为将 COM 接口的生命周期绑定到 JNI 实现的 Java 类的生命周期的方法。
我使用这样的方法通过 JNI 实现了与 Windows 上的某些 C 语言接口的 Java 桥接。 我手工制作了 COM 接口和用于构建 JNI DLL 的 .lib。
对于 WMI,困难的部分是您需要使用标准 COM API 来实例化 COM 对象,而我创建了自己的自定义“工厂”代码,因为它都是私有实现。
您可以下载我的 ODMJNI 1.0 0.50beta Function-Complete 的开发树快照发布。 如果您查看开发树中的 info.odma.odmjni100,您将看到 JNI DLL 是如何构建的(使用 VC++ 2005 Express Edition)和 Java 1.5。 OdmJniBind.java 类由 Java 类中使用的静态方法组成,用于协调 Java 类和 COM 对象接口之间的对象生命周期。 (树的 OdmNative 对等部分提供了 OdmNative100.lib 的实现,该库用于编译通过 JNI 使用的 odmjni100.dll。
The JNI interface itself is a derivative of COM and you will find those methods and the methods of the WMI interfaces much easier to use if you use enough C++ to treat interfaces as implemented by C++ classes.
The other thing that will be helpful is that you will be able to use the COM interface pointers and reference counting as a way to bind the lifecyle of the COM interface to the lifecycle of your JNI-implemented Java classes.
I used an approach like this to implement a Java bridge, via JNI, to some C Language interfaces on Windows. I hand-rolled COM interfaces and a .lib that is used in building the JNI DLL.
The difficult part, with WMI, is that you will want to use the standard COM APIs to Instantiate the COM objects, whereas I created my own custom "factory" code, since it was all a private implementation.
You can download a snapshot of my development tree for the ODMJNI 1.0 0.50beta Function-Complete Release. If you look at info.odma.odmjni100 in the development tree you'll see how the JNI DLL is built (using VC++ 2005 Express Edition) and Java 1.5. The OdmJniBind.java class consists of the static methods that are used in the Java classes to coordinate object lifecycles between Java Classes and COM Object interfaces. (the OdmNative peer section of the tree provides the implementation of the OdmNative100.lib that is used in compiling the odmjni100.dll that is used via JNI.
@z0ltan
您可以用C编写代码,但必须将文件保存为CPP。 正如前面有人提到的,为了 DCOM 支持,您的文件需要是 CPP 文件。
@乌米
对于 Java 集成 - 将 C/CPP 中的 WMI 代码编译为 DLL(带有适当的 JNI 头文件),然后必须加载 DLL 库文件。 完成此操作后,您可以像调用 Java 方法一样访问 DLL 文件中的 WMI 方法。
@z0ltan
You can write your code in C but you ll have to save the file as CPP. As someone had mentioned earlier, for DCOM support your file needs to be a CPP file.
@Umi
For Java Integration - compile your WMI code in C/CPP as a DLL (with proper JNI header files) and then you will have to load the DLL library file. Once this is done, you can access the WMI methods in DLL files just like calling a Java Method.