Can I use COM from Java via JNA? -
perhaps i'm crazy, i'm starting have fun idea if learning experience: i'm trying use com java calling ole32.dll functions via jna. according com spec com interface pointer pointer pointer points array of function pointers. thought since jna allows call function pointers should able call com interface methods if can vmt (the array of function pointers).
here iunknown interface:
@iid("00000000-0000-0000-c000-000000000046") public interface iunknown { int queryinterface(guid riid, pointerbyreference ppvobject); int addref(); int release(); } and here bit of code create iunkown interface, given clsid:
public static iunknown createinstance(guid clsid) { iid iida = iunknown.class.getannotation(iid.class); if (iida == null) throw new illegalargumentexception("interface needs annotated iid"); guid iid = new guid(iida.value()); ole32 ole32 = windowsjna.ole32.get(); pointerbyreference p = new pointerbyreference(); int res = ole32.cocreateinstance(clsid, null, ole32.clsctx_inproc, iid, p); if (res != ole32.s_ok) throw new winapiexception(res); final pointer interfacepointer = p.getvalue(); final pointer vtablepointer = interfacepointer.getpointer(0); final pointer[] vtable = new pointer[3]; vtablepointer.read(0, vtable, 0, 3); return new iunknown() { public int queryinterface(guid riid, pointerbyreference ppvobject) { function f = function.getfunction(vtable[0], function.alt_convention); return f.invokeint(new object[] { interfacepointer, riid, ppvobject }); } public int addref() { function f = function.getfunction(vtable[1], function.alt_convention); return f.invokeint(new object[] { interfacepointer }); } public int release() { function f = function.getfunction(vtable[2], function.alt_convention); return f.invokeint(new object[] { interfacepointer }); } }; } i'm still bit new jna, know if code makes sense? couln't work yet cause i'm stuck @ other silly error before code gets invoked.
will way of doing com slow or become feasible way of doing java, if 1 throw in java dynamic proxies along com's idispatch?
i know jacob , other com libraries java , i've used them. trying new for, mentioned, learning experience.
given native com built on c (not c++), technically work if native code using either c or stdcall calling conventions. jna gives tools need futz native memory , set stack function call.
ultimately, though, you'd want wind can parse typelib , generate interfaces you'll need, believe jacob , com4j do.
edit
jna provides comprehensive com support, including reading of type libraries , event callbacks.
Comments
Post a Comment