Windows OLE/COM with jextract code generation

Duncan Gittins duncan.gittins at
Tue Feb 16 18:13:43 UTC 2021

This may be outside the scope of jextract (too Windows specific), or a 
dumb question (then I'll send it to the usual place for dumb questions: 

I'm converting my handwritten Panama Java application over to use the 
corresponding data structures generated by jextract, but can't find 
definitions which could simplify the MethodHandle lookups for 
referencing Windows OLE/COM API calls. The jextract output of Ole32.h 
(which contains only "#include <objbase.h>") used is generated from:

      set "WINKIT=c:\Program Files (x86)\Windows 
      jextract -source -lole32    -t -d java -I "%WINKIT%\um" 

Retrieving any COM object IUnknown is easy with appropriate values for 
class+iid guids as follows:

      MemoryAddress comObj = Ole32_h.CoCreateInstance(rclsid.address(), 
pUnkOuter, dwClsContext, riid.address(), ptrComObj.address());

... and read the COM object vTable by following IUnknown.lpVtbl pointer 
to IUnknownVTbl:

     MemorySegment memseg = Ole32_h.IUnknown.ofAddressRestricted(comObj);
     MemoryAddress pVTableAddr = Ole32_h.IUnknown.lpVtbl$get(memseg);
     MemorySegment vtable = 

At this point I need downcallHandle for every COM method pointer in 
vtable - QueryInterface / AddRef... I have this for "QueryInterface" but 
is there a way to avoid my hardcoded mt/fd declarations for all each 
method handle?

     Addressable vtFuncPtr = (Addressable) 
     MethodType mt= MethodType.methodType(int.class, 
MemoryAddress.class, MemoryAddress.class, MemoryAddress.class);
     FunctionDescriptor fd = FunctionDescriptor.of(CLinker.C_LONG, 
     MethodHandle queryInterfaceMH = 
CLinker.getInstance().downcallHandle(vtFuncPtr, mt, fd);

The interface IUnknownVtbl.QueryInterface has support for upcalls (- so 
maybe I could implement QueryInterface for a COM object in Java), and 
has "fd" internally as xxx_constants.QueryInterface $FUNC, but no 
equivalent to "mt" or downcalls of this specific comObj - say as 

Kind regards

Duncan Gittins

More information about the panama-dev mailing list