1

I already know how to wrap an Structure , but I need some help with interfaces. The interface I want to wrap is the IImageList.

But I have now clue how to create an java class out of it.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
MrMarnic
  • 23
  • 6

2 Answers2

0

This can be done, but you will need access to the header file. The IImageList documentation tells you this comes from commoncontrols.h. Refer to this source file for the following discussion.

Although not mandatory, for organization you will want to create a Java class for the header file, e.g., CommonControls.java. Inside that create a class for the interface, extending JNA's Unknown class and add the method(s) you want. The methods should use _invokeNativeObject() (or the void or int variants). The key point here is that you'll need the vtableId. This is where you go back to the header file listed earlier. Find the IImageListVtbl entry in the header (it's on line 322) and count the methods (0-indexed) to find which ID corresponds to the method you want. vtableID = 0 would be QueryInterface. Id 1 is AddRef. And so on.

See, as an example, how I implemented several interfaces from wbemcli.h in this Wbemcli.java class. One example implements the IEnumWbemClassObject::Next method thus:

class IEnumWbemClassObject extends Unknown {

    public IEnumWbemClassObject(Pointer pvInstance) {
        super(pvInstance);
    }

    public HRESULT Next(int lTimeOut, int uCount, PointerByReference ppObjects, IntByReference puReturned) {
        // Next is 5th method of IEnumWbemClassObjectVtbl in
        // WbemCli.h
        return (HRESULT) _invokeNativeObject(4,
                new Object[] { getPointer(), lTimeOut, uCount, ppObjects, puReturned }, HRESULT.class);
    }
}

You can add multiple methods (or all of them) in the same class.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • There was something I did wrong but now a Invalid memory access Expection is thrown when i run the method – MrMarnic Aug 22 '18 at 22:05
  • @MrMarnic Can you explain for me how you fixed it? I try to use the ShGetImageList function with this IImagelist as a parameter but it also gives me an invalid memory access exception – Mano176 Mar 19 '19 at 15:35
  • I don’t remember how I did it but here is the source the library that I made that does exactly that : https://github.com/MrMarnic/JIconExtract – MrMarnic Mar 19 '19 at 15:37
-2

This ended up being the solution:

class IImageList extends Unknown {
    public IImageList(Pointer pvInstance) {
        super(pvInstance);
    }

    public WinNT.HRESULT GetIcon(int i, int flags, PointerByReference picon) {
        return (WinNT.HRESULT) _invokeNativeObject(10,
                new Object[]{this.getPointer(), i, flags, picon},WinNT.HRESULT.class);
    }
}

Solution pulled from question; written by mrmarnic.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182