Windows OLE/COM with jextract code generation

Duncan Gittins duncan.gittins at gmail.com
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: 
stackoverflow).

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 
Kits\10\Include\10.0.18362.0"
      jextract -source -lole32    -t duncan.win -d java -I "%WINKIT%\um" 
Ole32.h

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 = 
Ole32_h.IUnknownVtbl.ofAddressRestricted(pVTableAddr);

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) 
Ole32_h.IUnknownVtbl.QueryInterface$VH().get(vtable);
     MethodType mt= MethodType.methodType(int.class, 
MemoryAddress.class, MemoryAddress.class, MemoryAddress.class);
     FunctionDescriptor fd = FunctionDescriptor.of(CLinker.C_LONG, 
CLinker.C_POINTER, CLinker.C_POINTER, CLinker.C_POINTER);
     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 
"IUnknownVtbl.QueryInterface$MH(vtFuncPtr)".

Kind regards

Duncan Gittins


More information about the panama-dev mailing list