Implementação da interface COM IContextMenu usando JNA

Aug 25 2020

Eu preciso de todos os itens do Windows Explorer Shell Menu. Estou implementando com jna a interface de objeto COM IShellFolder. Mas agora estou com um problema na implementação da interface IContextMenu para consultar o menu de contexto. Quando invoco a função QueryContextMenu, o resultado de HResult é 0 como verdadeiro, mas o HMenu está vazio quando invoco a função GetMenuItemCount e o resultado é 0.

Então você pode me dizer onde eu tenho um bug. Muito obrigado.

Este é o meu código para 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 este é o meu código para invocar o 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 */

Respostas

DanielWiddis Aug 25 2020 at 03:18

Você pode não estar lendo o que pensa do IContextMenuVtbl. Este código indica que você está coletando apenas 3 ponteiros de função:

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

No entanto, esta interface (como a maioria das interfaces COM) estende IUnknown, então obtém automaticamente os métodos QueryInterfaceno índice 0, AddRefno índice 1 e Releaseno índice 2. Portanto, esses são os três métodos que você parece estar chamando, embora com os argumentos errados.

Do arquivo de cabeçalho , os índices de ponteiro continuam:

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

Você pode ler todos os 6 ponteiros de função e fazer referência aos últimos 3 ou tentar ler em um deslocamento de 3 ponteiros usando sua numeração existente (mas na ordem inversa!)

Acredito que você também deva adicionar um extends IUnknownà sua definição de interface.