Implementazione dell'interfaccia COM IContextMenu utilizzando JNA

Aug 25 2020

Ho bisogno di tutti gli elementi dal menu Shell di Windows Explorer. Sto implementando con jna l'interfaccia dell'oggetto COM IShellFolder. Ma ora ho un problema con l'implementazione dell'interfaccia IContextMenu per interrogare il menu contestuale. Quando invoco la funzione QueryContextMenu il risultato di HResult è 0 come true, ma HMenu è vuoto quando invoco la funzione GetMenuItemCount e il risultato è 0.

Quindi puoi dirmi dove ho un bug. Grazie mille.

Questo è il mio codice per IContextMenu:

public interface IContextMenu {
Guid.IID IID_IContextMenu = new Guid.IID("{000214E4-0000-0000-C000-000000000046}");

WinNT.HRESULT QueryContextMenu(WinDef.HMENU hMenu, int indexMenu, int idCmdFirst, int idCmdLast, int uFlags);
WinNT.HRESULT InvokeCommand(Pointer pici);
WinNT.HRESULT GetCommandString(IntByReference idCmd, int uType, IntByReference pReserved, WTypes.LPSTR pszName, int cchMax);

public static class Converter {
    public Converter() {
    }

    public static IContextMenu PointerToIContextMenu(PointerByReference ptr) {
        final Pointer interfacePointer = ptr.getValue();
        final Pointer vTablePointer = interfacePointer.getPointer(0);
        final Pointer[] vTable = new Pointer[3];
        vTablePointer.read(0, vTable, 0, 3);

        return new IContextMenu() {
            @Override
            public WinNT.HRESULT QueryContextMenu(WinDef.HMENU hMenu, int indexMenu, int idCmdFirst, int idCmdLast, int uFlags) {
                Function f = Function.getFunction(vTable[2], Function.ALT_CONVENTION);
                return new WinNT.HRESULT(f.invokeInt(new Object[] { interfacePointer, hMenu.getPointer(), indexMenu, idCmdFirst, idCmdLast, uFlags }));
            }

            @Override
            public WinNT.HRESULT InvokeCommand(Pointer pici) {
                Function f = Function.getFunction(vTable[1], Function.ALT_CONVENTION);
                return new WinNT.HRESULT(f.invokeInt(new Object[]{ interfacePointer, pici }));
            }

            @Override
            public WinNT.HRESULT GetCommandString(IntByReference idCmd, int uType, IntByReference pReserved, WTypes.LPSTR pszName, int cchMax) {
                Function f = Function.getFunction(vTable[0], Function.ALT_CONVENTION);
                return new WinNT.HRESULT(f.invokeInt(new Object[] { interfacePointer, idCmd, uType, pReserved, pszName, cchMax }));
            }
        };
    }
}

}

E questo è il mio codice per richiamare QueryContextMenu:

IContextMenu contextMenu = null;
PointerByReference contextMenuPtr = new PointerByReference();
Pointer ppidlsPointer = new Memory(Native.POINTER_SIZE * ppidls.length);
ppidlsPointer.setPointer(0, ppidls[0].getValue());
hResult = psfParentFolder.GetUIObjectOf(null, 1, ppidls[0].getPointer(), new Guid.REFIID(IContextMenu.IID_IContextMenu), new IntByReference(0), contextMenuPtr);
if (!COMUtils.SUCCEEDED(hResult))
   return false;

/* End Section */

/* Begin Get Context Menu Section */

contextMenu = IContextMenu.Converter.PointerToIContextMenu(contextMenuPtr);
WinDef.HMENU hMenu = User32Ex.INSTANCE.CreatePopupMenu();
hResult = contextMenu.QueryContextMenu(hMenu, 0, 1, 30, 0x00000004);
if (!COMUtils.SUCCEEDED(hResult)) {
   int error = Native.getLastError();
   return false;
}

int count = User32Ex.INSTANCE.GetMenuItemCount(hMenu.getPointer());
if (count > 0) {
}

/* End Section */

Risposte

DanielWiddis Aug 25 2020 at 03:18

Potresti non leggere ciò che pensi dal file IContextMenuVtbl. Questo codice indica che stai raccogliendo solo 3 puntatori di funzione:

final Pointer[] vTable = new Pointer[3];
vTablePointer.read(0, vTable, 0, 3);

Tuttavia, questa interfaccia (come la maggior parte delle interfacce COM) si estende IUnknown, quindi ottiene automaticamente i metodi QueryInterfaceall'indice 0, AddRefall'indice 1 e Releaseall'indice 2. Quindi questi sono i tre metodi che sembra chiamare, sebbene con gli argomenti sbagliati.

Dal file di intestazione , gli indici dei puntatori continuano:

  • 3:QueryContextMenu
  • 4:InvokeCommand
  • 5:GetCommandString

Puoi leggere tutti e 6 i puntatori di funzione e fare riferimento agli ultimi 3, oppure tentare di leggere con un offset di 3 puntatori utilizzando la numerazione esistente (ma in ordine inverso!)

Credo che dovresti anche aggiungere un extends IUnknownalla tua definizione di interfaccia.