tags:

views:

963

answers:

5

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.

+3  A: 

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".

CesarB
A: 

Thanks for replying CesarB ... It's not that I hate C++ but I would rather avoid it if possible. I love C on the other hand and will surely look into your suggestion. I have no prior experience in either COM or COM in C. Any pointers in this respect?

you might want to consider making this a comment on the original post. This way alternate sorting (e.g. by nr of votes) doesn't screw up the flow of conversation :)
borisCallens
A: 

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.

orcmid
A: 

Hi z0ltan,

One of my upcoming project has similar requirements. I would like to use Java to integrate with COM/WMI so that I can manage Hyper-V infrastructure using my java based application. Are you able to use C/C++ for developing your application using JNI? Can you please share some code examples or provide me some links where i can get such examples?

Any help in this regard is highly appreciated,

Regards,

Umi

A: 

@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.

Pratheeswaran.R